Skip to content

Commit 8efade6

Browse files
authored
Extract sender parameters to config carrier class (#7151)
1 parent 4d34b53 commit 8efade6

File tree

10 files changed

+242
-188
lines changed

10 files changed

+242
-188
lines changed

exporters/common/src/main/java/io/opentelemetry/exporter/internal/grpc/GrpcExporterBuilder.java

+12-11
Original file line numberDiff line numberDiff line change
@@ -205,17 +205,18 @@ public GrpcExporter<T> build() {
205205
GrpcSenderProvider grpcSenderProvider = resolveGrpcSenderProvider();
206206
GrpcSender<T> grpcSender =
207207
grpcSenderProvider.createSender(
208-
endpoint,
209-
grpcEndpointPath,
210-
compressor,
211-
timeoutNanos,
212-
connectTimeoutNanos,
213-
headerSupplier,
214-
grpcChannel,
215-
grpcStubFactory,
216-
retryPolicy,
217-
isPlainHttp ? null : tlsConfigHelper.getSslContext(),
218-
isPlainHttp ? null : tlsConfigHelper.getTrustManager());
208+
GrpcSenderConfig.create(
209+
endpoint,
210+
grpcEndpointPath,
211+
compressor,
212+
timeoutNanos,
213+
connectTimeoutNanos,
214+
headerSupplier,
215+
grpcChannel,
216+
grpcStubFactory,
217+
retryPolicy,
218+
isPlainHttp ? null : tlsConfigHelper.getSslContext(),
219+
isPlainHttp ? null : tlsConfigHelper.getTrustManager()));
219220
LOGGER.log(Level.FINE, "Using GrpcSender: " + grpcSender.getClass().getName());
220221

221222
return new GrpcExporter<>(exporterName, type, grpcSender, meterProviderSupplier);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.exporter.internal.grpc;
7+
8+
import com.google.auto.value.AutoValue;
9+
import io.grpc.Channel;
10+
import io.opentelemetry.exporter.internal.compression.Compressor;
11+
import io.opentelemetry.exporter.internal.marshal.Marshaler;
12+
import io.opentelemetry.sdk.common.export.RetryPolicy;
13+
import java.net.URI;
14+
import java.util.List;
15+
import java.util.Map;
16+
import java.util.function.BiFunction;
17+
import java.util.function.Supplier;
18+
import javax.annotation.Nullable;
19+
import javax.annotation.concurrent.Immutable;
20+
import javax.net.ssl.SSLContext;
21+
import javax.net.ssl.X509TrustManager;
22+
23+
/**
24+
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
25+
* any time.
26+
*/
27+
@AutoValue
28+
@Immutable
29+
public abstract class GrpcSenderConfig<T extends Marshaler> {
30+
31+
@SuppressWarnings("TooManyParameters")
32+
public static <T extends Marshaler> GrpcSenderConfig<T> create(
33+
URI endpoint,
34+
String endpointPath,
35+
@Nullable Compressor compressor,
36+
long timeoutNanos,
37+
long connectTimeoutNanos,
38+
Supplier<Map<String, List<String>>> headersSupplier,
39+
@Nullable Object managedChannel,
40+
Supplier<BiFunction<Channel, String, MarshalerServiceStub<T, ?, ?>>> stubFactory,
41+
@Nullable RetryPolicy retryPolicy,
42+
@Nullable SSLContext sslContext,
43+
@Nullable X509TrustManager trustManager) {
44+
return new AutoValue_GrpcSenderConfig<>(
45+
endpoint,
46+
endpointPath,
47+
compressor,
48+
timeoutNanos,
49+
connectTimeoutNanos,
50+
headersSupplier,
51+
managedChannel,
52+
stubFactory,
53+
retryPolicy,
54+
sslContext,
55+
trustManager);
56+
}
57+
58+
public abstract URI getEndpoint();
59+
60+
public abstract String getEndpointPath();
61+
62+
@Nullable
63+
public abstract Compressor getCompressor();
64+
65+
public abstract long getTimeoutNanos();
66+
67+
public abstract long getConnectTimeoutNanos();
68+
69+
public abstract Supplier<Map<String, List<String>>> getHeadersSupplier();
70+
71+
@Nullable
72+
public abstract Object getManagedChannel();
73+
74+
public abstract Supplier<BiFunction<Channel, String, MarshalerServiceStub<T, ?, ?>>>
75+
getStubFactory();
76+
77+
@Nullable
78+
public abstract RetryPolicy getRetryPolicy();
79+
80+
@Nullable
81+
public abstract SSLContext getSslContext();
82+
83+
@Nullable
84+
public abstract X509TrustManager getTrustManager();
85+
}

exporters/common/src/main/java/io/opentelemetry/exporter/internal/grpc/GrpcSenderProvider.java

+2-25
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,7 @@
55

66
package io.opentelemetry.exporter.internal.grpc;
77

8-
import io.grpc.Channel;
9-
import io.opentelemetry.exporter.internal.compression.Compressor;
108
import io.opentelemetry.exporter.internal.marshal.Marshaler;
11-
import io.opentelemetry.sdk.common.export.RetryPolicy;
12-
import java.net.URI;
13-
import java.util.List;
14-
import java.util.Map;
15-
import java.util.function.BiFunction;
16-
import java.util.function.Supplier;
17-
import javax.annotation.Nullable;
18-
import javax.net.ssl.SSLContext;
19-
import javax.net.ssl.X509TrustManager;
209

2110
/**
2211
* A service provider interface (SPI) for providing {@link GrpcSender}s backed by different client
@@ -27,18 +16,6 @@
2716
*/
2817
public interface GrpcSenderProvider {
2918

30-
/** Returns a {@link GrpcSender} configured with the provided parameters. */
31-
@SuppressWarnings("TooManyParameters")
32-
<T extends Marshaler> GrpcSender<T> createSender(
33-
URI endpoint,
34-
String endpointPath,
35-
@Nullable Compressor compressor,
36-
long timeoutNanos,
37-
long connectTimeoutNanos,
38-
Supplier<Map<String, List<String>>> headersSupplier,
39-
@Nullable Object managedChannel,
40-
Supplier<BiFunction<Channel, String, MarshalerServiceStub<T, ?, ?>>> stubFactory,
41-
@Nullable RetryPolicy retryPolicy,
42-
@Nullable SSLContext sslContext,
43-
@Nullable X509TrustManager trustManager);
19+
/** Returns a {@link GrpcSender} configured with the provided config. */
20+
<T extends Marshaler> GrpcSender<T> createSender(GrpcSenderConfig<T> grpcSenderConfig);
4421
}

exporters/common/src/main/java/io/opentelemetry/exporter/internal/http/HttpExporterBuilder.java

+12-11
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,18 @@ public HttpExporter<T> build() {
187187
HttpSenderProvider httpSenderProvider = resolveHttpSenderProvider();
188188
HttpSender httpSender =
189189
httpSenderProvider.createSender(
190-
endpoint,
191-
compressor,
192-
exportAsJson,
193-
exportAsJson ? "application/json" : "application/x-protobuf",
194-
timeoutNanos,
195-
connectTimeoutNanos,
196-
headerSupplier,
197-
proxyOptions,
198-
retryPolicy,
199-
isPlainHttp ? null : tlsConfigHelper.getSslContext(),
200-
isPlainHttp ? null : tlsConfigHelper.getTrustManager());
190+
HttpSenderConfig.create(
191+
endpoint,
192+
compressor,
193+
exportAsJson,
194+
exportAsJson ? "application/json" : "application/x-protobuf",
195+
timeoutNanos,
196+
connectTimeoutNanos,
197+
headerSupplier,
198+
proxyOptions,
199+
retryPolicy,
200+
isPlainHttp ? null : tlsConfigHelper.getSslContext(),
201+
isPlainHttp ? null : tlsConfigHelper.getTrustManager()));
201202
LOGGER.log(Level.FINE, "Using HttpSender: " + httpSender.getClass().getName());
202203

203204
return new HttpExporter<>(exporterName, type, httpSender, meterProviderSupplier, exportAsJson);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.exporter.internal.http;
7+
8+
import com.google.auto.value.AutoValue;
9+
import io.opentelemetry.exporter.internal.compression.Compressor;
10+
import io.opentelemetry.sdk.common.export.ProxyOptions;
11+
import io.opentelemetry.sdk.common.export.RetryPolicy;
12+
import java.util.List;
13+
import java.util.Map;
14+
import java.util.function.Supplier;
15+
import javax.annotation.Nullable;
16+
import javax.annotation.concurrent.Immutable;
17+
import javax.net.ssl.SSLContext;
18+
import javax.net.ssl.X509TrustManager;
19+
20+
/**
21+
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
22+
* any time.
23+
*/
24+
@AutoValue
25+
@Immutable
26+
public abstract class HttpSenderConfig {
27+
28+
@SuppressWarnings("TooManyParameters")
29+
public static HttpSenderConfig create(
30+
String endpoint,
31+
@Nullable Compressor compressor,
32+
boolean exportAsJson,
33+
String contentType,
34+
long timeoutNanos,
35+
long connectTimeoutNanos,
36+
Supplier<Map<String, List<String>>> headerSupplier,
37+
@Nullable ProxyOptions proxyOptions,
38+
@Nullable RetryPolicy retryPolicy,
39+
@Nullable SSLContext sslContext,
40+
@Nullable X509TrustManager trustManager) {
41+
return new AutoValue_HttpSenderConfig(
42+
endpoint,
43+
compressor,
44+
exportAsJson,
45+
contentType,
46+
timeoutNanos,
47+
connectTimeoutNanos,
48+
headerSupplier,
49+
proxyOptions,
50+
retryPolicy,
51+
sslContext,
52+
trustManager);
53+
}
54+
55+
public abstract String getEndpoint();
56+
57+
@Nullable
58+
public abstract Compressor getCompressor();
59+
60+
public abstract boolean getExportAsJson();
61+
62+
public abstract String getContentType();
63+
64+
public abstract long getTimeoutNanos();
65+
66+
public abstract long getConnectTimeoutNanos();
67+
68+
public abstract Supplier<Map<String, List<String>>> getHeadersSupplier();
69+
70+
@Nullable
71+
public abstract ProxyOptions getProxyOptions();
72+
73+
@Nullable
74+
public abstract RetryPolicy getRetryPolicy();
75+
76+
@Nullable
77+
public abstract SSLContext getSslContext();
78+
79+
@Nullable
80+
public abstract X509TrustManager getTrustManager();
81+
}

exporters/common/src/main/java/io/opentelemetry/exporter/internal/http/HttpSenderProvider.java

+2-24
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,6 @@
55

66
package io.opentelemetry.exporter.internal.http;
77

8-
import io.opentelemetry.exporter.internal.compression.Compressor;
9-
import io.opentelemetry.sdk.common.export.ProxyOptions;
10-
import io.opentelemetry.sdk.common.export.RetryPolicy;
11-
import java.util.List;
12-
import java.util.Map;
13-
import java.util.function.Supplier;
14-
import javax.annotation.Nullable;
15-
import javax.net.ssl.SSLContext;
16-
import javax.net.ssl.X509TrustManager;
17-
188
/**
199
* A service provider interface (SPI) for providing {@link HttpSender}s backed by different HTTP
2010
* client libraries.
@@ -24,18 +14,6 @@
2414
*/
2515
public interface HttpSenderProvider {
2616

27-
/** Returns a {@link HttpSender} configured with the provided parameters. */
28-
@SuppressWarnings("TooManyParameters")
29-
HttpSender createSender(
30-
String endpoint,
31-
@Nullable Compressor compressor,
32-
boolean exportAsJson,
33-
String contentType,
34-
long timeoutNanos,
35-
long connectTimeout,
36-
Supplier<Map<String, List<String>>> headerSupplier,
37-
@Nullable ProxyOptions proxyOptions,
38-
@Nullable RetryPolicy retryPolicy,
39-
@Nullable SSLContext sslContext,
40-
@Nullable X509TrustManager trustManager);
17+
/** Returns a {@link HttpSender} configured with the provided config. */
18+
HttpSender createSender(HttpSenderConfig httpSenderConfig);
4119
}

exporters/sender/grpc-managed-channel/src/main/java/io/opentelemetry/exporter/sender/grpc/managedchannel/internal/UpstreamGrpcSenderProvider.java

+13-22
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,15 @@
1212
import io.grpc.ManagedChannelBuilder;
1313
import io.opentelemetry.exporter.internal.compression.Compressor;
1414
import io.opentelemetry.exporter.internal.grpc.GrpcSender;
15+
import io.opentelemetry.exporter.internal.grpc.GrpcSenderConfig;
1516
import io.opentelemetry.exporter.internal.grpc.GrpcSenderProvider;
1617
import io.opentelemetry.exporter.internal.grpc.MarshalerServiceStub;
1718
import io.opentelemetry.exporter.internal.marshal.Marshaler;
18-
import io.opentelemetry.sdk.common.export.RetryPolicy;
1919
import java.io.IOException;
2020
import java.io.OutputStream;
2121
import java.net.URI;
2222
import java.util.List;
2323
import java.util.Map;
24-
import java.util.function.BiFunction;
25-
import java.util.function.Supplier;
26-
import javax.annotation.Nullable;
27-
import javax.net.ssl.SSLContext;
28-
import javax.net.ssl.X509TrustManager;
2924

3025
/**
3126
* {@link GrpcSender} SPI implementation for {@link UpstreamGrpcSender}.
@@ -36,27 +31,17 @@
3631
public class UpstreamGrpcSenderProvider implements GrpcSenderProvider {
3732

3833
@Override
39-
public <T extends Marshaler> GrpcSender<T> createSender(
40-
URI endpoint,
41-
String endpointPath,
42-
@Nullable Compressor compressor,
43-
long timeoutNanos,
44-
long connectTimeoutNanos,
45-
Supplier<Map<String, List<String>>> headersSupplier,
46-
@Nullable Object managedChannel,
47-
Supplier<BiFunction<Channel, String, MarshalerServiceStub<T, ?, ?>>> stubFactory,
48-
@Nullable RetryPolicy retryPolicy,
49-
@Nullable SSLContext sslContext,
50-
@Nullable X509TrustManager trustManager) {
34+
public <T extends Marshaler> GrpcSender<T> createSender(GrpcSenderConfig<T> grpcSenderConfig) {
5135
boolean shutdownChannel = false;
36+
Object managedChannel = grpcSenderConfig.getManagedChannel();
5237
if (managedChannel == null) {
5338
// Shutdown the channel as part of the exporter shutdown sequence if
5439
shutdownChannel = true;
55-
managedChannel = minimalFallbackManagedChannel(endpoint);
40+
managedChannel = minimalFallbackManagedChannel(grpcSenderConfig.getEndpoint());
5641
}
5742

5843
String authorityOverride = null;
59-
Map<String, List<String>> headers = headersSupplier.get();
44+
Map<String, List<String>> headers = grpcSenderConfig.getHeadersSupplier().get();
6045
if (headers != null) {
6146
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
6247
if (entry.getKey().equals("host") && !entry.getValue().isEmpty()) {
@@ -66,6 +51,7 @@ public <T extends Marshaler> GrpcSender<T> createSender(
6651
}
6752

6853
String compression = Codec.Identity.NONE.getMessageEncoding();
54+
Compressor compressor = grpcSenderConfig.getCompressor();
6955
if (compressor != null) {
7056
CompressorRegistry.getDefaultInstance()
7157
.register(
@@ -84,12 +70,17 @@ public OutputStream compress(OutputStream os) throws IOException {
8470
}
8571

8672
MarshalerServiceStub<T, ?, ?> stub =
87-
stubFactory
73+
grpcSenderConfig
74+
.getStubFactory()
8875
.get()
8976
.apply((Channel) managedChannel, authorityOverride)
9077
.withCompression(compression);
9178

92-
return new UpstreamGrpcSender<>(stub, shutdownChannel, timeoutNanos, headersSupplier);
79+
return new UpstreamGrpcSender<>(
80+
stub,
81+
shutdownChannel,
82+
grpcSenderConfig.getTimeoutNanos(),
83+
grpcSenderConfig.getHeadersSupplier());
9384
}
9485

9586
/**

0 commit comments

Comments
 (0)