11package io .a2a .extras .opentelemetry ;
22
3+
34import io .a2a .client .transport .spi .ClientTransport ;
45import io .a2a .client .transport .spi .interceptors .ClientCallContext ;
56import io .a2a .jsonrpc .common .wrappers .ListTasksResult ;
1819import io .a2a .spec .TaskIdParams ;
1920import io .a2a .spec .TaskPushNotificationConfig ;
2021import io .a2a .spec .TaskQueryParams ;
22+ import io .opentelemetry .api .OpenTelemetry ;
2123import io .opentelemetry .api .trace .Span ;
2224import io .opentelemetry .api .trace .SpanBuilder ;
2325import io .opentelemetry .api .trace .SpanContext ;
2426import io .opentelemetry .api .trace .SpanKind ;
2527import io .opentelemetry .api .trace .StatusCode ;
2628import io .opentelemetry .api .trace .Tracer ;
29+ import io .opentelemetry .context .Context ;
2730import io .opentelemetry .context .Scope ;
31+ import io .opentelemetry .context .propagation .TextMapSetter ;
32+ import java .util .HashMap ;
33+ import java .util .Map ;
2834import java .util .function .Consumer ;
2935import java .util .function .Function ;
3036import java .util .stream .Collectors ;
3339public 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
0 commit comments