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);
}
}
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)));
}
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());
}
Aggregations