Search in sources :

Example 1 with BoundDoubleUpDownCounter

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

the class SdkDoubleUpDownCounterTest method collectMetrics_NoRecords.

@Test
void collectMetrics_NoRecords() {
    DoubleUpDownCounter doubleUpDownCounter = sdkMeter.upDownCounterBuilder("testUpDownCounter").ofDoubles().build();
    BoundDoubleUpDownCounter bound = ((SdkDoubleUpDownCounter) doubleUpDownCounter).bind(Attributes.builder().put("foo", "bar").build());
    try {
        assertThat(sdkMeterReader.collectAllMetrics()).isEmpty();
    } finally {
        bound.unbind();
    }
}
Also used : DoubleUpDownCounter(io.opentelemetry.api.metrics.DoubleUpDownCounter) BoundDoubleUpDownCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleUpDownCounter) BoundDoubleUpDownCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleUpDownCounter) Test(org.junit.jupiter.api.Test)

Example 2 with BoundDoubleUpDownCounter

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

the class SdkDoubleUpDownCounterTest method collectMetrics_WithMultipleCollects.

@Test
void collectMetrics_WithMultipleCollects() {
    long startTime = testClock.now();
    DoubleUpDownCounter doubleUpDownCounter = sdkMeter.upDownCounterBuilder("testUpDownCounter").ofDoubles().build();
    BoundDoubleUpDownCounter bound = ((SdkDoubleUpDownCounter) doubleUpDownCounter).bind(Attributes.builder().put("K", "V").build());
    try {
        // Do some records using bounds and direct calls and bindings.
        doubleUpDownCounter.add(12.1d, Attributes.empty());
        bound.add(123.3d);
        doubleUpDownCounter.add(21.4d, Attributes.empty());
        // Advancing time here should not matter.
        testClock.advance(Duration.ofNanos(SECOND_NANOS));
        bound.add(321.5d);
        doubleUpDownCounter.add(111.1d, Attributes.builder().put("K", "V").build());
        assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testUpDownCounter").hasDoubleSum().isNotMonotonic().isCumulative().points().allSatisfy(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now())).satisfiesExactlyInAnyOrder(point -> assertThat(point).hasAttributes(Attributes.empty()).hasValue(33.5), point -> assertThat(point).hasValue(555.9).hasAttributes(Attributes.of(stringKey("K"), "V"))));
        // Repeat to prove we keep previous values.
        testClock.advance(Duration.ofNanos(SECOND_NANOS));
        bound.add(222d);
        doubleUpDownCounter.add(11d, Attributes.empty());
        assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testUpDownCounter").hasDoubleSum().isNotMonotonic().isCumulative().points().allSatisfy(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now())).satisfiesExactlyInAnyOrder(point -> assertThat(point).hasAttributes(Attributes.empty()).hasValue(44.5), point -> assertThat(point).hasAttributes(Attributes.of(stringKey("K"), "V")).hasValue(777.9)));
    } finally {
        bound.unbind();
    }
}
Also used : DoubleUpDownCounter(io.opentelemetry.api.metrics.DoubleUpDownCounter) Resource(io.opentelemetry.sdk.resources.Resource) 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) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) AttributeKey.stringKey(io.opentelemetry.api.common.AttributeKey.stringKey) BoundDoubleUpDownCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleUpDownCounter) 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) DoubleUpDownCounter(io.opentelemetry.api.metrics.DoubleUpDownCounter) BoundDoubleUpDownCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleUpDownCounter) BoundDoubleUpDownCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleUpDownCounter) Test(org.junit.jupiter.api.Test)

Aggregations

DoubleUpDownCounter (io.opentelemetry.api.metrics.DoubleUpDownCounter)2 BoundDoubleUpDownCounter (io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleUpDownCounter)2 Test (org.junit.jupiter.api.Test)2 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