Search in sources :

Example 1 with InMemoryMetricReader

use of io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader in project opentelemetry-java by open-telemetry.

the class SdkObservableDoubleCounterTest method collectMetrics_DeltaSumAggregator.

@Test
void collectMetrics_DeltaSumAggregator() {
    InMemoryMetricReader sdkMeterReader = InMemoryMetricReader.createDelta();
    SdkMeterProvider sdkMeterProvider = sdkMeterProviderBuilder.registerMetricReader(sdkMeterReader).registerView(InstrumentSelector.builder().setType(InstrumentType.OBSERVABLE_COUNTER).build(), View.builder().setAggregation(Aggregation.sum()).build()).build();
    sdkMeterProvider.get(getClass().getName()).counterBuilder("testObserver").ofDoubles().setDescription("My own DoubleSumObserver").setUnit("ms").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").hasDescription("My own DoubleSumObserver").hasUnit("ms").hasDoubleSum().isDelta().isMonotonic().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").hasDescription("My own DoubleSumObserver").hasUnit("ms").hasDoubleSum().isDelta().isMonotonic().points().satisfiesExactlyInAnyOrder(point -> assertThat(point).hasStartEpochNanos(testClock.now() - SECOND_NANOS).hasEpochNanos(testClock.now()).hasValue(0).attributes().hasSize(1).containsEntry("k", "v")));
}
Also used : ObservableDoubleCounter(io.opentelemetry.api.metrics.ObservableDoubleCounter) 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) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) Test(org.junit.jupiter.api.Test)

Example 2 with InMemoryMetricReader

use of io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader in project opentelemetry-java by open-telemetry.

the class SdkObservableDoubleCounterTest method collectMetrics_NoRecords.

@Test
void collectMetrics_NoRecords() {
    InMemoryMetricReader sdkMeterReader = InMemoryMetricReader.create();
    SdkMeterProvider sdkMeterProvider = sdkMeterProviderBuilder.registerMetricReader(sdkMeterReader).build();
    sdkMeterProvider.get(getClass().getName()).counterBuilder("testObserver").ofDoubles().setDescription("My own DoubleSumObserver").setUnit("ms").buildWithCallback(result -> {
    });
    assertThat(sdkMeterReader.collectAllMetrics()).isEmpty();
}
Also used : InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) Test(org.junit.jupiter.api.Test)

Example 3 with InMemoryMetricReader

use of io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader in project opentelemetry-java by open-telemetry.

the class SdkMeterProviderTest method collectAllSyncInstruments_OverwriteTemporality.

@Test
void collectAllSyncInstruments_OverwriteTemporality() {
    sdkMeterProviderBuilder.registerView(InstrumentSelector.builder().setType(InstrumentType.COUNTER).build(), View.builder().setAggregation(Aggregation.explicitBucketHistogram(Collections.emptyList())).build());
    InMemoryMetricReader sdkMeterReader = InMemoryMetricReader.createDelta();
    SdkMeterProvider sdkMeterProvider = sdkMeterProviderBuilder.registerMetricReader(sdkMeterReader).build();
    Meter sdkMeter = sdkMeterProvider.get(SdkMeterProviderTest.class.getName());
    LongCounter longCounter = sdkMeter.counterBuilder("testLongCounter").build();
    longCounter.add(10, Attributes.empty());
    testClock.advance(Duration.ofSeconds(1));
    assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testLongCounter").hasDoubleHistogram().isDelta().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now() - 1000000000).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasBucketCounts(1)));
    longCounter.add(10, Attributes.empty());
    testClock.advance(Duration.ofSeconds(1));
    assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasDoubleHistogram().isDelta().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now() - 1000000000).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasBucketCounts(1)));
}
Also used : LongPointData(io.opentelemetry.sdk.metrics.data.LongPointData) Resource(io.opentelemetry.sdk.resources.Resource) Mock(org.mockito.Mock) Attributes(io.opentelemetry.api.common.Attributes) Aggregation(io.opentelemetry.sdk.metrics.view.Aggregation) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) ViewBuilderImpl(io.opentelemetry.sdk.metrics.internal.view.ViewBuilderImpl) MetricReader(io.opentelemetry.sdk.metrics.export.MetricReader) View(io.opentelemetry.sdk.metrics.view.View) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Duration(java.time.Duration) LongHistogram(io.opentelemetry.api.metrics.LongHistogram) TestClock(io.opentelemetry.sdk.testing.time.TestClock) MetricAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat) Meter(io.opentelemetry.api.metrics.Meter) LongUpDownCounter(io.opentelemetry.api.metrics.LongUpDownCounter) ObservableLongCounter(io.opentelemetry.api.metrics.ObservableLongCounter) LongCounter(io.opentelemetry.api.metrics.LongCounter) Context(io.opentelemetry.context.Context) DoubleUpDownCounter(io.opentelemetry.api.metrics.DoubleUpDownCounter) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Scope(io.opentelemetry.context.Scope) DoubleHistogram(io.opentelemetry.api.metrics.DoubleHistogram) Mockito.when(org.mockito.Mockito.when) InstrumentType(io.opentelemetry.sdk.metrics.common.InstrumentType) InstrumentSelector(io.opentelemetry.sdk.metrics.view.InstrumentSelector) Assertions.entry(org.assertj.core.api.Assertions.entry) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) Baggage(io.opentelemetry.api.baggage.Baggage) DoubleCounter(io.opentelemetry.api.metrics.DoubleCounter) AttributeKey(io.opentelemetry.api.common.AttributeKey) MetricData(io.opentelemetry.sdk.metrics.data.MetricData) MeterProvider(io.opentelemetry.api.metrics.MeterProvider) Collections(java.util.Collections) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) Meter(io.opentelemetry.api.metrics.Meter) ObservableLongCounter(io.opentelemetry.api.metrics.ObservableLongCounter) LongCounter(io.opentelemetry.api.metrics.LongCounter) Test(org.junit.jupiter.api.Test)

