Skip to content

Commit 1ff8cdd

Browse files
Malletscursoragent
andcommitted
Document recorder initialization invariant for metrics.
Clarify that telemetry must be initialized before any metric emission so cached handles bind to the production recorder. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent f85c0d6 commit 1ff8cdd

4 files changed

Lines changed: 41 additions & 0 deletions

File tree

quickwit/quickwit-metrics/src/counter.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ static COUNTERS: LazyLock<DashMap<u64, Arc<CounterInner>>> = LazyLock::new(DashM
3333
/// the static metadata, key, and recorder handle that live for the
3434
/// lifetime of the process.
3535
///
36+
/// # Recorder-binding invariant
37+
///
38+
/// The `metrics::Counter` handle returned by `register_counter` is
39+
/// permanently bound to whichever [`metrics::Recorder`] is active at
40+
/// first-access time. If this function is called before the production
41+
/// recorder is installed (e.g. while the noop default is still active),
42+
/// the cached handle will silently discard all subsequent increments.
43+
///
44+
/// **Callers must ensure that the global recorder is installed before
45+
/// any metric is first accessed.** In Quickwit this is guaranteed by
46+
/// `init_telemetry()` running before any `LazyLock<Counter>` is forced.
47+
///
3648
/// Prefer the `counter!` macro over calling this directly.
3749
#[doc(hidden)]
3850
pub fn __counter_get_or_register(

quickwit/quickwit-metrics/src/gauge.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ static GAUGES: LazyLock<DashMap<u64, Arc<GaugeInner>>> = LazyLock::new(DashMap::
3434
/// the static metadata, key, and recorder handle that live for the
3535
/// lifetime of the process.
3636
///
37+
/// # Recorder-binding invariant
38+
///
39+
/// The `metrics::Gauge` handle returned by `register_gauge` is
40+
/// permanently bound to whichever [`metrics::Recorder`] is active at
41+
/// first-access time. If this function is called before the production
42+
/// recorder is installed (e.g. while the noop default is still active),
43+
/// the cached handle will silently discard all subsequent mutations.
44+
///
45+
/// **Callers must ensure that the global recorder is installed before
46+
/// any metric is first accessed.** In Quickwit this is guaranteed by
47+
/// `init_telemetry()` running before any `LazyLock<Gauge>` is forced.
48+
///
3749
/// Prefer the `gauge!` macro over calling this directly.
3850
#[doc(hidden)]
3951
pub fn __gauge_get_or_register(

quickwit/quickwit-metrics/src/histogram.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ static HISTOGRAMS: LazyLock<DashMap<u64, Arc<HistogramInner>>> = LazyLock::new(D
4848
/// the static metadata, key, and recorder handle that live for the
4949
/// lifetime of the process.
5050
///
51+
/// # Recorder-binding invariant
52+
///
53+
/// The `metrics::Histogram` handle returned by `register_histogram` is
54+
/// permanently bound to whichever [`metrics::Recorder`] is active at
55+
/// first-access time. If this function is called before the production
56+
/// recorder is installed (e.g. while the noop default is still active),
57+
/// the cached handle will silently discard all subsequent recordings.
58+
///
59+
/// **Callers must ensure that the global recorder is installed before
60+
/// any metric is first accessed.** In Quickwit this is guaranteed by
61+
/// `init_telemetry()` running before any `LazyLock<Histogram>` is forced.
62+
///
5163
/// Prefer the `histogram!` macro over calling this directly.
5264
#[doc(hidden)]
5365
pub fn __histogram_get_or_register(

quickwit/quickwit-telemetry-exporters/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ pub(crate) fn startup_env_filter(level: Level) -> anyhow::Result<EnvFilter> {
9090
pub(crate) type ReloadLayer =
9191
tracing_subscriber::reload::Layer<EnvFilter, tracing_subscriber::Registry>;
9292

93+
/// Initializes logging/tracing/metrics providers for the process.
94+
///
95+
/// NOTE: this function must be called before any metric is emitted so metric handles
96+
/// are registered against the production recorder instead of a noop/default
97+
/// recorder.
9398
pub fn init_telemetry(
9499
service_version: &str,
95100
level: Level,

0 commit comments

Comments
 (0)