enable auto-configuring the logging metrics exporter (#2580)
* enable auto-configuring the logging metrics exporter * fix the class name for the metrics exporter * extract a method for configuring the IMR
This commit is contained in:
parent
2ba472d0e5
commit
90574a3134
|
|
@ -5,12 +5,14 @@
|
|||
|
||||
package io.opentelemetry.sdk.autoconfigure;
|
||||
|
||||
import io.opentelemetry.exporter.logging.LoggingMetricExporter;
|
||||
import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter;
|
||||
import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporterBuilder;
|
||||
import io.opentelemetry.exporter.prometheus.PrometheusCollector;
|
||||
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
|
||||
import io.opentelemetry.sdk.metrics.export.IntervalMetricReader;
|
||||
import io.opentelemetry.sdk.metrics.export.IntervalMetricReaderBuilder;
|
||||
import io.opentelemetry.sdk.metrics.export.MetricExporter;
|
||||
import io.prometheus.client.exporter.HTTPServer;
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
|
|
@ -28,11 +30,24 @@ final class MetricExporterConfiguration {
|
|||
case "prometheus":
|
||||
configurePrometheusMetrics(config, meterProvider);
|
||||
return;
|
||||
case "logging":
|
||||
ClasspathUtil.checkClassExists(
|
||||
"io.opentelemetry.exporter.logging.LoggingMetricExporter",
|
||||
"Logging Metrics Exporter",
|
||||
"opentelemetry-exporter-logging");
|
||||
configureLoggingMetrics(config, meterProvider);
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private static void configureLoggingMetrics(
|
||||
ConfigProperties config, SdkMeterProvider meterProvider) {
|
||||
MetricExporter exporter = new LoggingMetricExporter();
|
||||
configureIntervalMetricReader(config, meterProvider, exporter);
|
||||
}
|
||||
|
||||
// Visible for testing
|
||||
static OtlpGrpcMetricExporter configureOtlpMetrics(
|
||||
ConfigProperties config, SdkMeterProvider meterProvider) {
|
||||
|
|
@ -56,6 +71,13 @@ final class MetricExporterConfiguration {
|
|||
|
||||
OtlpGrpcMetricExporter exporter = builder.build();
|
||||
|
||||
configureIntervalMetricReader(config, meterProvider, exporter);
|
||||
|
||||
return exporter;
|
||||
}
|
||||
|
||||
private static void configureIntervalMetricReader(
|
||||
ConfigProperties config, SdkMeterProvider meterProvider, MetricExporter exporter) {
|
||||
IntervalMetricReaderBuilder readerBuilder =
|
||||
IntervalMetricReader.builder()
|
||||
.setMetricProducers(Collections.singleton(meterProvider))
|
||||
|
|
@ -66,8 +88,6 @@ final class MetricExporterConfiguration {
|
|||
}
|
||||
IntervalMetricReader reader = readerBuilder.build();
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(reader::shutdown));
|
||||
|
||||
return exporter;
|
||||
}
|
||||
|
||||
private static void configurePrometheusMetrics(
|
||||
|
|
|
|||
|
|
@ -51,6 +51,18 @@ class NotOnClasspathTest {
|
|||
+ "classpath");
|
||||
}
|
||||
|
||||
@Test
|
||||
void logging_metrics() {
|
||||
assertThatThrownBy(
|
||||
() ->
|
||||
MetricExporterConfiguration.configureExporter(
|
||||
"logging", EMPTY, SdkMeterProvider.builder().build()))
|
||||
.isInstanceOf(ConfigurationException.class)
|
||||
.hasMessageContaining(
|
||||
"Logging Metrics Exporter enabled but opentelemetry-exporter-logging not found on "
|
||||
+ "classpath");
|
||||
}
|
||||
|
||||
@Test
|
||||
void otlpMetrics() {
|
||||
assertThatThrownBy(
|
||||
|
|
|
|||
Loading…
Reference in New Issue