Search in sources :

Example 6 with LongCounter

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

the class CardinalityTest method cardinalityLimits_synchronousInstrument.

/**
 * Records to sync instruments, many distinct attributes. Validates that the {@code
 * MetricStorageUtils#MAX_ACCUMULATIONS} is enforced for each instrument.
 */
@Test
void cardinalityLimits_synchronousInstrument() {
    LongCounter syncCounter1 = meter.counterBuilder("sync-counter1").build();
    LongCounter syncCounter2 = meter.counterBuilder("sync-counter2").build();
    for (int i = 0; i < MAX_ACCUMULATIONS + 1; i++) {
        syncCounter1.add(1, Attributes.builder().put("key", "value" + i).build());
        syncCounter2.add(1, Attributes.builder().put("key", "value" + i).build());
    }
    assertThat(deltaReader.collectAllMetrics()).as("Delta collection").hasSize(2).satisfiesExactlyInAnyOrder(metricData -> assertThat(metricData).hasName("sync-counter1").hasLongSum().isDelta().points().hasSize(MAX_ACCUMULATIONS), metricData -> assertThat(metricData).hasName("sync-counter2").hasLongSum().isDelta().points().hasSize(MAX_ACCUMULATIONS));
    assertThat(cumulativeReader.collectAllMetrics()).as("Cumulative collection").hasSize(2).satisfiesExactlyInAnyOrder(metricData -> assertThat(metricData).hasName("sync-counter1").hasLongSum().isCumulative().points().hasSize(MAX_ACCUMULATIONS), metricData -> assertThat(metricData).hasName("sync-counter2").hasLongSum().isCumulative().points().hasSize(MAX_ACCUMULATIONS));
}
Also used : LongCounter(io.opentelemetry.api.metrics.LongCounter) Test(org.junit.jupiter.api.Test)

Example 7 with LongCounter

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

the class OtlpExporterIntegrationTest method testMetricExport.

private static void testMetricExport(MetricExporter metricExporter) {
    MetricReaderFactory reader = PeriodicMetricReader.builder(metricExporter).setInterval(Duration.ofSeconds(5)).newMetricReaderFactory();
    SdkMeterProvider meterProvider = SdkMeterProvider.builder().setResource(RESOURCE).registerMetricReader(reader).build();
    Meter meter = meterProvider.meterBuilder(OtlpExporterIntegrationTest.class.getName()).build();
    LongCounter longCounter = meter.counterBuilder("my-counter").build();
    longCounter.add(100, Attributes.builder().put("key", "value").build());
    try {
        await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> assertThat(grpcServer.metricRequests).hasSize(1));
    } finally {
        meterProvider.close();
    }
    ExportMetricsServiceRequest request = grpcServer.metricRequests.get(0);
    assertThat(request.getResourceMetricsCount()).isEqualTo(1);
    ResourceMetrics resourceMetrics = request.getResourceMetrics(0);
    assertThat(resourceMetrics.getResource().getAttributesList()).contains(KeyValue.newBuilder().setKey(ResourceAttributes.SERVICE_NAME.getKey()).setValue(AnyValue.newBuilder().setStringValue("integration test").build()).build());
    assertThat(resourceMetrics.getInstrumentationLibraryMetricsCount()).isEqualTo(1);
    InstrumentationLibraryMetrics ilMetrics = resourceMetrics.getInstrumentationLibraryMetrics(0);
    assertThat(ilMetrics.getInstrumentationLibrary().getName()).isEqualTo(OtlpExporterIntegrationTest.class.getName());
    assertThat(ilMetrics.getMetricsCount()).isEqualTo(1);
    Metric metric = ilMetrics.getMetrics(0);
    assertThat(metric.getName()).isEqualTo("my-counter");
    assertThat(metric.getDataCase()).isEqualTo(Metric.DataCase.SUM);
    Sum sum = metric.getSum();
    assertThat(sum.getAggregationTemporality()).isEqualTo(AggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE);
    assertThat(sum.getDataPointsCount()).isEqualTo(1);
    NumberDataPoint dataPoint = sum.getDataPoints(0);
    assertThat(dataPoint.getAsInt()).isEqualTo(100);
    assertThat(dataPoint.getAttributesList()).isEqualTo(Collections.singletonList(KeyValue.newBuilder().setKey("key").setValue(AnyValue.newBuilder().setStringValue("value").build()).build()));
}
Also used : ResourceMetrics(io.opentelemetry.proto.metrics.v1.ResourceMetrics) SdkMeterProvider(io.opentelemetry.sdk.metrics.SdkMeterProvider) Meter(io.opentelemetry.api.metrics.Meter) NumberDataPoint(io.opentelemetry.proto.metrics.v1.NumberDataPoint) MetricReaderFactory(io.opentelemetry.sdk.metrics.export.MetricReaderFactory) Metric(io.opentelemetry.proto.metrics.v1.Metric) Sum(io.opentelemetry.proto.metrics.v1.Sum) InstrumentationLibraryMetrics(io.opentelemetry.proto.metrics.v1.InstrumentationLibraryMetrics) LongCounter(io.opentelemetry.api.metrics.LongCounter) ExportMetricsServiceRequest(io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest)

