Search in sources :

Example 1 with BoundDoubleHistogram

use of io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleHistogram 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 BoundDoubleHistogram

use of io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleHistogram in project opentelemetry-java by open-telemetry.

the class SdkDoubleHistogramTest method boundDoubleHistogramRecord_MonotonicityCheck.

@Test
@SuppressLogger(SdkDoubleHistogram.class)
void boundDoubleHistogramRecord_MonotonicityCheck() {
    DoubleHistogram histogram = sdkMeter.histogramBuilder("testHistogram").build();
    BoundDoubleHistogram bound = ((SdkDoubleHistogram) 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();
    }
}
Also used : DoubleHistogram(io.opentelemetry.api.metrics.DoubleHistogram) BoundDoubleHistogram(io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleHistogram) BoundDoubleHistogram(io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleHistogram) SuppressLogger(io.opentelemetry.internal.testing.slf4j.SuppressLogger) Test(org.junit.jupiter.api.Test)

Example 3 with BoundDoubleHistogram

use of io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleHistogram in project opentelemetry-java by open-telemetry.

the class SdkDoubleHistogramTest method collectMetrics_NoRecords.

@Test
void collectMetrics_NoRecords() {
    DoubleHistogram doubleRecorder = sdkMeter.histogramBuilder("testRecorder").build();
    BoundDoubleHistogram bound = ((SdkDoubleHistogram) doubleRecorder).bind(Attributes.builder().put("key", "value").build());
    try {
        assertThat(sdkMeterReader.collectAllMetrics()).isEmpty();
    } finally {
        bound.unbind();
    }
}
Also used : 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)

Aggregations

DoubleHistogram (io.opentelemetry.api.metrics.DoubleHistogram)3 BoundDoubleHistogram (io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleHistogram)3 Test (org.junit.jupiter.api.Test)3 SuppressLogger (io.opentelemetry.internal.testing.slf4j.SuppressLogger)2 LogCapturer (io.github.netmikey.logunit.api.LogCapturer)1 AttributeKey.stringKey (io.opentelemetry.api.common.AttributeKey.stringKey)1 Attributes (io.opentelemetry.api.common.Attributes)1 Meter (io.opentelemetry.api.metrics.Meter)1 InstrumentationLibraryInfo (io.opentelemetry.sdk.common.InstrumentationLibraryInfo)1 OperationUpdater (io.opentelemetry.sdk.metrics.StressTestRunner.OperationUpdater)1 PointData (io.opentelemetry.sdk.metrics.data.PointData)1 Resource (io.opentelemetry.sdk.resources.Resource)1 MetricAssertions.assertThat (io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat)1 InMemoryMetricReader (io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader)1 TestClock (io.opentelemetry.sdk.testing.time.TestClock)1 Duration (java.time.Duration)1 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)1 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)1