Search in sources :

Example 16 with LongCounter

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

the class SdkMeterProviderTest method viewSdk_capturesBaggageFromContext.

@Test
void viewSdk_capturesBaggageFromContext() {
    InstrumentSelector selector = InstrumentSelector.builder().setType(InstrumentType.COUNTER).setName("test").build();
    InMemoryMetricReader reader = InMemoryMetricReader.create();
    SdkMeterProvider provider = sdkMeterProviderBuilder.registerMetricReader(reader).registerView(selector, ((ViewBuilderImpl) View.builder().setAggregation(Aggregation.sum())).appendAllBaggageAttributes().build()).build();
    Meter meter = provider.get(SdkMeterProviderTest.class.getName());
    Baggage baggage = Baggage.builder().put("baggage", "value").build();
    Context context = Context.root().with(baggage);
    LongCounter counter = meter.counterBuilder("test").build();
    // Make sure whether or not we explicitly pass baggage, all values have it appended.
    counter.add(1, Attributes.empty(), context);
    // Also check implicit context
    try (Scope ignored = context.makeCurrent()) {
        counter.add(1, Attributes.empty());
    }
    // Now make sure all metrics have baggage appended.
    // Implicitly we should have ONLY ONE metric data point that has baggage appended.
    assertThat(reader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasName("test").hasLongSum().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasAttributes(Attributes.builder().put("baggage", "value").build())));
}
Also used : Context(io.opentelemetry.context.Context) 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) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) Scope(io.opentelemetry.context.Scope) Meter(io.opentelemetry.api.metrics.Meter) InstrumentSelector(io.opentelemetry.sdk.metrics.view.InstrumentSelector) Baggage(io.opentelemetry.api.baggage.Baggage) ObservableLongCounter(io.opentelemetry.api.metrics.ObservableLongCounter) LongCounter(io.opentelemetry.api.metrics.LongCounter) Test(org.junit.jupiter.api.Test)

Example 17 with LongCounter

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

the class SdkMeterProviderTest method collectAllSyncInstruments.

@Test
void collectAllSyncInstruments() {
    InMemoryMetricReader sdkMeterReader = InMemoryMetricReader.create();
    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("testLongHistogram").ofLongs().build();
    longValueRecorder.record(10, Attributes.empty());
    DoubleCounter doubleCounter = sdkMeter.counterBuilder("testDoubleCounter").ofDoubles().build();
    doubleCounter.add(10.1, Attributes.empty());
    DoubleUpDownCounter doubleUpDownCounter = sdkMeter.upDownCounterBuilder("testDoubleUpDownCounter").ofDoubles().build();
    doubleUpDownCounter.add(-10.1, Attributes.empty());
    DoubleHistogram doubleValueRecorder = sdkMeter.histogramBuilder("testDoubleHistogram").build();
    doubleValueRecorder.record(10.1, Attributes.empty());
    assertThat(sdkMeterReader.collectAllMetrics()).allSatisfy(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasDescription("").hasUnit("1")).satisfiesExactlyInAnyOrder(metric -> assertThat(metric).hasName("testDoubleHistogram").hasDoubleHistogram().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasCount(1).hasSum(10.1).hasBucketCounts(0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), metric -> assertThat(metric).hasName("testDoubleCounter").hasDoubleSum().isMonotonic().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasValue(10.1)), metric -> assertThat(metric).hasName("testLongHistogram").hasDoubleHistogram().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasCount(1).hasSum(10).hasBucketCounts(0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), metric -> assertThat(metric).hasName("testLongUpDownCounter").hasLongSum().isNotMonotonic().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasValue(-10)), metric -> assertThat(metric).hasName("testLongCounter").hasLongSum().isMonotonic().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasValue(10)), metric -> assertThat(metric).hasName("testDoubleUpDownCounter").hasDoubleSum().isNotMonotonic().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasValue(-10.1)));
}
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)

Example 18 with LongCounter

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

the class SdkLongCounterTest method collectMetrics_NoRecords.

@Test
void collectMetrics_NoRecords() {
    LongCounter longCounter = sdkMeter.counterBuilder("Counter").build();
    BoundLongCounter bound = ((SdkLongCounter) longCounter).bind(Attributes.builder().put("foo", "bar").build());
    try {
        assertThat(sdkMeterReader.collectAllMetrics()).isEmpty();
    } 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) Test(org.junit.jupiter.api.Test)

Example 19 with LongCounter

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

the class SdkLongCounterTest method collectMetrics_WithEmptyAttributes.

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

Example 20 with LongCounter

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

the class SdkLongCounterTest method stressTest.

@Test
void stressTest() {
    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(2_000, 1, new OperationUpdaterDirectCall(longCounter, "K", "V")));
        stressTestBuilder.addOperation(StressTestRunner.Operation.create(2_000, 1, new OperationUpdaterWithBinding(((SdkLongCounter) longCounter).bind(Attributes.builder().put("K", "V").build()))));
    }
    stressTestBuilder.build().run();
    assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testCounter").hasLongSum().isCumulative().isMonotonic().points().satisfiesExactlyInAnyOrder(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasValue(160_000).attributes().hasSize(1).containsEntry("K", "V")));
}
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) LongCounter(io.opentelemetry.api.metrics.LongCounter) BoundLongCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongCounter) 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