Example 8 with LongCounter

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

the class SdkMeterProviderTest method collectAllSyncInstruments_OverwriteTemporality.

@Test
void collectAllSyncInstruments_OverwriteTemporality() {
    sdkMeterProviderBuilder.registerView(InstrumentSelector.builder().setType(InstrumentType.COUNTER).build(), View.builder().setAggregation(Aggregation.explicitBucketHistogram(Collections.emptyList())).build());
    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());
    testClock.advance(Duration.ofSeconds(1));
    assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testLongCounter").hasDoubleHistogram().isDelta().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now() - 1000000000).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasBucketCounts(1)));
    longCounter.add(10, Attributes.empty());
    testClock.advance(Duration.ofSeconds(1));
    assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasDoubleHistogram().isDelta().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now() - 1000000000).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasBucketCounts(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) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) Meter(io.opentelemetry.api.metrics.Meter) ObservableLongCounter(io.opentelemetry.api.metrics.ObservableLongCounter) LongCounter(io.opentelemetry.api.metrics.LongCounter) Test(org.junit.jupiter.api.Test)

Example 9 with LongCounter

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

the class SdkMeterProviderTest method sdkMeterProvider_supportsMultipleCollectorsCumulative.

@Test
void sdkMeterProvider_supportsMultipleCollectorsCumulative() {
    InMemoryMetricReader collector1 = InMemoryMetricReader.create();
    InMemoryMetricReader collector2 = InMemoryMetricReader.create();
    SdkMeterProvider meterProvider = sdkMeterProviderBuilder.registerMetricReader(collector1).registerMetricReader(collector2).build();
    Meter sdkMeter = meterProvider.get(SdkMeterProviderTest.class.getName());
    LongCounter counter = sdkMeter.counterBuilder("testSum").build();
    long startTime = testClock.now();
    counter.add(1L);
    testClock.advance(Duration.ofSeconds(1));
    assertThat(collector1.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasName("testSum").hasLongSum().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now()).hasValue(1)));
    counter.add(1L);
    testClock.advance(Duration.ofSeconds(1));
    // Make sure collector 2 sees the value collector 1 saw
    assertThat(collector2.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasName("testSum").hasLongSum().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now()).hasValue(2)));
    // Make sure Collector 1 sees the same point as 2
    assertThat(collector1.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasName("testSum").hasLongSum().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now()).hasValue(2)));
}
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) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) Meter(io.opentelemetry.api.metrics.Meter) ObservableLongCounter(io.opentelemetry.api.metrics.ObservableLongCounter) LongCounter(io.opentelemetry.api.metrics.LongCounter) Test(org.junit.jupiter.api.Test)

Example 10 with LongCounter

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

the class SdkMeterTest method testLongCounter_upperCaseConflict.

@Test
void testLongCounter_upperCaseConflict() {
    LongCounter longCounter = sdkMeter.counterBuilder("testLongCounter").setDescription("My very own counter").setUnit("metric tonnes").build();
    assertThat(longCounter).isNotNull();
    sdkMeter.counterBuilder("testLongCounter".toUpperCase()).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) 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