-
Notifications
You must be signed in to change notification settings - Fork 402
[Instrumentation.Hangfire] Fix 'FailedToInjectActivityContext' when no ActivityContext exists #2990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Kielek
merged 6 commits into
open-telemetry:main
from
gao-artur:fix-FailedToInjectActivityContext
Aug 18, 2025
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8166e60
Fix 'FailedToInjectActivityContext' when no ActivityContext exists
gao-artur 1e20806
Update CHANGELOG
gao-artur 6ddce0a
Update src/OpenTelemetry.Instrumentation.Hangfire/Implementation/Hang…
gao-artur 0f3a354
Update src/OpenTelemetry.Instrumentation.Hangfire/Implementation/Hang…
gao-artur 3cc3eaf
make OpenTelemetryEventListener nested
gao-artur 99a8dff
Merge branch 'main' into fix-FailedToInjectActivityContext
Kielek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ public async Task Should_Create_Activity() | |
| using var tel = Sdk.CreateTracerProviderBuilder() | ||
| .AddHangfireInstrumentation() | ||
| .AddInMemoryExporter(exportedItems) | ||
| .SetSampler<AlwaysOnSampler>() | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For some reason, this test started to fail intermittently when I added a new test. I guess it's related to the static configuration in the |
||
| .Build(); | ||
|
|
||
| // Act | ||
|
|
@@ -170,6 +171,27 @@ public async Task Should_Respect_Filter_Option(string filter, bool shouldRecord) | |
| Assert.Equal(shouldRecord, activity.ActivityTraceFlags.HasFlag(ActivityTraceFlags.Recorded)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Should_Not_Inject_Invalid_Context() | ||
| { | ||
| // Arrange | ||
| var exportedItems = new List<Activity>(); | ||
| using var tel = Sdk.CreateTracerProviderBuilder() | ||
| .AddHangfireInstrumentation() | ||
| .AddInMemoryExporter(exportedItems) | ||
| .SetSampler<AlwaysOffSampler>() | ||
| .Build(); | ||
|
|
||
| using var listener = new OpenTelemetryEventListener(); | ||
|
|
||
| // Act | ||
| var jobId = BackgroundJob.Enqueue<TestJob>(x => x.Execute()); | ||
| await this.WaitJobProcessedAsync(jobId, 5); | ||
|
|
||
| // Assert | ||
| Assert.All(listener.Messages, args => Assert.NotEqual("FailedToInjectActivityContext", args.EventName)); | ||
| } | ||
|
|
||
| private async Task WaitJobProcessedAsync(string jobId, int timeToWaitInSeconds) | ||
| { | ||
| var timeout = TimeSpan.FromSeconds(timeToWaitInSeconds); | ||
|
|
||
61 changes: 61 additions & 0 deletions
61
test/OpenTelemetry.Instrumentation.Hangfire.Tests/OpenTelemetryEventListener.cs
|
gao-artur marked this conversation as resolved.
Outdated
|
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using System.Diagnostics.Tracing; | ||
|
|
||
| namespace OpenTelemetry.Instrumentation.Hangfire.Tests; | ||
|
|
||
| internal class OpenTelemetryEventListener : EventListener | ||
| { | ||
| private const string EventSourceName = "OpenTelemetry-Api"; | ||
|
|
||
| private readonly Queue<EventWrittenEventArgs> events = new(); | ||
| private readonly AutoResetEvent eventWritten = new(false); | ||
| private EventSource? apiEventSource; | ||
|
|
||
| public IEnumerable<EventWrittenEventArgs> Messages | ||
| { | ||
| get | ||
| { | ||
| if (this.events.Count == 0) | ||
| { | ||
| this.eventWritten.WaitOne(TimeSpan.FromSeconds(3)); | ||
| } | ||
|
|
||
| while (this.events.Count != 0) | ||
| { | ||
| yield return this.events.Dequeue(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public override void Dispose() | ||
| { | ||
| if (this.apiEventSource != null) | ||
| { | ||
| this.DisableEvents(this.apiEventSource); | ||
| } | ||
|
|
||
| base.Dispose(); | ||
| } | ||
|
|
||
| protected override void OnEventSourceCreated(EventSource eventSource) | ||
| { | ||
| if (eventSource.Name == EventSourceName) | ||
| { | ||
| this.apiEventSource = eventSource; | ||
| this.EnableEvents(eventSource, EventLevel.Verbose, EventKeywords.All); | ||
| } | ||
|
|
||
| base.OnEventSourceCreated(eventSource); | ||
| } | ||
|
|
||
| protected override void OnEventWritten(EventWrittenEventArgs eventData) | ||
| { | ||
| if (eventData.EventSource.Name == EventSourceName) | ||
| { | ||
| this.events.Enqueue(eventData); | ||
| this.eventWritten.Set(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.