-
Notifications
You must be signed in to change notification settings - Fork 648
/
Copy pathActivityFactory.cs
88 lines (70 loc) · 3.25 KB
/
ActivityFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
namespace NServiceBus;
using System.Diagnostics;
using Pipeline;
using Sagas;
using Transport;
class ActivityFactory : IActivityFactory
{
public Activity StartIncomingActivity(MessageContext context)
{
Activity activity;
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 (context.Headers.TryGetValue(Headers.DiagnosticsTraceParent, out var sendSpanId) && sendSpanId != transportActivity.Id)
{
if (ActivityContext.TryParse(sendSpanId, null, out var sendSpanContext))
{
links = new[] { new ActivityLink(sendSpanContext) };
}
}
activity = ActivitySources.Main.CreateActivity(name: ActivityNames.IncomingMessageActivityName,
ActivityKind.Consumer, transportActivity.Context, links: links, idFormat: ActivityIdFormat.W3C);
}
else if (context.Headers.TryGetValue(Headers.DiagnosticsTraceParent, out var sendSpanId) && ActivityContext.TryParse(sendSpanId, null, out var sendSpanContext)) // otherwise directly create child from logical send
{
activity = ActivitySources.Main.CreateActivity(name: ActivityNames.IncomingMessageActivityName, ActivityKind.Consumer, sendSpanContext);
}
else // otherwise start new trace
{
// This will set Activity.Current as parent if available
activity = ActivitySources.Main.CreateActivity(name: ActivityNames.IncomingMessageActivityName, ActivityKind.Consumer);
}
if (activity != null)
{
ContextPropagation.PropagateContextFromHeaders(activity, context.Headers);
activity.DisplayName = ActivityDisplayNames.ProcessMessage;
activity.SetIdFormat(ActivityIdFormat.W3C);
activity.AddTag(ActivityTags.NativeMessageId, context.NativeMessageId);
ActivityDecorator.PromoteHeadersToTags(activity, context.Headers);
activity.Start();
}
return activity;
}
public Activity StartOutgoingPipelineActivity(string activityName, string displayName, IBehaviorContext outgoingContext)
{
var activity = ActivitySources.Main.CreateActivity(activityName, ActivityKind.Producer);
if (activity != null)
{
activity.SetIdFormat(ActivityIdFormat.W3C);
activity.DisplayName = displayName;
activity.Start();
outgoingContext.Extensions.SetOutgoingPipelineActitvity(activity);
}
return activity;
}
public Activity StartHandlerActivity(MessageHandler messageHandler, ActiveSagaInstance saga)
{
var activity = ActivitySources.Main.StartActivity(ActivityNames.InvokeHandlerActivityName);
if (activity != null)
{
activity.DisplayName = messageHandler.HandlerType.Name;
activity.AddTag(ActivityTags.HandlerType, messageHandler.HandlerType.FullName);
if (saga != null)
{
activity.AddTag(ActivityTags.HandlerSagaId, saga.SagaId);
}
}
return activity;
}
}