use of io.opentelemetry.sdk.metrics.internal.instrument.BoundLongCounter in project opentelemetry-java by open-telemetry.
the class SdkLongCounterTest method collectMetrics_WithMultipleCollects.
@Test
void collectMetrics_WithMultipleCollects() {
long startTime = testClock.now();
LongCounter longCounter = sdkMeter.counterBuilder("testCounter").build();
BoundLongCounter bound = ((SdkLongCounter) longCounter).bind(Attributes.builder().put("K", "V").build());
try {
// Do some records using bounds and direct calls and bindings.
longCounter.add(12, Attributes.empty());
bound.add(123);
longCounter.add(21, Attributes.empty());
// Advancing time here should not matter.
testClock.advance(Duration.ofNanos(SECOND_NANOS));
bound.add(321);
longCounter.add(111, Attributes.builder().put("K", "V").build());
assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testCounter").hasLongSum().isMonotonic().isCumulative().points().allSatisfy(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now())).satisfiesExactlyInAnyOrder(point -> assertThat(point).hasAttributes(Attributes.empty()).hasValue(33), point -> assertThat(point).hasAttributes(Attributes.of(stringKey("K"), "V")).hasValue(555)));
// Repeat to prove we keep previous values.
testClock.advance(Duration.ofNanos(SECOND_NANOS));
bound.add(222);
longCounter.add(11, Attributes.empty());
assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testCounter").hasLongSum().isMonotonic().isCumulative().points().allSatisfy(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now())).satisfiesExactlyInAnyOrder(point -> assertThat(point).hasAttributes(Attributes.empty()).hasValue(44), point -> assertThat(point).hasAttributes(Attributes.of(stringKey("K"), "V")).hasValue(777)));
} finally {
bound.unbind();
}
}
use of io.opentelemetry.sdk.metrics.internal.instrument.BoundLongCounter in project opentelemetry-java by open-telemetry.
the class SdkLongCounterTest method boundLongCounterAdd_Monotonicity.
@Test
@SuppressLogger(SdkLongCounter.class)
void boundLongCounterAdd_Monotonicity() {
LongCounter longCounter = sdkMeter.counterBuilder("testCounter").build();
BoundLongCounter bound = ((SdkLongCounter) longCounter).bind(Attributes.empty());
try {
bound.add(-9);
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.BoundLongCounter in project opentelemetry-java by open-telemetry.
the class SdkLongCounterTest method collectMetrics_NoRecords.
@Test
void collectMetrics_NoRecords() {
LongCounter longCounter = sdkMeter.counterBuilder("Counter").build();
BoundLongCounter bound = ((SdkLongCounter) longCounter).bind(Attributes.builder().put("foo", "bar").build());
try {
assertThat(sdkMeterReader.collectAllMetrics()).isEmpty();
} finally {
bound.unbind();
}
}
Aggregations