1515 */
1616
1717
18- /// first do `npm install haystack-client`
19- const initTracer = require ( '../index' ) . initTracer ;
20- const opentracing = require ( 'opentracing' ) ;
21- import MyLogger from "./logger" ;
18+ "use strict" ;
2219
20+ /// first do `npm install haystack-client` and replace '../dist/index' with 'haystack-client'
21+ const initTracer = require ( '../dist/index' ) . initTracer ;
22+ const opentracing = require ( 'opentracing' ) ;
23+ const MyLogger = require ( './logger' ) ;
2324
2425/// setup a logger. if you skip providing logger to config, the library will not spit any errors, warning or info
2526/// you can provide the logger object already configured in your service, provided it has 4 methods defined:
2627// debug(msg), info(msg), error(msg), warn(msg)
27- const logger = new MyLogger ( ) ;
28+ const logger = new MyLogger . default ( ) ;
2829
2930
3031/// setup the config object required for initializing Tracer
@@ -42,7 +43,7 @@ const config = {
4243 // or
4344
4445 // type: 'haystack_agent',
45- // agentHost: 'haystack-agent ',
46+ // agentHost: '192.168.99.100 ',
4647 // agentPort: '35000'
4748 } ,
4849 logger : logger
@@ -53,26 +54,34 @@ const tracer = initTracer(config);
5354
5455/// now create a span, for e.g. at the time of incoming REST call.
5556/// Make sure to add SPAN_KIND tag. Possible values are 'server' or 'client'.
57+ /// Important: if you are receiving TraceId, SpanId, ParentSpanId in the http headers or message payload of your incoming REST call,
58+ /// then set it in the callerSpanContext
59+
5660const serverSpan = tracer
57- . startSpan ( 'dummy-operation' )
61+ . startSpan ( 'dummy-operation' , {
62+ callerSpanContext : {
63+ _traceId : '1848fadd-fa16-4b3e-8ad1-6d73339bbee7' ,
64+ _spanId : '7a7cc5bf-796e-4527-9b42-13ae5766c6fd' ,
65+ _parentSpanId : 'e96de653-ad6e-4ad5-b437-e81fd9d2d61d'
66+ }
67+ } )
5868 . setTag ( opentracing . Tags . SPAN_KIND , 'server' )
5969 . setTag ( opentracing . Tags . HTTP_METHOD , 'GET' ) ;
6070
71+ /// Or if you are the root service, skip callerSpanContext and use the following
72+
73+ // const serverSpan = tracer
74+ // .startSpan('dummy-operation')
75+ // .setTag(opentracing.Tags.SPAN_KIND, 'server')
76+ // .setTag(opentracing.Tags.HTTP_METHOD, 'GET');
6177
6278
63- /// Important:
64- // if you are receiving TraceId, SpanId, ParentSpanId in the http headers or message payload of your incoming REST call,
65- /// then update the IDs in the span context, else tracer will use the unique TraceId and SpanId.
66- /*
67- serverSpan.context().setTraceId(<TRACE ID> );
68- serverSpan.context().setSpanId(<SPAN ID>);
69- serverSpan.context().setParentSpanId(<PARENT SPAN ID>);
70- */
7179
7280
7381/// now say service is calling downstream service, then you start another span - a client span
7482/// since this span is a child of the main serverSpan, so pass it along as `childOf` attribute.
7583/// library will setup the traceId, spanId and parentSpanId by itself.
84+ /// You dont need to set the callerSpanContext if you are setting childOf
7685const clientChildSpan = tracer . startSpan ( 'downstream-service-call' , {
7786 childOf : serverSpan ,
7887 tags : {
@@ -82,9 +91,11 @@ const clientChildSpan = tracer.startSpan('downstream-service-call', {
8291
8392
8493/// add more tags or logs to your spans
85- clientChildSpan . setTag ( opentracing . Tags . ERROR , false ) ;
86- serverSpan . setTag ( opentracing . Tags . ERROR , false ) ;
94+ clientChildSpan . setTag ( opentracing . Tags . ERROR , true ) ;
95+ clientChildSpan . setTag ( opentracing . Tags . HTTP_STATUS_CODE , 503 ) ;
8796
97+ serverSpan . setTag ( opentracing . Tags . ERROR , true ) ;
98+ serverSpan . setTag ( 'my-custom-tag' , 10.5 ) ;
8899
89100/// finish the downstream call span. This will publish the span to either file or haystack-agent
90101clientChildSpan . finish ( ) ;
@@ -93,4 +104,6 @@ clientChildSpan.finish();
93104serverSpan . finish ( ) ;
94105
95106/// close the tracer at the time of service shutdown
96- tracer . close ( ) ;
107+ setTimeout ( ( ) => {
108+ tracer . close ( ) ;
109+ } , 3000 ) ;
0 commit comments