Skip to content

Commit

Permalink
Don't create handler activity when incoming message activity isn't set (
Browse files Browse the repository at this point in the history
  • Loading branch information
timbussmann authored Nov 3, 2022
1 parent 49d6733 commit 89fd67b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/NServiceBus.Core.Tests/OpenTelemetry/ActivityFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,23 @@ public void Should_set_activity_in_context()

class StartHandlerActivity : ActivityFactoryTests
{
[Test]
public void Should_not_start_activity_when_no_parent_activity_exists()
{
Type handlerType = typeof(StartHandlerActivity);
var activity = activityFactory.StartHandlerActivity(new MessageHandler((_, _, _) => Task.CompletedTask, handlerType), null);

Assert.IsNull(activity, "should not start handler activity when no parent activity exists");
}

[Test]
public void Should_set_handler_type_as_tag()
{
Type handlerType = typeof(StartHandlerActivity);

using var ambientActivity = new Activity("ambient activity");
ambientActivity.Start();

var activity = activityFactory.StartHandlerActivity(new MessageHandler((_, _, _) => Task.CompletedTask, handlerType), null);

Assert.IsNotNull(activity);
Expand All @@ -237,6 +250,9 @@ public void Should_set_saga_id_when_saga()
{
var sagaInstance = new ActiveSagaInstance(null, null, () => DateTimeOffset.UtcNow) { SagaId = Guid.NewGuid().ToString() };

using var ambientActivity = new Activity("ambient activity");
ambientActivity.Start();

var activity = activityFactory.StartHandlerActivity(new MessageHandler((_, _, _) => Task.CompletedTask, typeof(StartHandlerActivity)), sagaInstance);

Assert.IsNotNull(activity);
Expand Down
6 changes: 6 additions & 0 deletions src/NServiceBus.Core/OpenTelemetry/Tracing/ActivityFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ public Activity StartOutgoingPipelineActivity(string activityName, string displa

public Activity StartHandlerActivity(MessageHandler messageHandler, ActiveSagaInstance saga)
{
if (Activity.Current == null)
{
// don't call StartActivity if we haven't started an activity from the incoming pipeline to avoid the handlers being sampled although the incoming message isn't.
return null;
}

var activity = ActivitySources.Main.StartActivity(ActivityNames.InvokeHandlerActivityName);

if (activity != null)
Expand Down

0 comments on commit 89fd67b

Please sign in to comment.