Search in sources :

Example 1 with Aggregation

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

the class ViewConfig method loadViewConfig.

// Visible for testing
@SuppressWarnings("unchecked")
static List<ViewConfigSpecification> loadViewConfig(InputStream inputStream) {
    Yaml yaml = new Yaml();
    try {
        List<ViewConfigSpecification> result = new ArrayList<>();
        List<Map<String, Object>> viewConfigs = yaml.load(inputStream);
        for (Map<String, Object> viewConfigSpecMap : viewConfigs) {
            Map<String, Object> selectorSpecMap = requireNonNull(getAsType(viewConfigSpecMap, "selector", Map.class), "selector is required");
            Map<String, Object> viewSpecMap = requireNonNull(getAsType(viewConfigSpecMap, "view", Map.class), "view is required");
            InstrumentType instrumentType = Optional.ofNullable(getAsType(selectorSpecMap, "instrument_type", String.class)).map(InstrumentType::valueOf).orElse(null);
            List<String> attributeKeys = Optional.ofNullable(((List<Object>) getAsType(viewSpecMap, "attribute_keys", List.class))).map(objects -> objects.stream().map(String::valueOf).collect(toList())).orElse(null);
            result.add(ViewConfigSpecification.builder().selectorSpecification(SelectorSpecification.builder().instrumentName(getAsType(selectorSpecMap, "instrument_name", String.class)).instrumentType(instrumentType).meterName(getAsType(selectorSpecMap, "meter_name", String.class)).meterVersion(getAsType(selectorSpecMap, "meter_version", String.class)).meterSchemaUrl(getAsType(selectorSpecMap, "meter_schema_url", String.class)).build()).viewSpecification(ViewSpecification.builder().name(getAsType(viewSpecMap, "name", String.class)).description(getAsType(viewSpecMap, "description", String.class)).aggregation(getAsType(viewSpecMap, "aggregation", String.class)).attributeKeys(attributeKeys).build()).build());
        }
        return result;
    } catch (RuntimeException e) {
        throw new ConfigurationException("Failed to parse view config", e);
    }
}
Also used : Set(java.util.Set) InstrumentType(io.opentelemetry.sdk.metrics.common.InstrumentType) Aggregation(io.opentelemetry.sdk.metrics.view.Aggregation) InstrumentSelector(io.opentelemetry.sdk.metrics.view.InstrumentSelector) ConfigurationException(io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException) ViewBuilder(io.opentelemetry.sdk.metrics.view.ViewBuilder) ArrayList(java.util.ArrayList) Yaml(org.yaml.snakeyaml.Yaml) HashSet(java.util.HashSet) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) View(io.opentelemetry.sdk.metrics.view.View) SdkMeterProviderBuilder(io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder) InstrumentSelectorBuilder(io.opentelemetry.sdk.metrics.view.InstrumentSelectorBuilder) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) Optional(java.util.Optional) Nullable(javax.annotation.Nullable) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) InstrumentType(io.opentelemetry.sdk.metrics.common.InstrumentType) Yaml(org.yaml.snakeyaml.Yaml) ConfigurationException(io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Map(java.util.Map)

Example 2 with Aggregation

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

the class SdkMeterProviderTest method sdkMeterProvider_supportsMultipleCollectorsDelta.

@Test
void sdkMeterProvider_supportsMultipleCollectorsDelta() {
    // Note: we use a view to do delta aggregation, but any view ALWAYS uses double-precision right
    // now.
    InMemoryMetricReader collector1 = InMemoryMetricReader.createDelta();
    InMemoryMetricReader collector2 = InMemoryMetricReader.createDelta();
    SdkMeterProvider meterProvider = sdkMeterProviderBuilder.registerMetricReader(collector1).registerMetricReader(collector2).registerView(InstrumentSelector.builder().setType(InstrumentType.COUNTER).setName("testSum").build(), View.builder().setAggregation(Aggregation.sum()).build()).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().isDelta().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now()).hasValue(1)));
    long collectorOneTimeOne = testClock.now();
    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().isDelta().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now()).hasValue(2)));
    // Make sure Collector 1 sees the same point as 2, when it collects.
    assertThat(collector1.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasName("testSum").hasLongSum().isDelta().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(collectorOneTimeOne).hasEpochNanos(testClock.now()).hasValue(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 3 with Aggregation

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

the class ViewConfigTest method toView.

@Test
void toView() {
    View view = ViewConfig.toView(ViewSpecification.builder().name("name").description("description").aggregation("sum").attributeKeys(Arrays.asList("foo", "bar")).build());
    assertThat(view.getName()).isEqualTo("name");
    assertThat(view.getDescription()).isEqualTo("description");
    assertThat(view.getAggregation()).isEqualTo(Aggregation.sum());
    assertThat(ImmutableView.getAttributesProcessor(view).process(Attributes.builder().put("foo", "val").put("bar", "val").put("baz", "val").build(), Context.current())).containsEntry("foo", "val").containsEntry("bar", "val").satisfies((Consumer<Attributes>) attributes -> assertThat(attributes.get(AttributeKey.stringKey("baz"))).isBlank());
}
Also used : Context(io.opentelemetry.context.Context) Arrays(java.util.Arrays) ViewRegistryBuilder(io.opentelemetry.sdk.metrics.internal.view.ViewRegistryBuilder) URL(java.net.URL) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Attributes(io.opentelemetry.api.common.Attributes) InstrumentType(io.opentelemetry.sdk.metrics.common.InstrumentType) FileInputStream(java.io.FileInputStream) Aggregation(io.opentelemetry.sdk.metrics.view.Aggregation) InstrumentSelector(io.opentelemetry.sdk.metrics.view.InstrumentSelector) ConfigurationException(io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException) ImmutableView(io.opentelemetry.sdk.metrics.internal.view.ImmutableView) FileNotFoundException(java.io.FileNotFoundException) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) SdkMeterProvider(io.opentelemetry.sdk.metrics.SdkMeterProvider) List(java.util.List) View(io.opentelemetry.sdk.metrics.view.View) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) SdkMeterProviderBuilder(io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder) OpenTelemetryAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat) Assertions.as(org.assertj.core.api.Assertions.as) AttributeKey(io.opentelemetry.api.common.AttributeKey) InputStream(java.io.InputStream) Attributes(io.opentelemetry.api.common.Attributes) ImmutableView(io.opentelemetry.sdk.metrics.internal.view.ImmutableView) View(io.opentelemetry.sdk.metrics.view.View) Test(org.junit.jupiter.api.Test)

Aggregations

InstrumentType (io.opentelemetry.sdk.metrics.common.InstrumentType)3 Aggregation (io.opentelemetry.sdk.metrics.view.Aggregation)3 InstrumentSelector (io.opentelemetry.sdk.metrics.view.InstrumentSelector)3 View (io.opentelemetry.sdk.metrics.view.View)3 AttributeKey (io.opentelemetry.api.common.AttributeKey)2 Attributes (io.opentelemetry.api.common.Attributes)2 Context (io.opentelemetry.context.Context)2 ConfigurationException (io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException)2 SdkMeterProviderBuilder (io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder)2 InputStream (java.io.InputStream)2 Consumer (java.util.function.Consumer)2 Test (org.junit.jupiter.api.Test)2 Baggage (io.opentelemetry.api.baggage.Baggage)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