use of io.opentelemetry.api.metrics.DoubleUpDownCounter 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.DoubleUpDownCounter in project opentelemetry-java by open-telemetry.
the class SdkDoubleUpDownCounterTest method stressTest.
@Test
void stressTest() {
DoubleUpDownCounter doubleUpDownCounter = sdkMeter.upDownCounterBuilder("testUpDownCounter").ofDoubles().build();
StressTestRunner.Builder stressTestBuilder = StressTestRunner.builder().setInstrument((SdkDoubleUpDownCounter) doubleUpDownCounter).setCollectionIntervalMs(100);
for (int i = 0; i < 4; i++) {
stressTestBuilder.addOperation(StressTestRunner.Operation.create(1_000, 2, new OperationUpdaterDirectCall(doubleUpDownCounter, "K", "V")));
stressTestBuilder.addOperation(StressTestRunner.Operation.create(1_000, 2, new OperationUpdaterWithBinding(((SdkDoubleUpDownCounter) doubleUpDownCounter).bind(Attributes.builder().put("K", "V").build()))));
}
stressTestBuilder.build().run();
assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testUpDownCounter").hasDoubleSum().isCumulative().isNotMonotonic().points().satisfiesExactlyInAnyOrder(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasValue(80_000).attributes().hasSize(1).containsEntry("K", "V")));
}
use of io.opentelemetry.api.metrics.DoubleUpDownCounter in project opentelemetry-java by open-telemetry.
the class SdkDoubleUpDownCounterTest method collectMetrics_WithMultipleCollects.
@Test
void collectMetrics_WithMultipleCollects() {
long startTime = testClock.now();
DoubleUpDownCounter doubleUpDownCounter = sdkMeter.upDownCounterBuilder("testUpDownCounter").ofDoubles().build();
BoundDoubleUpDownCounter bound = ((SdkDoubleUpDownCounter) doubleUpDownCounter).bind(Attributes.builder().put("K", "V").build());
try {
// Do some records using bounds and direct calls and bindings.
doubleUpDownCounter.add(12.1d, Attributes.empty());
bound.add(123.3d);
doubleUpDownCounter.add(21.4d, Attributes.empty());
// Advancing time here should not matter.
testClock.advance(Duration.ofNanos(SECOND_NANOS));
bound.add(321.5d);
doubleUpDownCounter.add(111.1d, Attributes.builder().put("K", "V").build());
assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testUpDownCounter").hasDoubleSum().isNotMonotonic().isCumulative().points().allSatisfy(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now())).satisfiesExactlyInAnyOrder(point -> assertThat(point).hasAttributes(Attributes.empty()).hasValue(33.5), point -> assertThat(point).hasValue(555.9).hasAttributes(Attributes.of(stringKey("K"), "V"))));
// Repeat to prove we keep previous values.
testClock.advance(Duration.ofNanos(SECOND_NANOS));
bound.add(222d);
doubleUpDownCounter.add(11d, Attributes.empty());
assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testUpDownCounter").hasDoubleSum().isNotMonotonic().isCumulative().points().allSatisfy(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now())).satisfiesExactlyInAnyOrder(point -> assertThat(point).hasAttributes(Attributes.empty()).hasValue(44.5), point -> assertThat(point).hasAttributes(Attributes.of(stringKey("K"), "V")).hasValue(777.9)));
} finally {
bound.unbind();
}
}
use of io.opentelemetry.api.metrics.DoubleUpDownCounter in project opentelemetry-java by open-telemetry.
the class SdkDoubleUpDownCounterTest method collectMetrics_WithEmptyAttributes.
@Test
void collectMetrics_WithEmptyAttributes() {
DoubleUpDownCounter doubleUpDownCounter = sdkMeter.upDownCounterBuilder("testUpDownCounter").ofDoubles().setDescription("description").setUnit("ms").build();
testClock.advance(Duration.ofNanos(SECOND_NANOS));
doubleUpDownCounter.add(12d, Attributes.empty());
doubleUpDownCounter.add(12d);
assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testUpDownCounter").hasDescription("description").hasUnit("ms").hasDoubleSum().isNotMonotonic().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now() - SECOND_NANOS).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasValue(24)));
}
Aggregations