Search in sources :

Example 1 with InstrumentType

use of io.opentelemetry.sdk.metrics.common.InstrumentType in project opentelemetry-java by open-telemetry.

the class ViewConfig method toInstrumentSelector.

// Visible for testing
static InstrumentSelector toInstrumentSelector(SelectorSpecification selectorSpec) {
    InstrumentSelectorBuilder builder = InstrumentSelector.builder();
    String instrumentName = selectorSpec.getInstrumentName();
    if (instrumentName != null) {
        builder.setName(instrumentName);
    }
    InstrumentType instrumentType = selectorSpec.getInstrumentType();
    if (instrumentType != null) {
        builder.setType(instrumentType);
    }
    String meterName = selectorSpec.getMeterName();
    if (meterName != null) {
        builder.setMeterName(meterName);
    }
    String meterVersion = selectorSpec.getMeterVersion();
    if (meterVersion != null) {
        builder.setMeterVersion(meterVersion);
    }
    String meterSchemaUrl = selectorSpec.getMeterSchemaUrl();
    if (meterSchemaUrl != null) {
        builder.setMeterSchemaUrl(meterSchemaUrl);
    }
    return builder.build();
}
Also used : InstrumentSelectorBuilder(io.opentelemetry.sdk.metrics.view.InstrumentSelectorBuilder) InstrumentType(io.opentelemetry.sdk.metrics.common.InstrumentType)

Example 2 with InstrumentType

use of io.opentelemetry.sdk.metrics.common.InstrumentType 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 3 with InstrumentType

use of io.opentelemetry.sdk.metrics.common.InstrumentType in project opentelemetry-java by open-telemetry.

the class DoubleSumAggregatorTest method mergeAndDiff.

@Test
void mergeAndDiff() {
    Attributes attributes = Attributes.builder().put("test", "value").build();
    ExemplarData exemplar = DoubleExemplarData.create(attributes, 2L, SpanContext.create("00000000000000000000000000000001", "0000000000000002", TraceFlags.getDefault(), TraceState.getDefault()), 1);
    List<ExemplarData> exemplars = Collections.singletonList(exemplar);
    List<ExemplarData> previousExemplars = Collections.singletonList(DoubleExemplarData.create(attributes, 1L, SpanContext.create("00000000000000000000000000000001", "0000000000000002", TraceFlags.getDefault(), TraceState.getDefault()), 2));
    for (InstrumentType instrumentType : InstrumentType.values()) {
        for (AggregationTemporality temporality : AggregationTemporality.values()) {
            DoubleSumAggregator aggregator = new DoubleSumAggregator(InstrumentDescriptor.create("name", "description", "unit", instrumentType, InstrumentValueType.LONG), ExemplarReservoir::noSamples);
            DoubleAccumulation merged = aggregator.merge(DoubleAccumulation.create(1.0d, previousExemplars), DoubleAccumulation.create(2.0d, exemplars));
            assertThat(merged.getValue()).withFailMessage("Invalid merge result for instrumentType %s, temporality %s: %s", instrumentType, temporality, merged).isEqualTo(3.0d);
            assertThat(merged.getExemplars()).containsExactly(exemplar);
            DoubleAccumulation diffed = aggregator.diff(DoubleAccumulation.create(1d), DoubleAccumulation.create(2d, exemplars));
            assertThat(diffed.getValue()).withFailMessage("Invalid diff result for instrumentType %s, temporality %s: %s", instrumentType, temporality, merged).isEqualTo(1d);
            assertThat(diffed.getExemplars()).containsExactly(exemplar);
        }
    }
}
Also used : DoubleExemplarData(io.opentelemetry.sdk.metrics.data.DoubleExemplarData) ExemplarData(io.opentelemetry.sdk.metrics.data.ExemplarData) Attributes(io.opentelemetry.api.common.Attributes) InstrumentType(io.opentelemetry.sdk.metrics.common.InstrumentType) ExemplarReservoir(io.opentelemetry.sdk.metrics.exemplar.ExemplarReservoir) AggregationTemporality(io.opentelemetry.sdk.metrics.data.AggregationTemporality) Test(org.junit.jupiter.api.Test)

Example 4 with InstrumentType

use of io.opentelemetry.sdk.metrics.common.InstrumentType in project opentelemetry-java by open-telemetry.

the class LongSumAggregatorTest method mergeAndDiff.

@Test
void mergeAndDiff() {
    ExemplarData exemplar = DoubleExemplarData.create(Attributes.empty(), 2L, SpanContext.create("00000000000000000000000000000001", "0000000000000002", TraceFlags.getDefault(), TraceState.getDefault()), 1);
    List<ExemplarData> exemplars = Collections.singletonList(exemplar);
    for (InstrumentType instrumentType : InstrumentType.values()) {
        for (AggregationTemporality temporality : AggregationTemporality.values()) {
            LongSumAggregator aggregator = new LongSumAggregator(InstrumentDescriptor.create("name", "description", "unit", instrumentType, InstrumentValueType.LONG), ExemplarReservoir::noSamples);
            LongAccumulation merged = aggregator.merge(LongAccumulation.create(1L), LongAccumulation.create(2L, exemplars));
            assertThat(merged.getValue()).withFailMessage("Invalid merge result for instrumentType %s, temporality %s: %s", instrumentType, temporality, merged).isEqualTo(3);
            assertThat(merged.getExemplars()).containsExactly(exemplar);
            LongAccumulation diffed = aggregator.diff(LongAccumulation.create(1L), LongAccumulation.create(2L, exemplars));
            assertThat(diffed.getValue()).withFailMessage("Invalid diff result for instrumentType %s, temporality %s: %s", instrumentType, temporality, merged).isEqualTo(1);
            assertThat(diffed.getExemplars()).containsExactly(exemplar);
        }
    }
}
Also used : DoubleExemplarData(io.opentelemetry.sdk.metrics.data.DoubleExemplarData) ExemplarData(io.opentelemetry.sdk.metrics.data.ExemplarData) InstrumentType(io.opentelemetry.sdk.metrics.common.InstrumentType) ExemplarReservoir(io.opentelemetry.sdk.metrics.exemplar.ExemplarReservoir) AggregationTemporality(io.opentelemetry.sdk.metrics.data.AggregationTemporality) Test(org.junit.jupiter.api.Test)

Aggregations

InstrumentType (io.opentelemetry.sdk.metrics.common.InstrumentType)4 AggregationTemporality (io.opentelemetry.sdk.metrics.data.AggregationTemporality)2 DoubleExemplarData (io.opentelemetry.sdk.metrics.data.DoubleExemplarData)2 ExemplarData (io.opentelemetry.sdk.metrics.data.ExemplarData)2 ExemplarReservoir (io.opentelemetry.sdk.metrics.exemplar.ExemplarReservoir)2 InstrumentSelectorBuilder (io.opentelemetry.sdk.metrics.view.InstrumentSelectorBuilder)2 Test (org.junit.jupiter.api.Test)2 Attributes (io.opentelemetry.api.common.Attributes)1 ConfigurationException (io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException)1 SdkMeterProviderBuilder (io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder)1 Aggregation (io.opentelemetry.sdk.metrics.view.Aggregation)1 InstrumentSelector (io.opentelemetry.sdk.metrics.view.InstrumentSelector)1 View (io.opentelemetry.sdk.metrics.view.View)1 ViewBuilder (io.opentelemetry.sdk.metrics.view.ViewBuilder)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1