43 行
1.3 KiB
JavaScript
43 行
1.3 KiB
JavaScript
const { NodeSDK } = require('@opentelemetry/sdk-node');
|
|
const { ConsoleSpanExporter } = require('@opentelemetry/sdk-trace-node');
|
|
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
|
|
const { Resource } = require('@opentelemetry/resources');
|
|
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
|
|
|
|
let sdk = null;
|
|
|
|
function initTracing(config) {
|
|
if (config.otelTracesEnabled !== true) {
|
|
return;
|
|
}
|
|
|
|
const exporter = new ConsoleSpanExporter();
|
|
|
|
sdk = new NodeSDK({
|
|
resource: new Resource({
|
|
[SemanticResourceAttributes.SERVICE_NAME]: 'hyb-oa-server',
|
|
}),
|
|
traceExporter: exporter,
|
|
instrumentations: [getNodeAutoInstrumentations({
|
|
'@opentelemetry/instrumentation-fs': { enabled: false },
|
|
})]
|
|
});
|
|
|
|
try {
|
|
sdk.start();
|
|
console.log('OTel tracing initialized');
|
|
} catch (error) {
|
|
console.error('Error initializing OTel tracing', error);
|
|
}
|
|
|
|
process.on('SIGTERM', () => {
|
|
if (sdk) {
|
|
sdk.shutdown()
|
|
.then(() => console.log('OTel tracing terminated'))
|
|
.catch((error) => console.log('Error terminating OTel tracing', error));
|
|
}
|
|
});
|
|
}
|
|
|
|
module.exports = { initTracing };
|