|
| 1 | +# OpenTelemetry Integration for A2A |
| 2 | + |
| 3 | +This module provides OpenTelemetry observability integration for A2A servers, including distributed tracing, metrics, and context propagation across asynchronous boundaries. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Distributed Tracing**: Automatic span creation for all A2A protocol methods |
| 8 | +- **Context Propagation**: OpenTelemetry trace context propagation across async operations |
| 9 | +- **Request/Response Logging**: Optional extraction of request and response data into spans |
| 10 | +- **Error Tracking**: Automatic error status and error type attributes on failures |
| 11 | + |
| 12 | +## Modules |
| 13 | + |
| 14 | +### `opentelemetry-common` |
| 15 | +Common utilities and constants shared across OpenTelemetry modules. |
| 16 | + |
| 17 | +### `opentelemetry-client` |
| 18 | +OpenTelemetry integration for A2A clients. |
| 19 | + |
| 20 | +### `opentelemetry-client-propagation` |
| 21 | +Context propagation support for A2A clients. |
| 22 | + |
| 23 | +### `opentelemetry-server` |
| 24 | +OpenTelemetry integration for A2A servers, including the context-aware executor. |
| 25 | + |
| 26 | +### `opentelemetry-integration-tests` |
| 27 | +Integration tests for OpenTelemetry functionality. |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +### Basic Setup |
| 32 | + |
| 33 | +Add the OpenTelemetry server module to your dependencies: |
| 34 | + |
| 35 | +```xml |
| 36 | +<dependency> |
| 37 | + <groupId>io.a2a</groupId> |
| 38 | + <artifactId>a2a-extras-opentelemetry-server</artifactId> |
| 39 | + <version>${a2a.version}</version> |
| 40 | +</dependency> |
| 41 | +``` |
| 42 | + |
| 43 | +### Context-Aware Async Executor |
| 44 | + |
| 45 | +The `AsyncManagedExecutorProducer` provides a `ManagedExecutor` that automatically propagates OpenTelemetry trace context across asynchronous boundaries. This ensures that spans created in async tasks are properly linked to their parent spans. |
| 46 | + |
| 47 | +#### How It Works |
| 48 | + |
| 49 | +When the OpenTelemetry server module is included, the `AsyncManagedExecutorProducer` automatically replaces the default `AsyncExecutorProducer` using CDI alternatives: |
| 50 | + |
| 51 | +- **Priority 20**: Takes precedence over the default executor producer (priority 10) |
| 52 | +- **Automatic Activation**: No configuration needed - just include the module |
| 53 | +- **Context Propagation**: Uses MicroProfile Context Propagation to maintain trace context |
| 54 | + |
| 55 | +#### Configuration |
| 56 | + |
| 57 | +The `ManagedExecutor` is container-managed and configured through your runtime environment: |
| 58 | + |
| 59 | +**Quarkus:** |
| 60 | +```properties |
| 61 | +# Configure the managed executor pool |
| 62 | +quarkus.thread-pool.core-threads=10 |
| 63 | +quarkus.thread-pool.max-threads=50 |
| 64 | +quarkus.thread-pool.queue-size=100 |
| 65 | +``` |
| 66 | + |
| 67 | +**Other Runtimes:** |
| 68 | +Consult your MicroProfile Context Propagation implementation documentation for configuration options. |
| 69 | + |
| 70 | +> **Note**: Unlike the default `AsyncExecutorProducer`, the `AsyncManagedExecutorProducer` does not use the `a2a.executor.*` configuration properties. Pool sizing is controlled by the container's ManagedExecutor configuration. |
| 71 | +
|
| 72 | +#### Example |
| 73 | + |
| 74 | +```java |
| 75 | +@ApplicationScoped |
| 76 | +public class MyAgent implements Agent { |
| 77 | + |
| 78 | + @Inject |
| 79 | + @Internal |
| 80 | + Executor executor; // Automatically uses ManagedExecutor with context propagation |
| 81 | + |
| 82 | + @Override |
| 83 | + public void execute(RequestContext context, AgentEmitter emitter) { |
| 84 | + // Current span context is automatically propagated |
| 85 | + executor.execute(() -> { |
| 86 | + // This code runs in a different thread but maintains the trace context |
| 87 | + Span currentSpan = Span.current(); |
| 88 | + currentSpan.addEvent("Processing in async task"); |
| 89 | + |
| 90 | + // Do async work... |
| 91 | + }); |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +### Request/Response Extraction |
| 97 | + |
| 98 | +Enable request and response data extraction in spans: |
| 99 | + |
| 100 | +```properties |
| 101 | +# Extract request parameters into span attributes |
| 102 | +a2a.opentelemetry.extract-request=true |
| 103 | + |
| 104 | +# Extract response data into span attributes |
| 105 | +a2a.opentelemetry.extract-response=true |
| 106 | +``` |
| 107 | + |
| 108 | +> **Warning**: Extracting request/response data may expose sensitive information in traces. Use with caution in production environments. |
| 109 | +
|
| 110 | +### Span Attributes |
| 111 | + |
| 112 | +The following attributes are automatically added to spans: |
| 113 | + |
| 114 | +- `genai.request`: Request parameters (if extraction enabled) |
| 115 | +- `genai.response`: Response data (if extraction enabled) |
| 116 | +- `error.type`: Error message (on failures) |
| 117 | + |
| 118 | +## Architecture |
| 119 | + |
| 120 | +### Request Handler Decoration |
| 121 | + |
| 122 | +The `OpenTelemetryRequestHandlerDecorator` wraps the default request handler and creates spans for each A2A protocol method: |
| 123 | + |
| 124 | +``` |
| 125 | +Client Request |
| 126 | + ↓ |
| 127 | +OpenTelemetryRequestHandlerDecorator |
| 128 | + ↓ (creates span) |
| 129 | +Default RequestHandler |
| 130 | + ↓ |
| 131 | +Agent Execution (with context propagation) |
| 132 | + ↓ |
| 133 | +Response |
| 134 | +``` |
| 135 | + |
| 136 | +### Context Propagation Flow |
| 137 | + |
| 138 | +``` |
| 139 | +HTTP Request (with trace headers) |
| 140 | + ↓ |
| 141 | +OpenTelemetry extracts context |
| 142 | + ↓ |
| 143 | +Span created for A2A method |
| 144 | + ↓ |
| 145 | +ManagedExecutor propagates context |
| 146 | + ↓ |
| 147 | +Async agent execution (maintains trace context) |
| 148 | + ↓ |
| 149 | +Response (with trace headers) |
| 150 | +``` |
| 151 | + |
| 152 | +## Testing |
| 153 | + |
| 154 | +The module includes comprehensive unit tests: |
| 155 | + |
| 156 | +- `AsyncManagedExecutorProducerTest`: Tests for the context-aware executor producer |
| 157 | +- `OpenTelemetryRequestHandlerDecoratorTest`: Tests for span creation and error handling |
| 158 | + |
| 159 | +Run tests: |
| 160 | +```bash |
| 161 | +mvn test -pl extras/opentelemetry/server |
| 162 | +``` |
| 163 | + |
| 164 | +## Troubleshooting |
| 165 | + |
| 166 | +### Context Not Propagating |
| 167 | + |
| 168 | +**Symptom**: Spans in async tasks are not linked to parent spans. |
| 169 | + |
| 170 | +**Solution**: Ensure the OpenTelemetry server module is included and the `ManagedExecutor` is being injected correctly. Check logs for: |
| 171 | +``` |
| 172 | +Initializing OpenTelemetry-aware ManagedExecutor for async operations |
| 173 | +``` |
| 174 | + |
| 175 | +### ManagedExecutor Not Available |
| 176 | + |
| 177 | +**Symptom**: `IllegalStateException: ManagedExecutor not injected - ensure MicroProfile Context Propagation is available` |
| 178 | + |
| 179 | +**Solution**: Ensure your runtime provides MicroProfile Context Propagation support. For Quarkus, add: |
| 180 | +```xml |
| 181 | +<dependency> |
| 182 | + <groupId>io.quarkus</groupId> |
| 183 | + <artifactId>quarkus-smallrye-context-propagation</artifactId> |
| 184 | +</dependency> |
| 185 | +``` |
| 186 | + |
| 187 | +### Performance Impact |
| 188 | + |
| 189 | +**Symptom**: Increased latency with OpenTelemetry enabled. |
| 190 | + |
| 191 | +**Solution**: |
| 192 | +- Disable request/response extraction in production |
| 193 | +- Configure sampling rate to reduce trace volume |
| 194 | +- Ensure your OpenTelemetry collector is properly sized |
| 195 | + |
| 196 | +## Best Practices |
| 197 | + |
| 198 | +1. **Sampling**: Configure appropriate sampling rates for production environments |
| 199 | +2. **Sensitive Data**: Disable request/response extraction if handling sensitive data |
| 200 | +3. **Resource Attributes**: Add service name and version as resource attributes |
| 201 | +4. **Collector Configuration**: Use batch processors to reduce network overhead |
| 202 | +5. **Monitoring**: Monitor the OpenTelemetry collector's health and performance |
| 203 | + |
| 204 | +## Dependencies |
| 205 | + |
| 206 | +- MicroProfile Telemetry 2.0.1+ |
| 207 | +- MicroProfile Context Propagation 1.3+ |
| 208 | +- OpenTelemetry API |
| 209 | +- A2A Server Common |
| 210 | + |
| 211 | +## See Also |
| 212 | + |
| 213 | +- [OpenTelemetry Documentation](https://opentelemetry.io/docs/) |
| 214 | +- [MicroProfile Telemetry Specification](https://github.com/eclipse/microprofile-telemetry) |
| 215 | +- [MicroProfile Context Propagation](https://github.com/eclipse/microprofile-context-propagation) |
0 commit comments