|
| 1 | +const initialize = async () => { |
| 2 | + if (process.env.ENABLED_OPENINFERENCE_TELEMETRY === 'true') { |
| 3 | + const { registerInstrumentations } = await import('@opentelemetry/instrumentation'); |
| 4 | + const { |
| 5 | + NodeTracerProvider, |
| 6 | + SimpleSpanProcessor, |
| 7 | + } = await import('@opentelemetry/sdk-trace-node'); |
| 8 | + const { OTLPTraceExporter } = await import('@opentelemetry/exporter-trace-otlp-proto'); |
| 9 | + const { ATTR_SERVICE_NAME } = await import('@opentelemetry/semantic-conventions'); |
| 10 | + const { diag, DiagConsoleLogger, DiagLogLevel } = await import('@opentelemetry/api'); |
| 11 | + const { LangChainInstrumentation } = await import('@arizeai/openinference-instrumentation-langchain'); |
| 12 | + const lcCallbackManager = await import('@langchain/core/callbacks/manager'); |
| 13 | + const { resourceFromAttributes } = await import('@opentelemetry/resources'); |
| 14 | + const { LoggerProvider, SimpleLogRecordProcessor } = await import('@opentelemetry/sdk-logs'); |
| 15 | + const { OTLPLogExporter } = await import('@opentelemetry/exporter-logs-otlp-proto'); |
| 16 | + const api = await import("@opentelemetry/api-logs"); |
| 17 | + const { MeterProvider, PeriodicExportingMetricReader } = await import('@opentelemetry/sdk-metrics'); |
| 18 | + const { OTLPMetricExporter } = await import('@opentelemetry/exporter-metrics-otlp-proto'); |
| 19 | + |
| 20 | + // For troubleshooting, set the log level to DiagLogLevel.DEBUG |
| 21 | + diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO); |
| 22 | + |
| 23 | + const otlpApiKey = process.env.OTEL_EXPORTER_OTLP_API_KEY; |
| 24 | + const headers = otlpApiKey ? { |
| 25 | + 'x-api-key': otlpApiKey, |
| 26 | + 'Langsmith-Project': 'default', |
| 27 | + } : undefined; |
| 28 | + |
| 29 | + const otlpTracesEndpoint = process.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ?? 'http://localhost:6006/v1/traces'; |
| 30 | + const provider = new NodeTracerProvider({ |
| 31 | + spanProcessors: [new SimpleSpanProcessor( |
| 32 | + new OTLPTraceExporter({ |
| 33 | + url: otlpTracesEndpoint, |
| 34 | + headers, |
| 35 | + }), |
| 36 | + ), |
| 37 | + ], |
| 38 | + resource: resourceFromAttributes({ |
| 39 | + [ATTR_SERVICE_NAME]: 'chat-service', |
| 40 | + }), |
| 41 | + }); |
| 42 | + |
| 43 | + registerInstrumentations({ |
| 44 | + instrumentations: [], |
| 45 | + }); |
| 46 | + |
| 47 | + const otlpLogsEndpoint = process.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ?? 'http://localhost:6006/v1/logs'; |
| 48 | + const logExporter = new OTLPLogExporter({ |
| 49 | + url: otlpLogsEndpoint, // url is optional and can be omitted - default is http://localhost:4318/v1/logs |
| 50 | + headers, //an optional object containing custom headers to be sent with each request will only work with http |
| 51 | + }); |
| 52 | + const loggerProvider = new LoggerProvider({ |
| 53 | + resource: resourceFromAttributes({ 'service.name': 'testApp' }), |
| 54 | + processors: [new SimpleLogRecordProcessor(logExporter)] |
| 55 | + }); |
| 56 | + api.logs.setGlobalLoggerProvider(loggerProvider); |
| 57 | + |
| 58 | + const otlpMetricsEndpoint = process.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ?? 'http://localhost:6006/v1/metrics'; |
| 59 | + const collectorOptions = { |
| 60 | + url: otlpMetricsEndpoint, // url is optional and can be omitted - default is http://localhost:4318/v1/metrics |
| 61 | + headers, |
| 62 | + }; |
| 63 | + const metricExporter = new OTLPMetricExporter(collectorOptions); |
| 64 | + const meterProvider = new MeterProvider({ |
| 65 | + readers: [ |
| 66 | + new PeriodicExportingMetricReader({ |
| 67 | + exporter: metricExporter, |
| 68 | + exportIntervalMillis: 1000, |
| 69 | + }), |
| 70 | + ], |
| 71 | + }); |
| 72 | + |
| 73 | + |
| 74 | + // LangChain must be manually instrumented as it doesn't have a traditional module structure |
| 75 | + const lcInstrumentation = new LangChainInstrumentation(); |
| 76 | + lcInstrumentation.manuallyInstrument(lcCallbackManager); |
| 77 | + |
| 78 | + provider.register(); |
| 79 | + |
| 80 | + console.log('👀 OpenInference initialized'); |
| 81 | + }; |
| 82 | +} |
| 83 | + |
| 84 | +export { initialize }; |
0 commit comments