Search in sources :

Example 1 with ExemplarReservoir

use of io.opentelemetry.sdk.metrics.exemplar.ExemplarReservoir in project opentelemetry-java by open-telemetry.

the class DoubleSumAggregatorTest method mergeAndDiff.

@Test
void mergeAndDiff() {
    Attributes attributes = Attributes.builder().put("test", "value").build();
    ExemplarData exemplar = DoubleExemplarData.create(attributes, 2L, SpanContext.create("00000000000000000000000000000001", "0000000000000002", TraceFlags.getDefault(), TraceState.getDefault()), 1);
    List<ExemplarData> exemplars = Collections.singletonList(exemplar);
    List<ExemplarData> previousExemplars = Collections.singletonList(DoubleExemplarData.create(attributes, 1L, SpanContext.create("00000000000000000000000000000001", "0000000000000002", TraceFlags.getDefault(), TraceState.getDefault()), 2));
    for (InstrumentType instrumentType : InstrumentType.values()) {
        for (AggregationTemporality temporality : AggregationTemporality.values()) {
            DoubleSumAggregator aggregator = new DoubleSumAggregator(InstrumentDescriptor.create("name", "description", "unit", instrumentType, InstrumentValueType.LONG), ExemplarReservoir::noSamples);
            DoubleAccumulation merged = aggregator.merge(DoubleAccumulation.create(1.0d, previousExemplars), DoubleAccumulation.create(2.0d, exemplars));
            assertThat(merged.getValue()).withFailMessage("Invalid merge result for instrumentType %s, temporality %s: %s", instrumentType, temporality, merged).isEqualTo(3.0d);
            assertThat(merged.getExemplars()).containsExactly(exemplar);
            DoubleAccumulation diffed = aggregator.diff(DoubleAccumulation.create(1d), DoubleAccumulation.create(2d, exemplars));
            assertThat(diffed.getValue()).withFailMessage("Invalid diff result for instrumentType %s, temporality %s: %s", instrumentType, temporality, merged).isEqualTo(1d);
            assertThat(diffed.getExemplars()).containsExactly(exemplar);
        }
    }
}
Also used : DoubleExemplarData(io.opentelemetry.sdk.metrics.data.DoubleExemplarData) ExemplarData(io.opentelemetry.sdk.metrics.data.ExemplarData) Attributes(io.opentelemetry.api.common.Attributes) InstrumentType(io.opentelemetry.sdk.metrics.common.InstrumentType) ExemplarReservoir(io.opentelemetry.sdk.metrics.exemplar.ExemplarReservoir) AggregationTemporality(io.opentelemetry.sdk.metrics.data.AggregationTemporality) Test(org.junit.jupiter.api.Test)

Example 2 with ExemplarReservoir

use of io.opentelemetry.sdk.metrics.exemplar.ExemplarReservoir in project opentelemetry-java by open-telemetry.

the class DoubleExponentialHistogramAggregatorTest method testToMetricData.

