-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace.js
36 lines (31 loc) · 1.41 KB
/
trace.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
'use strict'
import process from 'process';
import opentelemetry from '@opentelemetry/sdk-node';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import sdkTraceBase from '@opentelemetry/sdk-trace-base';
import resources from '@opentelemetry/resources';
import semanticConventions from '@opentelemetry/semantic-conventions';
import { TraceExporter as GoogleCloudTraceExporter } from '@google-cloud/opentelemetry-cloud-trace-exporter';
const { ConsoleSpanExporter } = sdkTraceBase;
const { Resource } = resources;
const { SemanticResourceAttributes } = semanticConventions;
// configure the SDK to export telemetry data to the console
// enable all auto-instrumentations from the meta package
const traceExporter = process.env.NODE_ENV == 'production' ? new GoogleCloudTraceExporter() : new ConsoleSpanExporter();
const sdk = new opentelemetry.NodeSDK({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'service-one',
}),
traceExporter,
instrumentations: [getNodeAutoInstrumentations()]
});
// initialize the SDK and register with the OpenTelemetry API
// this enables the API to record telemetry
sdk.start();
// gracefully shut down the SDK on process exit
process.on('SIGTERM', () => {
sdk.shutdown()
.then(() => console.log('Tracing terminated'))
.catch((error) => console.log('Error terminating tracing', error))
.finally(() => process.exit(0));
});