Search in sources :

Example 41 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.

the class DoubleHistogramAggregatorTest method diffAccumulation.

@Test
void diffAccumulation() {
    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));
    HistogramAccumulation previousAccumulation = HistogramAccumulation.create(2, new long[] { 1, 1, 2 }, previousExemplars);
    HistogramAccumulation nextAccumulation = HistogramAccumulation.create(5, new long[] { 2, 2, 2 }, exemplars);
    // Assure most recent exemplars are kept.
    assertThat(aggregator.diff(previousAccumulation, nextAccumulation)).isEqualTo(HistogramAccumulation.create(3, new long[] { 1, 1, 0 }, exemplars));
}
Also used : DoubleExemplarData(io.opentelemetry.sdk.metrics.data.DoubleExemplarData) ExemplarData(io.opentelemetry.sdk.metrics.data.ExemplarData) Attributes(io.opentelemetry.api.common.Attributes) Test(org.junit.jupiter.api.Test)

Example 42 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.

the class DoubleSumAggregatorTest method testExemplarsInAccumulation.

@Test
void testExemplarsInAccumulation() {
    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);
    Mockito.when(reservoir.collectAndReset(Attributes.empty())).thenReturn(exemplars);
    DoubleSumAggregator aggregator = new DoubleSumAggregator(InstrumentDescriptor.create("instrument_name", "instrument_description", "instrument_unit", InstrumentType.COUNTER, InstrumentValueType.DOUBLE), () -> reservoir);
    AggregatorHandle<DoubleAccumulation> aggregatorHandle = aggregator.createHandle();
    aggregatorHandle.recordDouble(0, attributes, Context.root());
    assertThat(aggregatorHandle.accumulateThenReset(Attributes.empty())).isEqualTo(DoubleAccumulation.create(0, exemplars));
}
Also used : DoubleExemplarData(io.opentelemetry.sdk.metrics.data.DoubleExemplarData) ExemplarData(io.opentelemetry.sdk.metrics.data.ExemplarData) Attributes(io.opentelemetry.api.common.Attributes) Test(org.junit.jupiter.api.Test)

Example 43 with Attributes

use of io.opentelemetry.api.common.Attributes 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 44 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.

the class SdkObservableDoubleUpDownCounterTest method collectMetrics_DeltaSumAggregator.

@Test
void collectMetrics_DeltaSumAggregator() {
    InMemoryMetricReader sdkMeterReader = InMemoryMetricReader.createDelta();
    SdkMeterProvider sdkMeterProvider = sdkMeterProviderBuilder.registerMetricReader(sdkMeterReader).registerView(InstrumentSelector.builder().setType(InstrumentType.OBSERVABLE_UP_DOWN_COUNTER).build(), View.builder().setAggregation(Aggregation.sum()).build()).build();
    sdkMeterProvider.get(getClass().getName()).upDownCounterBuilder("testObserver").ofDoubles().buildWithCallback(result -> result.record(12.1d, Attributes.builder().put("k", "v").build()));
    testClock.advance(Duration.ofNanos(SECOND_NANOS));
    assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testObserver").hasDoubleSum().isDelta().isNotMonotonic().points().satisfiesExactlyInAnyOrder(point -> assertThat(point).hasStartEpochNanos(testClock.now() - SECOND_NANOS).hasEpochNanos(testClock.now()).hasValue(12.1).attributes().hasSize(1).containsEntry("k", "v")));
    testClock.advance(Duration.ofNanos(SECOND_NANOS));
    assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testObserver").hasDoubleSum().isDelta().isNotMonotonic().points().satisfiesExactlyInAnyOrder(point -> assertThat(point).hasStartEpochNanos(testClock.now() - SECOND_NANOS).hasEpochNanos(testClock.now()).hasValue(0).attributes().hasSize(1).containsEntry("k", "v")));
}
Also used : Resource(io.opentelemetry.sdk.resources.Resource) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Attributes(io.opentelemetry.api.common.Attributes) InstrumentType(io.opentelemetry.sdk.metrics.common.InstrumentType) Aggregation(io.opentelemetry.sdk.metrics.view.Aggregation) InstrumentSelector(io.opentelemetry.sdk.metrics.view.InstrumentSelector) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) Test(org.junit.jupiter.api.Test) ObservableDoubleUpDownCounter(io.opentelemetry.api.metrics.ObservableDoubleUpDownCounter) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) View(io.opentelemetry.sdk.metrics.view.View) AttributeKey.stringKey(io.opentelemetry.api.common.AttributeKey.stringKey) Duration(java.time.Duration) TestClock(io.opentelemetry.sdk.testing.time.TestClock) MetricAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) Test(org.junit.jupiter.api.Test)

