-
Notifications
You must be signed in to change notification settings - Fork 228
feat: propagate caller trace context when scheduling workflows #1783
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
javier-aliaga
wants to merge
2
commits into
dapr:master
Choose a base branch
from
javier-aliaga:fix/tracing-propagation-workflow-client
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
durabletask-client/src/test/java/io/dapr/durabletask/DurableTaskGrpcClientTracingTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| /* | ||
| * Copyright 2026 The Dapr Authors | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.dapr.durabletask; | ||
|
|
||
| import io.dapr.durabletask.implementation.protobuf.OrchestratorService; | ||
| import io.dapr.durabletask.implementation.protobuf.TaskHubSidecarServiceGrpc; | ||
| import io.grpc.ManagedChannel; | ||
| import io.grpc.Server; | ||
| import io.grpc.inprocess.InProcessChannelBuilder; | ||
| import io.grpc.inprocess.InProcessServerBuilder; | ||
| import io.grpc.stub.StreamObserver; | ||
| import io.opentelemetry.api.trace.Span; | ||
| import io.opentelemetry.api.trace.SpanContext; | ||
| import io.opentelemetry.api.trace.Tracer; | ||
| import io.opentelemetry.context.Scope; | ||
| import io.opentelemetry.sdk.trace.SdkTracerProvider; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| /** | ||
| * Tests that scheduling an orchestration propagates the caller's trace context | ||
| * to the sidecar when a Tracer is configured, and stays untraced when not. | ||
| */ | ||
| class DurableTaskGrpcClientTracingTest { | ||
|
|
||
| private static final String ORCHESTRATION_NAME = "TestOrchestration"; | ||
|
|
||
| private Server server; | ||
| private ManagedChannel channel; | ||
| private DurableTaskClient client; | ||
| private final AtomicReference<OrchestratorService.CreateInstanceRequest> capturedRequest = new AtomicReference<>(); | ||
| private final AtomicReference<SpanContext> spanContextDuringCall = new AtomicReference<>(); | ||
|
|
||
| @BeforeEach | ||
| void setUp() throws Exception { | ||
| String serverName = InProcessServerBuilder.generateName(); | ||
| server = InProcessServerBuilder.forName(serverName) | ||
| .directExecutor() | ||
| .addService(new TaskHubSidecarServiceGrpc.TaskHubSidecarServiceImplBase() { | ||
| @Override | ||
| public void startInstance( | ||
| OrchestratorService.CreateInstanceRequest request, | ||
| StreamObserver<OrchestratorService.CreateInstanceResponse> responseObserver) { | ||
| capturedRequest.set(request); | ||
| // directExecutor() runs this handler on the client thread, so this observes | ||
| // the span the client made current for the duration of the call. | ||
| spanContextDuringCall.set(Span.current().getSpanContext()); | ||
| responseObserver.onNext(OrchestratorService.CreateInstanceResponse.newBuilder() | ||
| .setInstanceId(request.getInstanceId()) | ||
| .build()); | ||
| responseObserver.onCompleted(); | ||
| } | ||
| }) | ||
| .build() | ||
| .start(); | ||
| channel = InProcessChannelBuilder.forName(serverName).directExecutor().build(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() throws Exception { | ||
| if (client != null) { | ||
| client.close(); | ||
| } | ||
| if (channel != null) { | ||
| channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS); | ||
| } | ||
| if (server != null) { | ||
| server.shutdownNow().awaitTermination(5, TimeUnit.SECONDS); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void scheduleWithoutTracerDoesNotSetParentTraceContext() { | ||
| client = new DurableTaskGrpcClientBuilder() | ||
| .grpcChannel(channel) | ||
| .build(); | ||
|
|
||
| client.scheduleNewOrchestrationInstance(ORCHESTRATION_NAME); | ||
|
|
||
| assertFalse(capturedRequest.get().hasParentTraceContext()); | ||
| } | ||
|
|
||
| @Test | ||
| void scheduleWithTracerPropagatesCallerTraceContext() { | ||
| SdkTracerProvider tracerProvider = SdkTracerProvider.builder().build(); | ||
| try { | ||
| Tracer tracer = tracerProvider.get("test"); | ||
| client = new DurableTaskGrpcClientBuilder() | ||
| .grpcChannel(channel) | ||
| .tracer(tracer) | ||
| .build(); | ||
|
|
||
| Span callerSpan = tracer.spanBuilder("caller").startSpan(); | ||
| try (Scope scope = callerSpan.makeCurrent()) { | ||
| client.scheduleNewOrchestrationInstance(ORCHESTRATION_NAME); | ||
| } finally { | ||
| callerSpan.end(); | ||
| } | ||
|
|
||
| OrchestratorService.CreateInstanceRequest request = capturedRequest.get(); | ||
| assertTrue(request.hasParentTraceContext()); | ||
|
|
||
| // traceparent format: 00-<trace-id>-<span-id>-<flags> | ||
| String traceParent = request.getParentTraceContext().getTraceParent(); | ||
| String[] parts = traceParent.split("-"); | ||
| assertEquals(4, parts.length); | ||
| // The scheduled orchestration must join the caller's trace, through a child | ||
| // span distinct from the caller's own span. | ||
| assertEquals(callerSpan.getSpanContext().getTraceId(), parts[1]); | ||
| assertFalse(callerSpan.getSpanContext().getSpanId().equals(parts[2])); | ||
|
|
||
| // The scheduling span must be current while the sidecar call runs, so nested | ||
| // instrumentation attaches to it. It must match the propagated trace context. | ||
| assertEquals(parts[1], spanContextDuringCall.get().getTraceId()); | ||
| assertEquals(parts[2], spanContextDuringCall.get().getSpanId()); | ||
| } finally { | ||
| tracerProvider.close(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void scheduleWithNoOpTracerDoesNotSetParentTraceContext() { | ||
| // A tracer that produces invalid span contexts (e.g. OpenTelemetry no-op) | ||
| // must not attach an unusable trace context to the request. | ||
| Tracer noopTracer = io.opentelemetry.api.OpenTelemetry.noop().getTracer("noop"); | ||
| client = new DurableTaskGrpcClientBuilder() | ||
| .grpcChannel(channel) | ||
| .tracer(noopTracer) | ||
| .build(); | ||
|
|
||
| client.scheduleNewOrchestrationInstance(ORCHESTRATION_NAME); | ||
|
|
||
| assertFalse(capturedRequest.get().hasParentTraceContext()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.