|
| 1 | +package org.a2aproject.sdk.extras.opentelemetry.server; |
| 2 | + |
| 3 | +import static org.a2aproject.sdk.extras.opentelemetry.A2AObservabilityNames.ERROR_TYPE; |
| 4 | +import static org.a2aproject.sdk.extras.opentelemetry.A2AObservabilityNames.EXTRACT_RESPONSE_SYS_PROPERTY; |
| 5 | +import static org.a2aproject.sdk.extras.opentelemetry.A2AObservabilityNames.GENAI_CONTEXT_ID; |
| 6 | +import static org.a2aproject.sdk.extras.opentelemetry.A2AObservabilityNames.GENAI_OPERATION_NAME; |
| 7 | +import static org.a2aproject.sdk.extras.opentelemetry.A2AObservabilityNames.GENAI_RESPONSE; |
| 8 | +import static org.a2aproject.sdk.extras.opentelemetry.A2AObservabilityNames.GENAI_TASK_ID; |
| 9 | +import static org.a2aproject.sdk.extras.opentelemetry.A2AObservabilityNames.PUSH_NOTIFICATION_EVENT_KIND; |
| 10 | + |
| 11 | +import io.opentelemetry.api.trace.Span; |
| 12 | +import io.opentelemetry.api.trace.SpanKind; |
| 13 | +import io.opentelemetry.api.trace.StatusCode; |
| 14 | +import io.opentelemetry.api.trace.Tracer; |
| 15 | +import io.opentelemetry.context.Scope; |
| 16 | +import jakarta.annotation.Priority; |
| 17 | +import jakarta.decorator.Decorator; |
| 18 | +import jakarta.decorator.Delegate; |
| 19 | +import jakarta.enterprise.inject.Any; |
| 20 | +import jakarta.inject.Inject; |
| 21 | +import org.a2aproject.sdk.server.tasks.PushNotificationSender; |
| 22 | +import org.a2aproject.sdk.spec.Message; |
| 23 | +import org.a2aproject.sdk.spec.StreamingEventKind; |
| 24 | +import org.a2aproject.sdk.spec.Task; |
| 25 | +import org.a2aproject.sdk.spec.TaskArtifactUpdateEvent; |
| 26 | +import org.a2aproject.sdk.spec.TaskStatusUpdateEvent; |
| 27 | +import org.jspecify.annotations.Nullable; |
| 28 | + |
| 29 | +/** |
| 30 | + * OpenTelemetry CDI Decorator for {@link PushNotificationSender}. |
| 31 | + * <p> |
| 32 | + * Creates a {@code CLIENT} span named {@code SendPushNotification} for every push |
| 33 | + * notification delivery attempt, capturing the task ID, context ID, and event kind |
| 34 | + * as span attributes. |
| 35 | + * <p> |
| 36 | + * To enable this decorator, add it to your beans.xml: |
| 37 | + * <pre>{@code |
| 38 | + * <decorators> |
| 39 | + * <class>org.a2aproject.sdk.extras.opentelemetry.server.OpenTelemetryPushNotificationSenderDecorator</class> |
| 40 | + * </decorators> |
| 41 | + * }</pre> |
| 42 | + */ |
| 43 | +@Decorator |
| 44 | +@Priority(100) |
| 45 | +public abstract class OpenTelemetryPushNotificationSenderDecorator implements PushNotificationSender { |
| 46 | + |
| 47 | + static final String SEND_PUSH_NOTIFICATION = "SendPushNotification"; |
| 48 | + |
| 49 | + @Inject |
| 50 | + @Delegate |
| 51 | + @Any |
| 52 | + private PushNotificationSender delegate; |
| 53 | + |
| 54 | + @Inject |
| 55 | + private Tracer tracer; |
| 56 | + |
| 57 | + /** |
| 58 | + * Default constructor for CDI. |
| 59 | + */ |
| 60 | + public OpenTelemetryPushNotificationSenderDecorator() { |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Constructor for unit testing. |
| 65 | + * |
| 66 | + * @param delegate the delegate push notification sender |
| 67 | + * @param tracer the tracer to use |
| 68 | + */ |
| 69 | + OpenTelemetryPushNotificationSenderDecorator(PushNotificationSender delegate, Tracer tracer) { |
| 70 | + this.delegate = delegate; |
| 71 | + this.tracer = tracer; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void sendNotification(StreamingEventKind event, @Nullable Task taskSnapshot) { |
| 76 | + var spanBuilder = tracer.spanBuilder(SEND_PUSH_NOTIFICATION) |
| 77 | + .setSpanKind(SpanKind.CLIENT) |
| 78 | + .setAttribute(GENAI_OPERATION_NAME, SEND_PUSH_NOTIFICATION) |
| 79 | + .setAttribute(PUSH_NOTIFICATION_EVENT_KIND, event.getClass().getSimpleName()); |
| 80 | + |
| 81 | + String taskId = extractTaskId(event, taskSnapshot); |
| 82 | + if (taskId != null) { |
| 83 | + spanBuilder.setAttribute(GENAI_TASK_ID, taskId); |
| 84 | + } |
| 85 | + String contextId = extractContextId(event, taskSnapshot); |
| 86 | + if (contextId != null) { |
| 87 | + spanBuilder.setAttribute(GENAI_CONTEXT_ID, contextId); |
| 88 | + } |
| 89 | + if (extractResponse()) { |
| 90 | + spanBuilder.setAttribute(GENAI_RESPONSE, event.toString()); |
| 91 | + } |
| 92 | + |
| 93 | + Span span = spanBuilder.startSpan(); |
| 94 | + try (Scope scope = span.makeCurrent()) { |
| 95 | + delegate.sendNotification(event, taskSnapshot); |
| 96 | + span.setStatus(StatusCode.OK); |
| 97 | + } catch (Exception ex) { |
| 98 | + span.setAttribute(ERROR_TYPE, ex.getMessage() != null ? ex.getMessage() : ex.getClass().getName()); |
| 99 | + span.setStatus(StatusCode.ERROR, ex.getMessage()); |
| 100 | + throw ex; |
| 101 | + } finally { |
| 102 | + span.end(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @Nullable |
| 107 | + private static String extractTaskId(StreamingEventKind event, @Nullable Task taskSnapshot) { |
| 108 | + if (taskSnapshot != null) { |
| 109 | + return taskSnapshot.id(); |
| 110 | + } |
| 111 | + if (event instanceof Task task) { |
| 112 | + return task.id(); |
| 113 | + } |
| 114 | + if (event instanceof TaskStatusUpdateEvent statusUpdate) { |
| 115 | + return statusUpdate.taskId(); |
| 116 | + } |
| 117 | + if (event instanceof TaskArtifactUpdateEvent artifactUpdate) { |
| 118 | + return artifactUpdate.taskId(); |
| 119 | + } |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + @Nullable |
| 124 | + private static String extractContextId(StreamingEventKind event, @Nullable Task taskSnapshot) { |
| 125 | + if (taskSnapshot != null) { |
| 126 | + return taskSnapshot.contextId(); |
| 127 | + } |
| 128 | + if (event instanceof Task task) { |
| 129 | + return task.contextId(); |
| 130 | + } |
| 131 | + if (event instanceof TaskStatusUpdateEvent statusUpdate) { |
| 132 | + return statusUpdate.contextId(); |
| 133 | + } |
| 134 | + if (event instanceof Message message) { |
| 135 | + return message.contextId(); |
| 136 | + } |
| 137 | + return null; |
| 138 | + } |
| 139 | + |
| 140 | + private boolean extractResponse() { |
| 141 | + return Boolean.getBoolean(EXTRACT_RESPONSE_SYS_PROPERTY); |
| 142 | + } |
| 143 | +} |
0 commit comments