use of io.opentelemetry.api.metrics.DoubleCounter in project opentelemetry-java by open-telemetry.
the class SdkMeterProviderTest method collectAllSyncInstruments_DeltaHistogram.
@Test
void collectAllSyncInstruments_DeltaHistogram() {
registerViewForAllTypes(sdkMeterProviderBuilder, Aggregation.explicitBucketHistogram(Collections.emptyList()));
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());
LongUpDownCounter longUpDownCounter = sdkMeter.upDownCounterBuilder("testLongUpDownCounter").build();
longUpDownCounter.add(10, Attributes.empty());
LongHistogram longValueRecorder = sdkMeter.histogramBuilder("testLongValueRecorder").ofLongs().build();
longValueRecorder.record(10, Attributes.empty());
DoubleCounter doubleCounter = sdkMeter.counterBuilder("testDoubleCounter").ofDoubles().build();
doubleCounter.add(10, Attributes.empty());
DoubleUpDownCounter doubleUpDownCounter = sdkMeter.upDownCounterBuilder("testDoubleUpDownCounter").ofDoubles().build();
doubleUpDownCounter.add(10, Attributes.empty());
DoubleHistogram doubleValueRecorder = sdkMeter.histogramBuilder("testDoubleValueRecorder").build();
doubleValueRecorder.record(10, Attributes.empty());
testClock.advance(Duration.ofSeconds(1));
assertThat(sdkMeterReader.collectAllMetrics()).allSatisfy(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasDescription("").hasUnit("1").hasDoubleHistogram().isDelta().points().satisfiesExactlyInAnyOrder(point -> assertThat(point).hasStartEpochNanos(testClock.now() - 1000000000).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasBucketCounts(1))).extracting(MetricData::getName).containsExactlyInAnyOrder("testLongCounter", "testDoubleCounter", "testLongUpDownCounter", "testDoubleUpDownCounter", "testLongValueRecorder", "testDoubleValueRecorder");
testClock.advance(Duration.ofSeconds(1));
longCounter.add(10, Attributes.empty());
longUpDownCounter.add(10, Attributes.empty());
longValueRecorder.record(10, Attributes.empty());
doubleCounter.add(10, Attributes.empty());
doubleUpDownCounter.add(10, Attributes.empty());
doubleValueRecorder.record(10, Attributes.empty());
assertThat(sdkMeterReader.collectAllMetrics()).allSatisfy(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasDescription("").hasUnit("1").hasDoubleHistogram().isDelta().points().satisfiesExactlyInAnyOrder(point -> assertThat(point).hasStartEpochNanos(testClock.now() - 1000000000).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasBucketCounts(1))).extracting(MetricData::getName).containsExactlyInAnyOrder("testLongCounter", "testDoubleCounter", "testLongUpDownCounter", "testDoubleUpDownCounter", "testLongValueRecorder", "testDoubleValueRecorder");
}
use of io.opentelemetry.api.metrics.DoubleCounter in project opentelemetry-java by open-telemetry.
the class SdkMeterProviderTest method collectAllSyncInstruments.
@Test
void collectAllSyncInstruments() {
InMemoryMetricReader sdkMeterReader = InMemoryMetricReader.create();
SdkMeterProvider sdkMeterProvider = sdkMeterProviderBuilder.registerMetricReader(sdkMeterReader).build();
Meter sdkMeter = sdkMeterProvider.get(SdkMeterProviderTest.class.getName());
LongCounter longCounter = sdkMeter.counterBuilder("testLongCounter").build();
longCounter.add(10, Attributes.empty());
LongUpDownCounter longUpDownCounter = sdkMeter.upDownCounterBuilder("testLongUpDownCounter").build();
longUpDownCounter.add(-10, Attributes.empty());
LongHistogram longValueRecorder = sdkMeter.histogramBuilder("testLongHistogram").ofLongs().build();
longValueRecorder.record(10, Attributes.empty());
DoubleCounter doubleCounter = sdkMeter.counterBuilder("testDoubleCounter").ofDoubles().build();
doubleCounter.add(10.1, Attributes.empty());
DoubleUpDownCounter doubleUpDownCounter = sdkMeter.upDownCounterBuilder("testDoubleUpDownCounter").ofDoubles().build();
doubleUpDownCounter.add(-10.1, Attributes.empty());
DoubleHistogram doubleValueRecorder = sdkMeter.histogramBuilder("testDoubleHistogram").build();
doubleValueRecorder.record(10.1, Attributes.empty());
assertThat(sdkMeterReader.collectAllMetrics()).allSatisfy(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasDescription("").hasUnit("1")).satisfiesExactlyInAnyOrder(metric -> assertThat(metric).hasName("testDoubleHistogram").hasDoubleHistogram().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasCount(1).hasSum(10.1).hasBucketCounts(0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), metric -> assertThat(metric).hasName("testDoubleCounter").hasDoubleSum().isMonotonic().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasValue(10.1)), metric -> assertThat(metric).hasName("testLongHistogram").hasDoubleHistogram().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasCount(1).hasSum(10).hasBucketCounts(0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), metric -> assertThat(metric).hasName("testLongUpDownCounter").hasLongSum().isNotMonotonic().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasValue(-10)), metric -> assertThat(metric).hasName("testLongCounter").hasLongSum().isMonotonic().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasValue(10)), metric -> assertThat(metric).hasName("testDoubleUpDownCounter").hasDoubleSum().isNotMonotonic().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasValue(-10.1)));
}
use of io.opentelemetry.api.metrics.DoubleCounter in project opentelemetry-java by open-telemetry.
the class SdkMeterTest method testDoubleCounter.
@Test
void testDoubleCounter() {
DoubleCounter doubleCounter = sdkMeter.counterBuilder("testDoubleCounter").ofDoubles().setDescription("My very own counter").setUnit("metric tonnes").build();
assertThat(doubleCounter).isNotNull();
// Note: We no longer get the same instrument instance as these instances are lightweight
// objects backed by storage now. Here we just make sure it doesn't log an error.
sdkMeter.counterBuilder("testDoubleCounter").ofDoubles().setDescription("My very own counter").setUnit("metric tonnes").build();
assertThat(logs.getEvents()).isEmpty();
sdkMeter.counterBuilder("testDoubleCounter").ofDoubles().build();
assertThat(logs.assertContains(loggingEvent -> loggingEvent.getLevel().equals(WARN), "Failed to register metric.").getThrowable()).hasMessageContaining("Metric with same name and different descriptor already created.");
}
use of io.opentelemetry.api.metrics.DoubleCounter in project opentelemetry-java by open-telemetry.
the class SdkDoubleCounterTest method collectMetrics_WithEmptyAttributes.
@Test
void collectMetrics_WithEmptyAttributes() {
DoubleCounter doubleCounter = sdkMeter.counterBuilder("testCounter").ofDoubles().setDescription("description").setUnit("ms").build();
testClock.advance(Duration.ofNanos(SECOND_NANOS));
doubleCounter.add(12d, Attributes.empty());
doubleCounter.add(12d);
assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testCounter").hasDescription("description").hasUnit("ms").hasDoubleSum().isMonotonic().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now() - SECOND_NANOS).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasValue(24)));
}
use of io.opentelemetry.api.metrics.DoubleCounter in project opentelemetry-java by open-telemetry.
the class SdkDoubleCounterTest method stressTest.
@Test
void stressTest() {
DoubleCounter doubleCounter = sdkMeter.counterBuilder("testCounter").ofDoubles().build();
StressTestRunner.Builder stressTestBuilder = StressTestRunner.builder().setInstrument((SdkDoubleCounter) doubleCounter).setCollectionIntervalMs(100);
for (int i = 0; i < 4; i++) {
stressTestBuilder.addOperation(StressTestRunner.Operation.create(1_000, 2, new OperationUpdaterDirectCall(doubleCounter, "K", "V")));
stressTestBuilder.addOperation(StressTestRunner.Operation.create(1_000, 2, new OperationUpdaterWithBinding(((SdkDoubleCounter) doubleCounter).bind(Attributes.builder().put("K", "V").build()))));
}
stressTestBuilder.build().run();
assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testCounter").hasDoubleSum().isCumulative().isMonotonic().points().satisfiesExactlyInAnyOrder(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasValue(80_000).attributes().hasSize(1).containsEntry("K", "V")));
}
Aggregations