Search in sources :

Example 1 with ObservableLongCounter

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

the class SdkMeterProviderTest method removeAsyncInstrument.

@Test
void removeAsyncInstrument() {
    InMemoryMetricReader reader = InMemoryMetricReader.create();
    Meter meter = sdkMeterProviderBuilder.registerMetricReader(reader).build().get(getClass().getName());
    ObservableLongCounter observableCounter1 = meter.counterBuilder("foo").buildWithCallback(measurement -> measurement.record(10, Attributes.builder().put("callback", "one").build()));
    ObservableLongCounter observableCounter2 = meter.counterBuilder("foo").buildWithCallback(measurement -> measurement.record(10, Attributes.builder().put("callback", "two").build()));
    assertThat(reader.collectAllMetrics()).hasSize(1).satisfiesExactly(metricData -> assertThat(metricData).hasLongSum().points().hasSize(2).satisfiesExactlyInAnyOrder(pointData -> assertThat(pointData).hasAttributes(Attributes.builder().put("callback", "one").build()), (Consumer<LongPointData>) longPointData -> assertThat(longPointData).hasAttributes(Attributes.builder().put("callback", "two").build())));
    observableCounter1.close();
    assertThat(reader.collectAllMetrics()).hasSize(1).satisfiesExactly(metricData -> assertThat(metricData).hasLongSum().points().hasSize(1).satisfiesExactlyInAnyOrder((Consumer<LongPointData>) longPointData -> assertThat(longPointData).hasAttributes(Attributes.builder().put("callback", "two").build())));
    observableCounter2.close();
    assertThat(reader.collectAllMetrics()).hasSize(0);
}
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) Consumer(java.util.function.Consumer) Meter(io.opentelemetry.api.metrics.Meter) ObservableLongCounter(io.opentelemetry.api.metrics.ObservableLongCounter) Test(org.junit.jupiter.api.Test)

Example 2 with ObservableLongCounter

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

the class SdkObservableLongCounterTest method removeCallback.

@Test
void removeCallback() {
    InMemoryMetricReader sdkMeterReader = InMemoryMetricReader.create();
    ObservableLongCounter counter = sdkMeterProviderBuilder.registerMetricReader(sdkMeterReader).build().get(getClass().getName()).counterBuilder("testCounter").buildWithCallback(measurement -> measurement.record(10));
    assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasName("testCounter").hasLongSum().points().hasSize(1));
    counter.close();
    assertThat(sdkMeterReader.collectAllMetrics()).hasSize(0);
}
Also used : InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) ObservableLongCounter(io.opentelemetry.api.metrics.ObservableLongCounter) Test(org.junit.jupiter.api.Test)

Aggregations

ObservableLongCounter (io.opentelemetry.api.metrics.ObservableLongCounter)2 InMemoryMetricReader (io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader)2 Test (org.junit.jupiter.api.Test)2 Baggage (io.opentelemetry.api.baggage.Baggage)1 AttributeKey (io.opentelemetry.api.common.AttributeKey)1 Attributes (io.opentelemetry.api.common.Attributes)1 DoubleCounter (io.opentelemetry.api.metrics.DoubleCounter)1 DoubleHistogram (io.opentelemetry.api.metrics.DoubleHistogram)1 DoubleUpDownCounter (io.opentelemetry.api.metrics.DoubleUpDownCounter)1 LongCounter (io.opentelemetry.api.metrics.LongCounter)1 LongHistogram (io.opentelemetry.api.metrics.LongHistogram)1 LongUpDownCounter (io.opentelemetry.api.metrics.LongUpDownCounter)1 Meter (io.opentelemetry.api.metrics.Meter)1 MeterProvider (io.opentelemetry.api.metrics.MeterProvider)1 Context (io.opentelemetry.context.Context)1 Scope (io.opentelemetry.context.Scope)1 CompletableResultCode (io.opentelemetry.sdk.common.CompletableResultCode)1 InstrumentationLibraryInfo (io.opentelemetry.sdk.common.InstrumentationLibraryInfo)1 InstrumentType (io.opentelemetry.sdk.metrics.common.InstrumentType)1 LongPointData (io.opentelemetry.sdk.metrics.data.LongPointData)1