Skip to content

Add tracing support to send information to the WebJobs extension #211

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

Merged
merged 9 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v1.5.2
* Add distributed tracing support for Azure Functions client scenarios ([#211](https://github.com/microsoft/durabletask-java/pull/211))


## v1.5.1
* Improve logging for unexpected connection failures in DurableTaskGrpcWorker ([#216](https://github.com/microsoft/durabletask-java/pull/216/files))
* Add User-Agent Header to gRPC Metadata ([#213](https://github.com/microsoft/durabletask-java/pull/213))
Expand Down
4 changes: 4 additions & 0 deletions client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ archivesBaseName = 'durabletask-client'
def grpcVersion = '1.59.0'
def protocVersion = '3.12.0'
def jacksonVersion = '2.15.3'
def openTelemetryVersion = '1.25.0'
// When build on local, you need to set this value to your local jdk11 directory.
// Java11 is used to compile and run all the tests.
// Example for Windows: C:/Program Files/Java/openjdk-11.0.12_7/
Expand All @@ -34,6 +35,9 @@ dependencies {
implementation "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}"
implementation "com.fasterxml.jackson.core:jackson-annotations:${jacksonVersion}"
implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:${jacksonVersion}"

implementation "io.opentelemetry:opentelemetry-api:${openTelemetryVersion}"
implementation "io.opentelemetry:opentelemetry-context:${openTelemetryVersion}"

testImplementation(platform('org.junit:junit-bom:5.7.2'))
testImplementation('org.junit.jupiter:junit-jupiter')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;

/**
* Durable Task client implementation that uses gRPC to connect to a remote "sidecar" process.
Expand Down Expand Up @@ -110,6 +114,37 @@ public String scheduleNewOrchestrationInstance(
builder.setScheduledStartTimestamp(ts);
}

Span currentSpan = Span.current();
String traceParent = null;
String traceState = null;

if (currentSpan != null && currentSpan.getSpanContext().isValid()) {
SpanContext spanContext = currentSpan.getSpanContext();

// Construct the traceparent according to the W3C Trace Context specification
// https://www.w3.org/TR/trace-context/#traceparent-header
traceParent = String.format("00-%s-%s-%02x",
spanContext.getTraceId(), // 32-character trace ID
spanContext.getSpanId(), // 16-character span ID
spanContext.getTraceFlags().asByte() // Trace flags (i.e. sampled or not)
);

// Get the tracestate
traceState = spanContext.getTraceState().asMap()
.entrySet()
.stream()
.map(entry -> entry.getKey() + "=" + entry.getValue())
.collect(Collectors.joining(","));
}

if (traceParent != null) {
TraceContext traceContext = TraceContext.newBuilder()
.setTraceParent(traceParent)
.setTraceState(traceState != null ? StringValue.of(traceState) : StringValue.getDefaultInstance())
.build();
builder.setParentTraceContext(traceContext); // Set the TraceContext in the CreateInstanceRequest
}

CreateInstanceRequest request = builder.build();
CreateInstanceResponse response = this.sidecarClient.startInstance(request);
return response.getInstanceId();
Expand Down
Loading