Search in sources :

Example 1 with LongCounter

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

the class SdkLongCounterTest method longCounterAdd_Monotonicity.

@Test
@SuppressLogger(SdkLongCounter.class)
void longCounterAdd_Monotonicity() {
    LongCounter longCounter = sdkMeter.counterBuilder("testCounter").build();
    longCounter.add(-45);
    assertThat(sdkMeterReader.collectAllMetrics()).hasSize(0);
    logs.assertContains("Counters can only increase. Instrument testCounter has recorded a negative value.");
}
Also used : LongCounter(io.opentelemetry.api.metrics.LongCounter) BoundLongCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongCounter) SuppressLogger(io.opentelemetry.internal.testing.slf4j.SuppressLogger) Test(org.junit.jupiter.api.Test)

Example 2 with LongCounter

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

the class SdkLongCounterTest 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" };
    LongCounter longCounter = sdkMeter.counterBuilder("testCounter").build();
    StressTestRunner.Builder stressTestBuilder = StressTestRunner.builder().setInstrument((SdkLongCounter) longCounter).setCollectionIntervalMs(100);
    for (int i = 0; i < 4; i++) {
        stressTestBuilder.addOperation(StressTestRunner.Operation.create(1_000, 2, new OperationUpdaterDirectCall(longCounter, keys[i], values[i])));
        stressTestBuilder.addOperation(StressTestRunner.Operation.create(1_000, 2, new OperationUpdaterWithBinding(((SdkLongCounter) longCounter).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("testCounter").hasLongSum().isCumulative().isMonotonic().points().allSatisfy(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasValue(20_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 : LongCounter(io.opentelemetry.api.metrics.LongCounter) 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) LogCapturer(io.github.netmikey.logunit.api.LogCapturer) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) BoundLongCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongCounter) 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) LongCounter(io.opentelemetry.api.metrics.LongCounter) BoundLongCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongCounter) Test(org.junit.jupiter.api.Test)

Example 3 with LongCounter

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

the class SdkLongCounterTest method collectMetrics_WithMultipleCollects.

@Test
void collectMetrics_WithMultipleCollects() {
    long startTime = testClock.now();
    LongCounter longCounter = sdkMeter.counterBuilder("testCounter").build();
    BoundLongCounter bound = ((SdkLongCounter) longCounter).bind(Attributes.builder().put("K", "V").build());
    try {
        // Do some records using bounds and direct calls and bindings.
        longCounter.add(12, Attributes.empty());
        bound.add(123);
        longCounter.add(21, Attributes.empty());
        // Advancing time here should not matter.
        testClock.advance(Duration.ofNanos(SECOND_NANOS));
        bound.add(321);
        longCounter.add(111, Attributes.builder().put("K", "V").build());
        assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testCounter").hasLongSum().isMonotonic().isCumulative().points().allSatisfy(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now())).satisfiesExactlyInAnyOrder(point -> assertThat(point).hasAttributes(Attributes.empty()).hasValue(33), point -> assertThat(point).hasAttributes(Attributes.of(stringKey("K"), "V")).hasValue(555)));
        // Repeat to prove we keep previous values.
        testClock.advance(Duration.ofNanos(SECOND_NANOS));
        bound.add(222);
        longCounter.add(11, Attributes.empty());
        assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testCounter").hasLongSum().isMonotonic().isCumulative().points().allSatisfy(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now())).satisfiesExactlyInAnyOrder(point -> assertThat(point).hasAttributes(Attributes.empty()).hasValue(44), point -> assertThat(point).hasAttributes(Attributes.of(stringKey("K"), "V")).hasValue(777)));
    } finally {
        bound.unbind();
    }
}
Also used : LongCounter(io.opentelemetry.api.metrics.LongCounter) 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) LogCapturer(io.github.netmikey.logunit.api.LogCapturer) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) BoundLongCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongCounter) 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) BoundLongCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongCounter) LongCounter(io.opentelemetry.api.metrics.LongCounter) BoundLongCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongCounter) Test(org.junit.jupiter.api.Test)

Example 4 with LongCounter

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

the class SdkLongCounterTest method boundLongCounterAdd_Monotonicity.

