Skip to content

Commit 5c0c9fc

Browse files
committed
refactor(tracing): unify event span emission methods and update tests
1 parent c0eb504 commit 5c0c9fc

File tree

4 files changed

+18
-63
lines changed

4 files changed

+18
-63
lines changed

client/src/main/java/com/microsoft/durabletask/DurableTaskGrpcClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public void raiseEvent(String instanceId, String eventName, Object eventPayload)
170170
Helpers.throwIfArgumentNull(eventName, "eventName");
171171

172172
// Emit an event span StartActivityForNewEventRaisedFromClient
173-
TracingHelper.emitEventRaisedFromClientSpan(eventName, instanceId);
173+
TracingHelper.emitEventSpan(eventName, null, instanceId);
174174

175175
RaiseEventRequest.Builder builder = RaiseEventRequest.newBuilder()
176176
.setInstanceId(instanceId)

client/src/main/java/com/microsoft/durabletask/TaskOrchestrationExecutor.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,7 @@ public <V> Task<V> callActivity(
320320
TraceContext propagatedCtx = this.orchestrationSpanContext != null
321321
? this.orchestrationSpanContext : this.parentTraceContext;
322322
if (propagatedCtx != null && !this.isReplaying) {
323-
TraceContext clientCtx = TracingHelper.createClientSpan(
324-
"activity:" + name,
325-
propagatedCtx,
326-
TracingHelper.TYPE_ACTIVITY,
327-
name,
328-
this.instanceId,
329-
id);
323+
TraceContext clientCtx = TracingHelper.createSyntheticClientContext(propagatedCtx);
330324
if (clientCtx != null) {
331325
scheduleTaskBuilder.setParentTraceContext(clientCtx);
332326
}
@@ -414,7 +408,7 @@ public void sendEvent(String instanceId, String eventName, Object eventData) {
414408
serializedEventData != null ? serializedEventData : "(null)"));
415409

416410
// Emit an event span StartTraceActivityForEventRaisedFromWorker
417-
TracingHelper.emitEventRaisedFromWorkerSpan(eventName, this.instanceId, instanceId);
411+
TracingHelper.emitEventSpan(eventName, this.instanceId, instanceId);
418412
}
419413
}
420414

@@ -452,22 +446,14 @@ public <V> Task<V> callSubOrchestrator(
452446
createSubOrchestrationActionBuilder.setVersion(StringValue.of(this.getDefaultVersion()));
453447
}
454448

