-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracing.js
49 lines (42 loc) · 1.92 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
// tracing.js
const process = require('process');
const { Metadata, credentials } = require('@grpc/grpc-js');
const { NodeSDK } = require('@opentelemetry/sdk-node');
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
const { Resource } = require('@opentelemetry/resources');
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
const { ConsoleSpanExporter, NoopSpanProcessor } = require('@opentelemetry/sdk-trace-base');
const metadata = new Metadata();
// The Trace Exporter exports the data to Honeycomb and uses
// the previously-configured metadata and the Honeycomb endpoint.
let traceExporter;
if ('OTEL_CONSOLE_TRACING' in process.env && process.env.OTEL_CONSOLE_TRACING.toLowerCase() !== 'false') {
console.log('writing tracing data to ConsoleSpanExporter');
traceExporter = new ConsoleSpanExporter();
} else {
console.log('writing tracing data to NoopSpanProcessor');
traceExporter = new NoopSpanProcessor();
}
// The service name is REQUIRED! It is a resource attribute,
// which means that it will be present on all observability data that your service generates.
//
// Your service name will be used as the Service Dataset in Honeycomb, which is where data is stored.
const sdk = new NodeSDK({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: process.env.SERVICE_NAME || 'Unknown',
}),
traceExporter,
// Instrumentations allow you to add auto-instrumentation packages
instrumentations: [getNodeAutoInstrumentations()],
});
sdk
.start()
.then(() => console.log('Tracing initialized'))
.catch((error) => console.error('Error initializing tracing', error));
process.on('SIGTERM', () => {
sdk
.shutdown()
.then(() => console.log('Tracing terminated'))
.catch((error) => console.error('Error terminating tracing', error))
.finally(() => process.exit(0)); // eslint-disable-line
}); // eslint-disable-line