Skip to content

Commit 1d2f172

Browse files
authored
minor improvements in the example tracer (#26)
1 parent 6ecd7b8 commit 1d2f172

1 file changed

Lines changed: 32 additions & 12 deletions

File tree

examples/index.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,32 +58,52 @@ const tracer = initTracer(config);
5858
/// now create a span, for e.g. at the time of incoming REST call.
5959
/// Make sure to add SPAN_KIND tag. Possible values are 'server' or 'client'.
6060
/// Important: if you are receiving TraceId, SpanId, ParentSpanId in the http headers or message payload of your incoming REST call,
61-
/// then create a SpanContext with those IDs and initialize the tracer with a childOf the received SpanContext
61+
/// then either use the Extractor to create a SpanContext or create by yourself.
62+
// Initialize the tracer with a childOf this newly built SpanContext
6263

64+
// Use the exatractor to create SpanContext
6365
const serverSpan = tracer
64-
.startSpan('dummy-operation', {
65-
childOf: new SpanContext(
66-
'1848fadd-fa16-4b3e-8ad1-6d73339bbee7',
67-
'7a7cc5bf-796e-4527-9b42-13ae5766c6fd',
68-
'e96de653-ad6e-4ad5-b437-e81fd9d2d61d')
66+
.startSpan('/foo', {
67+
childOf: tracer.extract(opentracing.FORMAT_HTTP_HEADERS, {
68+
'Trace-ID': '1848fadd-fa16-4b3e-8ad1-6d73339bbee7',
69+
'Span-ID': '7a7cc5bf-796e-4527-9b42-13ae5766c6fd',
70+
'Parent-ID': 'e96de653-ad6e-4ad5-b437-e81fd9d2d61d'
71+
}),
72+
tags: {
73+
'span.kind': 'server',
74+
'http.method': 'GET'
75+
}
6976
})
70-
.setTag(opentracing.Tags.SPAN_KIND, 'server')
71-
.setTag(opentracing.Tags.HTTP_METHOD, 'GET');
77+
// you can add more tags on server span later in the workflow
78+
serverSpan.setTag(opentracing.Tags.ERROR, true);
79+
80+
// Or
81+
// const serverSpan = tracer
82+
// .startSpan('/foo', {
83+
// childOf: new SpanContext(
84+
// '1848fadd-fa16-4b3e-8ad1-6d73339bbee7',
85+
// '7a7cc5bf-796e-4527-9b42-13ae5766c6fd',
86+
// 'e96de653-ad6e-4ad5-b437-e81fd9d2d61d'),
87+
// tags: {
88+
// 'span.kind': 'server'
89+
// }
90+
// })
91+
// you can add more tags on server span later in the workflow
92+
// serverSpan.setTag(opentracing.Tags.ERROR, true);
93+
7294

7395
/// Or if you are the root service
7496

7597
// const serverSpan = tracer
76-
// .startSpan('dummy-operation')
77-
// .setTag(opentracing.Tags.SPAN_KIND, 'server')
98+
// .startSpan('dummy-operation', tags: {'span.kind': 'client'})
7899
// .setTag(opentracing.Tags.HTTP_METHOD, 'GET');
79100

80101

81102

82-
83103
/// now say service is calling downstream service, then you start another span - a client span
84104
/// since this span is a child of the main serverSpan, so pass it along as `childOf` attribute.
85105
/// library will setup the traceId, spanId and parentSpanId by itself.
86-
const clientChildSpan = tracer.startSpan('downstream-service-call', {
106+
const clientChildSpan = tracer.startSpan('/bar', {
87107
childOf: serverSpan,
88108
tags: {
89109
'span.kind': 'client' // Note `span.kind` is now `client`

0 commit comments

Comments
 (0)