Skip to content

Commit d46781c

Browse files
committed
Use time_series_metric=gauge for delta counters
1 parent 9cea250 commit d46781c

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

x-pack/plugin/otel-data/src/internalClusterTest/java/org/elasticsearch/action/otlp/OTLPMetricsIndexingIT.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@
7575
import java.util.concurrent.TimeUnit;
7676

7777
import 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;
7880
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse;
7981
import static org.hamcrest.Matchers.aMapWithSize;
8082
import 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
);

x-pack/plugin/otel-data/src/main/java/org/elasticsearch/xpack/oteldata/otlp/datapoint/DataPoint.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.List;
2323
import java.util.Set;
2424

25+
import static io.opentelemetry.proto.metrics.v1.AggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE;
26+
2527
/**
2628
* Represents a metrics data point in the OpenTelemetry metrics data model.
2729
* This interface defines methods to access various properties of a data point,
@@ -147,7 +149,13 @@ public long getDocCount() {
147149

148150
@Override
149151
public String getDynamicTemplate(MappingHints mappingHints) {
150-
String prefix = metric.getDataCase() == Metric.DataCase.SUM ? "counter_" : "gauge_";
152+
String prefix;
153+
if (metric.hasSum()) {
154+
AggregationTemporality temporality = metric.getSum().getAggregationTemporality();
155+
prefix = temporality == AGGREGATION_TEMPORALITY_CUMULATIVE ? "counter_" : "gauge_";
156+
} else {
157+
prefix = "gauge_";
158+
}
151159
if (dataPoint.getValueCase() == NumberDataPoint.ValueCase.AS_INT) {
152160
return prefix + "long";
153161
} else if (dataPoint.getValueCase() == NumberDataPoint.ValueCase.AS_DOUBLE) {

0 commit comments

Comments
 (0)