Skip to content

Commit 076472d

Browse files
committed
Propagation
Signed-off-by: Emmanuel Hugonnet <ehugonne@redhat.com>
1 parent 143f3fd commit 076472d

12 files changed

Lines changed: 190 additions & 68 deletions

File tree

‎examples/helloworld/client/pom.xml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
<groupId>org.apache.maven.plugins</groupId>
5959
<artifactId>maven-surefire-plugin</artifactId>
6060
</plugin>
61-
<plugin>
61+
<!-- <plugin>
6262
<groupId>org.codehaus.gmavenplus</groupId>
6363
<artifactId>gmavenplus-plugin</artifactId>
6464
<executions>
@@ -75,7 +75,7 @@
7575
</configuration>
7676
</execution>
7777
</executions>
78-
</plugin>
78+
</plugin>-->
7979
<plugin>
8080
<groupId>org.codehaus.mojo</groupId>
8181
<artifactId>exec-maven-plugin</artifactId>

‎examples/helloworld/client/src/main/java/io/a2a/examples/helloworld/HelloWorldClient.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import io.a2a.spec.Message;
2222
import io.a2a.spec.Part;
2323
import io.a2a.spec.TextPart;
24+
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
25+
import io.opentelemetry.context.propagation.ContextPropagators;
2426
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
2527
import io.opentelemetry.sdk.OpenTelemetrySdk;
2628
import io.opentelemetry.sdk.resources.Resource;
@@ -90,8 +92,10 @@ public static void main(String[] args) {
9092
JSONRPCTransportConfig transportConfig = new JSONRPCTransportConfig();
9193
if (Boolean.getBoolean("opentelemetry")) {
9294
openTelemetrySdk = initOpenTelemetry();
93-
transportConfig.setParameters(Map.of("io.a2a.extras.opentelemetry.Tracer",
94-
openTelemetrySdk.getTracer("helloworld-client")));
95+
transportConfig.setParameters(Map.of(
96+
"io.a2a.extras.opentelemetry.Tracer", openTelemetrySdk.getTracer("helloworld-client"),
97+
"io.a2a.extras.opentelemetry.OpenTelemetry", openTelemetrySdk
98+
));
9599
}
96100
Client client = Client
97101
.builder(finalAgentCard)
@@ -139,6 +143,7 @@ static OpenTelemetrySdk initOpenTelemetry() {
139143

140144
return OpenTelemetrySdk.builder()
141145
.setTracerProvider(sdkTracerProvider)
146+
.setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance()))
142147
.build();
143148
}
144149
}

‎extras/opentelemetry/src/main/java/io/a2a/extras/opentelemetry/OpenTelemetryClientTransport.java‎

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.a2a.extras.opentelemetry;
22

