fix: use TimeProvider for span end time in BrighterTracer (#4087)#4088
Merged
iancooper merged 2 commits intoBrighterCommand:masterfrom Apr 26, 2026
Merged
Conversation
iancooper
approved these changes
Apr 26, 2026
Member
iancooper
left a comment
There was a problem hiding this comment.
Thanks, and thanks for the test
…mmand#4087) `BrighterTracer.EndSpan` disposed the Activity without setting an explicit end time, so `Activity.Stop` fell back to raw `DateTime.UtcNow` and ignored the injected `TimeProvider`. Under any non-system TimeProvider (e.g. `FakeTimeProvider` in tests) span start used simulated time but end used wall clock, so `Activity.Duration` was meaningless. Set the end time explicitly via `_timeProvider.GetUtcNow()` before disposing. `EndSpans` flows through `EndSpan` and is covered.
7d6378f to
8d1856f
Compare
There was a problem hiding this comment.
Gates Passed
4 Quality Gates Passed
See analysis details in CodeScene
Quality Gate Profile: Clean Code Collective
Install CodeScene MCP: safeguard and uplift AI-generated code. Catch issues early with our IDE extension and CLI tool.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #4087.
BrighterTracer.EndSpandisposed theActivitywithout setting an explicit end time, soActivity.Stop(called fromDispose) fell back to rawDateTime.UtcNowand ignored the injectedTimeProvider. Span start times use_timeProvider.GetUtcNow(), but end times did not — under any non-systemTimeProvider(e.g.FakeTimeProviderin tests) the start used simulated time and the end used real wall clock, soActivity.Durationwas wildly wrong (often hundreds of days off, depending on how far the simulated clock was from real time).Change
src/Paramore.Brighter/Observability/BrighterTracer.cs—EndSpannow callsspan.SetEndTime(_timeProvider.GetUtcNow().UtcDateTime)beforeDispose().Activity.Stophonours the explicit end time when one has been set, so the duration is now(end via TimeProvider) - (start via TimeProvider)and is correct under bothTimeProvider.System(production) and any custom provider (tests, deterministic replay, scheduling integration).EndSpansflows throughEndSpan, so it is covered without further change.Test plan
BrighterTracerEndSpanTimeProviderTests.When_Ending_A_Span_Duration_Reflects_TimeProvidercreates a span underFakeTimeProvider, advances by 5s, ends the span, and assertsDuration == TimeSpan.FromSeconds(5).Expected 00:00:05, Actual 115.13:19:00…(wall-clock leak).net9.0.Notes
The issue also called out an optional secondary fix — moving the
nowsnapshot to immediately beforeStartActivityin eachCreate*Spanmethod so tag-construction overhead (JsonSerializer.Serialize(...)forHeader/Body) is excluded from span start. That is acknowledged in the issue as "negligible in production" and is not addressed here to keep this change minimal and focused on the correctness bug. Happy to follow up in a separate PR if desired.