From 4cd802422363648f3ed93d30c6a942fcf3a02dd7 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Tue, 19 Mar 2024 11:15:40 +0200 Subject: [PATCH] Enable proxy configuration for OpenTelemetry exporters Closes: #39519 (cherry picked from commit 9e4eead073f1c9632d75e4fe4acea4f52b2b3acf) --- .../exporter/OtlpExporterTracesConfig.java | 30 +++++++++++ .../exporter/otlp/OTelExporterRecorder.java | 51 +++++++++++++++++++ .../otlp/OtlpExporterProviderTest.java | 26 ++++++++++ 3 files changed, 107 insertions(+) diff --git a/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/config/runtime/exporter/OtlpExporterTracesConfig.java b/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/config/runtime/exporter/OtlpExporterTracesConfig.java index 20aa1cbe976ff..d65ed16722eae 100644 --- a/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/config/runtime/exporter/OtlpExporterTracesConfig.java +++ b/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/config/runtime/exporter/OtlpExporterTracesConfig.java @@ -5,7 +5,9 @@ import java.time.Duration; import java.util.List; import java.util.Optional; +import java.util.OptionalInt; +import io.quarkus.runtime.annotations.ConfigDocDefault; import io.quarkus.runtime.annotations.ConfigGroup; import io.smallrye.config.WithDefault; import io.smallrye.config.WithName; @@ -72,6 +74,11 @@ public interface OtlpExporterTracesConfig { @WithName("trust-cert") TrustCert trustCert(); + /** + * Set proxy options + */ + ProxyConfig proxyOptions(); + interface KeyCert { /** * Comma-separated list of the path to the key files (Pem format). @@ -91,6 +98,29 @@ interface TrustCert { Optional> certs(); } + interface ProxyConfig { + /** + * Set proxy username. + */ + Optional username(); + + /** + * Set proxy password. + */ + Optional password(); + + /** + * Set proxy port. + */ + @ConfigDocDefault("3128") + OptionalInt port(); + + /** + * Set proxy host. + */ + Optional host(); + } + class Protocol { public static final String GRPC = "grpc"; public static final String HTTP_PROTOBUF = "http/protobuf"; diff --git a/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/exporter/otlp/OTelExporterRecorder.java b/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/exporter/otlp/OTelExporterRecorder.java index 8757df5a2dcdd..c572dd72aba3f 100644 --- a/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/exporter/otlp/OTelExporterRecorder.java +++ b/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/exporter/otlp/OTelExporterRecorder.java @@ -8,6 +8,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; @@ -35,6 +36,7 @@ import io.vertx.core.net.KeyCertOptions; import io.vertx.core.net.PemKeyCertOptions; import io.vertx.core.net.PemTrustOptions; +import io.vertx.core.net.ProxyOptions; @SuppressWarnings("deprecation") @Recorder @@ -201,6 +203,7 @@ public HttpClientOptionsConsumer(OtlpExporterTracesConfig tracesConfig, URI base @Override public void accept(HttpClientOptions options) { configureTLS(options); + configureProxyOptions(options); } private void configureTLS(HttpClientOptions options) { @@ -218,6 +221,54 @@ private void configureTLS(HttpClientOptions options) { } } + private void configureProxyOptions(HttpClientOptions options) { + var proxyConfig = tracesConfig.proxyOptions(); + Optional proxyHost = proxyConfig.host(); + if (proxyHost.isPresent()) { + ProxyOptions proxyOptions = new ProxyOptions() + .setHost(proxyHost.get()); + if (proxyConfig.port().isPresent()) { + proxyOptions.setPort(proxyConfig.port().getAsInt()); + } + if (proxyConfig.username().isPresent()) { + proxyOptions.setUsername(proxyConfig.username().get()); + } + if (proxyConfig.password().isPresent()) { + proxyOptions.setPassword(proxyConfig.password().get()); + } + options.setProxyOptions(proxyOptions); + } else { + configureProxyOptionsFromJDKSysProps(options); + } + } + + private void configureProxyOptionsFromJDKSysProps(HttpClientOptions options) { + String proxyHost = options.isSsl() + ? System.getProperty("https.proxyHost", "none") + : System.getProperty("http.proxyHost", "none"); + String proxyPortAsString = options.isSsl() + ? System.getProperty("https.proxyPort", "443") + : System.getProperty("http.proxyPort", "80"); + int proxyPort = Integer.parseInt(proxyPortAsString); + + if (!"none".equals(proxyHost)) { + ProxyOptions proxyOptions = new ProxyOptions().setHost(proxyHost).setPort(proxyPort); + String proxyUser = options.isSsl() + ? System.getProperty("https.proxyUser") + : System.getProperty("http.proxyUser"); + if (proxyUser != null && !proxyUser.isBlank()) { + proxyOptions.setUsername(proxyUser); + } + String proxyPassword = options.isSsl() + ? System.getProperty("https.proxyPassword") + : System.getProperty("http.proxyPassword"); + if (proxyPassword != null && !proxyPassword.isBlank()) { + proxyOptions.setPassword(proxyPassword); + } + options.setProxyOptions(proxyOptions); + } + } + private KeyCertOptions toPemKeyCertOptions() { OtlpExporterTracesConfig.KeyCert keyCert = tracesConfig.keyCert(); if (keyCert.certs().isEmpty() && keyCert.keys().isEmpty()) { diff --git a/extensions/opentelemetry/runtime/src/test/java/io/quarkus/opentelemetry/runtime/exporter/otlp/OtlpExporterProviderTest.java b/extensions/opentelemetry/runtime/src/test/java/io/quarkus/opentelemetry/runtime/exporter/otlp/OtlpExporterProviderTest.java index f7cc1d9451645..d9c749311967a 100644 --- a/extensions/opentelemetry/runtime/src/test/java/io/quarkus/opentelemetry/runtime/exporter/otlp/OtlpExporterProviderTest.java +++ b/extensions/opentelemetry/runtime/src/test/java/io/quarkus/opentelemetry/runtime/exporter/otlp/OtlpExporterProviderTest.java @@ -6,6 +6,7 @@ import java.time.Duration; import java.util.List; import java.util.Optional; +import java.util.OptionalInt; import org.junit.jupiter.api.Test; @@ -134,6 +135,31 @@ public Optional> certs() { } }; } + + @Override + public ProxyConfig proxyOptions() { + return new ProxyConfig() { + @Override + public Optional username() { + return Optional.empty(); + } + + @Override + public Optional password() { + return Optional.empty(); + } + + @Override + public OptionalInt port() { + return OptionalInt.empty(); + } + + @Override + public Optional host() { + return Optional.empty(); + } + }; + } }; } };