|
16 | 16 | import org.elasticsearch.exponentialhistogram.ExponentialHistogramMerger; |
17 | 17 | import org.elasticsearch.exponentialhistogram.ExponentialHistogramTestUtils; |
18 | 18 | import org.elasticsearch.exponentialhistogram.ExponentialHistogramXContent; |
| 19 | +import org.elasticsearch.exponentialhistogram.ExponentialScaleUtils; |
19 | 20 | import org.elasticsearch.test.ESTestCase; |
20 | 21 | import org.elasticsearch.xcontent.XContentBuilder; |
21 | 22 | import org.elasticsearch.xcontent.XContentParser; |
|
26 | 27 | import org.elasticsearch.xpack.oteldata.otlp.docbuilder.MappingHints; |
27 | 28 |
|
28 | 29 | import java.io.IOException; |
| 30 | +import java.util.List; |
29 | 31 | import java.util.stream.LongStream; |
30 | 32 |
|
31 | 33 | import static org.hamcrest.Matchers.closeTo; |
32 | 34 | import static org.hamcrest.Matchers.equalTo; |
| 35 | +import static org.hamcrest.Matchers.lessThan; |
33 | 36 |
|
34 | 37 | public class ParsedHistogramConverterTests extends ESTestCase { |
35 | 38 |
|
| 39 | + public void testExponentialHistogramRoundTrip() { |
| 40 | + ExponentialHistogram input = ExponentialHistogramTestUtils.randomHistogram(); |
| 41 | + HistogramParser.ParsedHistogram tdigest = ParsedHistogramConverter.exponentialToTDigest(toParsed(input)); |
| 42 | + ExponentialHistogramParser.ParsedExponentialHistogram output = ParsedHistogramConverter.tDigestToExponential(tdigest); |
| 43 | + |
| 44 | + // the conversion looses the width of the original buckets, but the bucket centers (arithmetic mean of boundaries) |
| 45 | + // should be very close |
| 46 | + |
| 47 | + assertThat(output.zeroCount(), equalTo(input.zeroBucket().count())); |
| 48 | + assertArithmeticBucketCentersClose(input.negativeBuckets().iterator(), output.negativeBuckets(), output.scale()); |
| 49 | + assertArithmeticBucketCentersClose(input.positiveBuckets().iterator(), output.positiveBuckets(), output.scale()); |
| 50 | + } |
| 51 | + |
| 52 | + private static void assertArithmeticBucketCentersClose( |
| 53 | + BucketIterator originalBuckets, |
| 54 | + List<IndexWithCount> convertedBuckets, |
| 55 | + int convertedScale |
| 56 | + ) { |
| 57 | + for (IndexWithCount convertedBucket : convertedBuckets) { |
| 58 | + assertThat(originalBuckets.hasNext(), equalTo(true)); |
| 59 | + |
| 60 | + double originalCenter = (ExponentialScaleUtils.getLowerBucketBoundary(originalBuckets.peekIndex(), originalBuckets.scale()) |
| 61 | + + ExponentialScaleUtils.getUpperBucketBoundary(originalBuckets.peekIndex(), originalBuckets.scale())) / 2.0; |
| 62 | + double convertedCenter = (ExponentialScaleUtils.getLowerBucketBoundary(convertedBucket.index(), convertedScale) |
| 63 | + + ExponentialScaleUtils.getUpperBucketBoundary(convertedBucket.index(), convertedScale)) / 2.0; |
| 64 | + |
| 65 | + double relativeError = Math.abs(convertedCenter - originalCenter) / Math.abs(originalCenter); |
| 66 | + assertThat( |
| 67 | + "original center=" + originalCenter + ", converted center=" + convertedCenter + ", relative error=" + relativeError, |
| 68 | + relativeError, |
| 69 | + closeTo(0, 0.0000001) |
| 70 | + ); |
| 71 | + |
| 72 | + originalBuckets.advance(); |
| 73 | + } |
| 74 | + assertThat(originalBuckets.hasNext(), equalTo(false)); |
| 75 | + } |
| 76 | + |
| 77 | + public void testToExponentialHistogramConversionWithCloseCentroids() { |
| 78 | + // build a t-digest with two centroids very close to each other |
| 79 | + List<Double> centroids = List.of(1.0, Math.nextAfter(1.0, 2)); |
| 80 | + List<Long> counts = List.of(1L, 2L); |
| 81 | + |
| 82 | + HistogramParser.ParsedHistogram input = new HistogramParser.ParsedHistogram(centroids, counts); |
| 83 | + ExponentialHistogramParser.ParsedExponentialHistogram converted = ParsedHistogramConverter.tDigestToExponential(input); |
| 84 | + |
| 85 | + assertThat(converted.zeroCount(), equalTo(0L)); |
| 86 | + List<IndexWithCount> posBuckets = converted.positiveBuckets(); |
| 87 | + assertThat(posBuckets.size(), equalTo(2)); |
| 88 | + assertThat(posBuckets.get(0).index(), lessThan(posBuckets.get(1).index())); |
| 89 | + assertThat(posBuckets.get(0).count(), equalTo(1L)); |
| 90 | + assertThat(posBuckets.get(1).count(), equalTo(2L)); |
| 91 | + } |
| 92 | + |
| 93 | + public void testToTDigestConversionMergesCentroids() { |
| 94 | + // build a histogram with two buckets very close to zero |
| 95 | + ExponentialHistogram input = ExponentialHistogram.builder(ExponentialHistogram.MAX_SCALE, ExponentialHistogramCircuitBreaker.noop()) |
| 96 | + .setPositiveBucket(ExponentialHistogram.MIN_INDEX, 1) |
| 97 | + .setPositiveBucket(ExponentialHistogram.MIN_INDEX + 1, 2) |
| 98 | + .build(); |
| 99 | + // due to rounding errors they end up as the same centroid, but should have the count merged |
| 100 | + HistogramParser.ParsedHistogram converted = ParsedHistogramConverter.exponentialToTDigest(toParsed(input)); |
| 101 | + assertThat(converted.values(), equalTo(List.of(0.0))); |
| 102 | + assertThat(converted.counts(), equalTo(List.of(3L))); |
| 103 | + } |
| 104 | + |
36 | 105 | public void testSameConversionBehaviourAsOtlpMetricsEndpoint() { |
37 | 106 | // our histograms are sparse, opentelemetry ones are dense. |
38 | 107 | // to test against the OTLP conversion algorithm, we need to make our random histogram dense enough first |
|
0 commit comments