Example 4 with InMemoryMetricReader

use of io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader in project opentelemetry-java by open-telemetry.

the class SdkMeterProviderTest method viewSdk_filterAttributes.

@Test
void viewSdk_filterAttributes() {
    InMemoryMetricReader reader = InMemoryMetricReader.create();
    SdkMeterProvider provider = sdkMeterProviderBuilder.registerMetricReader(reader).registerView(InstrumentSelector.builder().setType(InstrumentType.OBSERVABLE_GAUGE).setName("test").build(), View.builder().setAttributeFilter(name -> name.equals("allowed")).build()).build();
    Meter meter = provider.get(SdkMeterProviderTest.class.getName());
    meter.gaugeBuilder("test").setDescription("desc").setUnit("unit").buildWithCallback(o -> o.record(1, Attributes.builder().put("allowed", "bear").put("not allowed", "dog").build()));
    assertThat(reader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasDoubleGauge().points().satisfiesExactly(point -> assertThat(point.getAttributes().asMap()).containsOnly(entry(AttributeKey.stringKey("allowed"), "bear"))));
}
Also used : LongPointData(io.opentelemetry.sdk.metrics.data.LongPointData) Resource(io.opentelemetry.sdk.resources.Resource) Mock(org.mockito.Mock) Attributes(io.opentelemetry.api.common.Attributes) Aggregation(io.opentelemetry.sdk.metrics.view.Aggregation) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) ViewBuilderImpl(io.opentelemetry.sdk.metrics.internal.view.ViewBuilderImpl) MetricReader(io.opentelemetry.sdk.metrics.export.MetricReader) View(io.opentelemetry.sdk.metrics.view.View) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Duration(java.time.Duration) LongHistogram(io.opentelemetry.api.metrics.LongHistogram) TestClock(io.opentelemetry.sdk.testing.time.TestClock) MetricAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat) Meter(io.opentelemetry.api.metrics.Meter) LongUpDownCounter(io.opentelemetry.api.metrics.LongUpDownCounter) ObservableLongCounter(io.opentelemetry.api.metrics.ObservableLongCounter) LongCounter(io.opentelemetry.api.metrics.LongCounter) Context(io.opentelemetry.context.Context) DoubleUpDownCounter(io.opentelemetry.api.metrics.DoubleUpDownCounter) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Scope(io.opentelemetry.context.Scope) DoubleHistogram(io.opentelemetry.api.metrics.DoubleHistogram) Mockito.when(org.mockito.Mockito.when) InstrumentType(io.opentelemetry.sdk.metrics.common.InstrumentType) InstrumentSelector(io.opentelemetry.sdk.metrics.view.InstrumentSelector) Assertions.entry(org.assertj.core.api.Assertions.entry) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) Baggage(io.opentelemetry.api.baggage.Baggage) DoubleCounter(io.opentelemetry.api.metrics.DoubleCounter) AttributeKey(io.opentelemetry.api.common.AttributeKey) MetricData(io.opentelemetry.sdk.metrics.data.MetricData) MeterProvider(io.opentelemetry.api.metrics.MeterProvider) Collections(java.util.Collections) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) Meter(io.opentelemetry.api.metrics.Meter) Test(org.junit.jupiter.api.Test)

Example 5 with InMemoryMetricReader

use of io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader in project opentelemetry-java by open-telemetry.

the class SdkMeterProviderTest method sdkMeterProvider_supportsMultipleCollectorsCumulative.

