()
| 355 | } |
| 356 | |
| 357 | async function setMeterState(): Promise<void> { |
| 358 | // Lazy-load instrumentation to defer ~400KB of OpenTelemetry + protobuf |
| 359 | const { initializeTelemetry } = await import( |
| 360 | '../utils/telemetry/instrumentation.js' |
| 361 | ) |
| 362 | // Initialize customer OTLP telemetry (metrics, logs, traces) |
| 363 | const meter = await initializeTelemetry() |
| 364 | if (meter) { |
| 365 | // Create factory function for attributed counters |
| 366 | const createAttributedCounter = ( |
| 367 | name: string, |
| 368 | options: MetricOptions, |
| 369 | ): AttributedCounter => { |
| 370 | const counter = meter?.createCounter(name, options) |
| 371 | |
| 372 | return { |
| 373 | add(value: number, additionalAttributes: Attributes = {}) { |
| 374 | // Always fetch fresh telemetry attributes to ensure they're up to date |
| 375 | const currentAttributes = getTelemetryAttributes() |
| 376 | const mergedAttributes = { |
| 377 | ...currentAttributes, |
| 378 | ...additionalAttributes, |
| 379 | } |
| 380 | counter?.add(value, mergedAttributes) |
| 381 | }, |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | setMeter(meter, createAttributedCounter) |
| 386 | |
| 387 | // Increment session counter here because the startup telemetry path |
| 388 | // runs before this async initialization completes, so the counter |
| 389 | // would be null there. |
| 390 | getSessionCounter()?.add(1) |
| 391 | } |
| 392 | } |
no test coverage detected