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")));
}
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();
}
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)));
}
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"))));
}
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)));
}
Aggregations