Search in sources :

Example 1 with LongUpDownCounter

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

the class SdkLongUpDownCounterTest method collectMetrics_NoRecords.

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

Example 2 with LongUpDownCounter

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

the class SdkLongUpDownCounterTest method collectMetrics_WithEmptyAttributes.

@Test
void collectMetrics_WithEmptyAttributes() {
    LongUpDownCounter longUpDownCounter = sdkMeter.upDownCounterBuilder("testUpDownCounter").setDescription("description").setUnit("By").build();
    testClock.advance(Duration.ofNanos(SECOND_NANOS));
    longUpDownCounter.add(12, Attributes.empty());
    longUpDownCounter.add(12);
    assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testUpDownCounter").hasDescription("description").hasUnit("By").hasLongSum().isNotMonotonic().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now() - SECOND_NANOS).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasValue(24)));
}
Also used : 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) BoundLongUpDownCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongUpDownCounter) 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) 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) LongUpDownCounter(io.opentelemetry.api.metrics.LongUpDownCounter) BoundLongUpDownCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongUpDownCounter) LongUpDownCounter(io.opentelemetry.api.metrics.LongUpDownCounter) Test(org.junit.jupiter.api.Test)

Example 3 with LongUpDownCounter

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

the class SdkLongUpDownCounterTest 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" };
    LongUpDownCounter longUpDownCounter = sdkMeter.upDownCounterBuilder("testUpDownCounter").build();
    StressTestRunner.Builder stressTestBuilder = StressTestRunner.builder().setInstrument((SdkLongUpDownCounter) longUpDownCounter).setCollectionIntervalMs(100);
    for (int i = 0; i < 4; i++) {
        stressTestBuilder.addOperation(StressTestRunner.Operation.create(1_000, 2, new OperationUpdaterDirectCall(longUpDownCounter, keys[i], values[i])));
        stressTestBuilder.addOperation(StressTestRunner.Operation.create(1_000, 2, new OperationUpdaterWithBinding(((SdkLongUpDownCounter) longUpDownCounter).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("testUpDownCounter").hasLongSum().isCumulative().isNotMonotonic().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 : 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) BoundLongUpDownCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongUpDownCounter) 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) 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) LongUpDownCounter(io.opentelemetry.api.metrics.LongUpDownCounter) BoundLongUpDownCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongUpDownCounter) LongUpDownCounter(io.opentelemetry.api.metrics.LongUpDownCounter) PointData(io.opentelemetry.sdk.metrics.data.PointData) Test(org.junit.jupiter.api.Test)

Example 4 with LongUpDownCounter

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

the class SdkMeterTest method testLongUpDownCounter.

@Test
void testLongUpDownCounter() {
    LongUpDownCounter longUpDownCounter = sdkMeter.upDownCounterBuilder("testLongUpDownCounter").setDescription("My very own counter").setUnit("metric tonnes").build();
    assertThat(longUpDownCounter).isNotNull();
    // Note: We no longer get the same instrument instance as these instances are lightweight
    // objects backed by storage now.  Here we just make sure it doesn't throw to grab
    // a second instance.
    sdkMeter.upDownCounterBuilder("testLongUpDownCounter").setDescription("My very own counter").setUnit("metric tonnes").build();
    assertThat(logs.getEvents()).isEmpty();
    sdkMeter.upDownCounterBuilder("testLongUpDownCounter").build();
    assertThat(logs.assertContains(loggingEvent -> loggingEvent.getLevel().equals(WARN), "Failed to register metric.").getThrowable()).hasMessageContaining("Metric with same name and different descriptor already created.");
}
Also used : LongCounter(io.opentelemetry.api.metrics.LongCounter) DoubleUpDownCounter(io.opentelemetry.api.metrics.DoubleUpDownCounter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DoubleHistogram(io.opentelemetry.api.metrics.DoubleHistogram) Test(org.junit.jupiter.api.Test) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) LogCapturer(io.github.netmikey.logunit.api.LogCapturer) MeterSharedState(io.opentelemetry.sdk.metrics.internal.state.MeterSharedState) SuppressLogger(io.opentelemetry.internal.testing.slf4j.SuppressLogger) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) DoubleCounter(io.opentelemetry.api.metrics.DoubleCounter) LongHistogram(io.opentelemetry.api.metrics.LongHistogram) Meter(io.opentelemetry.api.metrics.Meter) WARN(org.slf4j.event.Level.WARN) LongUpDownCounter(io.opentelemetry.api.metrics.LongUpDownCounter) LongUpDownCounter(io.opentelemetry.api.metrics.LongUpDownCounter) Test(org.junit.jupiter.api.Test)

Example 5 with LongUpDownCounter

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

the class SdkMeterProviderTest method collectAllSyncInstruments_DeltaHistogram.

