Extract common OTLP config logic to utility function (#3525)

This commit is contained in:
jack-berg 2021-08-20 14:32:11 -05:00 committed by GitHub
parent 068bffba9e
commit 3555965a34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 91 additions and 87 deletions

View File

@ -5,6 +5,8 @@
package io.opentelemetry.sdk.autoconfigure;
import static io.opentelemetry.sdk.autoconfigure.OtlpConfigUtil.DATA_TYPE_METRICS;
import io.opentelemetry.exporter.logging.LoggingMetricExporter;
import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter;
import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporterBuilder;
@ -16,9 +18,6 @@ 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.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Collections;
import java.util.Map;
@ -92,47 +91,13 @@ final class MetricExporterConfiguration {
}
OtlpGrpcMetricExporterBuilder builder = OtlpGrpcMetricExporter.builder();
String endpoint = config.getString("otel.exporter.otlp.metrics.endpoint");
if (endpoint == null) {
endpoint = config.getString("otel.exporter.otlp.endpoint");
}
if (endpoint != null) {
builder.setEndpoint(endpoint);
}
Map<String, String> headers = config.getCommaSeparatedMap("otel.exporter.otlp.metrics.headers");
if (headers.isEmpty()) {
headers = config.getCommaSeparatedMap("otel.exporter.otlp.headers");
}
headers.forEach(builder::addHeader);
config.getCommaSeparatedMap("otel.exporter.otlp.headers").forEach(builder::addHeader);
Duration timeout = config.getDuration("otel.exporter.otlp.metrics.timeout");
if (timeout == null) {
timeout = config.getDuration("otel.exporter.otlp.timeout");
}
if (timeout != null) {
builder.setTimeout(timeout);
}
String certificate = config.getString("otel.exporter.otlp.metrics.certificate");
if (certificate == null) {
certificate = config.getString("otel.exporter.otlp.certificate");
}
if (certificate != null) {
Path path = Paths.get(certificate);
if (!Files.exists(path)) {
throw new ConfigurationException("Invalid OTLP certificate path: " + path);
}
final byte[] certificateBytes;
try {
certificateBytes = Files.readAllBytes(path);
} catch (IOException e) {
throw new ConfigurationException("Error reading OTLP certificate.", e);
}
builder.setTrustedCertificates(certificateBytes);
}
OtlpConfigUtil.configureOtlpExporterBuilder(
DATA_TYPE_METRICS,
config,
builder::setEndpoint,
builder::addHeader,
builder::setTimeout,
builder::setTrustedCertificates);
OtlpGrpcMetricExporter exporter = builder.build();

View File

@ -0,0 +1,73 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.sdk.autoconfigure;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
final class OtlpConfigUtil {
static final String DATA_TYPE_TRACES = "traces";
static final String DATA_TYPE_METRICS = "metrics";
static void configureOtlpExporterBuilder(
String dataType,
ConfigProperties config,
Consumer<String> setEndpoint,
BiConsumer<String, String> addHeader,
Consumer<Duration> setTimeout,
Consumer<byte[]> setTrustedCertificates) {
String endpoint = config.getString(String.format("otel.exporter.otlp.%s.endpoint", dataType));
if (endpoint == null) {
endpoint = config.getString("otel.exporter.otlp.endpoint");
}
if (endpoint != null) {
setEndpoint.accept(endpoint);
}
Map<String, String> headers =
config.getCommaSeparatedMap(String.format("otel.exporter.otlp.%s.headers", dataType));
if (headers.isEmpty()) {
headers = config.getCommaSeparatedMap("otel.exporter.otlp.headers");
}
headers.forEach(addHeader);
Duration timeout = config.getDuration(String.format("otel.exporter.otlp.%s.timeout", dataType));
if (timeout == null) {
timeout = config.getDuration("otel.exporter.otlp.timeout");
}
if (timeout != null) {
setTimeout.accept(timeout);
}
String certificate =
config.getString(String.format("otel.exporter.otlp.%s.certificate", dataType));
if (certificate == null) {
certificate = config.getString("otel.exporter.otlp.certificate");
}
if (certificate != null) {
Path path = Paths.get(certificate);
if (!Files.exists(path)) {
throw new ConfigurationException("Invalid OTLP certificate path: " + path);
}
final byte[] certificateBytes;
try {
certificateBytes = Files.readAllBytes(path);
} catch (IOException e) {
throw new ConfigurationException("Error reading OTLP certificate.", e);
}
setTrustedCertificates.accept(certificateBytes);
}
}
private OtlpConfigUtil() {}
}

View File

@ -5,6 +5,8 @@
package io.opentelemetry.sdk.autoconfigure;
import static io.opentelemetry.sdk.autoconfigure.OtlpConfigUtil.DATA_TYPE_TRACES;
import io.opentelemetry.exporter.jaeger.JaegerGrpcSpanExporter;
import io.opentelemetry.exporter.jaeger.JaegerGrpcSpanExporterBuilder;
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
@ -14,10 +16,6 @@ import io.opentelemetry.exporter.zipkin.ZipkinSpanExporter;
import io.opentelemetry.exporter.zipkin.ZipkinSpanExporterBuilder;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurableSpanExporterProvider;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Map;
import java.util.ServiceLoader;
@ -70,45 +68,13 @@ final class SpanExporterConfiguration {
"opentelemetry-exporter-otlp");
OtlpGrpcSpanExporterBuilder builder = OtlpGrpcSpanExporter.builder();
String endpoint = config.getString("otel.exporter.otlp.traces.endpoint");
if (endpoint == null) {
endpoint = config.getString("otel.exporter.otlp.endpoint");
}
if (endpoint != null) {
builder.setEndpoint(endpoint);
}
Map<String, String> headers = config.getCommaSeparatedMap("otel.exporter.otlp.traces.headers");
if (headers.isEmpty()) {
headers = config.getCommaSeparatedMap("otel.exporter.otlp.headers");
}
headers.forEach(builder::addHeader);
Duration timeout = config.getDuration("otel.exporter.otlp.traces.timeout");
if (timeout == null) {
timeout = config.getDuration("otel.exporter.otlp.timeout");
}
if (timeout != null) {
builder.setTimeout(timeout);
}
String certificate = config.getString("otel.exporter.otlp.traces.certificate");
if (certificate == null) {
certificate = config.getString("otel.exporter.otlp.certificate");
}
if (certificate != null) {
Path path = Paths.get(certificate);
if (!Files.exists(path)) {
throw new ConfigurationException("Invalid OTLP certificate path: " + path);
}
final byte[] certificateBytes;
try {
certificateBytes = Files.readAllBytes(path);
} catch (IOException e) {
throw new ConfigurationException("Error reading OTLP certificate.", e);
}
builder.setTrustedCertificates(certificateBytes);
}
OtlpConfigUtil.configureOtlpExporterBuilder(
DATA_TYPE_TRACES,
config,
builder::setEndpoint,
builder::addHeader,
builder::setTimeout,
builder::setTrustedCertificates);
return builder.build();
}