-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathTraceHelper.cs
More file actions
633 lines (549 loc) · 25.3 KB
/
TraceHelper.cs
File metadata and controls
633 lines (549 loc) · 25.3 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Diagnostics;
using DurableTask.Core.Command;
using Google.Protobuf.WellKnownTypes;
using P = Microsoft.DurableTask.Protobuf;
namespace Microsoft.DurableTask.Tracing;
/// <summary>
/// Methods for starting and managing trace activities related to Durable Task operations.
/// </summary>
/// <remarks>
/// Adapted from "https://github.com/Azure/durabletask/blob/main/src/DurableTask.Core/Tracing/TraceHelper.cs".
/// </remarks>
static class TraceHelper
{
const string Source = "Microsoft.DurableTask";
static readonly ActivitySource ActivityTraceSource = new ActivitySource(Source);
/// <summary>
/// Starts a new trace activity for scheduling an orchestration from the client.
/// </summary>
/// <param name="createInstanceRequest">The orchestration's creation request.</param>
/// <returns>
/// Returns a newly started <see cref="Activity"/> with orchestration-specific metadata.
/// </returns>
public static Activity? StartActivityForNewOrchestration(P.CreateInstanceRequest createInstanceRequest)
{
Activity? newActivity = ActivityTraceSource.StartActivity(
name: CreateSpanName(
TraceActivityConstants.CreateOrchestration,
createInstanceRequest.Name,
createInstanceRequest.Version),
kind: ActivityKind.Producer);
if (newActivity != null)
{
newActivity.SetTag(Schema.Task.Type, TraceActivityConstants.Orchestration);
newActivity.SetTag(Schema.Task.Name, createInstanceRequest.Name);
newActivity.SetTag(Schema.Task.InstanceId, createInstanceRequest.InstanceId);
newActivity.SetTag(Schema.Task.ExecutionId, createInstanceRequest.ExecutionId);
if (!string.IsNullOrEmpty(createInstanceRequest.Version))
{
newActivity.SetTag(Schema.Task.Version, createInstanceRequest.Version);
}
createInstanceRequest.ParentTraceContext ??= new P.TraceContext();
createInstanceRequest.ParentTraceContext.TraceParent = newActivity.Id!;
createInstanceRequest.ParentTraceContext.TraceState = newActivity.TraceStateString;
}
return newActivity;
}
/// <summary>
/// Starts a new trace activity for orchestration execution.
/// </summary>
/// <param name="startEvent">The orchestration's execution started event.</param>
/// <param name="orchestrationTraceContext">The orchestration trace context containing span metadata.</param>
/// <returns>
/// Returns a newly started <see cref="Activity"/> with orchestration-specific metadata.
/// </returns>
public static Activity? StartTraceActivityForOrchestrationExecution(
P.ExecutionStartedEvent? startEvent,
P.OrchestrationTraceContext? orchestrationTraceContext)
{
if (startEvent == null)
{
return null;
}
if (startEvent.ParentTraceContext is null
|| !ActivityContext.TryParse(
startEvent.ParentTraceContext.TraceParent,
startEvent.ParentTraceContext.TraceState,
out ActivityContext activityContext))
{
return null;
}
string activityName = CreateSpanName(TraceActivityConstants.Orchestration, startEvent.Name, startEvent.Version);
ActivityKind activityKind = ActivityKind.Server;
DateTimeOffset startTime = orchestrationTraceContext?.SpanStartTime?.ToDateTimeOffset() ?? default;
Activity? activity = ActivityTraceSource.StartActivity(
activityName,
kind: activityKind,
parentContext: activityContext,
startTime: startTime);
if (activity == null)
{
return null;
}
activity.SetTag(Schema.Task.Type, TraceActivityConstants.Orchestration);
activity.SetTag(Schema.Task.Name, startEvent.Name);
activity.SetTag(Schema.Task.InstanceId, startEvent.OrchestrationInstance.InstanceId);
if (!string.IsNullOrEmpty(startEvent.Version))
{
activity.SetTag(Schema.Task.Version, startEvent.Version);
}
if (orchestrationTraceContext?.SpanID != null)
{
activity.SetSpanId(orchestrationTraceContext.SpanID!);
}
return activity;
}
/// <summary>
/// Starts a new trace activity for (task) activity execution.
/// </summary>
/// <param name="request">The associated request to start a (task) activity.</param>
/// <returns>
/// Returns a newly started <see cref="Activity"/> with (task) activity and orchestration-specific metadata.
/// </returns>
public static Activity? StartTraceActivityForTaskExecution(
P.ActivityRequest request)
{
if (request.ParentTraceContext is null
|| !ActivityContext.TryParse(
request.ParentTraceContext.TraceParent,
request.ParentTraceContext.TraceState,
out ActivityContext activityContext))
{
return null;
}
Activity? newActivity = ActivityTraceSource.StartActivity(
CreateSpanName(TraceActivityConstants.Activity, request.Name, request.Version),
kind: ActivityKind.Server,
parentContext: activityContext);
if (newActivity == null)
{
return null;
}
newActivity.SetTag(Schema.Task.Type, TraceActivityConstants.Activity);
newActivity.SetTag(Schema.Task.Name, request.Name);
newActivity.SetTag(Schema.Task.InstanceId, request.OrchestrationInstance.InstanceId);
newActivity.SetTag(Schema.Task.TaskId, request.TaskId);
if (!string.IsNullOrEmpty(request.Version))
{
newActivity.SetTag(Schema.Task.Version, request.Version);
}
return newActivity;
}
/// <summary>
/// Starts a new trace activity for executing an entity operation.
/// </summary>
/// <param name="entityName">The name of the entity.</param>
/// <param name="operationName">The name of the operation being executed.</param>
/// <param name="instanceId">The instance ID of the entity.</param>
/// <param name="traceParent">The W3C traceparent header value from the parent orchestration.</param>
/// <param name="traceState">The W3C tracestate header value from the parent orchestration.</param>
/// <returns>
/// Returns a newly started <see cref="Activity"/> with entity operation metadata, or null if tracing is not enabled.
/// </returns>
public static Activity? StartTraceActivityForEntityOperation(
string entityName,
string? operationName,
string instanceId,
string? traceParent,
string? traceState)
{
if (traceParent is null || !ActivityContext.TryParse(
traceParent,
traceState,
out ActivityContext activityContext))
{
return null;
}
string spanName = string.IsNullOrEmpty(operationName)
? $"{TraceActivityConstants.EntityOperation}:{entityName}"
: $"{TraceActivityConstants.EntityOperation}:{entityName}:{operationName}";
Activity? newActivity = ActivityTraceSource.StartActivity(
spanName,
kind: ActivityKind.Server,
parentContext: activityContext);
if (newActivity == null)
{
return null;
}
newActivity.SetTag(Schema.Task.Type, TraceActivityConstants.EntityOperation);
newActivity.SetTag(Schema.Task.Name, entityName);
newActivity.SetTag(Schema.Task.InstanceId, instanceId);
if (!string.IsNullOrEmpty(operationName))
{
newActivity.SetTag(Schema.Task.EntityOperationName, operationName);
}
return newActivity;
}
/// <summary>
/// Emits a new trace activity for a (task) activity that successfully completes.
/// </summary>
/// <param name="instanceId">The ID of the associated orchestration.</param>
/// <param name="historyEvent">The associated <see cref="P.HistoryEvent" />.</param>
/// <param name="taskScheduledEvent">The associated <see cref="P.TaskScheduledEvent"/>.</param>
public static void EmitTraceActivityForTaskCompleted(
string? instanceId,
P.HistoryEvent? historyEvent,
P.TaskScheduledEvent? taskScheduledEvent)
{
// The parent of this is the parent orchestration span ID. It should be the client span which started this
Activity? activity = StartTraceActivityForSchedulingTask(instanceId, historyEvent, taskScheduledEvent);
activity?.Dispose();
}
/// <summary>
/// Emits a new trace activity for a (task) activity that fails.
/// </summary>
/// <param name="instanceId">The ID of the associated orchestration.</param>
/// <param name="historyEvent">The associated <see cref="P.HistoryEvent" />.</param>
/// <param name="taskScheduledEvent">The associated <see cref="P.TaskScheduledEvent"/>.</param>
/// <param name="failedEvent">The associated <see cref="P.TaskFailedEvent"/>.</param>
public static void EmitTraceActivityForTaskFailed(
string? instanceId,
P.HistoryEvent? historyEvent,
P.TaskScheduledEvent? taskScheduledEvent,
P.TaskFailedEvent? failedEvent)
{
Activity? activity = StartTraceActivityForSchedulingTask(instanceId, historyEvent, taskScheduledEvent);
if (activity is null)
{
return;
}
if (failedEvent != null)
{
string statusDescription = failedEvent.FailureDetails?.ErrorMessage ?? "Unspecified task activity failure";
activity?.SetStatus(ActivityStatusCode.Error, statusDescription);
}
activity?.Dispose();
}
/// <summary>
/// Emits a new trace activity for an entity operation that successfully completes.
/// </summary>
/// <param name="instanceId">The ID of the associated orchestration.</param>
/// <param name="historyEvent">The associated <see cref="P.HistoryEvent" />.</param>
/// <param name="calledEvent">The associated <see cref="P.EntityOperationCalledEvent"/>.</param>
public static void EmitTraceActivityForEntityOperationCompleted(
string? instanceId,
P.HistoryEvent? historyEvent,
P.EntityOperationCalledEvent? calledEvent)
{
Activity? activity = StartTraceActivityForSchedulingEntityOperation(instanceId, historyEvent, calledEvent);
activity?.Dispose();
}
/// <summary>
/// Emits a new trace activity for an entity operation that fails.
/// </summary>
/// <param name="instanceId">The ID of the associated orchestration.</param>
/// <param name="historyEvent">The associated <see cref="P.HistoryEvent" />.</param>
/// <param name="calledEvent">The associated <see cref="P.EntityOperationCalledEvent"/>.</param>
/// <param name="failedEvent">The associated <see cref="P.EntityOperationFailedEvent"/>.</param>
public static void EmitTraceActivityForEntityOperationFailed(
string? instanceId,
P.HistoryEvent? historyEvent,
P.EntityOperationCalledEvent? calledEvent,
P.EntityOperationFailedEvent? failedEvent)
{
Activity? activity = StartTraceActivityForSchedulingEntityOperation(instanceId, historyEvent, calledEvent);
if (activity is null)
{
return;
}
if (failedEvent != null)
{
string statusDescription = failedEvent.FailureDetails?.ErrorMessage ?? "Unspecified entity operation failure";
activity.SetStatus(ActivityStatusCode.Error, statusDescription);
}
activity.Dispose();
}
/// <summary>
/// Emits a new trace activity for sub-orchestration execution when the sub-orchestration
/// completes successfully.
/// </summary>
/// <param name="instanceId">The ID of the associated orchestration.</param>
/// <param name="historyEvent">The associated <see cref="P.HistoryEvent" />.</param>
/// <param name="createdEvent">The associated <see cref="P.SubOrchestrationInstanceCreatedEvent"/>.</param>
public static void EmitTraceActivityForSubOrchestrationCompleted(
string? instanceId,
P.HistoryEvent? historyEvent,
P.SubOrchestrationInstanceCreatedEvent? createdEvent)
{
// The parent of this is the parent orchestration span ID. It should be the client span which started this
Activity? activity = CreateTraceActivityForSchedulingSubOrchestration(instanceId, historyEvent, createdEvent);
activity?.Dispose();
}
/// <summary>
/// Emits a new trace activity for sub-orchestration execution when the sub-orchestration fails.
/// </summary>
/// <param name="instanceId">The ID of the associated orchestration.</param>
/// <param name="historyEvent">The associated <see cref="P.HistoryEvent" />.</param>
/// <param name="createdEvent">The associated <see cref="P.SubOrchestrationInstanceCreatedEvent"/>.</param>
/// <param name="failedEvent">The associated <see cref="P.SubOrchestrationInstanceFailedEvent"/>.</param>
public static void EmitTraceActivityForSubOrchestrationFailed(
string? instanceId,
P.HistoryEvent? historyEvent,
P.SubOrchestrationInstanceCreatedEvent? createdEvent,
P.SubOrchestrationInstanceFailedEvent? failedEvent)
{
Activity? activity = CreateTraceActivityForSchedulingSubOrchestration(instanceId, historyEvent, createdEvent);
if (activity is null)
{
return;
}
if (failedEvent != null)
{
string statusDescription = failedEvent.FailureDetails.ErrorMessage
?? "Unspecified sub-orchestration failure";
activity?.SetStatus(ActivityStatusCode.Error, statusDescription);
}
activity?.Dispose();
}
/// <summary>
/// Emits a new trace activity for events raised from the worker.
/// </summary>
/// <param name="eventRaisedEvent">The associated <see cref="SendEventOrchestratorAction"/>.</param>
/// <param name="instanceId">The instance ID of the associated orchestration.</param>
/// <param name="executionId">The execution ID of the associated orchestration.</param>
/// <returns>
/// Returns a newly started <see cref="Activity"/> with (task) activity and orchestration-specific metadata.
/// </returns>
public static Activity? StartTraceActivityForEventRaisedFromWorker(
SendEventOrchestratorAction eventRaisedEvent,
string? instanceId,
string? executionId)
{
Activity? newActivity = ActivityTraceSource.StartActivity(
CreateSpanName(TraceActivityConstants.OrchestrationEvent, eventRaisedEvent.EventName, null),
kind: ActivityKind.Producer,
parentContext: Activity.Current?.Context ?? default);
if (newActivity == null)
{
return null;
}
newActivity.AddTag(Schema.Task.Type, TraceActivityConstants.Event);
newActivity.AddTag(Schema.Task.Name, eventRaisedEvent.EventName);
newActivity.AddTag(Schema.Task.InstanceId, instanceId);
newActivity.AddTag(Schema.Task.ExecutionId, executionId);
if (!string.IsNullOrEmpty(eventRaisedEvent.Instance?.InstanceId))
{
newActivity.AddTag(Schema.Task.EventTargetInstanceId, eventRaisedEvent.Instance!.InstanceId);
}
return newActivity;
}
/// <summary>
/// Creates a new trace activity for events created from the client.
/// </summary>
/// <param name="eventRaised">The associated <see cref="P.EventRaisedEvent"/>.</param>
/// <param name="instanceId">The ID of the associated orchestration.</param>
/// <returns>
/// Returns a newly started <see cref="Activity"/> with (task) activity and orchestration-specific metadata.
/// </returns>
public static Activity? StartActivityForNewEventRaisedFromClient(P.RaiseEventRequest eventRaised, string instanceId)
{
Activity? newActivity = ActivityTraceSource.StartActivity(
CreateSpanName(TraceActivityConstants.OrchestrationEvent, eventRaised.Name, null),
kind: ActivityKind.Producer,
parentContext: Activity.Current?.Context ?? default,
tags: new KeyValuePair<string, object?>[]
{
new(Schema.Task.Type, TraceActivityConstants.Event),
new(Schema.Task.Name, eventRaised.Name),
new(Schema.Task.EventTargetInstanceId, instanceId),
});
return newActivity;
}
/// <summary>
/// Emits a new trace activity for timers.
/// </summary>
/// <param name="instanceId">The ID of the associated orchestration.</param>
/// <param name="orchestrationName">The name of the orchestration invoking the timer.</param>
/// <param name="startTime">The timer's start time.</param>
/// <param name="timerFiredEvent">The associated <see cref="P.TimerFiredEvent"/>.</param>
public static void EmitTraceActivityForTimer(
string? instanceId,
string orchestrationName,
DateTime startTime,
P.TimerFiredEvent timerFiredEvent)
{
Activity? newActivity = ActivityTraceSource.StartActivity(
CreateTimerSpanName(orchestrationName),
kind: ActivityKind.Internal,
startTime: startTime,
parentContext: Activity.Current?.Context ?? default);
if (newActivity is not null)
{
newActivity.AddTag(Schema.Task.Type, TraceActivityConstants.Timer);
newActivity.AddTag(Schema.Task.Name, orchestrationName);
newActivity.AddTag(Schema.Task.InstanceId, instanceId);
newActivity.AddTag(Schema.Task.FireAt, timerFiredEvent.FireAt.ToDateTime().ToString("o"));
newActivity.AddTag(Schema.Task.TaskId, timerFiredEvent.TimerId);
newActivity.Dispose();
}
}
static string CreateSpanName(string spanDescription, string? taskName, string? taskVersion)
{
if (!string.IsNullOrEmpty(taskVersion))
{
return $"{spanDescription}:{taskName}@({taskVersion})";
}
else
{
return $"{spanDescription}:{taskName}";
}
}
/// <summary>
/// Starts a new trace activity for (task) activity that represents the time between when the task message
/// is enqueued and when the response message is received.
/// </summary>
/// <param name="instanceId">The ID of the associated instance.</param>
/// <param name="historyEvent">The associated <see cref="P.HistoryEvent" />.</param>
/// <param name="taskScheduledEvent">The associated <see cref="P.TaskScheduledEvent"/>.</param>
/// <returns>
/// Returns a newly started <see cref="Activity"/> with (task) activity and orchestration-specific metadata.
/// </returns>
static Activity? StartTraceActivityForSchedulingTask(
string? instanceId,
P.HistoryEvent? historyEvent,
P.TaskScheduledEvent? taskScheduledEvent)
{
if (taskScheduledEvent == null)
{
return null;
}
Activity? newActivity = ActivityTraceSource.StartActivity(
CreateSpanName(TraceActivityConstants.Activity, taskScheduledEvent.Name, taskScheduledEvent.Version),
kind: ActivityKind.Client,
startTime: historyEvent?.Timestamp?.ToDateTimeOffset() ?? default,
parentContext: Activity.Current?.Context ?? default);
if (newActivity == null)
{
return null;
}
if (taskScheduledEvent.ParentTraceContext != null)
{
if (ActivityContext.TryParse(
taskScheduledEvent.ParentTraceContext.TraceParent,
taskScheduledEvent.ParentTraceContext?.TraceState,
out ActivityContext parentContext))
{
newActivity.SetSpanId(parentContext.SpanId.ToString());
}
}
newActivity.AddTag(Schema.Task.Type, TraceActivityConstants.Activity);
newActivity.AddTag(Schema.Task.Name, taskScheduledEvent.Name);
newActivity.AddTag(Schema.Task.InstanceId, instanceId);
newActivity.AddTag(Schema.Task.TaskId, historyEvent?.EventId);
if (!string.IsNullOrEmpty(taskScheduledEvent.Version))
{
newActivity.AddTag(Schema.Task.Version, taskScheduledEvent.Version);
}
return newActivity;
}
/// <summary>
/// Starts a new trace activity for scheduling an entity operation. Represents the time between
/// enqueuing the entity operation message and it completing.
/// </summary>
/// <param name="instanceId">The ID of the associated orchestration.</param>
/// <param name="historyEvent">The associated <see cref="P.HistoryEvent" />.</param>
/// <param name="calledEvent">The associated <see cref="P.EntityOperationCalledEvent"/>.</param>
/// <returns>
/// Returns a newly started <see cref="Activity"/> with entity operation metadata.
/// </returns>
static Activity? StartTraceActivityForSchedulingEntityOperation(
string? instanceId,
P.HistoryEvent? historyEvent,
P.EntityOperationCalledEvent? calledEvent)
{
if (calledEvent == null)
{
return null;
}
string targetInstanceId = historyEvent?.EntityOperationCalled?.TargetInstanceId ?? string.Empty;
// Extract entity name from instance ID (format: "@name@key")
string entityName = targetInstanceId;
if (targetInstanceId.Length > 1 && targetInstanceId[0] == '@')
{
int secondAt = targetInstanceId.IndexOf('@', 1);
if (secondAt > 1)
{
entityName = targetInstanceId.Substring(1, secondAt - 1);
}
}
string spanName = string.IsNullOrEmpty(calledEvent.Operation)
? $"{TraceActivityConstants.EntityOperation}:{entityName}"
: $"{TraceActivityConstants.EntityOperation}:{entityName}:{calledEvent.Operation}";
Activity? newActivity = ActivityTraceSource.StartActivity(
spanName,
kind: ActivityKind.Client,
startTime: historyEvent?.Timestamp?.ToDateTimeOffset() ?? default,
parentContext: Activity.Current?.Context ?? default);
if (newActivity == null)
{
return null;
}
if (calledEvent.ParentTraceContext != null
&& ActivityContext.TryParse(
calledEvent.ParentTraceContext.TraceParent,
calledEvent.ParentTraceContext?.TraceState,
out ActivityContext parentContext))
{
newActivity.SetSpanId(parentContext.SpanId.ToString());
}
newActivity.AddTag(Schema.Task.Type, TraceActivityConstants.EntityOperation);
newActivity.AddTag(Schema.Task.Name, entityName);
newActivity.AddTag(Schema.Task.InstanceId, instanceId);
if (!string.IsNullOrEmpty(calledEvent.Operation))
{
newActivity.AddTag(Schema.Task.EntityOperationName, calledEvent.Operation);
}
return newActivity;
}
/// <summary>
/// Starts a new trace activity for sub-orchestrations. Represents the time between enqueuing
/// the sub-orchestration message and it completing.
/// </summary>
/// <param name="instanceId">The ID of the associated orchestration.</param>
/// <param name="historyEvent">The associated <see cref="P.HistoryEvent" />.</param>
/// <param name="createdEvent">The associated <see cref="P.SubOrchestrationInstanceCreatedEvent"/>.</param>
/// <returns>
/// Returns a newly started <see cref="Activity"/> with (task) activity and orchestration-specific metadata.
/// </returns>
static Activity? CreateTraceActivityForSchedulingSubOrchestration(
string? instanceId,
P.HistoryEvent? historyEvent,
P.SubOrchestrationInstanceCreatedEvent? createdEvent)
{
if (instanceId == null || createdEvent == null)
{
return null;
}
Activity? activity = ActivityTraceSource.StartActivity(
CreateSpanName(TraceActivityConstants.Orchestration, createdEvent.Name, createdEvent.Version),
kind: ActivityKind.Client,
startTime: historyEvent?.Timestamp?.ToDateTimeOffset() ?? default,
parentContext: Activity.Current?.Context ?? default);
if (activity == null)
{
return null;
}
if (createdEvent.ParentTraceContext != null
&& ActivityContext.TryParse(
createdEvent.ParentTraceContext.TraceParent,
createdEvent.ParentTraceContext.TraceState,
out ActivityContext parentContext))
{
activity.SetSpanId(parentContext.SpanId.ToString());
}
activity.SetTag(Schema.Task.Type, TraceActivityConstants.Orchestration);
activity.SetTag(Schema.Task.Name, createdEvent.Name);
activity.SetTag(Schema.Task.InstanceId, instanceId);
if (!string.IsNullOrEmpty(createdEvent.Version))
{
activity.SetTag(Schema.Task.Version, createdEvent.Version);
}
return activity;
}
static string CreateTimerSpanName(string orchestrationName)
{
return $"{TraceActivityConstants.Orchestration}:{orchestrationName}:{TraceActivityConstants.Timer}";
}
}