44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
/**
|
|
* OpenTelemetry instrumentation — auto-instrumentat pe Express, HTTP, pg, redis, ioredis.
|
|
* Trimite trace-uri la OTel Collector → Jaeger.
|
|
*
|
|
* Activează cu env var OTEL_ENABLED=true (off implicit ca să nu impacteze cold start).
|
|
*/
|
|
|
|
import { NodeSDK } from '@opentelemetry/sdk-node';
|
|
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
|
|
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
|
|
import { Resource } from '@opentelemetry/resources';
|
|
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
|
|
|
|
const SERVICE_NAME = process.env.OTEL_SERVICE_NAME || 'didi-framework';
|
|
const ENDPOINT = process.env.OTEL_EXPORTER_OTLP_ENDPOINT || 'http://didi-otel-collector:4318/v1/traces';
|
|
|
|
let sdk: NodeSDK | null = null;
|
|
|
|
export function startOtel() {
|
|
if (process.env.OTEL_ENABLED !== 'true') return;
|
|
if (sdk) return;
|
|
|
|
sdk = new NodeSDK({
|
|
resource: new Resource({
|
|
[SemanticResourceAttributes.SERVICE_NAME]: SERVICE_NAME,
|
|
[SemanticResourceAttributes.SERVICE_VERSION]: process.env.SERVICE_VERSION || '1.0.0',
|
|
}),
|
|
traceExporter: new OTLPTraceExporter({ url: ENDPOINT }),
|
|
instrumentations: [
|
|
getNodeAutoInstrumentations({
|
|
'@opentelemetry/instrumentation-fs': { enabled: false },
|
|
}),
|
|
],
|
|
});
|
|
sdk.start();
|
|
// eslint-disable-next-line no-console
|
|
console.log(`[otel] started, exporting to ${ENDPOINT} as ${SERVICE_NAME}`);
|
|
}
|
|
|
|
process.on('SIGTERM', () => {
|
|
if (sdk) {
|
|
sdk.shutdown().finally(() => process.exit(0));
|
|
}
|
|
});
|