|
| 1 | +package org.a2aproject.sdk.extras.opentelemetry.it; |
| 2 | + |
| 3 | +import com.google.gson.JsonArray; |
| 4 | +import com.google.gson.JsonElement; |
| 5 | +import com.google.gson.JsonObject; |
| 6 | +import io.opentelemetry.api.common.Attributes; |
| 7 | +import io.opentelemetry.sdk.metrics.data.HistogramData; |
| 8 | +import io.opentelemetry.sdk.metrics.data.HistogramPointData; |
| 9 | +import io.opentelemetry.sdk.metrics.data.MetricData; |
| 10 | +import io.opentelemetry.sdk.metrics.data.MetricDataType; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 13 | +import org.mockito.Mock; |
| 14 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | +import static org.mockito.Mockito.when; |
| 22 | + |
| 23 | +@ExtendWith(MockitoExtension.class) |
| 24 | +class A2ATestRoutesTest { |
| 25 | + |
| 26 | + @Mock |
| 27 | + private MetricData metricData; |
| 28 | + @Mock |
| 29 | + private HistogramData histogramData; |
| 30 | + @Mock |
| 31 | + private HistogramPointData point1; |
| 32 | + @Mock |
| 33 | + private HistogramPointData point2; |
| 34 | + |
| 35 | + private final A2ATestRoutes routes = new A2ATestRoutes(); |
| 36 | + |
| 37 | + @Test |
| 38 | + void serializeMetrics_singleHistogramPoint_returnsPointsArray() { |
| 39 | + when(metricData.getName()).thenReturn("gen_ai.agent.a2a.streaming.duration"); |
| 40 | + when(metricData.getDescription()).thenReturn("desc"); |
| 41 | + when(metricData.getUnit()).thenReturn("s"); |
| 42 | + when(metricData.getType()).thenReturn(MetricDataType.HISTOGRAM); |
| 43 | + when(metricData.getHistogramData()).thenReturn(histogramData); |
| 44 | + when(histogramData.getPoints()).thenReturn(List.of(point1)); |
| 45 | + when(point1.getCount()).thenReturn(1L); |
| 46 | + when(point1.getSum()).thenReturn(0.5); |
| 47 | + when(point1.getAttributes()).thenReturn(Attributes.empty()); |
| 48 | + |
| 49 | + JsonElement result = routes.serializeMetrics(List.of(metricData)); |
| 50 | + |
| 51 | + JsonObject metric = result.getAsJsonArray().get(0).getAsJsonObject(); |
| 52 | + assertTrue(metric.has("points"), "Should have 'points' JsonArray"); |
| 53 | + assertFalse(metric.has("point"), "Should not have single-overwriting 'point' key"); |
| 54 | + assertEquals(1, metric.getAsJsonArray("points").size()); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void serializeMetrics_multipleHistogramPoints_allPointsSerialized() { |
| 59 | + when(metricData.getName()).thenReturn("gen_ai.agent.a2a.streaming.duration"); |
| 60 | + when(metricData.getDescription()).thenReturn("desc"); |
| 61 | + when(metricData.getUnit()).thenReturn("s"); |
| 62 | + when(metricData.getType()).thenReturn(MetricDataType.HISTOGRAM); |
| 63 | + when(metricData.getHistogramData()).thenReturn(histogramData); |
| 64 | + when(histogramData.getPoints()).thenReturn(List.of(point1, point2)); |
| 65 | + when(point1.getCount()).thenReturn(1L); |
| 66 | + when(point1.getSum()).thenReturn(0.5); |
| 67 | + when(point1.getAttributes()).thenReturn(Attributes.empty()); |
| 68 | + when(point2.getCount()).thenReturn(3L); |
| 69 | + when(point2.getSum()).thenReturn(2.0); |
| 70 | + when(point2.getAttributes()).thenReturn(Attributes.empty()); |
| 71 | + |
| 72 | + JsonElement result = routes.serializeMetrics(List.of(metricData)); |
| 73 | + |
| 74 | + JsonObject metric = result.getAsJsonArray().get(0).getAsJsonObject(); |
| 75 | + JsonArray points = metric.getAsJsonArray("points"); |
| 76 | + assertEquals(2, points.size(), "Both histogram data points must be serialized"); |
| 77 | + assertEquals(1L, points.get(0).getAsJsonObject().get("count").getAsLong()); |
| 78 | + assertEquals(0.5, points.get(0).getAsJsonObject().get("sum").getAsDouble()); |
| 79 | + assertEquals(3L, points.get(1).getAsJsonObject().get("count").getAsLong()); |
| 80 | + assertEquals(2.0, points.get(1).getAsJsonObject().get("sum").getAsDouble()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void serializeMetrics_nonHistogramMetric_hasNoPointsKey() { |
| 85 | + when(metricData.getName()).thenReturn("gen_ai.client.operation.duration"); |
| 86 | + when(metricData.getDescription()).thenReturn("desc"); |
| 87 | + when(metricData.getUnit()).thenReturn("s"); |
| 88 | + when(metricData.getType()).thenReturn(MetricDataType.LONG_SUM); |
| 89 | + |
| 90 | + JsonElement result = routes.serializeMetrics(List.of(metricData)); |
| 91 | + |
| 92 | + JsonObject metric = result.getAsJsonArray().get(0).getAsJsonObject(); |
| 93 | + assertFalse(metric.has("points"), "Non-histogram metric should not have a 'points' key"); |
| 94 | + assertFalse(metric.has("point"), "Non-histogram metric should not have a 'point' key"); |
| 95 | + } |
| 96 | +} |
0 commit comments