Skip to content

Commit 764c94a

Browse files
cgillumCopilot
andcommitted
Address PR review feedback
- Add Schema.Task.EntityOperationName constant for entity operation tag - Replace magic string 'durabletask.entity.operation' with constant - Combine nested if statements for ParentTraceContext null check - Extract entity name from TargetInstanceId (format '@name@key') - Add comment explaining Server span trace context for entity batches Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 1d199d5 commit 764c94a

3 files changed

Lines changed: 29 additions & 12 deletions

File tree

src/Shared/Grpc/Tracing/Schema.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,10 @@ public static class Task
6060
/// The time at which the timer is scheduled to fire.
6161
/// </summary>
6262
public const string FireAt = "durabletask.fire_at";
63+
64+
/// <summary>
65+
/// The name of the entity operation being executed.
66+
/// </summary>
67+
public const string EntityOperationName = "durabletask.entity.operation";
6368
}
6469
}

src/Shared/Grpc/Tracing/TraceHelper.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ static class TraceHelper
202202

203203
if (!string.IsNullOrEmpty(operationName))
204204
{
205-
newActivity.SetTag("durabletask.entity.operation", operationName);
205+
newActivity.SetTag(Schema.Task.EntityOperationName, operationName);
206206
}
207207

208208
return newActivity;
@@ -526,7 +526,18 @@ static string CreateSpanName(string spanDescription, string? taskName, string? t
526526
return null;
527527
}
528528

529-
string entityName = historyEvent?.EntityOperationCalled?.TargetInstanceId ?? string.Empty;
529+
string targetInstanceId = historyEvent?.EntityOperationCalled?.TargetInstanceId ?? string.Empty;
530+
531+
// Extract entity name from instance ID (format: "@name@key")
532+
string entityName = targetInstanceId;
533+
if (targetInstanceId.Length > 1 && targetInstanceId[0] == '@')
534+
{
535+
int secondAt = targetInstanceId.IndexOf('@', 1);
536+
if (secondAt > 1)
537+
{
538+
entityName = targetInstanceId.Substring(1, secondAt - 1);
539+
}
540+
}
530541
string spanName = string.IsNullOrEmpty(calledEvent.Operation)
531542
? $"{TraceActivityConstants.EntityOperation}:{entityName}"
532543
: $"{TraceActivityConstants.EntityOperation}:{entityName}:{calledEvent.Operation}";
@@ -542,15 +553,13 @@ static string CreateSpanName(string spanDescription, string? taskName, string? t
542553
return null;
543554
}
544555

545-
if (calledEvent.ParentTraceContext != null)
556+
if (calledEvent.ParentTraceContext != null
557+
&& ActivityContext.TryParse(
558+
calledEvent.ParentTraceContext.TraceParent,
559+
calledEvent.ParentTraceContext?.TraceState,
560+
out ActivityContext parentContext))
546561
{
547-
if (ActivityContext.TryParse(
548-
calledEvent.ParentTraceContext.TraceParent,
549-
calledEvent.ParentTraceContext?.TraceState,
550-
out ActivityContext parentContext))
551-
{
552-
newActivity.SetSpanId(parentContext.SpanId.ToString());
553-
}
562+
newActivity.SetSpanId(parentContext.SpanId.ToString());
554563
}
555564

556565
newActivity.AddTag(Schema.Task.Type, TraceActivityConstants.EntityOperation);
@@ -559,7 +568,7 @@ static string CreateSpanName(string spanDescription, string? taskName, string? t
559568

560569
if (!string.IsNullOrEmpty(calledEvent.Operation))
561570
{
562-
newActivity.AddTag("durabletask.entity.operation", calledEvent.Operation);
571+
newActivity.AddTag(Schema.Task.EntityOperationName, calledEvent.Operation);
563572
}
564573

565574
return newActivity;

src/Worker/Grpc/GrpcDurableTaskWorker.Processor.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,10 @@ async Task OnRunEntityBatchAsync(
900900
var coreEntityId = DTCore.Entities.EntityId.FromString(batchRequest.InstanceId!);
901901
EntityId entityId = new(coreEntityId.Name, coreEntityId.Key);
902902

903-
// Start a trace span for entity operation execution using the first operation's trace context.
903+
// Start a Server trace span for entity operation execution using the first operation's
904+
// trace context. In multi-operation batches, only the first operation's trace context is
905+
// used as the parent. This is acceptable because entity batches are typically single-operation,
906+
// and individual Client spans are emitted per-operation in the orchestrator replay path.
904907
OperationRequest? firstOp = batchRequest.Operations?.FirstOrDefault();
905908
using Activity? traceActivity = TraceHelper.StartTraceActivityForEntityOperation(
906909
entityId.Name,

0 commit comments

Comments
 (0)