Skip to content

Conversation

@ongardie-atomix
Copy link

This prevents code like the following from compiling:

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.

Fixes #
Design discussion issue (if applicable) #

Changes

Please provide a brief description of the changes here.

Merge requirement checklist

  • CONTRIBUTING guidelines followed
  • Unit tests added/updated (if applicable)
  • Appropriate CHANGELOG.md files updated for non-trivial, user-facing changes
  • Changes in public API reviewed (if applicable)

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.
@ongardie-atomix ongardie-atomix requested a review from a team as a code owner November 15, 2025 01:06
@codecov
Copy link

codecov bot commented Nov 15, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.8%. Comparing base (95af815) to head (720ca4a).

Additional details and impacted files
@@          Coverage Diff          @@
##            main   #3246   +/-   ##
=====================================
  Coverage   80.8%   80.8%           
=====================================
  Files        129     129           
  Lines      23199   23199           
=====================================
+ Hits       18745   18750    +5     
+ Misses      4454    4449    -5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant