Skip to content

fix: Avoid constructing events when telemetry is disabled#332

Merged
jlizen merged 4 commits into
mainfrom
check-disabled
May 2, 2026
Merged

fix: Avoid constructing events when telemetry is disabled#332
jlizen merged 4 commits into
mainfrom
check-disabled

Conversation

@rcoh
Copy link
Copy Markdown
Contributor

@rcoh rcoh commented Apr 30, 2026

To help ensure we don't regress on this, I extracted an if_enabled wrapper that can be used as well

rcoh added 2 commits April 30, 2026 18:45
The `enabled()` callback pattern now structurally prevents event
construction when disabled, making the counter-based test redundant.

Removes the static counter, the two helper functions, the four
`#[cfg(test)] fetch_add` calls in make_* helpers, and the
`hooks_skip_event_construction_when_disabled` test.
@rcoh rcoh requested a review from jlizen April 30, 2026 20:16
- Fix 3 broken rustdoc links: Self::enabled → Self::if_enabled,
  SharedState::enabled → SharedState::if_enabled
- Use is_enabled() instead of if_enabled(|_buf|) in flush_once CPU
  profiling path, since the EventBuffer token is unused and this is
  a control-flow decision, not an event-recording path
Copy link
Copy Markdown
Member

@jlizen jlizen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change seems fine but some somewhat load-bearing bike shedding about naming since there is an overlap with #256 .

Also, was there any good way to test these events never land in the buffer? Not seeing anything obvious that wouldn't involve adding a lot more machinery, which I don't think is worth it, but just asking

}

/// Returns whether telemetry is enabled
pub fn enabled() -> bool {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do is_enabled() instead for symmetry with shared_state() and (and also to avoid colliding with #256 's enabled() setter)?

There still will be some overlap with #256 's own is_enabled() but I suspect you would land first and they can rebase (but either ordering works fine)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah is_enabled is fine

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, you should be good to rebase on top of main and unify with #256 shortly, ping when ready for a new review

Unify PR #332 (if_enabled callback pattern) with PR #256 (Option<HandleInner> structure):

- TelemetryHandle methods delegate through inner.shared.if_enabled()
  to avoid event construction when disabled
- Replace static TelemetryHandle::enabled() with
  TelemetryHandle::current().is_enabled() (equivalent after #256)
- Keep all work inside with_encoder closures in tracing_layer
  (the core optimization from #332)
@jlizen jlizen enabled auto-merge May 2, 2026 00:16
@jlizen jlizen added this pull request to the merge queue May 2, 2026
Merged via the queue into main with commit 75ae5bc May 2, 2026
18 of 20 checks passed
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.

2 participants