forked from ExpediaDotCom/haystack-client-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (86 loc) · 3.97 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Copyright 2018 Expedia, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
"use strict";
/// first do `npm install haystack-client` and replace '../dist/index' with 'haystack-client'
const initTracer = require('../dist/index').initTracer;
const SpanContext = require('../dist/index').SpanContext;
const opentracing = require('opentracing');
const MyLogger = require('./logger');
/// setup a logger. if you skip providing logger to config, the library will not spit any errors, warning or info
/// you can provide the logger object already configured in your service, provided it has 4 methods defined:
// debug(msg), info(msg), error(msg), warn(msg)
const logger = new MyLogger.default();
/// setup the config object required for initializing Tracer
/// Use `file` dispatcher for local development else use haystack_agent for environments like prod
/// commonTags are the tags that are injected in every span emitted by your service.
const config = {
serviceName: 'dummy-service',
commonTags: {
'dummy-service-version': '0.1.0'
},
dispatcher: {
type: 'file',
filePath: 'logs/spans' //make sure the 'logs' directory exists
// or
// type: 'haystack_agent',
// agentHost: '192.168.99.100',
// agentPort: '35000'
},
logger: logger
};
/// initialize the tracer only once at the time of your service startup
const tracer = initTracer(config);
/// now create a span, for e.g. at the time of incoming REST call.
/// Make sure to add SPAN_KIND tag. Possible values are 'server' or 'client'.
/// Important: if you are receiving TraceId, SpanId, ParentSpanId in the http headers or message payload of your incoming REST call,
/// then set it in the callerSpanContext
const serverSpan = tracer
.startSpan('dummy-operation', {
callerSpanContext: new SpanContext(
'1848fadd-fa16-4b3e-8ad1-6d73339bbee7',
'7a7cc5bf-796e-4527-9b42-13ae5766c6fd',
'e96de653-ad6e-4ad5-b437-e81fd9d2d61d')
})
.setTag(opentracing.Tags.SPAN_KIND, 'server')
.setTag(opentracing.Tags.HTTP_METHOD, 'GET');
/// Or if you are the root service, skip callerSpanContext and use the following
// const serverSpan = tracer
// .startSpan('dummy-operation')
// .setTag(opentracing.Tags.SPAN_KIND, 'server')
// .setTag(opentracing.Tags.HTTP_METHOD, 'GET');
/// now say service is calling downstream service, then you start another span - a client span
/// since this span is a child of the main serverSpan, so pass it along as `childOf` attribute.
/// library will setup the traceId, spanId and parentSpanId by itself.
/// You dont need to set the callerSpanContext if you are setting childOf
const clientChildSpan = tracer.startSpan('downstream-service-call', {
childOf: serverSpan,
tags: {
'span.kind': 'client' // Note `span.kind` is now `client`
}
});
/// add more tags or logs to your spans
clientChildSpan.setTag(opentracing.Tags.ERROR, true);
clientChildSpan.setTag(opentracing.Tags.HTTP_STATUS_CODE, 503);
serverSpan.setTag(opentracing.Tags.ERROR, true);
serverSpan.setTag('my-custom-tag', 10.5);
/// finish the downstream call span. This will publish the span to either file or haystack-agent
clientChildSpan.finish();
/// finish the server span when your service is ready to send the response back to upstream
serverSpan.finish();
/// close the tracer at the time of service shutdown
setTimeout(() => {
tracer.close();
}, 3000);