()
| 306 | } |
| 307 | |
| 308 | async function setMeterState(): Promise<void> { |
| 309 | // Lazy-load instrumentation to defer ~400KB of OpenTelemetry + protobuf |
| 310 | const { initializeTelemetry } = await import( |
| 311 | '../utils/telemetry/instrumentation.js' |
| 312 | ) |
| 313 | // Initialize customer OTLP telemetry (metrics, logs, traces) |
| 314 | const meter = await initializeTelemetry() |
| 315 | if (meter) { |
| 316 | // Create factory function for attributed counters |
| 317 | const createAttributedCounter = ( |
| 318 | name: string, |
| 319 | options: MetricOptions, |
| 320 | ): AttributedCounter => { |
| 321 | const counter = meter?.createCounter(name, options) |
| 322 | |
| 323 | return { |
| 324 | add(value: number, additionalAttributes: Attributes = {}) { |
| 325 | // Always fetch fresh telemetry attributes to ensure they're up to date |
| 326 | const currentAttributes = getTelemetryAttributes() |
| 327 | const mergedAttributes = { |
| 328 | ...currentAttributes, |
| 329 | ...additionalAttributes, |
| 330 | } |
| 331 | counter?.add(value, mergedAttributes) |
| 332 | }, |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | setMeter(meter, createAttributedCounter) |
| 337 | |
| 338 | // Increment session counter here because the startup telemetry path |
| 339 | // runs before this async initialization completes, so the counter |
| 340 | // would be null there. |
| 341 | getSessionCounter()?.add(1) |
| 342 | } |
| 343 | } |
no test coverage detected