use of io.opentelemetry.sdk.metrics.internal.instrument.BoundLongHistogram in project opentelemetry-java by open-telemetry.
the class SdkLongHistogramTest method collectMetrics_WithMultipleCollects.
@Test
void collectMetrics_WithMultipleCollects() {
long startTime = testClock.now();
LongHistogram longRecorder = sdkMeter.histogramBuilder("testRecorder").ofLongs().build();
BoundLongHistogram bound = ((SdkLongHistogram) longRecorder).bind(Attributes.builder().put("K", "V").build());
try {
// Do some records using bounds and direct calls and bindings.
longRecorder.record(9, Attributes.empty());
bound.record(123);
longRecorder.record(14, Attributes.empty());
// Advancing time here should not matter.
testClock.advance(Duration.ofNanos(SECOND_NANOS));
bound.record(321);
longRecorder.record(1, Attributes.builder().put("K", "V").build());
assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testRecorder").hasDoubleHistogram().points().allSatisfy(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now())).satisfiesExactlyInAnyOrder(point -> assertThat(point).hasCount(3).hasSum(445).hasBucketCounts(1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0).hasAttributes(Attributes.builder().put("K", "V").build()), point -> assertThat(point).hasCount(2).hasSum(23).hasBucketCounts(0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0).hasAttributes(Attributes.empty())));
// Histograms are cumulative by default.
testClock.advance(Duration.ofNanos(SECOND_NANOS));
bound.record(222);
longRecorder.record(17, Attributes.empty());
assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testRecorder").hasDoubleHistogram().points().allSatisfy(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now())).satisfiesExactlyInAnyOrder(point -> assertThat(point).hasCount(4).hasSum(667).hasBucketCounts(1, 0, 0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0).hasAttributes(Attributes.builder().put("K", "V").build()), point -> assertThat(point).hasCount(3).hasSum(40).hasBucketCounts(0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0).hasAttributes(Attributes.empty())));
} finally {
bound.unbind();
}
}
use of io.opentelemetry.sdk.metrics.internal.instrument.BoundLongHistogram in project opentelemetry-java by open-telemetry.
the class SdkLongHistogramTest method collectMetrics_NoRecords.
@Test
void collectMetrics_NoRecords() {
LongHistogram longRecorder = sdkMeter.histogramBuilder("testRecorder").ofLongs().build();
BoundLongHistogram bound = ((SdkLongHistogram) longRecorder).bind(Attributes.builder().put("key", "value").build());
try {
assertThat(sdkMeterReader.collectAllMetrics()).isEmpty();
} finally {
bound.unbind();
}
}
use of io.opentelemetry.sdk.metrics.internal.instrument.BoundLongHistogram in project opentelemetry-java by open-telemetry.
the class SdkLongHistogramTest method boundLongHistogramRecord_MonotonicityCheck.
@Test
@SuppressLogger(SdkLongHistogram.class)
void boundLongHistogramRecord_MonotonicityCheck() {
LongHistogram histogram = sdkMeter.histogramBuilder("testHistogram").ofLongs().build();
BoundLongHistogram bound = ((SdkLongHistogram) histogram).bind(Attributes.empty());
try {
bound.record(-9);
assertThat(sdkMeterReader.collectAllMetrics()).hasSize(0);
logs.assertContains("Histograms can only record non-negative values. Instrument testHistogram has recorded a negative value.");
} finally {
bound.unbind();
}
}
Aggregations