org.apache.rocketmq.shaded.io.opentelemetry.exporter.internal.grpc.OkHttpGrpcExporterBuilder Maven / Gradle / Ivy
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.apache.rocketmq.shaded.io.opentelemetry.exporter.internal.grpc;
import org.apache.rocketmq.shaded.io.grpc.ManagedChannel;
import org.apache.rocketmq.shaded.io.opentelemetry.api.metrics.MeterProvider;
import org.apache.rocketmq.shaded.io.opentelemetry.exporter.internal.ExporterBuilderUtil;
import org.apache.rocketmq.shaded.io.opentelemetry.exporter.internal.TlsUtil;
import org.apache.rocketmq.shaded.io.opentelemetry.exporter.internal.marshal.Marshaler;
import org.apache.rocketmq.shaded.io.opentelemetry.exporter.internal.okhttp.OkHttpUtil;
import org.apache.rocketmq.shaded.io.opentelemetry.exporter.internal.retry.RetryInterceptor;
import org.apache.rocketmq.shaded.io.opentelemetry.exporter.internal.retry.RetryPolicy;
import java.net.URI;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import javax.net.ssl.SSLException;
import javax.net.ssl.X509KeyManager;
import javax.net.ssl.X509TrustManager;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
/**
* A builder for {@link OkHttpGrpcExporter}.
*
* This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
public final class OkHttpGrpcExporterBuilder
implements GrpcExporterBuilder {
private final String type;
private final String grpcEndpointPath;
private long timeoutNanos;
private URI endpoint;
private boolean compressionEnabled = false;
private final Headers.Builder headers = new Headers.Builder();
@Nullable private byte[] trustedCertificatesPem;
@Nullable private byte[] privateKeyPem;
@Nullable private byte[] certificatePem;
@Nullable private RetryPolicy retryPolicy;
private MeterProvider meterProvider = MeterProvider.noop();
/** Creates a new {@link OkHttpGrpcExporterBuilder}. */
// Visible for testing
public OkHttpGrpcExporterBuilder(
String type, String grpcEndpointPath, long defaultTimeoutSecs, URI defaultEndpoint) {
this.type = type;
this.grpcEndpointPath = grpcEndpointPath;
timeoutNanos = TimeUnit.SECONDS.toNanos(defaultTimeoutSecs);
endpoint = defaultEndpoint;
}
@Override
public OkHttpGrpcExporterBuilder setChannel(ManagedChannel channel) {
throw new UnsupportedOperationException("Only available on DefaultGrpcExporter");
}
@Override
public OkHttpGrpcExporterBuilder setTimeout(long timeout, TimeUnit unit) {
timeoutNanos = unit.toNanos(timeout);
return this;
}
@Override
public OkHttpGrpcExporterBuilder setTimeout(Duration timeout) {
return setTimeout(timeout.toNanos(), TimeUnit.NANOSECONDS);
}
@Override
public OkHttpGrpcExporterBuilder setEndpoint(String endpoint) {
this.endpoint = ExporterBuilderUtil.validateEndpoint(endpoint);
return this;
}
@Override
public OkHttpGrpcExporterBuilder setCompression(String compressionMethod) {
this.compressionEnabled = true;
return this;
}
@Override
public OkHttpGrpcExporterBuilder setTrustedCertificates(byte[] trustedCertificatesPem) {
this.trustedCertificatesPem = trustedCertificatesPem;
return this;
}
@Override
public GrpcExporterBuilder setClientTls(byte[] privateKeyPem, byte[] certificatePem) {
this.privateKeyPem = privateKeyPem;
this.certificatePem = certificatePem;
return this;
}
@Override
public OkHttpGrpcExporterBuilder addHeader(String key, String value) {
headers.add(key, value);
return this;
}
@Override
public GrpcExporterBuilder setRetryPolicy(RetryPolicy retryPolicy) {
this.retryPolicy = retryPolicy;
return this;
}
@Override
public GrpcExporterBuilder setMeterProvider(MeterProvider meterProvider) {
this.meterProvider = meterProvider;
return this;
}
@Override
public GrpcExporter build() {
OkHttpClient.Builder clientBuilder =
new OkHttpClient.Builder().dispatcher(OkHttpUtil.newDispatcher());
clientBuilder.callTimeout(Duration.ofNanos(timeoutNanos));
if (trustedCertificatesPem != null) {
try {
X509TrustManager trustManager = TlsUtil.trustManager(trustedCertificatesPem);
X509KeyManager keyManager = null;
if (privateKeyPem != null && certificatePem != null) {
keyManager = TlsUtil.keyManager(privateKeyPem, certificatePem);
}
clientBuilder.sslSocketFactory(
TlsUtil.sslSocketFactory(keyManager, trustManager), trustManager);
} catch (SSLException e) {
throw new IllegalStateException(
"Could not set trusted certificates, are they valid X.509 in PEM format?", e);
}
}
String endpoint = this.endpoint.resolve(grpcEndpointPath).toString();
if (endpoint.startsWith("http://")) {
clientBuilder.protocols(Collections.singletonList(Protocol.H2_PRIOR_KNOWLEDGE));
} else {
clientBuilder.protocols(Arrays.asList(Protocol.HTTP_2, Protocol.HTTP_1_1));
}
headers.add("te", "trailers");
if (compressionEnabled) {
headers.add("grpc-encoding", "gzip");
}
if (retryPolicy != null) {
clientBuilder.addInterceptor(
new RetryInterceptor(retryPolicy, OkHttpGrpcExporter::isRetryable));
}
return new OkHttpGrpcExporter<>(
type, clientBuilder.build(), meterProvider, endpoint, headers.build(), compressionEnabled);
}
}