455-
// Need final copy for lambda capture
456-
final String subInstanceId = instanceId;
457449
TaskFactory<V> taskFactory = () -> {
458450
int id = this.sequenceNumber++;
459451

460452
// Create a Client-kind span for scheduling and propagate its context.
461453
TraceContext propagatedCtx = this.orchestrationSpanContext != null
462454
? this.orchestrationSpanContext : this.parentTraceContext;
463455
if (propagatedCtx != null && !this.isReplaying) {
464-
TraceContext clientCtx = TracingHelper.createClientSpan(
465-
"orchestration:" + name,
466-
propagatedCtx,
467-
TracingHelper.TYPE_ORCHESTRATION,
468-
name,
469-
subInstanceId,
470-
id);
456+
TraceContext clientCtx = TracingHelper.createSyntheticClientContext(propagatedCtx);
471457
if (clientCtx != null) {
472458
createSubOrchestrationActionBuilder.setParentTraceContext(clientCtx);
473459
}

client/src/main/java/com/microsoft/durabletask/TracingHelper.java

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,9 @@ static void endSpan(@Nullable Span span, @Nullable Throwable error) {
311311
}
312312

313313
/**
314-
* Creates a synthetic Client-kind trace context for scheduling an activity or sub-orchestration.
315-
* Does NOT create or export a real span — just generates a new span ID under the parent trace.
316-
* The returned context is propagated as {@code parentTraceContext} on the action so the
314+
* Creates a synthetic trace context with a new span ID under the parent trace.
315+
* Does NOT create or export a real span — just generates a new span ID for propagation.
316+
* The returned context is set as {@code parentTraceContext} on the action so the
317317
* server-side span becomes a child of the synthetic client span ID.
318318
* At completion time, {@link #emitRetroactiveClientSpan} creates a real span with this
319319
* span ID (via {@link #setSpanId}) to provide proper duration and attributes.
@@ -323,13 +323,7 @@ static void endSpan(@Nullable Span span, @Nullable Throwable error) {
323323
* the parent context is invalid.
324324
*/
325325
@Nullable
326-
static TraceContext createClientSpan(
327-
String spanName,
328-
@Nullable TraceContext parentContext,
329-
String type,
330-
String taskName,
331-
@Nullable String instanceId,
332-
int taskId) {
326+
static TraceContext createSyntheticClientContext(@Nullable TraceContext parentContext) {
333327
if (parentContext == null || parentContext.getTraceParent() == null
334328
|| parentContext.getTraceParent().isEmpty()) {
335329
return null;
@@ -454,14 +448,15 @@ static void emitTimerSpan(
454448
}
455449

456450
/**
457-
* Emits a short-lived Producer span for an event raised from the orchestrator (worker side).
458-
* Matches .NET SDK's {@code StartTraceActivityForEventRaisedFromWorker}.
451+
* Emits a short-lived Producer span for an event raised from orchestrator (worker) or client.
452+
* Matches .NET SDK's {@code StartTraceActivityForEventRaisedFromWorker} and
453+
* {@code StartActivityForNewEventRaisedFromClient}.
459454
*
460455
* @param eventName The name of the event being raised.
461-
* @param instanceId The orchestration instance ID sending the event.
456+
* @param instanceId The orchestration instance ID (worker-side), may be {@code null}.
462457
* @param targetInstanceId The target orchestration instance ID, may be {@code null}.
463458
*/
464-
static void emitEventRaisedFromWorkerSpan(
459+
static void emitEventSpan(
465460
String eventName,
466461
@Nullable String instanceId,
467462
@Nullable String targetInstanceId) {
@@ -479,32 +474,6 @@ static void emitEventRaisedFromWorkerSpan(
479474
spanBuilder.setAttribute(ATTR_EVENT_TARGET_INSTANCE_ID, targetInstanceId);
480475
}
481476

482-
Span span = spanBuilder.startSpan();
483-
span.end();
484-
}
485-
486-
/**
487-
* Emits a short-lived Producer span for an event raised from the client.
488-
* Matches .NET SDK's {@code StartActivityForNewEventRaisedFromClient}.
489-
*
490-
* @param eventName The name of the event being raised.
491-
* @param targetInstanceId The target orchestration instance ID.
492-
*/
493-
static void emitEventRaisedFromClientSpan(
494-
String eventName,
495-
@Nullable String targetInstanceId) {
496-
Tracer tracer = GlobalOpenTelemetry.getTracer(TRACER_NAME);
497-
SpanBuilder spanBuilder = tracer.spanBuilder(
498-
TYPE_ORCHESTRATION_EVENT + ":" + eventName)
499-
.setSpanKind(SpanKind.PRODUCER)
500-
.setAttribute(ATTR_TYPE, TYPE_EVENT)
501-
.setAttribute(ATTR_TASK_NAME, eventName);
502-
503-
if (targetInstanceId != null) {
504-
spanBuilder.setAttribute(ATTR_EVENT_TARGET_INSTANCE_ID, targetInstanceId);
505-
}
506-
507-
Span span = spanBuilder.startSpan();
508-
span.end();
477+
spanBuilder.startSpan().end();
509478
}
510479
}

client/src/test/java/com/microsoft/durabletask/TracingHelperTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,8 @@ void extractSpanIdFromTraceparent_null_returnsNull() {
332332
}
333333

334334
@Test
335-
void emitEventRaisedFromWorkerSpan_createsProducerSpan() {
336-
TracingHelper.emitEventRaisedFromWorkerSpan("ApprovalEvent", "orch-1", "target-orch-2");
335+
void emitEventSpan_fromWorker_createsProducerSpan() {
336+
TracingHelper.emitEventSpan("ApprovalEvent", "orch-1", "target-orch-2");
337337

338338
List<SpanData> spans = spanExporter.getFinishedSpanItems();
339339
assertEquals(1, spans.size());
@@ -347,8 +347,8 @@ void emitEventRaisedFromWorkerSpan_createsProducerSpan() {
347347
}
348348

349349
@Test
350-
void emitEventRaisedFromClientSpan_createsProducerSpan() {
351-
TracingHelper.emitEventRaisedFromClientSpan("ApprovalEvent", "target-orch-1");
350+
void emitEventSpan_fromClient_createsProducerSpan() {
351+
TracingHelper.emitEventSpan("ApprovalEvent", null, "target-orch-1");
352352

353353
List<SpanData> spans = spanExporter.getFinishedSpanItems();
354354
assertEquals(1, spans.size());

0 commit comments

Comments
 (0)