Skip to content

Commit

Permalink
create a new trace when user requests us to
Browse files Browse the repository at this point in the history
  • Loading branch information
lailabougria committed Jun 3, 2024
1 parent cb11608 commit 40b29ae
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/NServiceBus.Core/OpenTelemetry/Tracing/ActivityFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ public Activity StartIncomingPipelineActivity(MessageContext context)
var incomingTraceParentExists = context.Headers.TryGetValue(Headers.DiagnosticsTraceParent, out var sendSpanId);
var activityContextCreatedFromIncomingTraceParent = ActivityContext.TryParse(sendSpanId, null, out var sendSpanContext);

if (context.Extensions.TryGet(out Activity transportActivity) && transportActivity != null) // attach to transport span but link receive pipeline span to send pipeline span
if (context.Extensions.TryGet(out Activity transportActivity) && transportActivity != null)
{
// attach to transport span but link receive pipeline span to send pipeline span
ActivityLink[] links = null;
if (incomingTraceParentExists && sendSpanId != transportActivity.Id)
{
Expand All @@ -24,9 +25,22 @@ public Activity StartIncomingPipelineActivity(MessageContext context)
}
}

activity = ActivitySources.Main.CreateActivity(name: ActivityNames.IncomingMessageActivityName,
ActivityKind.Consumer, transportActivity.Context, links: links, idFormat: ActivityIdFormat.W3C);

if (context.Headers.ContainsKey(Headers.StartNewTrace))
{
// The user indicated to start a new trace when receiving this message but there's a transport span
// so check whether a new trace was already started by the transport client SDK
if (incomingTraceParentExists && !sendSpanContext.TraceId.Equals(transportActivity.TraceId))
{
// no new trace was started, so start a new one
activity = ActivitySources.Main.StartActivity(name: ActivityNames.IncomingMessageActivityName, ActivityKind.Consumer, CreateNewRootActivityContext(), tags: null, links: links);
}
}
else
{
// no new trace was requested, so create a child span of the transport span, linking receive pipeline span to send pipeline span
activity = ActivitySources.Main.CreateActivity(name: ActivityNames.IncomingMessageActivityName,
ActivityKind.Consumer, transportActivity.Context, links: links, idFormat: ActivityIdFormat.W3C);
}
}
else if (incomingTraceParentExists && activityContextCreatedFromIncomingTraceParent) // otherwise directly create child from logical send
{
Expand Down

0 comments on commit 40b29ae

Please sign in to comment.