use of io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleCounter in project opentelemetry-java by open-telemetry.
the class SdkDoubleCounterTest method boundDoubleCounterAdd_Monotonicity.
@Test
void boundDoubleCounterAdd_Monotonicity() {
DoubleCounter doubleCounter = sdkMeter.counterBuilder("testCounter").ofDoubles().build();
BoundDoubleCounter bound = ((SdkDoubleCounter) doubleCounter).bind(Attributes.empty());
try {
bound.add(-9.3);
assertThat(sdkMeterReader.collectAllMetrics()).hasSize(0);
logs.assertContains("Counters can only increase. Instrument testCounter has recorded a negative value.");
} finally {
bound.unbind();
}
}
use of io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleCounter in project opentelemetry-java by open-telemetry.
the class SdkDoubleCounterTest method collectMetrics_NoRecords.
@Test
void collectMetrics_NoRecords() {
DoubleCounter doubleCounter = sdkMeter.counterBuilder("testCounter").ofDoubles().build();
BoundDoubleCounter bound = ((SdkDoubleCounter) doubleCounter).bind(Attributes.builder().put("foo", "bar").build());
try {
assertThat(sdkMeterReader.collectAllMetrics()).isEmpty();
} finally {
bound.unbind();
}
}
use of io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleCounter in project opentelemetry-java by open-telemetry.
the class SdkDoubleCounterTest method collectMetrics_WithMultipleCollects.
@Test
void collectMetrics_WithMultipleCollects() {
long startTime = testClock.now();
DoubleCounter doubleCounter = sdkMeter.counterBuilder("testCounter").ofDoubles().build();
BoundDoubleCounter bound = ((SdkDoubleCounter) doubleCounter).bind(Attributes.builder().put("K", "V").build());
try {
// Do some records using bounds and direct calls and bindings.
doubleCounter.add(12.1d, Attributes.empty());
bound.add(123.3d);
doubleCounter.add(21.4d, Attributes.empty());
// Advancing time here should not matter.
testClock.advance(Duration.ofNanos(SECOND_NANOS));
bound.add(321.5d);
doubleCounter.add(111.1d, Attributes.builder().put("K", "V").build());
assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testCounter").hasDescription("").hasUnit("1").hasDoubleSum().isMonotonic().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).attributes().hasSize(1).containsEntry("K", "V")));
// Repeat to prove we keep previous values.
testClock.advance(Duration.ofNanos(SECOND_NANOS));
bound.add(222d);
doubleCounter.add(11d, Attributes.empty());
assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasDoubleSum().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();
}
}
Aggregations