@Test
@SuppressLogger(SdkLongCounter.class)
void boundLongCounterAdd_Monotonicity() {
    LongCounter longCounter = sdkMeter.counterBuilder("testCounter").build();
    BoundLongCounter bound = ((SdkLongCounter) longCounter).bind(Attributes.empty());
    try {
        bound.add(-9);
        assertThat(sdkMeterReader.collectAllMetrics()).hasSize(0);
        logs.assertContains("Counters can only increase. Instrument testCounter has recorded a negative value.");
    } finally {
        bound.unbind();
    }
}
Also used : BoundLongCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongCounter) LongCounter(io.opentelemetry.api.metrics.LongCounter) BoundLongCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongCounter) SuppressLogger(io.opentelemetry.internal.testing.slf4j.SuppressLogger) Test(org.junit.jupiter.api.Test)

Example 5 with LongCounter

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

the class CardinalityTest method staleMetricsDropped_synchronousInstrument.

/**
 * Records to sync instruments, with distinct attributes each time. Validates that stale metrics
 * are dropped for delta and cumulative readers. Stale metrics are those with attributes that did
 * not receive recordings in the most recent collection.
 *
 * <p>Effectively, we make sure we cap-out at attribute size = 2000 (constant in
 * MetricStorageutils).
 */
@Test
void staleMetricsDropped_synchronousInstrument() {
    LongCounter syncCounter = meter.counterBuilder("sync-counter").build();
    // Note: This constant comes from MetricStorageUtils, but it's package-private.
    for (int i = 1; i <= 2000; i++) {
        syncCounter.add(1, Attributes.builder().put("key", "num_" + i).build());
        // DELTA reader only has latest
        assertThat(deltaReader.collectAllMetrics()).as("Delta collection " + i).hasSize(1).satisfiesExactly(metricData -> assertThat(metricData).hasName("sync-counter").hasLongSum().isDelta().points().hasSize(1));
        // Make sure we preserve previous cumulatives
        int currentSize = i;
        assertThat(cumulativeReader.collectAllMetrics()).as("Cumulative collection " + i).hasSize(1).satisfiesExactly(metricData -> assertThat(metricData).hasName("sync-counter").hasLongSum().isCumulative().points().hasSize(currentSize));
    }
    // Now punch the limit and ONLY metrics we just recorded stay, due to simplistic GC.
    for (int i = 2001; i <= 2010; i++) {
        syncCounter.add(1, Attributes.builder().put("key", "num_" + i).build());
    }
    assertThat(deltaReader.collectAllMetrics()).as("Delta collection - post limit @ 10").hasSize(1).satisfiesExactly(metricData -> assertThat(metricData).hasName("sync-counter").hasLongSum().isDelta().points().hasSize(10));
    assertThat(cumulativeReader.collectAllMetrics()).as("Cumulative collection - post limit @ 10").hasSize(1).satisfiesExactly(metricData -> assertThat(metricData).hasName("sync-counter").hasLongSum().isCumulative().points().hasSize(10));
}
Also used : LongCounter(io.opentelemetry.api.metrics.LongCounter) Test(org.junit.jupiter.api.Test)

Aggregations

LongCounter (io.opentelemetry.api.metrics.LongCounter)21 Test (org.junit.jupiter.api.Test)19 Meter (io.opentelemetry.api.metrics.Meter)16 InMemoryMetricReader (io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader)13 Attributes (io.opentelemetry.api.common.Attributes)12 MetricAssertions.assertThat (io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat)12 InstrumentationLibraryInfo (io.opentelemetry.sdk.common.InstrumentationLibraryInfo)11 Resource (io.opentelemetry.sdk.resources.Resource)11 TestClock (io.opentelemetry.sdk.testing.time.TestClock)11 Duration (java.time.Duration)10 DoubleCounter (io.opentelemetry.api.metrics.DoubleCounter)9 DoubleHistogram (io.opentelemetry.api.metrics.DoubleHistogram)9 DoubleUpDownCounter (io.opentelemetry.api.metrics.DoubleUpDownCounter)9 LongHistogram (io.opentelemetry.api.metrics.LongHistogram)9 LongUpDownCounter (io.opentelemetry.api.metrics.LongUpDownCounter)9 AttributeKey (io.opentelemetry.api.common.AttributeKey)7 MetricData (io.opentelemetry.sdk.metrics.data.MetricData)7 BoundLongCounter (io.opentelemetry.sdk.metrics.internal.instrument.BoundLongCounter)7 Baggage (io.opentelemetry.api.baggage.Baggage)6 MeterProvider (io.opentelemetry.api.metrics.MeterProvider)6