3+
34
import io.a2a.client.transport.spi.ClientTransport;
45
import io.a2a.client.transport.spi.interceptors.ClientCallContext;
56
import io.a2a.jsonrpc.common.wrappers.ListTasksResult;
@@ -18,13 +19,18 @@
1819
import io.a2a.spec.TaskIdParams;
1920
import io.a2a.spec.TaskPushNotificationConfig;
2021
import io.a2a.spec.TaskQueryParams;
22+
import io.opentelemetry.api.OpenTelemetry;
2123
import io.opentelemetry.api.trace.Span;
2224
import io.opentelemetry.api.trace.SpanBuilder;
2325
import io.opentelemetry.api.trace.SpanContext;
2426
import io.opentelemetry.api.trace.SpanKind;
2527
import io.opentelemetry.api.trace.StatusCode;
2628
import io.opentelemetry.api.trace.Tracer;
29+
import io.opentelemetry.context.Context;
2730
import io.opentelemetry.context.Scope;
31+
import io.opentelemetry.context.propagation.TextMapSetter;
32+
import java.util.HashMap;
33+
import java.util.Map;
2834
import java.util.function.Consumer;
2935
import java.util.function.Function;
3036
import java.util.stream.Collectors;
@@ -33,12 +39,23 @@
3339
public class OpenTelemetryClientTransport implements ClientTransport {
3440

3541
private final Tracer tracer;
42+
private final OpenTelemetry openTelemetry;
3643
private final ClientTransport delegate;
3744
private static final String REQUEST_TRACE_ATTRIBUTE = "request";
3845
private static final String RESPONSE_TRACE_ATTRIBUTE = "response";
3946

40-
public OpenTelemetryClientTransport(ClientTransport delegate, Tracer tracer) {
47+
private static final TextMapSetter<Map<String, String>> MAP_SETTER = new TextMapSetter<Map<String, String>>() {
48+
@Override
49+
public void set(@Nullable Map<String, String> carrier, String key, String value) {
50+
if (carrier != null) {
51+
carrier.put(key, value);
52+
}
53+
}
54+
};
55+
56+
public OpenTelemetryClientTransport(ClientTransport delegate, OpenTelemetry openTelemetry, Tracer tracer) {
4157
this.delegate = delegate;
58+
this.openTelemetry = openTelemetry;
4259
this.tracer = tracer;
4360
}
4461

@@ -54,13 +71,13 @@ public OpenTelemetryClientTransport(ClientTransport delegate, Tracer tracer) {
5471
* @return the result of the operation
5572
* @throws A2AClientException if the operation fails
5673
*/
57-
private <R, T> T traceOperation(String operationName, R request,
58-
ThrowingSupplier<T> operation,
74+
private <R, T> T traceOperation(String operationName, R request, ClientCallContext context, ThrowingSupplier<T> operation,
5975
@Nullable Function<T, String> responseFormatter) throws A2AClientException {
6076
SpanBuilder spanBuilder = tracer.spanBuilder(operationName).setSpanKind(SpanKind.CLIENT);
6177
spanBuilder.setAttribute(REQUEST_TRACE_ATTRIBUTE, request.toString());
6278
Span span = spanBuilder.startSpan();
6379
try (Scope scope = span.makeCurrent()) {
80+
openTelemetry.getPropagators().getTextMapPropagator().inject(Context.current(), context.getHeaders(), MAP_SETTER);
6481
T result = operation.get();
6582
if (result != null) {
6683
String responseValue = responseFormatter != null ? responseFormatter.apply(result) : result.toString();
@@ -85,10 +102,13 @@ private <R, T> T traceOperation(String operationName, R request,
85102
* @return the result of the operation
86103
* @throws A2AClientException if the operation fails
87104
*/
88-
private <T> T traceOperationNoRequest(String operationName, ThrowingSupplier<T> operation) throws A2AClientException {
105+
private <T> T traceOperationNoRequest(String operationName,
106+
ClientCallContext context,
107+
ThrowingSupplier<T> operation) throws A2AClientException {
89108
SpanBuilder spanBuilder = tracer.spanBuilder(operationName).setSpanKind(SpanKind.CLIENT);
90109
Span span = spanBuilder.startSpan();
91110
try (Scope scope = span.makeCurrent()) {
111+
openTelemetry.getPropagators().getTextMapPropagator().inject(Context.current(), context.getHeaders(), MAP_SETTER);
92112
T result = operation.get();
93113
if (result != null) {
94114
span.setAttribute(RESPONSE_TRACE_ATTRIBUTE, result.toString());
@@ -112,11 +132,15 @@ private <T> T traceOperationNoRequest(String operationName, ThrowingSupplier<T>
112132
* @param <R> the request type
113133
* @throws A2AClientException if the operation fails
114134
*/
115-
private <R> void traceVoidOperation(String operationName, R request, ThrowingRunnable operation) throws A2AClientException {
135+
private <R> void traceVoidOperation(String operationName,
136+
R request,
137+
ClientCallContext context,
138+
ThrowingRunnable operation) throws A2AClientException {
116139
SpanBuilder spanBuilder = tracer.spanBuilder(operationName).setSpanKind(SpanKind.CLIENT);
117140
spanBuilder.setAttribute(REQUEST_TRACE_ATTRIBUTE, request.toString());
118141
Span span = spanBuilder.startSpan();
119142
try (Scope scope = span.makeCurrent()) {
143+
openTelemetry.getPropagators().getTextMapPropagator().inject(Context.current(), context.getHeaders(), MAP_SETTER);
120144
operation.run();
121145
span.setStatus(StatusCode.OK);
122146
} catch (Exception ex) {
@@ -139,13 +163,15 @@ private <R> void traceVoidOperation(String operationName, R request, ThrowingRun
139163
* @throws A2AClientException if the operation fails
140164
*/
141165
private <R> void traceStreamingOperation(String operationName, R request,
166+
ClientCallContext context,
142167
Consumer<StreamingEventKind> eventConsumer,
143168
Consumer<Throwable> errorConsumer,
144169
StreamingOperation operation) throws A2AClientException {
145170
SpanBuilder spanBuilder = tracer.spanBuilder(operationName).setSpanKind(SpanKind.CLIENT);
146171
spanBuilder.setAttribute(REQUEST_TRACE_ATTRIBUTE, request.toString());
147172
Span span = spanBuilder.startSpan();
148173
try (Scope scope = span.makeCurrent()) {
174+
openTelemetry.getPropagators().getTextMapPropagator().inject(Context.current(), context.getHeaders(), MAP_SETTER);
149175
operation.execute(
150176
new OpenTelemetryEventConsumer(operationName + "-event", eventConsumer, tracer, span.getSpanContext()),
151177
new OpenTelemetryErrorConsumer(operationName + "-error", errorConsumer, tracer, span.getSpanContext())
@@ -161,70 +187,88 @@ private <R> void traceStreamingOperation(String operationName, R request,
161187

162188
@Override
163189
public EventKind sendMessage(MessageSendParams request, @Nullable ClientCallContext context) throws A2AClientException {
164-
return traceOperation(A2AMethods.SEND_MESSAGE_METHOD, request, () -> delegate.sendMessage(request, context), null);
190+
ClientCallContext clientContext = createContext(context);
191+
return traceOperation(A2AMethods.SEND_MESSAGE_METHOD, request, clientContext, () -> delegate.sendMessage(request, clientContext), null);
165192
}
166193

167194
@Override
168195
public void sendMessageStreaming(MessageSendParams request, Consumer<StreamingEventKind> eventConsumer,
169196
Consumer<Throwable> errorConsumer, @Nullable ClientCallContext context) throws A2AClientException {
170-
traceStreamingOperation(A2AMethods.SEND_STREAMING_MESSAGE_METHOD, request, eventConsumer, errorConsumer,
171-
(wrappedEvent, wrappedError) -> delegate.sendMessageStreaming(request, wrappedEvent, wrappedError, context));
197+
ClientCallContext clientContext = createContext(context);
198+
traceStreamingOperation(A2AMethods.SEND_STREAMING_MESSAGE_METHOD, request, clientContext, eventConsumer, errorConsumer,
199+
(wrappedEvent, wrappedError) -> delegate.sendMessageStreaming(request, wrappedEvent, wrappedError, clientContext));
172200
}
173201

174202
@Override
175203
public Task getTask(TaskQueryParams request, @Nullable ClientCallContext context) throws A2AClientException {
176-
return traceOperation(A2AMethods.GET_TASK_METHOD, request, () -> delegate.getTask(request, context), null);
204+
ClientCallContext clientContext = createContext(context);
205+
return traceOperation(A2AMethods.GET_TASK_METHOD, request, clientContext, () -> delegate.getTask(request, clientContext), null);
177206
}
178207

179208
@Override
180209
public Task cancelTask(TaskIdParams request, @Nullable ClientCallContext context) throws A2AClientException {
181-
return traceOperation(A2AMethods.CANCEL_TASK_METHOD, request, () -> delegate.cancelTask(request, context), null);
210+
ClientCallContext clientContext = createContext(context);
211+
return traceOperation(A2AMethods.CANCEL_TASK_METHOD, request, clientContext, () -> delegate.cancelTask(request, clientContext), null);
182212
}
183213

184214
@Override
185215
public ListTasksResult listTasks(ListTasksParams request, @Nullable ClientCallContext context) throws A2AClientException {
186-
return traceOperation(A2AMethods.LIST_TASK_METHOD, request, () -> delegate.listTasks(request, context), null);
216+
ClientCallContext clientContext = createContext(context);
217+
return traceOperation(A2AMethods.LIST_TASK_METHOD, request, clientContext, () -> delegate.listTasks(request, clientContext), null);
187218
}
188219

189220
@Override
190221
public TaskPushNotificationConfig setTaskPushNotificationConfiguration(TaskPushNotificationConfig request,
191222
@Nullable ClientCallContext context) throws A2AClientException {
192-
return traceOperation(A2AMethods.SET_TASK_PUSH_NOTIFICATION_CONFIG_METHOD, request,
193-
() -> delegate.setTaskPushNotificationConfiguration(request, context), null);
223+
ClientCallContext clientContext = createContext(context);
224+
return traceOperation(A2AMethods.SET_TASK_PUSH_NOTIFICATION_CONFIG_METHOD, request, clientContext,
225+
() -> delegate.setTaskPushNotificationConfiguration(request, clientContext), null);
194226
}
195227

196228
@Override
197229
public TaskPushNotificationConfig getTaskPushNotificationConfiguration(GetTaskPushNotificationConfigParams request,
198230
@Nullable ClientCallContext context) throws A2AClientException {
199-
return traceOperation(A2AMethods.GET_TASK_PUSH_NOTIFICATION_CONFIG_METHOD, request,
200-
() -> delegate.getTaskPushNotificationConfiguration(request, context), null);
231+
ClientCallContext clientContext = createContext(context);
232+
return traceOperation(A2AMethods.GET_TASK_PUSH_NOTIFICATION_CONFIG_METHOD, request, clientContext,
233+
() -> delegate.getTaskPushNotificationConfiguration(request, clientContext), null);
201234
}
202235

203236
@Override
204237
public ListTaskPushNotificationConfigResult listTaskPushNotificationConfigurations(ListTaskPushNotificationConfigParams request,
205238
@Nullable ClientCallContext context) throws A2AClientException {
206-
return traceOperation(A2AMethods.LIST_TASK_PUSH_NOTIFICATION_CONFIG_METHOD, request,
207-
() -> delegate.listTaskPushNotificationConfigurations(request, context),
239+
ClientCallContext clientContext = createContext(context);
240+
return traceOperation(A2AMethods.LIST_TASK_PUSH_NOTIFICATION_CONFIG_METHOD, request, clientContext,
241+
() -> delegate.listTaskPushNotificationConfigurations(request, clientContext),
208242
result -> result.configs().stream().map(TaskPushNotificationConfig::toString).collect(Collectors.joining(",")));
209243
}
210244

211245
@Override
212246
public void deleteTaskPushNotificationConfigurations(DeleteTaskPushNotificationConfigParams request,
213247
@Nullable ClientCallContext context) throws A2AClientException {
214-
traceVoidOperation(A2AMethods.DELETE_TASK_PUSH_NOTIFICATION_CONFIG_METHOD, request,
215-
() -> delegate.deleteTaskPushNotificationConfigurations(request, context));
248+
ClientCallContext clientContext = createContext(context);
249+
traceVoidOperation(A2AMethods.DELETE_TASK_PUSH_NOTIFICATION_CONFIG_METHOD, request, clientContext,
250+
() -> delegate.deleteTaskPushNotificationConfigurations(request, clientContext));
216251
}
217252

218253
@Override
219254
public void resubscribe(TaskIdParams request, Consumer<StreamingEventKind> eventConsumer,
220255
Consumer<Throwable> errorConsumer, @Nullable ClientCallContext context) throws A2AClientException {
221-
traceStreamingOperation(A2AMethods.SUBSCRIBE_TO_TASK_METHOD, request, eventConsumer, errorConsumer,
222-
(wrappedEvent, wrappedError) -> delegate.resubscribe(request, wrappedEvent, wrappedError, context));
256+
ClientCallContext clientContext = createContext(context);
257+
traceStreamingOperation(A2AMethods.SUBSCRIBE_TO_TASK_METHOD, request, clientContext, eventConsumer, errorConsumer,
258+
(wrappedEvent, wrappedError) -> delegate.resubscribe(request, wrappedEvent, wrappedError, clientContext));
223259
}
224260

225261
@Override
226262
public AgentCard getAgentCard(@Nullable ClientCallContext context) throws A2AClientException {
227-
return traceOperationNoRequest(A2AMethods.GET_EXTENDED_AGENT_CARD_METHOD, () -> delegate.getAgentCard(context));
263+
ClientCallContext clientContext = createContext(context);
264+
return traceOperationNoRequest(A2AMethods.GET_EXTENDED_AGENT_CARD_METHOD, clientContext, () -> delegate.getAgentCard(clientContext));
265+
}
266+
267+
private ClientCallContext createContext(@Nullable ClientCallContext context) {
268+
if(context == null) {
269+
return new ClientCallContext(Map.of(), new HashMap<>());
270+
}
271+
return new ClientCallContext(context.getState(), new HashMap<>(context.getHeaders()));
228272
}
229273

230274
@Override

‎extras/opentelemetry/src/main/java/io/a2a/extras/opentelemetry/OpenTelemetryClientTransportWrapper.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.a2a.client.transport.spi.ClientTransport;
44
import io.a2a.client.transport.spi.ClientTransportConfig;
55
import io.a2a.client.transport.spi.ClientTransportWrapper;
6+
import io.opentelemetry.api.OpenTelemetry;
67
import io.opentelemetry.api.trace.Tracer;
78

89
/**
@@ -25,12 +26,14 @@ public class OpenTelemetryClientTransportWrapper implements ClientTransportWrapp
2526
* Value must be of type {@link Tracer}.
2627
*/
2728
public static final String OTEL_TRACER_KEY = "io.a2a.extras.opentelemetry.Tracer";
29+
public static final String OTEL_OPEN_TELEMETRY_KEY = "io.a2a.extras.opentelemetry.OpenTelemetry";
2830

2931
@Override
3032
public ClientTransport wrap(ClientTransport transport, ClientTransportConfig<?> config) {
3133
Object tracerObj = config.getParameters().get(OTEL_TRACER_KEY);
32-
if (tracerObj instanceof Tracer tracer) {
33-
return new OpenTelemetryClientTransport(transport, tracer);
34+
Object openTelemetryObj = config.getParameters().get(OTEL_OPEN_TELEMETRY_KEY);
35+
if (tracerObj instanceof Tracer tracer && openTelemetryObj instanceof OpenTelemetry openTelemetry) {
36+
return new OpenTelemetryClientTransport(transport, openTelemetry, tracer);
3437
}
3538
// No tracer configured, return unwrapped transport
3639
return transport;

0 commit comments

Comments
 (0)