@Test
void testToMetricData() {
    Attributes attributes = Attributes.builder().put("test", "value").build();
    ExemplarData exemplar = DoubleExemplarData.create(attributes, 2L, SpanContext.create("00000000000000000000000000000001", "0000000000000002", TraceFlags.getDefault(), TraceState.getDefault()), 1);
    @SuppressWarnings("unchecked") Supplier<ExemplarReservoir> reservoirSupplier = Mockito.mock(Supplier.class);
    Mockito.when(reservoir.collectAndReset(Attributes.empty())).thenReturn(Collections.singletonList(exemplar));
    Mockito.when(reservoirSupplier.get()).thenReturn(reservoir);
    DoubleExponentialHistogramAggregator cumulativeAggregator = new DoubleExponentialHistogramAggregator(reservoirSupplier);
    AggregatorHandle<ExponentialHistogramAccumulation> aggregatorHandle = cumulativeAggregator.createHandle();
    aggregatorHandle.recordDouble(0);
    aggregatorHandle.recordDouble(0);
    aggregatorHandle.recordDouble(123.456);
    ExponentialHistogramAccumulation acc = aggregatorHandle.accumulateThenReset(Attributes.empty());
    MetricData metricDataCumulative = cumulativeAggregator.toMetricData(RESOURCE, INSTRUMENTATION_LIBRARY_INFO, METRIC_DESCRIPTOR, Collections.singletonMap(Attributes.empty(), acc), AggregationTemporality.CUMULATIVE, 0, 10, 100);
    // Assertions run twice to verify immutability; recordings shouldn't modify the metric data
    for (int i = 0; i < 2; i++) {
        assertThat(metricDataCumulative).hasExponentialHistogram().isCumulative().points().satisfiesExactly(point -> {
            assertThat(point).hasSum(123.456).hasScale(20).hasZeroCount(2).hasTotalCount(3).hasExemplars(exemplar);
            assertThat(point.getPositiveBuckets()).hasCounts(Collections.singletonList(1L)).hasOffset(valueToIndex(20, 123.456)).hasTotalCount(1);
            assertThat(point.getNegativeBuckets()).hasTotalCount(0).hasCounts(Collections.emptyList());
        });
        aggregatorHandle.recordDouble(1);
        aggregatorHandle.recordDouble(-1);
        aggregatorHandle.recordDouble(0);
    }
    MetricData metricDataDelta = cumulativeAggregator.toMetricData(RESOURCE, INSTRUMENTATION_LIBRARY_INFO, METRIC_DESCRIPTOR, Collections.singletonMap(Attributes.empty(), acc), AggregationTemporality.DELTA, 0, 10, 100);
    assertThat(ExponentialHistogramData.fromMetricData(metricDataDelta).getAggregationTemporality()).isEqualTo(AggregationTemporality.DELTA);
}
Also used : DoubleExemplarData(io.opentelemetry.sdk.metrics.data.DoubleExemplarData) ExemplarData(io.opentelemetry.sdk.metrics.data.ExemplarData) Attributes(io.opentelemetry.api.common.Attributes) ExemplarReservoir(io.opentelemetry.sdk.metrics.exemplar.ExemplarReservoir) MetricData(io.opentelemetry.sdk.metrics.data.MetricData) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with ExemplarReservoir

use of io.opentelemetry.sdk.metrics.exemplar.ExemplarReservoir in project opentelemetry-java by open-telemetry.

the class LongSumAggregatorTest method mergeAndDiff.

@Test
void mergeAndDiff() {
    ExemplarData exemplar = DoubleExemplarData.create(Attributes.empty(), 2L, SpanContext.create("00000000000000000000000000000001", "0000000000000002", TraceFlags.getDefault(), TraceState.getDefault()), 1);
    List<ExemplarData> exemplars = Collections.singletonList(exemplar);
    for (InstrumentType instrumentType : InstrumentType.values()) {
        for (AggregationTemporality temporality : AggregationTemporality.values()) {
            LongSumAggregator aggregator = new LongSumAggregator(InstrumentDescriptor.create("name", "description", "unit", instrumentType, InstrumentValueType.LONG), ExemplarReservoir::noSamples);
            LongAccumulation merged = aggregator.merge(LongAccumulation.create(1L), LongAccumulation.create(2L, exemplars));
            assertThat(merged.getValue()).withFailMessage("Invalid merge result for instrumentType %s, temporality %s: %s", instrumentType, temporality, merged).isEqualTo(3);
            assertThat(merged.getExemplars()).containsExactly(exemplar);
            LongAccumulation diffed = aggregator.diff(LongAccumulation.create(1L), LongAccumulation.create(2L, exemplars));
            assertThat(diffed.getValue()).withFailMessage("Invalid diff result for instrumentType %s, temporality %s: %s", instrumentType, temporality, merged).isEqualTo(1);
            assertThat(diffed.getExemplars()).containsExactly(exemplar);
        }
    }
}
Also used : DoubleExemplarData(io.opentelemetry.sdk.metrics.data.DoubleExemplarData) ExemplarData(io.opentelemetry.sdk.metrics.data.ExemplarData) InstrumentType(io.opentelemetry.sdk.metrics.common.InstrumentType) ExemplarReservoir(io.opentelemetry.sdk.metrics.exemplar.ExemplarReservoir) AggregationTemporality(io.opentelemetry.sdk.metrics.data.AggregationTemporality) Test(org.junit.jupiter.api.Test)

Aggregations

DoubleExemplarData (io.opentelemetry.sdk.metrics.data.DoubleExemplarData)3 ExemplarData (io.opentelemetry.sdk.metrics.data.ExemplarData)3 ExemplarReservoir (io.opentelemetry.sdk.metrics.exemplar.ExemplarReservoir)3 Test (org.junit.jupiter.api.Test)3 Attributes (io.opentelemetry.api.common.Attributes)2 InstrumentType (io.opentelemetry.sdk.metrics.common.InstrumentType)2 AggregationTemporality (io.opentelemetry.sdk.metrics.data.AggregationTemporality)2 MetricData (io.opentelemetry.sdk.metrics.data.MetricData)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1