7575import java .util .concurrent .TimeUnit ;
7676
7777import static io .opentelemetry .api .common .AttributeKey .stringKey ;
78+ import static io .opentelemetry .sdk .metrics .data .AggregationTemporality .CUMULATIVE ;
79+ import static io .opentelemetry .sdk .metrics .data .AggregationTemporality .DELTA ;
7880import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertResponse ;
7981import static org .hamcrest .Matchers .aMapWithSize ;
8082import static org .hamcrest .Matchers .anEmptyMap ;
@@ -286,7 +288,7 @@ public void testGroupingDifferentGroup() throws Exception {
286288
287289 public void testTimeSeriesMetrics () throws Exception {
288290 long now = Clock .getDefault ().now ();
289- MetricData metric1 = createCounter (TEST_RESOURCE , Attributes .empty (), "counter" , 42 , "By" , now );
291+ MetricData metric1 = createCounter (TEST_RESOURCE , Attributes .empty (), "counter" , 42 , "By" , now , CUMULATIVE );
290292 MetricData metric2 = createDoubleGauge (TEST_RESOURCE , Attributes .empty (), "gauge" , 42 , "By" , now );
291293 export (List .of (metric1 , metric2 ));
292294
@@ -303,9 +305,30 @@ public void testTimeSeriesMetrics() throws Exception {
303305 });
304306 }
305307
308+ public void testCounterTemporality () throws Exception {
309+ long now = Clock .getDefault ().now ();
310+ export (
311+ List .of (
312+ createCounter (TEST_RESOURCE , Attributes .empty (), "cumulative_counter" , 42 , "By" , now , CUMULATIVE ),
313+ createCounter (TEST_RESOURCE , Attributes .empty (), "delta_counter" , 42 , "By" , now , DELTA )
314+ )
315+ );
316+
317+ assertResponse (client ().admin ().indices ().prepareGetMappings (TEST_REQUEST_TIMEOUT , "metrics-generic.otel-default" ), resp -> {
318+ Map <String , MappingMetadata > mappings = resp .getMappings ();
319+ assertThat (mappings , aMapWithSize (1 ));
320+ Map <String , Object > mapping = mappings .values ().iterator ().next ().getSourceAsMap ();
321+ assertThat (mapping , not (anEmptyMap ()));
322+ assertThat (evaluate (mapping , "properties.metrics.properties.cumulative_counter.type" ), equalTo ("long" ));
323+ assertThat (evaluate (mapping , "properties.metrics.properties.cumulative_counter.time_series_metric" ), equalTo ("counter" ));
324+ assertThat (evaluate (mapping , "properties.metrics.properties.delta_counter.type" ), equalTo ("long" ));
325+ assertThat (evaluate (mapping , "properties.metrics.properties.delta_counter.time_series_metric" ), equalTo ("gauge" ));
326+ });
327+ }
328+
306329 public void testExponentialHistograms () throws Exception {
307330 long now = Clock .getDefault ().now ();
308- export (List .of (createExponentialHistogram (now , "exponential_histogram" , AggregationTemporality . DELTA , Attributes .empty ())));
331+ export (List .of (createExponentialHistogram (now , "exponential_histogram" , DELTA , Attributes .empty ())));
309332
310333 assertResponse (client ().admin ().indices ().prepareGetMappings (TEST_REQUEST_TIMEOUT , "metrics-generic.otel-default" ), resp -> {
311334 Map <String , MappingMetadata > mappings = resp .getMappings ();
@@ -334,7 +357,7 @@ public void testExponentialHistogramsAsAggregateMetricDouble() throws Exception
334357 createExponentialHistogram (
335358 now ,
336359 "exponential_histogram_summary" ,
337- AggregationTemporality . DELTA ,
360+ DELTA ,
338361 Attributes .of (
339362 AttributeKey .stringArrayKey ("elasticsearch.mapping.hints" ),
340363 List .of ("aggregate_metric_double" , "_doc_count" )
@@ -364,7 +387,7 @@ public void testExponentialHistogramsAsAggregateMetricDouble() throws Exception
364387
365388 public void testHistogram () throws Exception {
366389 long now = Clock .getDefault ().now ();
367- export (List .of (createHistogram (now , "histogram" , AggregationTemporality . DELTA , Attributes .empty ())));
390+ export (List .of (createHistogram (now , "histogram" , DELTA , Attributes .empty ())));
368391
369392 assertResponse (client ().admin ().indices ().prepareGetMappings (TEST_REQUEST_TIMEOUT , "metrics-generic.otel-default" ), resp -> {
370393 Map <String , MappingMetadata > mappings = resp .getMappings ();
@@ -391,7 +414,7 @@ public void testHistogramAsAggregateMetricDouble() throws Exception {
391414 createHistogram (
392415 now ,
393416 "histogram_summary" ,
394- AggregationTemporality . DELTA ,
417+ DELTA ,
395418 Attributes .of (
396419 AttributeKey .stringArrayKey ("elasticsearch.mapping.hints" ),
397420 List .of ("aggregate_metric_double" , "_doc_count" )
@@ -422,8 +445,8 @@ public void testCumulativeHistograms() {
422445 RuntimeException .class ,
423446 () -> export (
424447 List .of (
425- createExponentialHistogram (now , "exponential_histogram" , AggregationTemporality . CUMULATIVE , Attributes .empty ()),
426- createHistogram (now , "histogram" , AggregationTemporality . CUMULATIVE , Attributes .empty ())
448+ createExponentialHistogram (now , "exponential_histogram" , CUMULATIVE , Attributes .empty ()),
449+ createHistogram (now , "histogram" , CUMULATIVE , Attributes .empty ())
427450 )
428451 )
429452 );
@@ -509,7 +532,8 @@ private static MetricData createCounter(
509532 String name ,
510533 long value ,
511534 String unit ,
512- long timeEpochNanos
535+ long timeEpochNanos ,
536+ AggregationTemporality temporality
513537 ) {
514538 return ImmutableMetricData .createLongSum (
515539 resource ,
@@ -519,7 +543,7 @@ private static MetricData createCounter(
519543 unit ,
520544 ImmutableSumData .create (
521545 true ,
522- AggregationTemporality . CUMULATIVE ,
546+ temporality ,
523547 List .of (ImmutableLongPointData .create (timeEpochNanos , timeEpochNanos , attributes , value ))
524548 )
525549 );
0 commit comments