|
10 | 10 | import io.opentelemetry.proto.common.v1.InstrumentationScope; |
11 | 11 | import io.opentelemetry.proto.common.v1.KeyValue; |
12 | 12 | import io.opentelemetry.proto.metrics.v1.AggregationTemporality; |
| 13 | +import io.opentelemetry.proto.metrics.v1.ExponentialHistogramDataPoint; |
| 14 | +import io.opentelemetry.proto.metrics.v1.HistogramDataPoint; |
| 15 | +import io.opentelemetry.proto.metrics.v1.Metric; |
13 | 16 | import io.opentelemetry.proto.metrics.v1.NumberDataPoint; |
14 | 17 | import io.opentelemetry.proto.resource.v1.Resource; |
15 | 18 |
|
|
32 | 35 | import java.util.List; |
33 | 36 | import java.util.concurrent.TimeUnit; |
34 | 37 |
|
| 38 | +import static io.opentelemetry.proto.metrics.v1.AggregationTemporality.AGGREGATION_TEMPORALITY_DELTA; |
35 | 39 | import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createDoubleDataPoint; |
| 40 | +import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createExponentialHistogramMetric; |
36 | 41 | import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createGaugeMetric; |
| 42 | +import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createHistogramMetric; |
37 | 43 | import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createLongDataPoint; |
38 | 44 | import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createSumMetric; |
39 | 45 | import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.keyValue; |
@@ -191,4 +197,71 @@ public void testEmptyFields() throws IOException { |
191 | 197 | assertThat(doc.evaluate("unit"), is(nullValue())); |
192 | 198 | } |
193 | 199 |
|
| 200 | + public void testExponentialHistogram() throws Exception{ |
| 201 | + Resource resource = Resource.newBuilder().build(); |
| 202 | + InstrumentationScope scope = InstrumentationScope.newBuilder().build(); |
| 203 | + |
| 204 | + ExponentialHistogramDataPoint dataPoint = ExponentialHistogramDataPoint.newBuilder() |
| 205 | + .setTimeUnixNano(timestamp) |
| 206 | + .setStartTimeUnixNano(startTimestamp) |
| 207 | + .setZeroCount(1) |
| 208 | + .setPositive(ExponentialHistogramDataPoint.Buckets.newBuilder().setOffset(0).addAllBucketCounts(List.of(1L, 1L))) |
| 209 | + .setNegative(ExponentialHistogramDataPoint.Buckets.newBuilder().setOffset(0).addAllBucketCounts(List.of(1L, 1L))) |
| 210 | + .build(); |
| 211 | + Metric metric = createExponentialHistogramMetric("exponential_histogram", "", List.of(), AGGREGATION_TEMPORALITY_DELTA); |
| 212 | + List<DataPoint> dataPoints = List.of(new DataPoint.ExponentialHistogram(dataPoint, metric)); |
| 213 | + |
| 214 | + DataPointGroupingContext.DataPointGroup dataPointGroup = new DataPointGroupingContext.DataPointGroup( |
| 215 | + resource, |
| 216 | + null, |
| 217 | + scope, |
| 218 | + null, |
| 219 | + List.of(), |
| 220 | + "", |
| 221 | + dataPoints, |
| 222 | + "metrics-generic.otel-default" |
| 223 | + ); |
| 224 | + |
| 225 | + XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); |
| 226 | + HashMap<String, String> dynamicTemplates = documentBuilder.buildMetricDocument(builder, dataPointGroup); |
| 227 | + |
| 228 | + ObjectPath doc = ObjectPath.createFromXContent(JsonXContent.jsonXContent, BytesReference.bytes(builder)); |
| 229 | + assertThat(doc.evaluate("metrics.exponential_histogram.values"), equalTo(List.of(-3.0, -1.5, 0.0, 1.5, 3.0))); |
| 230 | + assertThat(doc.evaluate("metrics.exponential_histogram.counts"), equalTo(List.of(1, 1, 1, 1, 1))); |
| 231 | + assertThat(dynamicTemplates, hasEntry("metrics.exponential_histogram", "histogram")); |
| 232 | + } |
| 233 | + |
| 234 | + public void testHistogram() throws Exception{ |
| 235 | + Resource resource = Resource.newBuilder().build(); |
| 236 | + InstrumentationScope scope = InstrumentationScope.newBuilder().build(); |
| 237 | + |
| 238 | + HistogramDataPoint dataPoint = HistogramDataPoint.newBuilder() |
| 239 | + .setTimeUnixNano(timestamp) |
| 240 | + .setStartTimeUnixNano(startTimestamp) |
| 241 | + .addBucketCounts(10L) |
| 242 | + .addExplicitBounds(5.0) |
| 243 | + .build(); |
| 244 | + Metric metric = createHistogramMetric("histogram", "", List.of(), AGGREGATION_TEMPORALITY_DELTA); |
| 245 | + List<DataPoint> dataPoints = List.of(new DataPoint.Histogram(dataPoint, metric)); |
| 246 | + |
| 247 | + DataPointGroupingContext.DataPointGroup dataPointGroup = new DataPointGroupingContext.DataPointGroup( |
| 248 | + resource, |
| 249 | + null, |
| 250 | + scope, |
| 251 | + null, |
| 252 | + List.of(), |
| 253 | + "", |
| 254 | + dataPoints, |
| 255 | + "metrics-generic.otel-default" |
| 256 | + ); |
| 257 | + |
| 258 | + XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); |
| 259 | + HashMap<String, String> dynamicTemplates = documentBuilder.buildMetricDocument(builder, dataPointGroup); |
| 260 | + |
| 261 | + ObjectPath doc = ObjectPath.createFromXContent(JsonXContent.jsonXContent, BytesReference.bytes(builder)); |
| 262 | + assertThat(doc.evaluate("metrics.histogram.values"), equalTo(List.of(2.5))); |
| 263 | + assertThat(doc.evaluate("metrics.histogram.counts"), equalTo(List.of(10))); |
| 264 | + assertThat(dynamicTemplates, hasEntry("metrics.histogram", "histogram")); |
| 265 | + } |
| 266 | + |
194 | 267 | } |
0 commit comments