Example 45 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.

the class SdkObservableLongCounterTest method collectMetrics_WithOneRecord.

@Test
void collectMetrics_WithOneRecord() {
    InMemoryMetricReader sdkMeterReader = InMemoryMetricReader.create();
    SdkMeterProvider sdkMeterProvider = sdkMeterProviderBuilder.registerMetricReader(sdkMeterReader).build();
    sdkMeterProvider.get(getClass().getName()).counterBuilder("testObserver").buildWithCallback(result -> result.record(12, Attributes.builder().put("k", "v").build()));
    testClock.advance(Duration.ofNanos(SECOND_NANOS));
    assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testObserver").hasLongSum().isMonotonic().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now() - SECOND_NANOS).hasEpochNanos(testClock.now()).hasValue(12).attributes().hasSize(1).containsEntry("k", "v")));
    testClock.advance(Duration.ofNanos(SECOND_NANOS));
    assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testObserver").hasLongSum().isMonotonic().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now() - 2 * SECOND_NANOS).hasEpochNanos(testClock.now()).hasValue(12).attributes().hasSize(1).containsEntry("k", "v")));
}
Also used : Resource(io.opentelemetry.sdk.resources.Resource) Attributes(io.opentelemetry.api.common.Attributes) InstrumentType(io.opentelemetry.sdk.metrics.common.InstrumentType) Aggregation(io.opentelemetry.sdk.metrics.view.Aggregation) InstrumentSelector(io.opentelemetry.sdk.metrics.view.InstrumentSelector) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) Test(org.junit.jupiter.api.Test) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) View(io.opentelemetry.sdk.metrics.view.View) AttributeKey.stringKey(io.opentelemetry.api.common.AttributeKey.stringKey) Duration(java.time.Duration) TestClock(io.opentelemetry.sdk.testing.time.TestClock) MetricAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat) ObservableLongCounter(io.opentelemetry.api.metrics.ObservableLongCounter) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) Test(org.junit.jupiter.api.Test)

Aggregations

Attributes (io.opentelemetry.api.common.Attributes)175 Test (org.junit.jupiter.api.Test)128 ResourceAttributes (io.opentelemetry.semconv.resource.attributes.ResourceAttributes)45 Resource (io.opentelemetry.sdk.resources.Resource)33 InstrumentationLibraryInfo (io.opentelemetry.sdk.common.InstrumentationLibraryInfo)27 Context (io.opentelemetry.context.Context)25 SemanticAttributes (io.opentelemetry.semconv.trace.attributes.SemanticAttributes)24 SpanContext (io.opentelemetry.api.trace.SpanContext)22 MetricAssertions.assertThat (io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat)22 SpanData (io.opentelemetry.sdk.trace.data.SpanData)22 HashMap (java.util.HashMap)21 Duration (java.time.Duration)20 ExemplarData (io.opentelemetry.sdk.metrics.data.ExemplarData)18 InMemoryMetricReader (io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader)18 TestClock (io.opentelemetry.sdk.testing.time.TestClock)18 AttributeKey (io.opentelemetry.api.common.AttributeKey)17 Span (io.opentelemetry.api.trace.Span)17 DoubleExemplarData (io.opentelemetry.sdk.metrics.data.DoubleExemplarData)17 AttributeKey.stringKey (io.opentelemetry.api.common.AttributeKey.stringKey)16 Map (java.util.Map)16