|
9 | 9 | import io.a2a.spec.AgentCard; |
10 | 10 | import io.a2a.spec.AgentInterface; |
11 | 11 | import io.a2a.spec.TransportProtocol; |
| 12 | +import java.lang.reflect.InvocationTargetException; |
12 | 13 |
|
13 | 14 | import java.util.ArrayList; |
14 | 15 | import java.util.HashMap; |
@@ -37,7 +38,8 @@ public class ClientBuilder { |
37 | 38 | private final AgentCard agentCard; |
38 | 39 |
|
39 | 40 | private final List<BiConsumer<ClientEvent, AgentCard>> consumers = new ArrayList<>(); |
40 | | - private @Nullable Consumer<Throwable> streamErrorHandler; |
| 41 | + private @Nullable |
| 42 | + Consumer<Throwable> streamErrorHandler; |
41 | 43 | private ClientConfig clientConfig = new ClientConfig.Builder().build(); |
42 | 44 |
|
43 | 45 | private final Map<Class<? extends ClientTransport>, ClientTransportConfig<? extends ClientTransport>> clientTransports = new LinkedHashMap<>(); |
@@ -105,7 +107,7 @@ private ClientTransport buildClientTransport() throws A2AClientException { |
105 | 107 | throw new A2AClientException("Missing required TransportConfig for " + agentInterface.transport()); |
106 | 108 | } |
107 | 109 |
|
108 | | - return clientTransportProvider.create(clientTransportConfig, agentCard, agentInterface.url()); |
| 110 | + return wrap(clientTransportProvider.create(clientTransportConfig, agentCard, agentInterface.url()), clientTransportConfig); |
109 | 111 | } |
110 | 112 |
|
111 | 113 | private Map<String, String> getServerPreferredTransports() { |
@@ -160,10 +162,21 @@ private AgentInterface findBestClientTransport() throws A2AClientException { |
160 | 162 | if (transportProtocol == null || transportUrl == null) { |
161 | 163 | throw new A2AClientException("No compatible transport found"); |
162 | 164 | } |
163 | | - if (! transportProviderRegistry.containsKey(transportProtocol)) { |
| 165 | + if (!transportProviderRegistry.containsKey(transportProtocol)) { |
164 | 166 | throw new A2AClientException("No client available for " + transportProtocol); |
165 | 167 | } |
166 | 168 |
|
167 | 169 | return new AgentInterface(transportProtocol, transportUrl); |
168 | 170 | } |
| 171 | + |
| 172 | + private ClientTransport wrap(ClientTransport transport, ClientTransportConfig<? extends ClientTransport> clientTransportConfig) { |
| 173 | + try { |
| 174 | + Class<?> factoryClass = this.getClass().getClassLoader().loadClass("io.a2a.extras.opentelemetry.OpenTelemetryClientTransportFactory"); |
| 175 | + Object openTelemetryClientTransportFactory = factoryClass.getConstructor(new Class<?>[0]).newInstance(new Object[0]); |
| 176 | + factoryClass.getDeclaredMethod("config", ClientTransportConfig.class).invoke(openTelemetryClientTransportFactory, clientTransportConfig); |
| 177 | + return (ClientTransport) factoryClass.getDeclaredMethod("wrap", ClientTransport.class).invoke(openTelemetryClientTransportFactory, transport); |
| 178 | + } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { |
| 179 | + return transport; |
| 180 | + } |
| 181 | + } |
169 | 182 | } |
0 commit comments