Skip to content

Commit abe1449

Browse files
authored
Add tracing support to send information to the WebJobs extension (#211)
1 parent f0757b0 commit abe1449

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v1.5.2
2+
* Add distributed tracing support for Azure Functions client scenarios ([#211](https://github.com/microsoft/durabletask-java/pull/211))
3+
4+
15
## v1.5.1
26
* Improve logging for unexpected connection failures in DurableTaskGrpcWorker ([#216](https://github.com/microsoft/durabletask-java/pull/216/files))
37
* Add User-Agent Header to gRPC Metadata ([#213](https://github.com/microsoft/durabletask-java/pull/213))

client/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ archivesBaseName = 'durabletask-client'
1515
def grpcVersion = '1.59.0'
1616
def protocVersion = '3.12.0'
1717
def jacksonVersion = '2.15.3'
18+
def openTelemetryVersion = '1.25.0'
1819
// When build on local, you need to set this value to your local jdk11 directory.
1920
// Java11 is used to compile and run all the tests.
2021
// Example for Windows: C:/Program Files/Java/openjdk-11.0.12_7/
@@ -34,6 +35,9 @@ dependencies {
3435
implementation "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}"
3536
implementation "com.fasterxml.jackson.core:jackson-annotations:${jacksonVersion}"
3637
implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:${jacksonVersion}"
38+
39+
implementation "io.opentelemetry:opentelemetry-api:${openTelemetryVersion}"
40+
implementation "io.opentelemetry:opentelemetry-context:${openTelemetryVersion}"
3741

3842
testImplementation(platform('org.junit:junit-bom:5.7.2'))
3943
testImplementation('org.junit.jupiter:junit-jupiter')

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
import java.util.concurrent.TimeUnit;
1818
import java.util.concurrent.TimeoutException;
1919
import java.util.logging.Logger;
20+
import java.util.stream.Collectors;
21+
22+
import io.opentelemetry.api.trace.Span;
23+
import io.opentelemetry.api.trace.SpanContext;
2024

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

117+
Span currentSpan = Span.current();
118+
String traceParent = null;
119+
String traceState = null;
120+
121+
if (currentSpan != null && currentSpan.getSpanContext().isValid()) {
122+
SpanContext spanContext = currentSpan.getSpanContext();
123+
124+
// Construct the traceparent according to the W3C Trace Context specification
125+
// https://www.w3.org/TR/trace-context/#traceparent-header
126+
traceParent = String.format("00-%s-%s-%02x",
127+
spanContext.getTraceId(), // 32-character trace ID
128+
spanContext.getSpanId(), // 16-character span ID
129+
spanContext.getTraceFlags().asByte() // Trace flags (i.e. sampled or not)
130+
);
131+
132+
// Get the tracestate
133+
traceState = spanContext.getTraceState().asMap()
134+
.entrySet()
135+
.stream()
136+
.map(entry -> entry.getKey() + "=" + entry.getValue())
137+
.collect(Collectors.joining(","));
138+
}
139+
140+
if (traceParent != null) {
141+
TraceContext traceContext = TraceContext.newBuilder()
142+
.setTraceParent(traceParent)
143+
.setTraceState(traceState != null ? StringValue.of(traceState) : StringValue.getDefaultInstance())
144+
.build();
145+
builder.setParentTraceContext(traceContext); // Set the TraceContext in the CreateInstanceRequest
146+
}
147+
113148
CreateInstanceRequest request = builder.build();
114149
CreateInstanceResponse response = this.sidecarClient.startInstance(request);
115150
return response.getInstanceId();

0 commit comments

Comments
 (0)