Skip to content
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

Fix delayed message null activity #7264

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@

public class When_incoming_message_was_delayed : OpenTelemetryAcceptanceTest // assuming W3C trace!
{
[Test]
public async Task By_sendoptions_Should_create_new_trace_and_set_current_activity()
{
var context = await Scenario.Define<Context>()
.WithEndpoint<TestEndpoint>(b => b
.When(s =>
{
var sendOptions = new SendOptions();
sendOptions.DelayDeliveryWith(TimeSpan.FromMilliseconds(100));
sendOptions.RouteToThisEndpoint();
return s.Send(new DelayedMessage(), sendOptions);
}))
.Done(c => c.DelayedMessageReceived)
.Run();

Assert.That(context.DelayedMessageCurrentActivityId, Is.Not.Null, "delayed message current activityId is not null");
}

[Test]
public async Task By_sendoptions_Should_create_new_trace_and_link_to_send()
{
Expand Down Expand Up @@ -141,6 +159,10 @@ class Context : ScenarioContext
public string IncomingMessageId { get; set; }
public string ReplyMessageId { get; set; }
public bool IncomingMessageReceived { get; set; }
public bool DelayedMessageReceived { get; set; }
#nullable enable
public string? DelayedMessageCurrentActivityId { get; set; }
#nullable disable
}
class SagaContext : ScenarioContext
{
Expand All @@ -162,6 +184,7 @@ public Task Handle(IncomingMessage message, IMessageHandlerContext context)
{
testContext.IncomingMessageId = context.MessageId;
testContext.IncomingMessageReceived = true;
//testContext.IncomingMessageCurrentActivityId = Activity.Current?.Id;
return context.Reply(new ReplyMessage());
}
}
Expand Down Expand Up @@ -198,6 +221,20 @@ public Task Handle(ReplyMessage message, IMessageHandlerContext context)
return Task.CompletedTask;
}
}

class DelayedMessageHandler : IHandleMessages<DelayedMessage>
{
Context testContext;

public DelayedMessageHandler(Context testContext) => this.testContext = testContext;

public Task Handle(DelayedMessage message, IMessageHandlerContext context)
{
testContext.DelayedMessageReceived = true;
testContext.DelayedMessageCurrentActivityId = Activity.Current?.Id;
return Task.CompletedTask;
}
}
}

class SagaOtelEndpoint : EndpointConfigurationBuilder
Expand Down Expand Up @@ -286,6 +323,10 @@ public class MessageToBeRetried : IMessage
{
}

public class DelayedMessage : IMessage
{
}

public class IncomingMessage : IMessage
{
}
Expand Down
5 changes: 3 additions & 2 deletions src/NServiceBus.Core/OpenTelemetry/Tracing/ActivityFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public Activity StartIncomingPipelineActivity(MessageContext context)
{
// create a new trace or root activity
ActivityLink[] links = [new ActivityLink(sendSpanContext)];
activity = ActivitySources.Main.StartActivity(name: ActivityNames.IncomingMessageActivityName, ActivityKind.Consumer, CreateNewRootActivityContext(), tags: null, links: links);
//pass in traceFlags of current activity, otherwise the activity returned is null
activity = ActivitySources.Main.StartActivity(name: ActivityNames.IncomingMessageActivityName, ActivityKind.Consumer, CreateNewRootActivityContext(sendSpanContext.TraceFlags), tags: null, links: links);
}
else
{
Expand Down Expand Up @@ -69,7 +70,7 @@ public Activity StartIncomingPipelineActivity(MessageContext context)
/// <summary>
/// This could be cleaned up once a dedicated API is created, see https://github.com/dotnet/runtime/issues/65528
/// </summary>
static ActivityContext CreateNewRootActivityContext() => new(Activity.TraceIdGenerator is null ? ActivityTraceId.CreateRandom() : Activity.TraceIdGenerator(), default, default, default);
static ActivityContext CreateNewRootActivityContext(ActivityTraceFlags traceFlags) => new(Activity.TraceIdGenerator is null ? ActivityTraceId.CreateRandom() : Activity.TraceIdGenerator(), default, traceFlags, default);

public Activity StartOutgoingPipelineActivity(string activityName, string displayName, IBehaviorContext outgoingContext)
{
Expand Down
Loading