@Test
void sdkMeterProvider_supportsMultipleCollectorsCumulative() {
    InMemoryMetricReader collector1 = InMemoryMetricReader.create();
    InMemoryMetricReader collector2 = InMemoryMetricReader.create();
    SdkMeterProvider meterProvider = sdkMeterProviderBuilder.registerMetricReader(collector1).registerMetricReader(collector2).build();
    Meter sdkMeter = meterProvider.get(SdkMeterProviderTest.class.getName());
    LongCounter counter = sdkMeter.counterBuilder("testSum").build();
    long startTime = testClock.now();
    counter.add(1L);
    testClock.advance(Duration.ofSeconds(1));
    assertThat(collector1.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasName("testSum").hasLongSum().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now()).hasValue(1)));
    counter.add(1L);
    testClock.advance(Duration.ofSeconds(1));
    // Make sure collector 2 sees the value collector 1 saw
    assertThat(collector2.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasName("testSum").hasLongSum().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now()).hasValue(2)));
    // Make sure Collector 1 sees the same point as 2
    assertThat(collector1.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasName("testSum").hasLongSum().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now()).hasValue(2)));
}
Also used : LongPointData(io.opentelemetry.sdk.metrics.data.LongPointData) Resource(io.opentelemetry.sdk.resources.Resource) Mock(org.mockito.Mock) Attributes(io.opentelemetry.api.common.Attributes) Aggregation(io.opentelemetry.sdk.metrics.view.Aggregation) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) ViewBuilderImpl(io.opentelemetry.sdk.metrics.internal.view.ViewBuilderImpl) MetricReader(io.opentelemetry.sdk.metrics.export.MetricReader) View(io.opentelemetry.sdk.metrics.view.View) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Duration(java.time.Duration) LongHistogram(io.opentelemetry.api.metrics.LongHistogram) TestClock(io.opentelemetry.sdk.testing.time.TestClock) MetricAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat) Meter(io.opentelemetry.api.metrics.Meter) LongUpDownCounter(io.opentelemetry.api.metrics.LongUpDownCounter) ObservableLongCounter(io.opentelemetry.api.metrics.ObservableLongCounter) LongCounter(io.opentelemetry.api.metrics.LongCounter) Context(io.opentelemetry.context.Context) DoubleUpDownCounter(io.opentelemetry.api.metrics.DoubleUpDownCounter) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Scope(io.opentelemetry.context.Scope) DoubleHistogram(io.opentelemetry.api.metrics.DoubleHistogram) Mockito.when(org.mockito.Mockito.when) InstrumentType(io.opentelemetry.sdk.metrics.common.InstrumentType) InstrumentSelector(io.opentelemetry.sdk.metrics.view.InstrumentSelector) Assertions.entry(org.assertj.core.api.Assertions.entry) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) Baggage(io.opentelemetry.api.baggage.Baggage) DoubleCounter(io.opentelemetry.api.metrics.DoubleCounter) AttributeKey(io.opentelemetry.api.common.AttributeKey) MetricData(io.opentelemetry.sdk.metrics.data.MetricData) MeterProvider(io.opentelemetry.api.metrics.MeterProvider) Collections(java.util.Collections) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) Meter(io.opentelemetry.api.metrics.Meter) ObservableLongCounter(io.opentelemetry.api.metrics.ObservableLongCounter) LongCounter(io.opentelemetry.api.metrics.LongCounter) Test(org.junit.jupiter.api.Test)

Aggregations

InMemoryMetricReader (io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader)37 Test (org.junit.jupiter.api.Test)37 MetricAssertions.assertThat (io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat)24 Duration (java.time.Duration)24 Attributes (io.opentelemetry.api.common.Attributes)23 InstrumentSelector (io.opentelemetry.sdk.metrics.view.InstrumentSelector)20 InstrumentationLibraryInfo (io.opentelemetry.sdk.common.InstrumentationLibraryInfo)18 InstrumentType (io.opentelemetry.sdk.metrics.common.InstrumentType)18 Aggregation (io.opentelemetry.sdk.metrics.view.Aggregation)18 View (io.opentelemetry.sdk.metrics.view.View)18 Resource (io.opentelemetry.sdk.resources.Resource)18 TestClock (io.opentelemetry.sdk.testing.time.TestClock)18 Context (io.opentelemetry.context.Context)15 TimeUnit (java.util.concurrent.TimeUnit)15 Meter (io.opentelemetry.api.metrics.Meter)14 ObservableLongCounter (io.opentelemetry.api.metrics.ObservableLongCounter)13 DoubleHistogram (io.opentelemetry.api.metrics.DoubleHistogram)11 Collections (java.util.Collections)11 Baggage (io.opentelemetry.api.baggage.Baggage)10 AttributeKey (io.opentelemetry.api.common.AttributeKey)10