use of io.opentelemetry.api.metrics.DoubleHistogram in project opentelemetry-java by open-telemetry.
the class SdkDoubleHistogramTest method collectMetrics_WithMultipleCollects.
@Test
void collectMetrics_WithMultipleCollects() {
long startTime = testClock.now();
DoubleHistogram doubleRecorder = sdkMeter.histogramBuilder("testRecorder").build();
BoundDoubleHistogram bound = ((SdkDoubleHistogram) doubleRecorder).bind(Attributes.builder().put("K", "V").build());
try {
// Do some records using bounds and direct calls and bindings.
doubleRecorder.record(9.1d, Attributes.empty());
bound.record(123.3d);
doubleRecorder.record(13.1d, Attributes.empty());
// Advancing time here should not matter.
testClock.advance(Duration.ofNanos(SECOND_NANOS));
bound.record(321.5d);
doubleRecorder.record(121.5d, 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(566.3d).hasBucketCounts(0, 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(2).hasSum(22.2d).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(222d);
doubleRecorder.record(17d, 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(788.3).hasBucketCounts(0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0).hasAttributes(Attributes.builder().put("K", "V").build()), point -> assertThat(point).hasCount(3).hasSum(39.2).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.api.metrics.DoubleHistogram in project opentelemetry-java by open-telemetry.
the class SdkDoubleHistogramTest method stressTest_WithDifferentLabelSet.
@Test
void stressTest_WithDifferentLabelSet() {
String[] keys = { "Key_1", "Key_2", "Key_3", "Key_4" };
String[] values = { "Value_1", "Value_2", "Value_3", "Value_4" };
DoubleHistogram doubleRecorder = sdkMeter.histogramBuilder("testRecorder").build();
StressTestRunner.Builder stressTestBuilder = StressTestRunner.builder().setInstrument((SdkDoubleHistogram) doubleRecorder).setCollectionIntervalMs(100);
for (int i = 0; i < 4; i++) {
stressTestBuilder.addOperation(StressTestRunner.Operation.create(2_000, 1, new SdkDoubleHistogramTest.OperationUpdaterDirectCall(doubleRecorder, keys[i], values[i])));
stressTestBuilder.addOperation(StressTestRunner.Operation.create(2_000, 1, new OperationUpdaterWithBinding(((SdkDoubleHistogram) doubleRecorder).bind(Attributes.builder().put(keys[i], values[i]).build()))));
}
stressTestBuilder.build().run();
assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testRecorder").hasDoubleHistogram().points().allSatisfy(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasCount(4_000).hasSum(40_000).hasBucketCounts(0, 2000, 2000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)).extracting(PointData::getAttributes).containsExactlyInAnyOrder(Attributes.of(stringKey(keys[0]), values[0]), Attributes.of(stringKey(keys[1]), values[1]), Attributes.of(stringKey(keys[2]), values[2]), Attributes.of(stringKey(keys[3]), values[3])));
}
use of io.opentelemetry.api.metrics.DoubleHistogram in project opentelemetry-java by open-telemetry.
the class SdkDoubleHistogramTest method doubleHistogramRecord_NonNegativeCheck.
@Test
@SuppressLogger(SdkDoubleHistogram.class)
void doubleHistogramRecord_NonNegativeCheck() {
DoubleHistogram histogram = sdkMeter.histogramBuilder("testHistogram").build();
histogram.record(-45);
assertThat(sdkMeterReader.collectAllMetrics()).hasSize(0);
logs.assertContains("Histograms can only record non-negative values. Instrument testHistogram has recorded a negative value.");
}
use of io.opentelemetry.api.metrics.DoubleHistogram in project opentelemetry-java by open-telemetry.
the class SdkMeterTest method testDoubleHistogram.
@Test
void testDoubleHistogram() {
DoubleHistogram doubleValueRecorder = sdkMeter.histogramBuilder("testDoubleValueRecorder").setDescription("My very own ValueRecorder").setUnit("metric tonnes").build();
assertThat(doubleValueRecorder).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.histogramBuilder("testDoubleValueRecorder").setDescription("My very own ValueRecorder").setUnit("metric tonnes").build();
assertThat(logs.getEvents()).isEmpty();
sdkMeter.histogramBuilder("testDoubleValueRecorder").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.DoubleHistogram in project opentelemetry-java-instrumentation by open-telemetry.
the class MeterTest method doubleHistogram.
@Test
void doubleHistogram() {
DoubleHistogram instrument = meter.histogramBuilder("test").setDescription("d").setUnit("u").build();
instrument.record(5.5, Attributes.of(AttributeKey.stringKey("q"), "r"));
instrument.record(6.6, Attributes.of(AttributeKey.stringKey("q"), "r"));
testing.waitAndAssertMetrics(instrumentationName, "test", metrics -> metrics.anySatisfy(metric -> {
assertThat(metric).hasDescription("d").hasUnit("u").hasInstrumentationLibrary(InstrumentationLibraryInfo.create(instrumentationName, "1.2.3")).hasDoubleHistogram().points().allSatisfy(point -> {
Assertions.assertThat(point.getSum()).isEqualTo(12.1);
Assertions.assertThat(point.getAttributes()).isEqualTo(Attributes.of(AttributeKey.stringKey("q"), "r"));
});
}));
}
Aggregations