forked from clementduveau/intro-to-mltp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracing.js
70 lines (61 loc) · 2.53 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
// This is shared between the requester, the recorder and the server
// As such, to only do what's needed, we init using a
// function and then pass the service context to
// determine what to initialise.
module.exports = (context, serviceName) => {
// Include all OpenTelemetry dependencies for tracing
const api = require("@opentelemetry/api");
const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node");
const { SimpleSpanProcessor } = require("@opentelemetry/sdk-trace-base");
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc');
const { envDetector, Resource } = require('@opentelemetry/resources');
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
return async () => {
let W3CTraceContextPropagator;
if (context === 'requester') {
W3CTraceContextPropagator = require("@opentelemetry/core").W3CTraceContextPropagator;
}
const detected = await envDetector.detect();
const resources = new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: serviceName,
}).merge(detected);
// Create a tracer provider
const provider = new NodeTracerProvider({
resource: resources,
});
// Export via OTLP gRPC
const exporter = new OTLPTraceExporter({
url: `${process.env.TRACING_COLLECTOR_HOST}:${process.env.TRACING_COLLECTOR_PORT}`
});
// Use simple span (should probably use Batch)
const processor = new SimpleSpanProcessor(exporter);
provider.addSpanProcessor(processor);
provider.register();
// Create a new header for propagation from a given span
let createPropagationHeader;
if (context === 'requester') {
const propagator = new W3CTraceContextPropagator();
createPropagationHeader = (span) => {
let carrier = {};
// Inject the current trace context into the carrier object
propagator.inject(
api.trace.setSpanContext(api.ROOT_CONTEXT, span.spanContext()),
carrier,
api.defaultTextMapSetter
);
return carrier;
};
}
registerInstrumentations({
instrumentations: [getNodeAutoInstrumentations()],
});
// Return instances of the API and the tracer to the calling app
return {
tracer: api.trace.getTracer(serviceName),
api: api,
propagator: createPropagationHeader,
}
};
};