Skip to content

Commit

Permalink
Enable proxy configuration for OpenTelemetry exporters
Browse files Browse the repository at this point in the history
Closes: #39519
(cherry picked from commit 9e4eead)
  • Loading branch information
geoand authored and gsmet committed Mar 19, 2024
1 parent f82d815 commit 4cd8024
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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).
Expand All @@ -91,6 +98,29 @@ interface TrustCert {
Optional<List<String>> certs();
}

interface ProxyConfig {
/**
* Set proxy username.
*/
Optional<String> username();

/**
* Set proxy password.
*/
Optional<String> password();

/**
* Set proxy port.
*/
@ConfigDocDefault("3128")
OptionalInt port();

/**
* Set proxy host.
*/
Optional<String> host();
}

class Protocol {
public static final String GRPC = "grpc";
public static final String HTTP_PROTOBUF = "http/protobuf";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -218,6 +221,54 @@ private void configureTLS(HttpClientOptions options) {
}
}

private void configureProxyOptions(HttpClientOptions options) {
var proxyConfig = tracesConfig.proxyOptions();
Optional<String> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -134,6 +135,31 @@ public Optional<List<String>> certs() {
}
};
}

@Override
public ProxyConfig proxyOptions() {
return new ProxyConfig() {
@Override
public Optional<String> username() {
return Optional.empty();
}

@Override
public Optional<String> password() {
return Optional.empty();
}

@Override
public OptionalInt port() {
return OptionalInt.empty();
}

@Override
public Optional<String> host() {
return Optional.empty();
}
};
}
};
}
};
Expand Down

0 comments on commit 4cd8024

Please sign in to comment.