Search in sources :

Example 1 with DoubleHistogram

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();
    }
}
Also used : Resource(io.opentelemetry.sdk.resources.Resource) DoubleHistogram(io.opentelemetry.api.metrics.DoubleHistogram) Attributes(io.opentelemetry.api.common.Attributes) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) OperationUpdater(io.opentelemetry.sdk.metrics.StressTestRunner.OperationUpdater) Test(org.junit.jupiter.api.Test) PointData(io.opentelemetry.sdk.metrics.data.PointData) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) LogCapturer(io.github.netmikey.logunit.api.LogCapturer) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) BoundDoubleHistogram(io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleHistogram) SuppressLogger(io.opentelemetry.internal.testing.slf4j.SuppressLogger) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) AttributeKey.stringKey(io.opentelemetry.api.common.AttributeKey.stringKey) Duration(java.time.Duration) TestClock(io.opentelemetry.sdk.testing.time.TestClock) MetricAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat) Meter(io.opentelemetry.api.metrics.Meter) DoubleHistogram(io.opentelemetry.api.metrics.DoubleHistogram) BoundDoubleHistogram(io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleHistogram) BoundDoubleHistogram(io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleHistogram) Test(org.junit.jupiter.api.Test)

Example 2 with DoubleHistogram

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])));
}
Also used : Resource(io.opentelemetry.sdk.resources.Resource) DoubleHistogram(io.opentelemetry.api.metrics.DoubleHistogram) Attributes(io.opentelemetry.api.common.Attributes) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) OperationUpdater(io.opentelemetry.sdk.metrics.StressTestRunner.OperationUpdater) Test(org.junit.jupiter.api.Test) PointData(io.opentelemetry.sdk.metrics.data.PointData) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) LogCapturer(io.github.netmikey.logunit.api.LogCapturer) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) BoundDoubleHistogram(io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleHistogram) SuppressLogger(io.opentelemetry.internal.testing.slf4j.SuppressLogger) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) AttributeKey.stringKey(io.opentelemetry.api.common.AttributeKey.stringKey) Duration(java.time.Duration) TestClock(io.opentelemetry.sdk.testing.time.TestClock) MetricAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat) Meter(io.opentelemetry.api.metrics.Meter) PointData(io.opentelemetry.sdk.metrics.data.PointData) DoubleHistogram(io.opentelemetry.api.metrics.DoubleHistogram) BoundDoubleHistogram(io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleHistogram) Test(org.junit.jupiter.api.Test)

Example 3 with DoubleHistogram

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.");
}
Also used : DoubleHistogram(io.opentelemetry.api.metrics.DoubleHistogram) BoundDoubleHistogram(io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleHistogram) SuppressLogger(io.opentelemetry.internal.testing.slf4j.SuppressLogger) Test(org.junit.jupiter.api.Test)

Example 4 with DoubleHistogram

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.");
}
Also used : LongCounter(io.opentelemetry.api.metrics.LongCounter) DoubleUpDownCounter(io.opentelemetry.api.metrics.DoubleUpDownCounter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DoubleHistogram(io.opentelemetry.api.metrics.DoubleHistogram) Test(org.junit.jupiter.api.Test) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) LogCapturer(io.github.netmikey.logunit.api.LogCapturer) MeterSharedState(io.opentelemetry.sdk.metrics.internal.state.MeterSharedState) SuppressLogger(io.opentelemetry.internal.testing.slf4j.SuppressLogger) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) DoubleCounter(io.opentelemetry.api.metrics.DoubleCounter) LongHistogram(io.opentelemetry.api.metrics.LongHistogram) Meter(io.opentelemetry.api.metrics.Meter) WARN(org.slf4j.event.Level.WARN) LongUpDownCounter(io.opentelemetry.api.metrics.LongUpDownCounter) DoubleHistogram(io.opentelemetry.api.metrics.DoubleHistogram) Test(org.junit.jupiter.api.Test)

Example 5 with DoubleHistogram

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"));
        });
    }));
}
Also used : OpenTelemetryAssertions.attributeEntry(io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.attributeEntry) LongCounter(io.opentelemetry.api.metrics.LongCounter) AgentInstrumentationExtension(io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension) BeforeEach(org.junit.jupiter.api.BeforeEach) DoubleUpDownCounter(io.opentelemetry.api.metrics.DoubleUpDownCounter) DoubleHistogram(io.opentelemetry.api.metrics.DoubleHistogram) Attributes(io.opentelemetry.api.common.Attributes) TestInfo(org.junit.jupiter.api.TestInfo) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) Test(org.junit.jupiter.api.Test) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) DoubleCounter(io.opentelemetry.api.metrics.DoubleCounter) AttributeKey(io.opentelemetry.api.common.AttributeKey) LongHistogram(io.opentelemetry.api.metrics.LongHistogram) Assertions(org.assertj.core.api.Assertions) MetricAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat) Meter(io.opentelemetry.api.metrics.Meter) LongUpDownCounter(io.opentelemetry.api.metrics.LongUpDownCounter) DoubleHistogram(io.opentelemetry.api.metrics.DoubleHistogram) Test(org.junit.jupiter.api.Test)

Aggregations

DoubleHistogram (io.opentelemetry.api.metrics.DoubleHistogram)12 Test (org.junit.jupiter.api.Test)12 Meter (io.opentelemetry.api.metrics.Meter)9 InMemoryMetricReader (io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader)8 Attributes (io.opentelemetry.api.common.Attributes)7 SuppressLogger (io.opentelemetry.internal.testing.slf4j.SuppressLogger)7 InstrumentationLibraryInfo (io.opentelemetry.sdk.common.InstrumentationLibraryInfo)7 BoundDoubleHistogram (io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleHistogram)7 MetricAssertions.assertThat (io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat)7 Resource (io.opentelemetry.sdk.resources.Resource)6 TestClock (io.opentelemetry.sdk.testing.time.TestClock)6 Duration (java.time.Duration)6 LogCapturer (io.github.netmikey.logunit.api.LogCapturer)5 AttributeKey.stringKey (io.opentelemetry.api.common.AttributeKey.stringKey)4 DoubleCounter (io.opentelemetry.api.metrics.DoubleCounter)4 DoubleUpDownCounter (io.opentelemetry.api.metrics.DoubleUpDownCounter)4 LongCounter (io.opentelemetry.api.metrics.LongCounter)4 LongHistogram (io.opentelemetry.api.metrics.LongHistogram)4 LongUpDownCounter (io.opentelemetry.api.metrics.LongUpDownCounter)4 OperationUpdater (io.opentelemetry.sdk.metrics.StressTestRunner.OperationUpdater)4