Skip to content

Commit

Permalink
Adding proxy functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
marcschumacher committed Feb 7, 2024
1 parent 3e7302e commit db4da92
Show file tree
Hide file tree
Showing 34 changed files with 206 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class GrpcExporterBuilder<T extends Marshaler> {
grpcStubFactory;

private long timeoutNanos;
@Nullable private String proxyHost;
@Nullable private Integer proxyPort;
private URI endpoint;
@Nullable private Compressor compressor;
private final Map<String, String> constantHeaders = new HashMap<>();
Expand Down Expand Up @@ -92,6 +94,12 @@ public GrpcExporterBuilder<T> setTimeout(Duration timeout) {
return setTimeout(timeout.toNanos(), TimeUnit.NANOSECONDS);
}

public GrpcExporterBuilder<T> setProxy(String proxyHost, Integer proxyPort) {
this.proxyHost = proxyHost;
this.proxyPort = proxyPort;
return this;
}

public GrpcExporterBuilder<T> setEndpoint(String endpoint) {
this.endpoint = ExporterBuilderUtil.validateEndpoint(endpoint);
return this;
Expand Down Expand Up @@ -194,6 +202,8 @@ public GrpcExporter<T> build() {
compressor,
timeoutNanos,
headerSupplier,
proxyHost,
proxyPort,
grpcChannel,
grpcStubFactory,
retryPolicy,
Expand All @@ -214,6 +224,8 @@ public String toString(boolean includePrefixAndSuffix) {
joiner.add("endpoint=" + endpoint.toString());
joiner.add("endpointPath=" + grpcEndpointPath);
joiner.add("timeoutNanos=" + timeoutNanos);
joiner.add("proxyHost=" + proxyHost);
joiner.add("proxyPort=" + proxyPort);
joiner.add(
"compressorEncoding="
+ Optional.ofNullable(compressor).map(Compressor::getEncoding).orElse(null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ <T extends Marshaler> GrpcSender<T> createSender(
@Nullable Compressor compressor,
long timeoutNanos,
Supplier<Map<String, List<String>>> headersSupplier,
@Nullable String proxyHost,
@Nullable Integer proxyPort,
@Nullable Object managedChannel,
Supplier<BiFunction<Channel, String, MarshalerServiceStub<T, ?, ?>>> stubFactory,
@Nullable RetryPolicy retryPolicy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public final class HttpExporterBuilder<T extends Marshaler> {
private long timeoutNanos = TimeUnit.SECONDS.toNanos(DEFAULT_TIMEOUT_SECS);
@Nullable private Compressor compressor;
private long connectTimeoutNanos = TimeUnit.SECONDS.toNanos(DEFAULT_CONNECT_TIMEOUT_SECS);
@Nullable
private String proxyHost;
@Nullable
private Integer proxyPort;
private boolean exportAsJson = false;
private final Map<String, String> constantHeaders = new HashMap<>();
private Supplier<Map<String, String>> headerSupplier = Collections::emptyMap;
Expand Down Expand Up @@ -131,6 +135,12 @@ public HttpExporterBuilder<T> setRetryPolicy(RetryPolicy retryPolicy) {
return this;
}

public HttpExporterBuilder<T> setProxy(String host, int port) {
this.proxyHost = host;
this.proxyPort = port;
return this;
}

public HttpExporterBuilder<T> exportAsJson() {
this.exportAsJson = true;
return this;
Expand Down Expand Up @@ -187,6 +197,8 @@ public HttpExporter<T> build() {
timeoutNanos,
connectTimeoutNanos,
headerSupplier,
proxyHost,
proxyPort,
authenticator,
retryPolicy,
tlsConfigHelper.getSslContext(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ HttpSender createSender(
long timeoutNanos,
long connectTimeout,
Supplier<Map<String, List<String>>> headerSupplier,
@Nullable String proxyHost,
@Nullable Integer proxyPort,
@Nullable Authenticator authenticator,
@Nullable RetryPolicy retryPolicy,
@Nullable SSLContext sslContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public void setUp() {
Collections::emptyMap,
null,
null,
null,
null,
null),
MeterProvider::noop);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ public OtlpHttpLogRecordExporterBuilder setRetryPolicy(RetryPolicy retryPolicy)
return this;
}

/**
* Sets the proxy to be used.
*/
public OtlpHttpLogRecordExporterBuilder setProxy(String proxyHost, int proxyPort) {
requireNonNull(proxyHost, "proxyHost");
delegate.setProxy(proxyHost, proxyPort);
return this;
}

/**
* Sets the {@link MeterProvider} to use to collect metrics related to export. If not set, uses
* {@link GlobalOpenTelemetry#getMeterProvider()}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@ public OtlpHttpMetricExporterBuilder setRetryPolicy(RetryPolicy retryPolicy) {
return this;
}

/**
* Sets the proxy to be used.
*/
public OtlpHttpMetricExporterBuilder setProxy(String proxyHost, int proxyPort) {
requireNonNull(proxyHost, "proxyHost");
delegate.setProxy(proxyHost, proxyPort);
return this;
}

OtlpHttpMetricExporterBuilder exportAsJson() {
delegate.exportAsJson();
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ public OtlpHttpSpanExporterBuilder setRetryPolicy(RetryPolicy retryPolicy) {
return this;
}

/**
* Sets the proxy to be used.
*/
public OtlpHttpSpanExporterBuilder setProxy(String proxyHost, int proxyPort) {
requireNonNull(proxyHost, "proxyHost");
delegate.setProxy(proxyHost, proxyPort);
return this;
}

/**
* Sets the {@link MeterProvider} to use to collect metrics related to export. If not set, uses
* {@link GlobalOpenTelemetry#getMeterProvider()}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public static String getOtlpProtocol(String dataType, ConfigProperties config) {
}

/** Invoke the setters with the OTLP configuration for the {@code dataType}. */
@SuppressWarnings("TooManyParameters")
public static void configureOtlpExporterBuilder(
String dataType,
ConfigProperties config,
Expand All @@ -62,7 +63,8 @@ public static void configureOtlpExporterBuilder(
Consumer<Duration> setTimeout,
Consumer<byte[]> setTrustedCertificates,
BiConsumer<byte[], byte[]> setClientTls,
Consumer<RetryPolicy> setRetryPolicy) {
Consumer<RetryPolicy> setRetryPolicy,
BiConsumer<String,Integer> setProxy) {
String protocol = getOtlpProtocol(dataType, config);
boolean isHttpProtobuf = protocol.equals(PROTOCOL_HTTP_PROTOBUF);
URL endpoint =
Expand Down Expand Up @@ -152,6 +154,12 @@ public static void configureOtlpExporterBuilder(
if (retryEnabled) {
setRetryPolicy.accept(RetryPolicy.getDefault());
}

String proxyHost = config.getString("otel.exporter.otlp." + dataType + ".proxy.host");
Integer proxyPort = config.getInt("otel.exporter.otlp." + dataType + ".proxy.port");
if (proxyHost != null && proxyPort != null) {
setProxy.accept(proxyHost, proxyPort);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public LogRecordExporter createExporter(ConfigProperties config) {
builder::setTimeout,
builder::setTrustedCertificates,
builder::setClientTls,
builder::setRetryPolicy);
builder::setRetryPolicy,
builder::setProxy);
builder.setMeterProvider(meterProviderRef::get);

return builder.build();
Expand All @@ -67,7 +68,8 @@ public LogRecordExporter createExporter(ConfigProperties config) {
builder::setTimeout,
builder::setTrustedCertificates,
builder::setClientTls,
builder::setRetryPolicy);
builder::setRetryPolicy,
builder::setProxy);
builder.setMeterProvider(meterProviderRef::get);

return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public MetricExporter createExporter(ConfigProperties config) {
builder::setTimeout,
builder::setTrustedCertificates,
builder::setClientTls,
builder::setRetryPolicy);
builder::setRetryPolicy,
builder::setProxy);
OtlpConfigUtil.configureOtlpAggregationTemporality(
config, builder::setAggregationTemporalitySelector);
OtlpConfigUtil.configureOtlpHistogramDefaultAggregation(
Expand All @@ -62,7 +63,8 @@ public MetricExporter createExporter(ConfigProperties config) {
builder::setTimeout,
builder::setTrustedCertificates,
builder::setClientTls,
builder::setRetryPolicy);
builder::setRetryPolicy,
builder::setProxy);
OtlpConfigUtil.configureOtlpAggregationTemporality(
config, builder::setAggregationTemporalitySelector);
OtlpConfigUtil.configureOtlpHistogramDefaultAggregation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public SpanExporter createExporter(ConfigProperties config) {
builder::setTimeout,
builder::setTrustedCertificates,
builder::setClientTls,
builder::setRetryPolicy);
builder::setRetryPolicy,
builder::setProxy);
builder.setMeterProvider(meterProviderRef::get);

return builder.build();
Expand All @@ -66,7 +67,8 @@ public SpanExporter createExporter(ConfigProperties config) {
builder::setTimeout,
builder::setTrustedCertificates,
builder::setClientTls,
builder::setRetryPolicy);
builder::setRetryPolicy,
builder::setProxy);
builder.setMeterProvider(meterProviderRef::get);

return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ public OtlpGrpcLogRecordExporterBuilder setRetryPolicy(RetryPolicy retryPolicy)
return this;
}

/**
* Sets the proxy to be used.
*/
public OtlpGrpcLogRecordExporterBuilder setProxy(String proxyHost, Integer proxyPort) {
requireNonNull(proxyHost, "proxyHost");
requireNonNull(proxyPort, "proxyPort");
delegate.setProxy(proxyHost, proxyPort);
return this;
}

/**
* Sets the {@link MeterProvider} to use to collect metrics related to export. If not set, uses
* {@link GlobalOpenTelemetry#getMeterProvider()}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,16 @@ public OtlpGrpcMetricExporterBuilder setRetryPolicy(RetryPolicy retryPolicy) {
return this;
}

/**
* Sets the proxy to be used.
*/
public OtlpGrpcMetricExporterBuilder setProxy(String proxyHost, Integer proxyPort) {
requireNonNull(proxyHost, "proxyHost");
requireNonNull(proxyPort, "proxyPort");
delegate.setProxy(proxyHost, proxyPort);
return this;
}

/**
* Constructs a new instance of the exporter based on the builder's values.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ public OtlpGrpcSpanExporterBuilder setRetryPolicy(RetryPolicy retryPolicy) {
return this;
}

/**
* Sets the proxy to be used.
*/
public OtlpGrpcSpanExporterBuilder setProxy(String proxyHost, Integer proxyPort) {
requireNonNull(proxyHost, "proxyHost");
requireNonNull(proxyPort, "proxyPort");
delegate.setProxy(proxyHost, proxyPort);
return this;
}

/**
* Sets the {@link MeterProvider} to use to collect metrics related to export. If not set, uses
* {@link GlobalOpenTelemetry#getMeterProvider()}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ private static String configureEndpoint(String dataType, Map<String, String> pro
value -> {},
value -> {},
(value1, value2) -> {},
value -> {});
value -> {},
(value1, value2) -> {});

return endpoint.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ public TelemetryExporterBuilder<LogRecordData> setRetryPolicy(RetryPolicy retryP
return this;
}

@Override
public TelemetryExporterBuilder<LogRecordData> setProxy(String proxyHost, int proxyPort) {
builder.setProxy(proxyHost, proxyPort);
return this;
}

@Override
@SuppressWarnings("deprecation") // testing deprecated functionality
public TelemetryExporterBuilder<LogRecordData> setChannel(Object channel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ public TelemetryExporterBuilder<MetricData> setRetryPolicy(RetryPolicy retryPoli
return this;
}

@Override
public TelemetryExporterBuilder<MetricData> setProxy(String proxyHost, int proxyPort) {
builder.setProxy(proxyHost, proxyPort);
return this;
}

@Override
@SuppressWarnings("deprecation") // testing deprecated functionality
public TelemetryExporterBuilder<MetricData> setChannel(Object channel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ public TelemetryExporterBuilder<SpanData> setRetryPolicy(RetryPolicy retryPolicy
return this;
}

@Override
public TelemetryExporterBuilder<SpanData> setProxy(String proxyHost, int proxyPort) {
builder.setProxy(proxyHost, proxyPort);
return this;
}

@Override
@SuppressWarnings("deprecation") // testing deprecated functionality
public TelemetryExporterBuilder<SpanData> setChannel(Object channel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ public TelemetryExporterBuilder<LogRecordData> setRetryPolicy(RetryPolicy retryP
return this;
}

@Override
public TelemetryExporterBuilder<LogRecordData> setProxy(String proxyHost, int proxyPort) {
builder.setProxy(proxyHost, proxyPort);
return this;
}

@Override
public TelemetryExporterBuilder<LogRecordData> setChannel(Object channel) {
throw new UnsupportedOperationException("Not implemented");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ public TelemetryExporterBuilder<MetricData> setRetryPolicy(RetryPolicy retryPoli
return this;
}

@Override
public TelemetryExporterBuilder<MetricData> setProxy(String proxyHost, int proxyPort) {
builder.setProxy(proxyHost, proxyPort);
return this;
}

@Override
public TelemetryExporterBuilder<MetricData> setChannel(Object channel) {
throw new UnsupportedOperationException("Not implemented");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ public TelemetryExporterBuilder<SpanData> setRetryPolicy(RetryPolicy retryPolicy
return this;
}

@Override
public TelemetryExporterBuilder<SpanData> setProxy(String proxyHost, int proxyPort) {
builder.setProxy(proxyHost, proxyPort);
return this;
}

@Override
public TelemetryExporterBuilder<SpanData> setChannel(Object channel) {
throw new UnsupportedOperationException("Not implemented");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ public TelemetryExporterBuilder<T> setRetryPolicy(RetryPolicy retryPolicy) {
return this;
}

@Override
public TelemetryExporterBuilder<T> setProxy(String proxyHost, int proxyPort) {
delegate.setProxy(proxyHost, proxyPort);
return this;
}

@Override
public TelemetryExporterBuilder<T> setChannel(Object channel) {
throw new UnsupportedOperationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ static TelemetryExporterBuilder<LogRecordData> wrap(OtlpGrpcLogRecordExporterBui

TelemetryExporterBuilder<T> setRetryPolicy(RetryPolicy retryPolicy);

TelemetryExporterBuilder<T> setProxy(String proxyHost, int proxyPort);

TelemetryExporterBuilder<T> setChannel(Object channel);

TelemetryExporter<T> build();
Expand Down
Loading

0 comments on commit db4da92

Please sign in to comment.