-
Notifications
You must be signed in to change notification settings - Fork 25.6k
OTLP: add support for histograms #133902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
OTLP: add support for histograms #133902
Changes from 6 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
b7584ad
OTLP: add support for histograms
felixbarny 8492a44
[CI] Auto commit changes from spotless
b3c2f4b
Apply suggestions from code review
felixbarny f9e64c6
[CI] Auto commit changes from spotless
444f614
Merge remote-tracking branch 'origin/main' into otlp-histograms
felixbarny 222f9aa
Add missing import
felixbarny 2ed63ea
Merge remote-tracking branch 'origin/main' into otlp-histograms
felixbarny 95b1bda
Add buildMetricValue method to histogram data points
felixbarny 6842cd4
[CI] Auto commit changes from spotless
f159188
Simplify boolean expression
felixbarny cf6a514
Merge remote-tracking branch 'origin/main' into otlp-histograms
felixbarny ed203a4
Add support for mapping hints
felixbarny 0d588ff
Merge branch 'main' into otlp-histograms
felixbarny 64cd047
Merge remote-tracking branch 'origin/main' into otlp-histograms
felixbarny d3cfe08
Fix compile error after merge
felixbarny 8c4c621
Merge remote-tracking branch 'origin/main' into otlp-histograms
felixbarny 7f5d421
Fix compile error after merge
felixbarny b26d645
Merge remote-tracking branch 'origin/main' into otlp-histograms
felixbarny a8efd23
[CI] Auto commit changes from spotless
2e5866d
Merge branch 'main' into otlp-histograms
felixbarny f019162
Add rest tests for histograms
felixbarny 90cbac2
Add comment about converting exponential histograms to TDigest
felixbarny ed73673
[CI] Auto commit changes from spotless
18e7f76
Add accuracy tests for histogram converter
felixbarny 06a0039
Merge remote-tracking branch 'felixbarny/otlp-histograms' into otlp-h…
felixbarny ca49987
[CI] Auto commit changes from spotless
9cf95b0
Test with more distributions
felixbarny 10d6556
Merge remote-tracking branch 'origin/main' into otlp-histograms
felixbarny 35d4c08
Merge remote-tracking branch 'felixbarny/otlp-histograms' into otlp-h…
felixbarny 2bbdd25
Add comment about error bound
felixbarny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
...ata/src/main/java/org/elasticsearch/xpack/oteldata/otlp/datapoint/HistogramConverter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.oteldata.otlp.datapoint; | ||
|
||
import io.opentelemetry.proto.metrics.v1.ExponentialHistogramDataPoint; | ||
import io.opentelemetry.proto.metrics.v1.HistogramDataPoint; | ||
|
||
import org.elasticsearch.exponentialhistogram.ExponentialScaleUtils; | ||
|
||
/** | ||
* Utility class to convert OpenTelemetry histogram data points into counts and centroid values | ||
* so that we can use it with the {@code histogram} field type. | ||
* This class provides methods to extract counts and centroid values from both | ||
* {@link ExponentialHistogramDataPoint} and {@link HistogramDataPoint}. | ||
* The algorithm is ported over from the OpenTelemetry collector's Elasticsearch exporter. | ||
* @see <a href="https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/v0.132.0/exporter/elasticsearchexporter"> | ||
* Elasticsearch exporter on GitHub | ||
* </a> | ||
*/ | ||
class HistogramConverter { | ||
|
||
static <E extends Exception> void counts(ExponentialHistogramDataPoint dp, CheckedLongConsumer<E> counts) throws E { | ||
ExponentialHistogramDataPoint.Buckets negative = dp.getNegative(); | ||
|
||
for (int i = negative.getBucketCountsCount() - 1; i >= 0; i--) { | ||
long count = negative.getBucketCounts(i); | ||
if (count != 0) { | ||
counts.accept(count); | ||
} | ||
} | ||
|
||
long zeroCount = dp.getZeroCount(); | ||
if (zeroCount > 0) { | ||
counts.accept(zeroCount); | ||
} | ||
|
||
ExponentialHistogramDataPoint.Buckets positive = dp.getPositive(); | ||
for (int i = 0; i < positive.getBucketCountsCount(); i++) { | ||
long count = positive.getBucketCounts(i); | ||
if (count != 0) { | ||
counts.accept(count); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @see <a | ||
* href="https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/v0.132.0/exporter/elasticsearchexporter/internal/exphistogram/exphistogram.go"> | ||
* <code>ToTDigest</code> function | ||
* </a> | ||
*/ | ||
static <E extends Exception> void centroidValues(ExponentialHistogramDataPoint dp, CheckedDoubleConsumer<E> values) throws E { | ||
int scale = dp.getScale(); | ||
ExponentialHistogramDataPoint.Buckets negative = dp.getNegative(); | ||
|
||
int offset = negative.getOffset(); | ||
for (int i = negative.getBucketCountsCount() - 1; i >= 0; i--) { | ||
long count = negative.getBucketCounts(i); | ||
if (count != 0) { | ||
double lb = -ExponentialScaleUtils.getUpperBucketBoundary(offset + i, scale); | ||
double ub = -ExponentialScaleUtils.getLowerBucketBoundary(offset + i, scale); | ||
values.accept(lb + (ub - lb) / 2); | ||
} | ||
} | ||
|
||
long zeroCount = dp.getZeroCount(); | ||
if (zeroCount > 0) { | ||
values.accept(0.0); | ||
} | ||
|
||
ExponentialHistogramDataPoint.Buckets positive = dp.getPositive(); | ||
offset = positive.getOffset(); | ||
for (int i = 0; i < positive.getBucketCountsCount(); i++) { | ||
long count = positive.getBucketCounts(i); | ||
if (count != 0) { | ||
double lb = ExponentialScaleUtils.getLowerBucketBoundary(offset + i, scale); | ||
double ub = ExponentialScaleUtils.getUpperBucketBoundary(offset + i, scale); | ||
values.accept(lb + (ub - lb) / 2); | ||
} | ||
} | ||
} | ||
|
||
static <E extends Exception> void counts(HistogramDataPoint dp, CheckedLongConsumer<E> counts) throws E { | ||
for (int i = 0; i < dp.getBucketCountsCount(); i++) { | ||
long count = dp.getBucketCounts(i); | ||
if (count != 0) { | ||
counts.accept(count); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @see <a | ||
* href="https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/v0.132.0/exporter/elasticsearchexporter/internal/datapoints/histogram.go"> | ||
* <code>histogramToValue</code> function | ||
* </a> | ||
*/ | ||
static <E extends Exception> void centroidValues(HistogramDataPoint dp, CheckedDoubleConsumer<E> values) throws E { | ||
int size = dp.getBucketCountsCount(); | ||
for (int i = 0; i < size; i++) { | ||
long count = dp.getBucketCounts(i); | ||
if (count != 0) { | ||
double value; | ||
if (i == 0) { | ||
// (-infinity, explicit_bounds[i]] | ||
value = dp.getExplicitBounds(i); | ||
if (value > 0) { | ||
value /= 2; | ||
} | ||
} else if (i == size - 1) { | ||
// (explicit_bounds[i], +infinity) | ||
value = dp.getExplicitBounds(i - 1); | ||
} else { | ||
// [explicit_bounds[i-1], explicit_bounds[i]) | ||
// Use the midpoint between the boundaries. | ||
value = dp.getExplicitBounds(i - 1) + (dp.getExplicitBounds(i) - dp.getExplicitBounds(i - 1)) / 2.0; | ||
} | ||
values.accept(value); | ||
} | ||
} | ||
} | ||
|
||
interface CheckedLongConsumer<E extends Exception> { | ||
void accept(long value) throws E; | ||
} | ||
|
||
interface CheckedDoubleConsumer<E extends Exception> { | ||
void accept(double value) throws E; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...a/org/elasticsearch/xpack/oteldata/otlp/datapoint/DataPointExponentialHistogramTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.oteldata.otlp.datapoint; | ||
|
||
import io.opentelemetry.proto.metrics.v1.ExponentialHistogram; | ||
import io.opentelemetry.proto.metrics.v1.ExponentialHistogramDataPoint; | ||
import io.opentelemetry.proto.metrics.v1.Metric; | ||
|
||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import java.util.HashSet; | ||
|
||
import static io.opentelemetry.proto.metrics.v1.AggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE; | ||
import static io.opentelemetry.proto.metrics.v1.AggregationTemporality.AGGREGATION_TEMPORALITY_DELTA; | ||
import static org.hamcrest.Matchers.contains; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.empty; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class DataPointExponentialHistogramTests extends ESTestCase { | ||
|
||
private final HashSet<String> validationErrors = new HashSet<>(); | ||
|
||
public void testExponentialHistogram() { | ||
DataPoint.ExponentialHistogram doubleGauge = new DataPoint.ExponentialHistogram( | ||
ExponentialHistogramDataPoint.newBuilder().build(), | ||
Metric.newBuilder() | ||
.setExponentialHistogram(ExponentialHistogram.newBuilder().setAggregationTemporality(AGGREGATION_TEMPORALITY_DELTA).build()) | ||
.build() | ||
); | ||
assertThat(doubleGauge.getDynamicTemplate(), equalTo("histogram")); | ||
assertThat(doubleGauge.isValid(validationErrors), equalTo(true)); | ||
assertThat(validationErrors, empty()); | ||
} | ||
|
||
public void testExponentialHistogramUnsupportedTemporality() { | ||
DataPoint.ExponentialHistogram doubleGauge = new DataPoint.ExponentialHistogram( | ||
ExponentialHistogramDataPoint.newBuilder().build(), | ||
Metric.newBuilder() | ||
.setExponentialHistogram( | ||
ExponentialHistogram.newBuilder().setAggregationTemporality(AGGREGATION_TEMPORALITY_CUMULATIVE).build() | ||
) | ||
.build() | ||
); | ||
assertThat(doubleGauge.getDynamicTemplate(), equalTo("histogram")); | ||
assertThat(doubleGauge.isValid(validationErrors), equalTo(false)); | ||
assertThat(validationErrors, contains(containsString("cumulative exponential histogram metrics are not supported"))); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.