Search in sources :

Example 1 with DoubleCounter

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

the class SdkDoubleCounterTest 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" };
    DoubleCounter doubleCounter = sdkMeter.counterBuilder("testCounter").ofDoubles().build();
    StressTestRunner.Builder stressTestBuilder = StressTestRunner.builder().setInstrument((SdkDoubleCounter) doubleCounter).setCollectionIntervalMs(100);
    for (int i = 0; i < 4; i++) {
        stressTestBuilder.addOperation(StressTestRunner.Operation.create(2_000, 1, new OperationUpdaterDirectCall(doubleCounter, keys[i], values[i])));
        stressTestBuilder.addOperation(StressTestRunner.Operation.create(2_000, 1, new OperationUpdaterWithBinding(((SdkDoubleCounter) doubleCounter).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).hasDoubleSum().isCumulative().isMonotonic().points().allSatisfy(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasValue(40_000)).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) BoundDoubleCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleCounter) 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) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) AttributeKey.stringKey(io.opentelemetry.api.common.AttributeKey.stringKey) DoubleCounter(io.opentelemetry.api.metrics.DoubleCounter) 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) BoundDoubleCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleCounter) DoubleCounter(io.opentelemetry.api.metrics.DoubleCounter) Test(org.junit.jupiter.api.Test)

Example 2 with DoubleCounter

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

the class SdkDoubleCounterTest method boundDoubleCounterAdd_Monotonicity.

@Test
void boundDoubleCounterAdd_Monotonicity() {
    DoubleCounter doubleCounter = sdkMeter.counterBuilder("testCounter").ofDoubles().build();
    BoundDoubleCounter bound = ((SdkDoubleCounter) doubleCounter).bind(Attributes.empty());
    try {
        bound.add(-9.3);
        assertThat(sdkMeterReader.collectAllMetrics()).hasSize(0);
        logs.assertContains("Counters can only increase. Instrument testCounter has recorded a negative value.");
    } finally {
        bound.unbind();
    }
}
Also used : BoundDoubleCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleCounter) DoubleCounter(io.opentelemetry.api.metrics.DoubleCounter) BoundDoubleCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleCounter) Test(org.junit.jupiter.api.Test)

Example 3 with DoubleCounter

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

the class SdkDoubleCounterTest method collectMetrics_NoRecords.

@Test
void collectMetrics_NoRecords() {
    DoubleCounter doubleCounter = sdkMeter.counterBuilder("testCounter").ofDoubles().build();
    BoundDoubleCounter bound = ((SdkDoubleCounter) doubleCounter).bind(Attributes.builder().put("foo", "bar").build());
    try {
        assertThat(sdkMeterReader.collectAllMetrics()).isEmpty();
    } finally {
        bound.unbind();
    }
}
Also used : BoundDoubleCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleCounter) DoubleCounter(io.opentelemetry.api.metrics.DoubleCounter) BoundDoubleCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleCounter) Test(org.junit.jupiter.api.Test)

Example 4 with DoubleCounter

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

the class SdkDoubleCounterTest method collectMetrics_WithMultipleCollects.

@Test
void collectMetrics_WithMultipleCollects() {
    long startTime = testClock.now();
    DoubleCounter doubleCounter = sdkMeter.counterBuilder("testCounter").ofDoubles().build();
    BoundDoubleCounter bound = ((SdkDoubleCounter) doubleCounter).bind(Attributes.builder().put("K", "V").build());
    try {
        // Do some records using bounds and direct calls and bindings.
        doubleCounter.add(12.1d, Attributes.empty());
        bound.add(123.3d);
        doubleCounter.add(21.4d, Attributes.empty());
        // Advancing time here should not matter.
        testClock.advance(Duration.ofNanos(SECOND_NANOS));
        bound.add(321.5d);
        doubleCounter.add(111.1d, Attributes.builder().put("K", "V").build());
        assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testCounter").hasDescription("").hasUnit("1").hasDoubleSum().isMonotonic().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).attributes().hasSize(1).containsEntry("K", "V")));
        // Repeat to prove we keep previous values.
        testClock.advance(Duration.ofNanos(SECOND_NANOS));
        bound.add(222d);
        doubleCounter.add(11d, Attributes.empty());
        assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasDoubleSum().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 : Resource(io.opentelemetry.sdk.resources.Resource) Attributes(io.opentelemetry.api.common.Attributes) BoundDoubleCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleCounter) 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) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) AttributeKey.stringKey(io.opentelemetry.api.common.AttributeKey.stringKey) DoubleCounter(io.opentelemetry.api.metrics.DoubleCounter) 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) BoundDoubleCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleCounter) DoubleCounter(io.opentelemetry.api.metrics.DoubleCounter) BoundDoubleCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleCounter) Test(org.junit.jupiter.api.Test)

Example 5 with DoubleCounter

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

the class SdkDoubleCounterTest method doubleCounterAdd_Monotonicity.

@Test
void doubleCounterAdd_Monotonicity() {
    DoubleCounter doubleCounter = sdkMeter.counterBuilder("testCounter").ofDoubles().build();
    doubleCounter.add(-45.77d);
    assertThat(sdkMeterReader.collectAllMetrics()).hasSize(0);
    logs.assertContains("Counters can only increase. Instrument testCounter has recorded a negative value.");
}
Also used : BoundDoubleCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleCounter) DoubleCounter(io.opentelemetry.api.metrics.DoubleCounter) Test(org.junit.jupiter.api.Test)

Aggregations

DoubleCounter (io.opentelemetry.api.metrics.DoubleCounter)11 Test (org.junit.jupiter.api.Test)11 Meter (io.opentelemetry.api.metrics.Meter)8 Attributes (io.opentelemetry.api.common.Attributes)7 InstrumentationLibraryInfo (io.opentelemetry.sdk.common.InstrumentationLibraryInfo)7 BoundDoubleCounter (io.opentelemetry.sdk.metrics.internal.instrument.BoundDoubleCounter)7 MetricAssertions.assertThat (io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat)7 InMemoryMetricReader (io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader)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 AttributeKey.stringKey (io.opentelemetry.api.common.AttributeKey.stringKey)4 DoubleHistogram (io.opentelemetry.api.metrics.DoubleHistogram)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