@Test
void collectAllSyncInstruments_DeltaHistogram() {
    registerViewForAllTypes(sdkMeterProviderBuilder, Aggregation.explicitBucketHistogram(Collections.emptyList()));
    InMemoryMetricReader sdkMeterReader = InMemoryMetricReader.createDelta();
    SdkMeterProvider sdkMeterProvider = sdkMeterProviderBuilder.registerMetricReader(sdkMeterReader).build();
    Meter sdkMeter = sdkMeterProvider.get(SdkMeterProviderTest.class.getName());
    LongCounter longCounter = sdkMeter.counterBuilder("testLongCounter").build();
    longCounter.add(10, Attributes.empty());
    LongUpDownCounter longUpDownCounter = sdkMeter.upDownCounterBuilder("testLongUpDownCounter").build();
    longUpDownCounter.add(10, Attributes.empty());
    LongHistogram longValueRecorder = sdkMeter.histogramBuilder("testLongValueRecorder").ofLongs().build();
    longValueRecorder.record(10, Attributes.empty());
    DoubleCounter doubleCounter = sdkMeter.counterBuilder("testDoubleCounter").ofDoubles().build();
    doubleCounter.add(10, Attributes.empty());
    DoubleUpDownCounter doubleUpDownCounter = sdkMeter.upDownCounterBuilder("testDoubleUpDownCounter").ofDoubles().build();
    doubleUpDownCounter.add(10, Attributes.empty());
    DoubleHistogram doubleValueRecorder = sdkMeter.histogramBuilder("testDoubleValueRecorder").build();
    doubleValueRecorder.record(10, Attributes.empty());
    testClock.advance(Duration.ofSeconds(1));
    assertThat(sdkMeterReader.collectAllMetrics()).allSatisfy(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasDescription("").hasUnit("1").hasDoubleHistogram().isDelta().points().satisfiesExactlyInAnyOrder(point -> assertThat(point).hasStartEpochNanos(testClock.now() - 1000000000).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasBucketCounts(1))).extracting(MetricData::getName).containsExactlyInAnyOrder("testLongCounter", "testDoubleCounter", "testLongUpDownCounter", "testDoubleUpDownCounter", "testLongValueRecorder", "testDoubleValueRecorder");
    testClock.advance(Duration.ofSeconds(1));
    longCounter.add(10, Attributes.empty());
    longUpDownCounter.add(10, Attributes.empty());
    longValueRecorder.record(10, Attributes.empty());
    doubleCounter.add(10, Attributes.empty());
    doubleUpDownCounter.add(10, Attributes.empty());
    doubleValueRecorder.record(10, Attributes.empty());
    assertThat(sdkMeterReader.collectAllMetrics()).allSatisfy(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasDescription("").hasUnit("1").hasDoubleHistogram().isDelta().points().satisfiesExactlyInAnyOrder(point -> assertThat(point).hasStartEpochNanos(testClock.now() - 1000000000).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasBucketCounts(1))).extracting(MetricData::getName).containsExactlyInAnyOrder("testLongCounter", "testDoubleCounter", "testLongUpDownCounter", "testDoubleUpDownCounter", "testLongValueRecorder", "testDoubleValueRecorder");
}
Also used : LongPointData(io.opentelemetry.sdk.metrics.data.LongPointData) Resource(io.opentelemetry.sdk.resources.Resource) Mock(org.mockito.Mock) Attributes(io.opentelemetry.api.common.Attributes) Aggregation(io.opentelemetry.sdk.metrics.view.Aggregation) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) ViewBuilderImpl(io.opentelemetry.sdk.metrics.internal.view.ViewBuilderImpl) MetricReader(io.opentelemetry.sdk.metrics.export.MetricReader) View(io.opentelemetry.sdk.metrics.view.View) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) 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) LongUpDownCounter(io.opentelemetry.api.metrics.LongUpDownCounter) ObservableLongCounter(io.opentelemetry.api.metrics.ObservableLongCounter) LongCounter(io.opentelemetry.api.metrics.LongCounter) Context(io.opentelemetry.context.Context) DoubleUpDownCounter(io.opentelemetry.api.metrics.DoubleUpDownCounter) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Scope(io.opentelemetry.context.Scope) DoubleHistogram(io.opentelemetry.api.metrics.DoubleHistogram) Mockito.when(org.mockito.Mockito.when) InstrumentType(io.opentelemetry.sdk.metrics.common.InstrumentType) InstrumentSelector(io.opentelemetry.sdk.metrics.view.InstrumentSelector) Assertions.entry(org.assertj.core.api.Assertions.entry) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) Baggage(io.opentelemetry.api.baggage.Baggage) DoubleCounter(io.opentelemetry.api.metrics.DoubleCounter) AttributeKey(io.opentelemetry.api.common.AttributeKey) MetricData(io.opentelemetry.sdk.metrics.data.MetricData) MeterProvider(io.opentelemetry.api.metrics.MeterProvider) Collections(java.util.Collections) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) LongUpDownCounter(io.opentelemetry.api.metrics.LongUpDownCounter) DoubleUpDownCounter(io.opentelemetry.api.metrics.DoubleUpDownCounter) DoubleHistogram(io.opentelemetry.api.metrics.DoubleHistogram) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) Meter(io.opentelemetry.api.metrics.Meter) DoubleCounter(io.opentelemetry.api.metrics.DoubleCounter) LongHistogram(io.opentelemetry.api.metrics.LongHistogram) ObservableLongCounter(io.opentelemetry.api.metrics.ObservableLongCounter) LongCounter(io.opentelemetry.api.metrics.LongCounter) Test(org.junit.jupiter.api.Test)

Aggregations

LongUpDownCounter (io.opentelemetry.api.metrics.LongUpDownCounter)10 Test (org.junit.jupiter.api.Test)10 Meter (io.opentelemetry.api.metrics.Meter)9 InMemoryMetricReader (io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader)8 Attributes (io.opentelemetry.api.common.Attributes)7 InstrumentationLibraryInfo (io.opentelemetry.sdk.common.InstrumentationLibraryInfo)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 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 LongHistogram (io.opentelemetry.api.metrics.LongHistogram)5 BoundLongUpDownCounter (io.opentelemetry.sdk.metrics.internal.instrument.BoundLongUpDownCounter)5 AttributeKey.stringKey (io.opentelemetry.api.common.AttributeKey.stringKey)4 OperationUpdater (io.opentelemetry.sdk.metrics.StressTestRunner.OperationUpdater)4 PointData (io.opentelemetry.sdk.metrics.data.PointData)4 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)4