|
| 1 | +// OpenTelemetry tracing setup |
| 2 | +// This file is loaded automatically by the Azure Functions host, |
| 3 | +// as configured in package.json "main" field. |
| 4 | + |
| 5 | +import { useAzureMonitor } from "@azure/monitor-opentelemetry"; |
| 6 | +import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http"; |
| 7 | +import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node"; |
| 8 | +import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base"; |
| 9 | +import { LangChainInstrumentation } from "@traceloop/instrumentation-langchain"; |
| 10 | +import { registerInstrumentations } from "@opentelemetry/instrumentation"; |
| 11 | + |
| 12 | +let isTracingInitialized = false; |
| 13 | +if (!isTracingInitialized) { |
| 14 | + // Initialize tracing and export to Azure Monitor or OTLP endpoint |
| 15 | + // When running locally, you can use a local OpenTelemetry collector to receive the traces, |
| 16 | + // for example VS Code AI Toolkit's extension trace collector: |
| 17 | + // https://marketplace.visualstudio.com/items?itemName=ms-windows-ai-studio.windows-ai-studio |
| 18 | + const appInsightsConnectionString = process.env["APPLICATIONINSIGHTS_CONNECTION_STRING"]; |
| 19 | + if (appInsightsConnectionString) { |
| 20 | + useAzureMonitor({ |
| 21 | + azureMonitorExporterOptions: { connectionString: appInsightsConnectionString } |
| 22 | + }); |
| 23 | + } else { |
| 24 | + console.warn("Using local OTLP endpoint at http://localhost:4318, run a local OpenTelemetry collector to receive the traces"); |
| 25 | + |
| 26 | + const exporter = new OTLPTraceExporter({ |
| 27 | + url: "http://localhost:4318/v1/traces", |
| 28 | + }); |
| 29 | + const provider = new NodeTracerProvider({ |
| 30 | + spanProcessors: [new BatchSpanProcessor(exporter as any) as any], |
| 31 | + }); |
| 32 | + provider.register(); |
| 33 | + } |
| 34 | + |
| 35 | + registerInstrumentations({ |
| 36 | + instrumentations: [new LangChainInstrumentation()], |
| 37 | + }); |
| 38 | + |
| 39 | + isTracingInitialized = true; |
| 40 | +} |
0 commit comments