Search in sources :

Example 1 with InstrumentSelector

use of io.opentelemetry.sdk.metrics.view.InstrumentSelector in project opentelemetry-java by open-telemetry.

the class ViewConfigTest method toInstrumentSelector_Empty.

@Test
void toInstrumentSelector_Empty() {
    InstrumentSelector selector = ViewConfig.toInstrumentSelector(SelectorSpecification.builder().build());
    assertThat(selector).isEqualTo(InstrumentSelector.builder().build());
}
Also used : InstrumentSelector(io.opentelemetry.sdk.metrics.view.InstrumentSelector) Test(org.junit.jupiter.api.Test)

Example 2 with InstrumentSelector

use of io.opentelemetry.sdk.metrics.view.InstrumentSelector in project opentelemetry-java by open-telemetry.

the class ViewConfigTest method toInstrumentSelector.

@Test
void toInstrumentSelector() {
    InstrumentSelector selector = ViewConfig.toInstrumentSelector(SelectorSpecification.builder().instrumentName("name").instrumentType(InstrumentType.COUNTER).meterName("meterName").meterVersion("meterVersion").meterSchemaUrl("http://example.com").build());
    assertThat(selector.getInstrumentNameFilter().test("name")).isTrue();
    assertThat(selector.getInstrumentNameFilter().test("name1")).isFalse();
    assertThat(selector.getInstrumentType()).isEqualTo(InstrumentType.COUNTER);
    assertThat(selector.getMeterNameFilter().test("meterName")).isTrue();
    assertThat(selector.getMeterNameFilter().test("meterName1")).isFalse();
    assertThat(selector.getMeterVersionFilter().test("meterVersion")).isTrue();
    assertThat(selector.getMeterVersionFilter().test("meterVersion1")).isFalse();
    assertThat(selector.getMeterSchemaUrlFilter().test("http://example.com")).isTrue();
    assertThat(selector.getMeterSchemaUrlFilter().test("http://example1.com")).isFalse();
}
Also used : InstrumentSelector(io.opentelemetry.sdk.metrics.view.InstrumentSelector) Test(org.junit.jupiter.api.Test)

Example 3 with InstrumentSelector

use of io.opentelemetry.sdk.metrics.view.InstrumentSelector in project opentelemetry-java by open-telemetry.

the class SdkMeterProviderTest method viewSdk_AllowMulitpleViewsPerAsynchronousInstrument.

@Test
void viewSdk_AllowMulitpleViewsPerAsynchronousInstrument() {
    InstrumentSelector selector = InstrumentSelector.builder().setType(InstrumentType.OBSERVABLE_GAUGE).setName("test").build();
    InMemoryMetricReader reader = InMemoryMetricReader.create();
    SdkMeterProvider provider = sdkMeterProviderBuilder.registerMetricReader(reader).registerView(selector, View.builder().setName("not_test").setDescription("not_desc").setAggregation(Aggregation.lastValue()).build()).registerView(selector, View.builder().setName("not_test_2").setDescription("not_desc_2").setAggregation(Aggregation.sum()).build()).build();
    Meter meter = provider.get(SdkMeterProviderTest.class.getName());
    meter.gaugeBuilder("test").setDescription("desc").setUnit("unit").buildWithCallback(obs -> obs.record(1.0));
    assertThat(reader.collectAllMetrics()).satisfiesExactlyInAnyOrder(metric -> assertThat(metric).hasName("not_test").hasDescription("not_desc").hasUnit("unit").hasDoubleGauge(), metric -> assertThat(metric).hasName("not_test_2").hasDescription("not_desc_2").hasUnit("unit").hasDoubleSum());
}
Also used : InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) Meter(io.opentelemetry.api.metrics.Meter) InstrumentSelector(io.opentelemetry.sdk.metrics.view.InstrumentSelector) Test(org.junit.jupiter.api.Test)

Example 4 with InstrumentSelector

use of io.opentelemetry.sdk.metrics.view.InstrumentSelector 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 5 with InstrumentSelector

use of io.opentelemetry.sdk.metrics.view.InstrumentSelector in project opentelemetry-java by open-telemetry.

the class SdkMeterProviderTest method viewSdk_AllowMulitpleViewsPerSynchronousInstrument.

@Test
void viewSdk_AllowMulitpleViewsPerSynchronousInstrument() {
    InstrumentSelector selector = InstrumentSelector.builder().setType(InstrumentType.HISTOGRAM).setName("test").build();
    InMemoryMetricReader reader = InMemoryMetricReader.create();
    SdkMeterProvider provider = sdkMeterProviderBuilder.registerMetricReader(reader).registerView(selector, View.builder().setName("not_test").setDescription("not_desc").setAggregation(Aggregation.lastValue()).build()).registerView(selector, View.builder().setName("not_test_2").setDescription("not_desc_2").setAggregation(Aggregation.sum()).build()).build();
    Meter meter = provider.get(SdkMeterProviderTest.class.getName());
    DoubleHistogram histogram = meter.histogramBuilder("test").setDescription("desc").setUnit("unit").build();
    histogram.record(1.0);
    assertThat(reader.collectAllMetrics()).satisfiesExactlyInAnyOrder(metric -> assertThat(metric).hasName("not_test").hasDescription("not_desc").hasUnit("unit").hasDoubleGauge(), metric -> assertThat(metric).hasName("not_test_2").hasDescription("not_desc_2").hasUnit("unit").hasDoubleSum());
}
Also used : DoubleHistogram(io.opentelemetry.api.metrics.DoubleHistogram) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) Meter(io.opentelemetry.api.metrics.Meter) InstrumentSelector(io.opentelemetry.sdk.metrics.view.InstrumentSelector) Test(org.junit.jupiter.api.Test)

Aggregations

InstrumentSelector (io.opentelemetry.sdk.metrics.view.InstrumentSelector)5 Test (org.junit.jupiter.api.Test)5 Meter (io.opentelemetry.api.metrics.Meter)3 InMemoryMetricReader (io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader)3 DoubleHistogram (io.opentelemetry.api.metrics.DoubleHistogram)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 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 MeterProvider (io.opentelemetry.api.metrics.MeterProvider)1 ObservableLongCounter (io.opentelemetry.api.metrics.ObservableLongCounter)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