use of io.opentelemetry.sdk.metrics.internal.view.ViewBuilderImpl 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())));
}
Aggregations