1- // telemetry.ts - OpenTelemetry configuration
1+ // OpenTelemetry configuration
22import { NodeSDK } from "@opentelemetry/sdk-node" ;
33import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node" ;
44import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http" ;
@@ -7,13 +7,12 @@ import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from "@opentelemetry/semantic
77import { diag , DiagConsoleLogger , DiagLogLevel } from "@opentelemetry/api" ;
88
99// Configuration from environment variables
10- const OTEL_ENABLED = true ; // process.env.OTEL_ENABLED !== "false"; // Default to true
10+ const OTEL_ENABLED = process . env . OTEL_ENABLED !== "false" ; // Default to true
1111const OTEL_EXPORTER_OTLP_ENDPOINT = process . env . OTEL_EXPORTER_OTLP_ENDPOINT || "http://localhost:4318" ;
1212const OTEL_SERVICE_NAME = process . env . OTEL_SERVICE_NAME || "items-service" ;
1313const OTEL_SERVICE_VERSION = process . env . OTEL_SERVICE_VERSION || "1.0.0" ;
1414const OTEL_LOG_LEVEL = process . env . OTEL_LOG_LEVEL || "info" ;
1515
16- // Enable OpenTelemetry diagnostic logging for debugging
1716if ( OTEL_LOG_LEVEL === "debug" ) {
1817 diag . setLogger ( new DiagConsoleLogger ( ) , DiagLogLevel . DEBUG ) ;
1918} else if ( OTEL_LOG_LEVEL === "verbose" ) {
@@ -24,7 +23,7 @@ let sdk: NodeSDK | null = null;
2423
2524export function initTelemetry ( ) {
2625 if ( ! OTEL_ENABLED ) {
27- console . log ( "📊 OpenTelemetry is disabled" ) ;
26+ console . log ( "OpenTelemetry is disabled" ) ;
2827 return ;
2928 }
3029
@@ -41,7 +40,6 @@ export function initTelemetry() {
4140 [ ATTR_SERVICE_VERSION ] : OTEL_SERVICE_VERSION ,
4241 } ) ;
4342
44- // Initialize the SDK
4543 sdk = new NodeSDK ( {
4644 resource,
4745 traceExporter,
@@ -63,12 +61,11 @@ export function initTelemetry() {
6361 ] ,
6462 } ) ;
6563
66- // Start the SDK
6764 sdk . start ( ) ;
6865
69- console . log ( `📊 OpenTelemetry initialized` ) ;
70- console . log ( ` Service: ${ OTEL_SERVICE_NAME } v${ OTEL_SERVICE_VERSION } ` ) ;
71- console . log ( ` Endpoint: ${ OTEL_EXPORTER_OTLP_ENDPOINT } ` ) ;
66+ console . log ( `OpenTelemetry initialized` ) ;
67+ console . log ( `Service: ${ OTEL_SERVICE_NAME } v${ OTEL_SERVICE_VERSION } ` ) ;
68+ console . log ( `Endpoint: ${ OTEL_EXPORTER_OTLP_ENDPOINT } ` ) ;
7269
7370 // Handle graceful shutdown
7471 process . on ( "SIGTERM" , async ( ) => {
@@ -81,17 +78,17 @@ export function initTelemetry() {
8178 process . exit ( 0 ) ;
8279 } ) ;
8380 } catch ( error ) {
84- console . error ( "❌ Failed to initialize OpenTelemetry:" , error ) ;
81+ console . error ( "Failed to initialize OpenTelemetry:" , error ) ;
8582 }
8683}
8784
8885export async function shutdownTelemetry ( ) {
8986 if ( sdk ) {
9087 try {
9188 await sdk . shutdown ( ) ;
92- console . log ( "📊 OpenTelemetry shut down successfully" ) ;
89+ console . log ( "OpenTelemetry shut down successfully" ) ;
9390 } catch ( error ) {
94- console . error ( "❌ Error shutting down OpenTelemetry:" , error ) ;
91+ console . error ( "Error shutting down OpenTelemetry:" , error ) ;
9592 }
9693 }
9794}
0 commit comments