Search in sources :

Example 1 with LongHistogram

use of io.opentelemetry.api.metrics.LongHistogram in project opentelemetry-java by open-telemetry.

the class SdkLongHistogramTest method longHistogramRecord_NonNegativeCheck.

@Test
@SuppressLogger(SdkLongHistogram.class)
void longHistogramRecord_NonNegativeCheck() {
    LongHistogram histogram = sdkMeter.histogramBuilder("testHistogram").ofLongs().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 : BoundLongHistogram(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongHistogram) LongHistogram(io.opentelemetry.api.metrics.LongHistogram) SuppressLogger(io.opentelemetry.internal.testing.slf4j.SuppressLogger) Test(org.junit.jupiter.api.Test)

Example 2 with LongHistogram

use of io.opentelemetry.api.metrics.LongHistogram 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();
    }
}
Also used : Resource(io.opentelemetry.sdk.resources.Resource) Attributes(io.opentelemetry.api.common.Attributes) BoundLongHistogram(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongHistogram) 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) 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) LongHistogram(io.opentelemetry.api.metrics.LongHistogram) TestClock(io.opentelemetry.sdk.testing.time.TestClock) MetricAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat) Meter(io.opentelemetry.api.metrics.Meter) BoundLongHistogram(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongHistogram) BoundLongHistogram(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongHistogram) LongHistogram(io.opentelemetry.api.metrics.LongHistogram) Test(org.junit.jupiter.api.Test)

Example 3 with LongHistogram

use of io.opentelemetry.api.metrics.LongHistogram in project opentelemetry-java by open-telemetry.

the class SdkLongHistogramTest method stressTest.

@Test
void stressTest() {
    LongHistogram longRecorder = sdkMeter.histogramBuilder("testRecorder").ofLongs().build();
    StressTestRunner.Builder stressTestBuilder = StressTestRunner.builder().setInstrument((SdkLongHistogram) longRecorder).setCollectionIntervalMs(100);
    for (int i = 0; i < 4; i++) {
        stressTestBuilder.addOperation(StressTestRunner.Operation.create(2_000, 1, new SdkLongHistogramTest.OperationUpdaterDirectCall(longRecorder, "K", "V")));
        stressTestBuilder.addOperation(StressTestRunner.Operation.create(2_000, 1, new SdkLongHistogramTest.OperationUpdaterWithBinding(((SdkLongHistogram) longRecorder).bind(Attributes.builder().put("K", "V").build()))));
    }
    stressTestBuilder.build().run();
    assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testRecorder").hasDoubleHistogram().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasAttributes(Attributes.of(stringKey("K"), "V")).hasCount(16_000).hasSum(160_000)));
}
Also used : Resource(io.opentelemetry.sdk.resources.Resource) Attributes(io.opentelemetry.api.common.Attributes) BoundLongHistogram(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongHistogram) 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) 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) LongHistogram(io.opentelemetry.api.metrics.LongHistogram) TestClock(io.opentelemetry.sdk.testing.time.TestClock) MetricAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat) Meter(io.opentelemetry.api.metrics.Meter) BoundLongHistogram(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongHistogram) LongHistogram(io.opentelemetry.api.metrics.LongHistogram) Test(org.junit.jupiter.api.Test)

Example 4 with LongHistogram

use of io.opentelemetry.api.metrics.LongHistogram in project opentelemetry-java by open-telemetry.

the class SdkLongHistogramTest 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" };
    LongHistogram longRecorder = sdkMeter.histogramBuilder("testRecorder").ofLongs().build();
    StressTestRunner.Builder stressTestBuilder = StressTestRunner.builder().setInstrument((SdkLongHistogram) longRecorder).setCollectionIntervalMs(100);
    for (int i = 0; i < 4; i++) {
        stressTestBuilder.addOperation(StressTestRunner.Operation.create(1_000, 2, new SdkLongHistogramTest.OperationUpdaterDirectCall(longRecorder, keys[i], values[i])));
        stressTestBuilder.addOperation(StressTestRunner.Operation.create(1_000, 2, new SdkLongHistogramTest.OperationUpdaterWithBinding(((SdkLongHistogram) longRecorder).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(2_000).hasSum(20_000).hasBucketCounts(0, 1000, 1000, 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) Attributes(io.opentelemetry.api.common.Attributes) BoundLongHistogram(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongHistogram) 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) 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) LongHistogram(io.opentelemetry.api.metrics.LongHistogram) 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) BoundLongHistogram(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongHistogram) LongHistogram(io.opentelemetry.api.metrics.LongHistogram) Test(org.junit.jupiter.api.Test)

Example 5 with LongHistogram

use of io.opentelemetry.api.metrics.LongHistogram 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();
    }
}
Also used : BoundLongHistogram(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongHistogram) BoundLongHistogram(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongHistogram) LongHistogram(io.opentelemetry.api.metrics.LongHistogram) Test(org.junit.jupiter.api.Test)

Aggregations

LongHistogram (io.opentelemetry.api.metrics.LongHistogram)12 Test (org.junit.jupiter.api.Test)12 Meter (io.opentelemetry.api.metrics.Meter)9 SuppressLogger (io.opentelemetry.internal.testing.slf4j.SuppressLogger)8 InMemoryMetricReader (io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader)8 Attributes (io.opentelemetry.api.common.Attributes)7 InstrumentationLibraryInfo (io.opentelemetry.sdk.common.InstrumentationLibraryInfo)7 BoundLongHistogram (io.opentelemetry.sdk.metrics.internal.instrument.BoundLongHistogram)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 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)6 LogCapturer (io.github.netmikey.logunit.api.LogCapturer)5 DoubleCounter (io.opentelemetry.api.metrics.DoubleCounter)5 DoubleHistogram (io.opentelemetry.api.metrics.DoubleHistogram)5 DoubleUpDownCounter (io.opentelemetry.api.metrics.DoubleUpDownCounter)5 LongCounter (io.opentelemetry.api.metrics.LongCounter)5 LongUpDownCounter (io.opentelemetry.api.metrics.LongUpDownCounter)5 AttributeKey (io.opentelemetry.api.common.AttributeKey)3