use of io.opentelemetry.api.metrics.MeterProvider in project opentelemetry-java by open-telemetry.
the class SdkMeterProviderTest method sdkMeterProvider_supportsMultipleCollectorsCumulative.
@Test
void sdkMeterProvider_supportsMultipleCollectorsCumulative() {
InMemoryMetricReader collector1 = InMemoryMetricReader.create();
InMemoryMetricReader collector2 = InMemoryMetricReader.create();
SdkMeterProvider meterProvider = sdkMeterProviderBuilder.registerMetricReader(collector1).registerMetricReader(collector2).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().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now()).hasValue(1)));
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().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now()).hasValue(2)));
// Make sure Collector 1 sees the same point as 2
assertThat(collector1.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasName("testSum").hasLongSum().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now()).hasValue(2)));
}
use of io.opentelemetry.api.metrics.MeterProvider in project opentelemetry-java by open-telemetry.
the class SpanExporterConfiguration method configureOtlp.
// Visible for testing
static SpanExporter configureOtlp(ConfigProperties config, MeterProvider meterProvider) {
String protocol = OtlpConfigUtil.getOtlpProtocol(DATA_TYPE_TRACES, config);
if (protocol.equals(PROTOCOL_HTTP_PROTOBUF)) {
ClasspathUtil.checkClassExists("io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter", "OTLP HTTP Trace Exporter", "opentelemetry-exporter-otlp-http-trace");
OtlpHttpSpanExporterBuilder builder = OtlpHttpSpanExporter.builder();
OtlpConfigUtil.configureOtlpExporterBuilder(DATA_TYPE_TRACES, config, builder::setEndpoint, builder::addHeader, builder::setCompression, builder::setTimeout, builder::setTrustedCertificates, retryPolicy -> RetryUtil.setRetryPolicyOnDelegate(builder, retryPolicy));
builder.setMeterProvider(meterProvider);
return builder.build();
} else if (protocol.equals(PROTOCOL_GRPC)) {
ClasspathUtil.checkClassExists("io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter", "OTLP gRPC Trace Exporter", "opentelemetry-exporter-otlp");
OtlpGrpcSpanExporterBuilder builder = OtlpGrpcSpanExporter.builder();
OtlpConfigUtil.configureOtlpExporterBuilder(DATA_TYPE_TRACES, config, builder::setEndpoint, builder::addHeader, builder::setCompression, builder::setTimeout, builder::setTrustedCertificates, retryPolicy -> RetryUtil.setRetryPolicyOnDelegate(builder, retryPolicy));
builder.setMeterProvider(meterProvider);
return builder.build();
} else {
throw new ConfigurationException("Unsupported OTLP traces protocol: " + protocol);
}
}
use of io.opentelemetry.api.metrics.MeterProvider in project opentelemetry-java by open-telemetry.
the class SpanExporterConfiguration method configureSpanExporters.
// Visible for testing
static Map<String, SpanExporter> configureSpanExporters(ConfigProperties config, ClassLoader serviceClassLoader, MeterProvider meterProvider, BiFunction<? super SpanExporter, ConfigProperties, ? extends SpanExporter> spanExporterCustomizer) {
Set<String> exporterNames = DefaultConfigProperties.getSet(config, "otel.traces.exporter");
if (exporterNames.contains(EXPORTER_NONE)) {
if (exporterNames.size() > 1) {
throw new ConfigurationException("otel.traces.exporter contains " + EXPORTER_NONE + " along with other exporters");
}
SpanExporter noop = SpanExporter.composite();
SpanExporter customized = spanExporterCustomizer.apply(noop, config);
if (customized == noop) {
return Collections.emptyMap();
}
return Collections.singletonMap(EXPORTER_NONE, customized);
}
if (exporterNames.isEmpty()) {
exporterNames = Collections.singleton("otlp");
}
NamedSpiManager<SpanExporter> spiExportersManager = SpiUtil.loadConfigurable(ConfigurableSpanExporterProvider.class, ConfigurableSpanExporterProvider::getName, ConfigurableSpanExporterProvider::createExporter, config, serviceClassLoader);
return exporterNames.stream().collect(toMap(Function.identity(), exporterName -> spanExporterCustomizer.apply(configureExporter(exporterName, config, spiExportersManager, meterProvider), config)));
}
use of io.opentelemetry.api.metrics.MeterProvider in project opentelemetry-java by open-telemetry.
the class LogExporterConfiguration method configureOtlpLogs.
// Visible for testing
@Nullable
static LogExporter configureOtlpLogs(ConfigProperties config, MeterProvider meterProvider) {
String protocol = OtlpConfigUtil.getOtlpProtocol(DATA_TYPE_LOGS, config);
if (protocol.equals(PROTOCOL_HTTP_PROTOBUF)) {
try {
ClasspathUtil.checkClassExists("io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogExporter", "OTLP HTTP Log Exporter", "opentelemetry-exporter-otlp-http-logs");
} catch (ConfigurationException e) {
// Squash this for now until logs are stable
return null;
}
OtlpHttpLogExporterBuilder builder = OtlpHttpLogExporter.builder();
OtlpConfigUtil.configureOtlpExporterBuilder(DATA_TYPE_LOGS, config, builder::setEndpoint, builder::addHeader, builder::setCompression, builder::setTimeout, builder::setTrustedCertificates, retryPolicy -> RetryUtil.setRetryPolicyOnDelegate(builder, retryPolicy));
builder.setMeterProvider(meterProvider);
return builder.build();
} else if (protocol.equals(PROTOCOL_GRPC)) {
try {
ClasspathUtil.checkClassExists("io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogExporter", "OTLP gRPC Log Exporter", "opentelemetry-exporter-otlp-logs");
} catch (ConfigurationException e) {
// Squash this for now until logs are stable
return null;
}
OtlpGrpcLogExporterBuilder builder = OtlpGrpcLogExporter.builder();
OtlpConfigUtil.configureOtlpExporterBuilder(DATA_TYPE_LOGS, config, builder::setEndpoint, builder::addHeader, builder::setCompression, builder::setTimeout, builder::setTrustedCertificates, retryPolicy -> RetryUtil.setRetryPolicyOnDelegate(builder, retryPolicy));
builder.setMeterProvider(meterProvider);
return builder.build();
} else {
throw new ConfigurationException("Unsupported OTLP logs protocol: " + protocol);
}
}
use of io.opentelemetry.api.metrics.MeterProvider 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)));
}
Aggregations