|
17 | 17 | import java.util.concurrent.TimeUnit; |
18 | 18 | import java.util.concurrent.TimeoutException; |
19 | 19 | 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; |
20 | 24 |
|
21 | 25 | /** |
22 | 26 | * Durable Task client implementation that uses gRPC to connect to a remote "sidecar" process. |
@@ -110,6 +114,37 @@ public String scheduleNewOrchestrationInstance( |
110 | 114 | builder.setScheduledStartTimestamp(ts); |
111 | 115 | } |
112 | 116 |
|
| 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 | + |
113 | 148 | CreateInstanceRequest request = builder.build(); |
114 | 149 | CreateInstanceResponse response = this.sidecarClient.startInstance(request); |
115 | 150 | return response.getInstanceId(); |
|
0 commit comments