Add lazy telemetry instruments#7
Conversation
Greptile SummaryThis PR adds
Confidence Score: 5/5The change is additive — no existing behaviour is modified, only new lazy() factory methods are introduced alongside well-targeted tests. All four instruments receive a consistent, minimal implementation with a single null-coalescing assignment guarding deferred creation. Each abstract method is correctly satisfied, all constructor parameters are forwarded faithfully to the adapter, and the test suite validates both the deferred-registration guarantee and inner-instance reuse for every instrument type. No files require special attention. Important Files Changed
Reviews (2): Last reviewed commit: "Add lazy factories for remaining instrum..." | Re-trigger Greptile |
Summary
lazy()factory methods toCounter,Gauge,Histogram, andUpDownCounter.add()orrecord()call.Why
Some OpenTelemetry SDKs register instruments with the meter as soon as they are created. Periodic export then walks every registered instrument, even if the application never recorded data for it. In downstream packages with many rare/event-driven metrics, that can produce empty metric streams and dropped/filtered output noise for instruments that were only registered eagerly.
The intended behavior for event-driven instruments is different: if an event never happens, the instrument should not be registered or exported at all. A counter for fallback executions, transition events, callback failures, or an event timestamp should appear only after the first matching event is recorded.
This PR adds lazy factories at the instrument abstraction layer instead of changing adapters. Adapters continue to represent telemetry backends such as OpenTelemetry, test, or none. Laziness is an instrument lifecycle policy, so callers can opt into it explicitly while still receiving the same abstract instrument type.
Example usage:
This keeps steady-state instruments eager where a zero value is meaningful, while allowing event-driven instruments to avoid registration until they actually emit data.
Testing