Skip to content

Commit 720ca4a

Browse files
feat: Add #[must_use] to AsyncInstrumentBuilder
This prevents code like the following from compiling: ```rust meter .i64_observable_up_down_counter("my_counter") .with_callback(|observer| { observer.observe(1, &[]); }); ``` This code is missing the `.build()` at the end. Before this commit, it would compile, but the callback would never be called. After this commit, it will cause a mostly helpful compile error: ``` warning: unused `AsyncInstrumentBuilder` that must be used --> examples/metrics-basic/src/main.rs:71:5 | 71 | / meter 72 | | .i64_observable_up_down_counter("my_counter") 73 | | .with_callback(|observer| { 74 | | observer.observe(1, &[]); 75 | | }); | |__________^ | = note: AsyncInstrumentBuilder must be built to receive callbacks. = note: `#[warn(unused_must_use)]` on by default help: use `let _ = ...` to ignore the resulting value | 71 | let _ = meter | +++++++ ``` Most builders need to be built to be useful. Normal metrics are built and then observations are reported to them, where a failure to call `build()` would be discovered at compile-time. However, observable metrics (async instruments) don't have that natural use point, as the result of building an `AsyncInstrumentBuilder` isn't typically needed. This makes `AsyncInstrumentBuilder` especially error-prone without the `#[must_use]` declaration.
1 parent 95af815 commit 720ca4a

File tree

1 file changed

+1
-0
lines changed
  • opentelemetry/src/metrics/instruments

1 file changed

+1
-0
lines changed

opentelemetry/src/metrics/instruments/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ impl<T> fmt::Debug for HistogramBuilder<'_, T> {
241241
pub type Callback<T> = Box<dyn Fn(&dyn AsyncInstrument<T>) + Send + Sync>;
242242

243243
/// Configuration for building an async instrument.
244+
#[must_use = "AsyncInstrumentBuilder must be built to receive callbacks."]
244245
#[non_exhaustive] // We expect to add more configuration fields in the future
245246
pub struct AsyncInstrumentBuilder<'a, I, M> {
246247
/// Instrument provider is used to create the instrument.

0 commit comments

Comments
 (0)