@@ -18,38 +18,46 @@ import 'dotenv/config';
1818
1919import { setTimeout } from 'node:timers/promises' ;
2020
21- import { NodeSDK , resources , metrics } from '@opentelemetry/sdk-node' ;
21+ import { NodeSDK , resources , metrics , tracing } from '@opentelemetry/sdk-node' ;
2222import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node' ;
2323import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions' ;
2424import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http' ;
2525import { ATTR_DEPLOYMENT_ENVIRONMENT_NAME } from '@opentelemetry/semantic-conventions/incubating' ;
26+ import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto' ;
2627
2728const ENV = process . env . ENVIRONMENT ;
2829
30+ export const isOpenTelemetryEnabled = Boolean ( process . env . NODE_ENV === 'production' ) ;
31+ export const batchSpanProcessor = new tracing . BatchSpanProcessor ( new OTLPTraceExporter ( { } ) ) ;
32+
2933export const opentelemetrySDK = new NodeSDK ( {
3034 resource : new resources . Resource ( {
3135 [ ATTR_SERVICE_NAME ] : `bee-api` ,
3236 [ ATTR_DEPLOYMENT_ENVIRONMENT_NAME ] : ENV
3337 } ) ,
38+ spanProcessors : [ batchSpanProcessor ] ,
3439 metricReader : new metrics . PeriodicExportingMetricReader ( { exporter : new OTLPMetricExporter ( ) } ) ,
3540 instrumentations : [ ...getNodeAutoInstrumentations ( ) ]
3641} ) ;
37- opentelemetrySDK . start ( ) ;
38-
39- let isShuttingDown = false ;
40- const { promise, resolve } = Promise . withResolvers < void > ( ) ;
41-
42- for ( const event of [ 'beforeExit' , 'SIGINT' , 'SIGTERM' ] ) {
43- process . once ( event , ( ) => {
44- if ( ! isShuttingDown ) {
45- isShuttingDown = true ;
46- Promise . race ( [ opentelemetrySDK . shutdown ( ) , setTimeout ( 5_000 , null , { ref : false } ) ] )
47- . catch ( ( err ) => {
48- // eslint-disable-next-line no-console
49- console . error ( `Failed to execute shutdown hook` , err ) ;
50- } )
51- . finally ( ( ) => resolve ( ) ) ;
52- }
53- return promise ;
54- } ) ;
42+
43+ if ( isOpenTelemetryEnabled ) {
44+ opentelemetrySDK . start ( ) ;
45+
46+ let isShuttingDown = false ;
47+ const { promise, resolve } = Promise . withResolvers < void > ( ) ;
48+
49+ for ( const event of [ 'beforeExit' , 'SIGINT' , 'SIGTERM' ] ) {
50+ process . once ( event , ( ) => {
51+ if ( ! isShuttingDown ) {
52+ isShuttingDown = true ;
53+ Promise . race ( [ opentelemetrySDK . shutdown ( ) , setTimeout ( 5_000 , null , { ref : false } ) ] )
54+ . catch ( ( err ) => {
55+ // eslint-disable-next-line no-console
56+ console . error ( `Failed to execute shutdown hook` , err ) ;
57+ } )
58+ . finally ( ( ) => resolve ( ) ) ;
59+ }
60+ return promise ;
61+ } ) ;
62+ }
5563}
0 commit comments