This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
tracing.js
83 lines (69 loc) · 2.16 KB
/
tracing.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
"use strict";
const url = require('url');
const tracing = require("signalfx-tracing");
const FORMATS = require("signalfx-tracing/ext/formats.js");
const opentracing = require("opentracing");
const NoopTracer = require("signalfx-tracing/src/noop/tracer");
const _defaultEndpointPath = '/v1/trace';
// must init tracer here before other libraries are imported in order to apply
// auto-instrumentation patches.
let _tracer;
if ((process.env.SIGNALFX_TRACING_ENABLED || "true").toLowerCase() == "false") {
_tracer = new NoopTracer();
} else {
const options = {
service:
process.env.SIGNALFX_SERVICE_NAME || process.env.AWS_LAMBDA_FUNCTION_NAME,
};
if (process.env.SIGNALFX_ENDPOINT_URL) {
const parsed = url.parse(process.env.SIGNALFX_ENDPOINT_URL);
if (parsed.pathname === "/") {
parsed.pathname = _defaultEndpointPath;
}
options.url = url.format(parsed);
}
_tracer = tracing.init(options);
}
opentracing.initGlobalTracer(_tracer);
module.exports = {
init: function(disabled) {
if (!disabled && !process.env.SIGNALFX_ENDPOINT_URL) {
console.warn('Tracing is not disabled but SIGNALFX_ENDPOINT_URL is not specified either');
}
},
tracer: function (tracingDisabled) {
return tracingDisabled ? new NoopTracer() : _tracer;
},
flush: function () {
return _tracer.flush();
},
startSpan: function (tracer, event, meta) {
const opName = "lambda_node_" + meta.aws_function_name;
let childOf = tracer.extract(
opentracing.FORMAT_HTTP_HEADERS,
event.headers || {}
);
var span;
if (childOf) {
span = tracer.startSpan(opName, { childOf });
} else {
span = tracer.startSpan(opName);
}
Object.entries(meta).forEach((pair) => {
if (pair[1]) {
span.setTag(pair[0], pair[1]);
}
});
span.setTag("span.kind", "server");
span.setTag("component", "node-lambda-wrapper");
return span;
},
inject: function inject(carrier) {
const activeSpan = _tracer.scope().active();
if (!activeSpan) {
console.warn('There are no active spans to inject.');
return;
}
_tracer.inject(activeSpan, FORMATS.HTTP_HEADERS, carrier);
},
};