Skip to content

Commit

Permalink
Add OtlpHttp{Signal}Exporter#toBuilder() methods (#5652)
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-berg authored Aug 3, 2023
1 parent 90ab366 commit d03b485
Show file tree
Hide file tree
Showing 21 changed files with 537 additions and 494 deletions.
10 changes: 9 additions & 1 deletion docs/apidiffs/current_vs_latest/opentelemetry-exporter-otlp.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
Comparing source compatibility of against
No changes.
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporter (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporterBuilder toBuilder()
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporter (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporterBuilder toBuilder()
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporterBuilder toBuilder()
4 changes: 3 additions & 1 deletion docs/apidiffs/current_vs_latest/opentelemetry-sdk-common.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Comparing source compatibility of against
No changes.
**** MODIFIED CLASS: PUBLIC ABSTRACT io.opentelemetry.sdk.common.export.RetryPolicy (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++* NEW METHOD: PUBLIC(+) ABSTRACT(+) io.opentelemetry.sdk.common.export.RetryPolicy$RetryPolicyBuilder toBuilder()
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,13 @@ public SSLContext getSslContext() {
throw new IllegalArgumentException(e);
}
}

/** Return a shallow copy of this instance. */
public TlsConfigHelper copy() {
TlsConfigHelper copy = new TlsConfigHelper();
copy.keyManager = keyManager;
copy.trustManager = trustManager;
copy.sslContext = sslContext;
return copy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public final class HttpExporterBuilder<T extends Marshaler> {
private boolean exportAsJson = false;
@Nullable private Map<String, String> headers;

private final TlsConfigHelper tlsConfigHelper = new TlsConfigHelper();
private TlsConfigHelper tlsConfigHelper = new TlsConfigHelper();
@Nullable private RetryPolicy retryPolicy;
private Supplier<MeterProvider> meterProviderSupplier = GlobalOpenTelemetry::getMeterProvider;
@Nullable private Authenticator authenticator;
Expand Down Expand Up @@ -126,6 +126,25 @@ public HttpExporterBuilder<T> exportAsJson() {
return this;
}

@SuppressWarnings("BuilderReturnThis")
public HttpExporterBuilder<T> copy() {
HttpExporterBuilder<T> copy = new HttpExporterBuilder<>(exporterName, type, endpoint);
copy.endpoint = endpoint;
copy.timeoutNanos = timeoutNanos;
copy.exportAsJson = exportAsJson;
copy.compressionEnabled = compressionEnabled;
if (headers != null) {
copy.headers = new HashMap<>(headers);
}
copy.tlsConfigHelper = tlsConfigHelper.copy();
if (retryPolicy != null) {
copy.retryPolicy = retryPolicy.toBuilder().build();
}
copy.meterProviderSupplier = meterProviderSupplier;
copy.authenticator = authenticator;
return copy;
}

public HttpExporter<T> build() {
Map<String, String> headers = this.headers == null ? Collections.emptyMap() : this.headers;
Supplier<Map<String, String>> headerSupplier = () -> headers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.exporter.otlp.http.logs;

import io.opentelemetry.exporter.internal.http.HttpExporter;
import io.opentelemetry.exporter.internal.http.HttpExporterBuilder;
import io.opentelemetry.exporter.internal.otlp.logs.LogsRequestMarshaler;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.logs.data.LogRecordData;
Expand All @@ -21,9 +22,13 @@
@ThreadSafe
public final class OtlpHttpLogRecordExporter implements LogRecordExporter {

private final HttpExporterBuilder<LogsRequestMarshaler> builder;
private final HttpExporter<LogsRequestMarshaler> delegate;

OtlpHttpLogRecordExporter(HttpExporter<LogsRequestMarshaler> delegate) {
OtlpHttpLogRecordExporter(
HttpExporterBuilder<LogsRequestMarshaler> builder,
HttpExporter<LogsRequestMarshaler> delegate) {
this.builder = builder;
this.delegate = delegate;
}

Expand All @@ -48,6 +53,15 @@ public static OtlpHttpLogRecordExporterBuilder builder() {
return new OtlpHttpLogRecordExporterBuilder();
}

/**
* Returns a builder with configuration values equal to those for this exporter.
*
* <p>IMPORTANT: Be sure to {@link #shutdown()} this instance if it will no longer be used.
*/
public OtlpHttpLogRecordExporterBuilder toBuilder() {
return new OtlpHttpLogRecordExporterBuilder(builder.copy());
}

/**
* Submits all the given logs in a single batch to the OpenTelemetry collector.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ public final class OtlpHttpLogRecordExporterBuilder {

private final HttpExporterBuilder<LogsRequestMarshaler> delegate;

OtlpHttpLogRecordExporterBuilder() {
delegate = new HttpExporterBuilder<>("otlp", "log", DEFAULT_ENDPOINT);
OtlpHttpLogRecordExporterBuilder(HttpExporterBuilder<LogsRequestMarshaler> delegate) {
this.delegate = delegate;
OtlpUserAgent.addUserAgentHeader(delegate::addHeader);
}

OtlpHttpLogRecordExporterBuilder() {
this(new HttpExporterBuilder<>("otlp", "log", DEFAULT_ENDPOINT));
}

/**
* Sets the maximum time to wait for the collector to process an exported batch of logs. If unset,
* defaults to {@value HttpExporterBuilder#DEFAULT_TIMEOUT_SECS}s.
Expand Down Expand Up @@ -141,6 +145,6 @@ public OtlpHttpLogRecordExporterBuilder setMeterProvider(MeterProvider meterProv
* @return a new exporter's instance
*/
public OtlpHttpLogRecordExporter build() {
return new OtlpHttpLogRecordExporter(delegate.build());
return new OtlpHttpLogRecordExporter(delegate, delegate.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.exporter.otlp.http.metrics;

import io.opentelemetry.exporter.internal.http.HttpExporter;
import io.opentelemetry.exporter.internal.http.HttpExporterBuilder;
import io.opentelemetry.exporter.internal.otlp.metrics.MetricsRequestMarshaler;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.metrics.Aggregation;
Expand All @@ -26,14 +27,17 @@
@ThreadSafe
public final class OtlpHttpMetricExporter implements MetricExporter {

private final HttpExporterBuilder<MetricsRequestMarshaler> builder;
private final HttpExporter<MetricsRequestMarshaler> delegate;
private final AggregationTemporalitySelector aggregationTemporalitySelector;
private final DefaultAggregationSelector defaultAggregationSelector;

OtlpHttpMetricExporter(
HttpExporterBuilder<MetricsRequestMarshaler> builder,
HttpExporter<MetricsRequestMarshaler> delegate,
AggregationTemporalitySelector aggregationTemporalitySelector,
DefaultAggregationSelector defaultAggregationSelector) {
this.builder = builder;
this.delegate = delegate;
this.aggregationTemporalitySelector = aggregationTemporalitySelector;
this.defaultAggregationSelector = defaultAggregationSelector;
Expand All @@ -60,6 +64,15 @@ public static OtlpHttpMetricExporterBuilder builder() {
return new OtlpHttpMetricExporterBuilder();
}

/**
* Returns a builder with configuration values equal to those for this exporter.
*
* <p>IMPORTANT: Be sure to {@link #shutdown()} this instance if it will no longer be used.
*/
public OtlpHttpMetricExporterBuilder toBuilder() {
return new OtlpHttpMetricExporterBuilder(builder.copy());
}

@Override
public AggregationTemporality getAggregationTemporality(InstrumentType instrumentType) {
return aggregationTemporalitySelector.getAggregationTemporality(instrumentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,16 @@ public final class OtlpHttpMetricExporterBuilder {
private DefaultAggregationSelector defaultAggregationSelector =
DefaultAggregationSelector.getDefault();

OtlpHttpMetricExporterBuilder() {
delegate = new HttpExporterBuilder<>("otlp", "metric", DEFAULT_ENDPOINT);
OtlpHttpMetricExporterBuilder(HttpExporterBuilder<MetricsRequestMarshaler> delegate) {
this.delegate = delegate;
delegate.setMeterProvider(MeterProvider.noop());
OtlpUserAgent.addUserAgentHeader(delegate::addHeader);
}

OtlpHttpMetricExporterBuilder() {
this(new HttpExporterBuilder<>("otlp", "metric", DEFAULT_ENDPOINT));
}

/**
* Sets the maximum time to wait for the collector to process an exported batch of metrics. If
* unset, defaults to {@value HttpExporterBuilder#DEFAULT_TIMEOUT_SECS}s.
Expand Down Expand Up @@ -181,6 +185,6 @@ OtlpHttpMetricExporterBuilder exportAsJson() {
*/
public OtlpHttpMetricExporter build() {
return new OtlpHttpMetricExporter(
delegate.build(), aggregationTemporalitySelector, defaultAggregationSelector);
delegate, delegate.build(), aggregationTemporalitySelector, defaultAggregationSelector);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.exporter.otlp.http.trace;

import io.opentelemetry.exporter.internal.http.HttpExporter;
import io.opentelemetry.exporter.internal.http.HttpExporterBuilder;
import io.opentelemetry.exporter.internal.otlp.traces.TraceRequestMarshaler;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.trace.data.SpanData;
Expand All @@ -21,9 +22,13 @@
@ThreadSafe
public final class OtlpHttpSpanExporter implements SpanExporter {

private final HttpExporterBuilder<TraceRequestMarshaler> builder;
private final HttpExporter<TraceRequestMarshaler> delegate;

OtlpHttpSpanExporter(HttpExporter<TraceRequestMarshaler> delegate) {
OtlpHttpSpanExporter(
HttpExporterBuilder<TraceRequestMarshaler> builder,
HttpExporter<TraceRequestMarshaler> delegate) {
this.builder = builder;
this.delegate = delegate;
}

Expand All @@ -48,6 +53,15 @@ public static OtlpHttpSpanExporterBuilder builder() {
return new OtlpHttpSpanExporterBuilder();
}

/**
* Returns a builder with configuration values equal to those for this exporter.
*
* <p>IMPORTANT: Be sure to {@link #shutdown()} this instance if it will no longer be used.
*/
public OtlpHttpSpanExporterBuilder toBuilder() {
return new OtlpHttpSpanExporterBuilder(builder.copy());
}

/**
* Submits all the given spans in a single batch to the OpenTelemetry collector.
*
Expand All @@ -70,7 +84,7 @@ public CompletableResultCode flush() {
return CompletableResultCode.ofSuccess();
}

/** Shutdown the exporter. */
/** Shutdown the exporter, releasing any resources and preventing subsequent exports. */
@Override
public CompletableResultCode shutdown() {
return delegate.shutdown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ public final class OtlpHttpSpanExporterBuilder {

private final HttpExporterBuilder<TraceRequestMarshaler> delegate;

OtlpHttpSpanExporterBuilder() {
delegate = new HttpExporterBuilder<>("otlp", "span", DEFAULT_ENDPOINT);
OtlpHttpSpanExporterBuilder(HttpExporterBuilder<TraceRequestMarshaler> delegate) {
this.delegate = delegate;
OtlpUserAgent.addUserAgentHeader(delegate::addHeader);
}

OtlpHttpSpanExporterBuilder() {
this(new HttpExporterBuilder<>("otlp", "span", DEFAULT_ENDPOINT));
}

/**
* Sets the maximum time to wait for the collector to process an exported batch of spans. If
* unset, defaults to {@value HttpExporterBuilder#DEFAULT_TIMEOUT_SECS}s.
Expand Down Expand Up @@ -142,6 +146,6 @@ public OtlpHttpSpanExporterBuilder setMeterProvider(MeterProvider meterProvider)
* @return a new exporter's instance
*/
public OtlpHttpSpanExporter build() {
return new OtlpHttpSpanExporter(delegate.build());
return new OtlpHttpSpanExporter(delegate, delegate.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,16 @@

package io.opentelemetry.exporter.otlp.http.logs;

import io.opentelemetry.exporter.internal.auth.Authenticator;
import io.opentelemetry.exporter.internal.marshal.Marshaler;
import io.opentelemetry.exporter.internal.otlp.logs.ResourceLogsMarshaler;
import io.opentelemetry.exporter.otlp.testing.internal.AbstractHttpTelemetryExporterTest;
import io.opentelemetry.exporter.otlp.testing.internal.FakeTelemetryUtil;
import io.opentelemetry.exporter.otlp.testing.internal.HttpLogRecordExporterBuilderWrapper;
import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporter;
import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporterBuilder;
import io.opentelemetry.proto.logs.v1.ResourceLogs;
import io.opentelemetry.sdk.common.export.RetryPolicy;
import io.opentelemetry.sdk.logs.data.LogRecordData;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLContext;
import javax.net.ssl.X509TrustManager;

class OtlpHttpLogRecordExporterOkHttpSenderTest
extends AbstractHttpTelemetryExporterTest<LogRecordData, ResourceLogs> {
Expand All @@ -30,80 +25,14 @@ protected OtlpHttpLogRecordExporterOkHttpSenderTest() {

@Override
protected TelemetryExporterBuilder<LogRecordData> exporterBuilder() {
OtlpHttpLogRecordExporterBuilder builder = OtlpHttpLogRecordExporter.builder();
return new TelemetryExporterBuilder<LogRecordData>() {
@Override
public TelemetryExporterBuilder<LogRecordData> setEndpoint(String endpoint) {
builder.setEndpoint(endpoint);
return this;
}

@Override
public TelemetryExporterBuilder<LogRecordData> setTimeout(long timeout, TimeUnit unit) {
builder.setTimeout(timeout, unit);
return this;
}

@Override
public TelemetryExporterBuilder<LogRecordData> setTimeout(Duration timeout) {
builder.setTimeout(timeout);
return this;
}

@Override
public TelemetryExporterBuilder<LogRecordData> setCompression(String compression) {
builder.setCompression(compression);
return this;
}

@Override
public TelemetryExporterBuilder<LogRecordData> addHeader(String key, String value) {
builder.addHeader(key, value);
return this;
}

@Override
public TelemetryExporterBuilder<LogRecordData> setAuthenticator(Authenticator authenticator) {
Authenticator.setAuthenticatorOnDelegate(builder, authenticator);
return this;
}

@Override
public TelemetryExporterBuilder<LogRecordData> setTrustedCertificates(byte[] certificates) {
builder.setTrustedCertificates(certificates);
return this;
}

@Override
public TelemetryExporterBuilder<LogRecordData> setSslContext(
SSLContext ssLContext, X509TrustManager trustManager) {
builder.setSslContext(ssLContext, trustManager);
return this;
}

@Override
public TelemetryExporterBuilder<LogRecordData> setClientTls(
byte[] privateKeyPem, byte[] certificatePem) {
builder.setClientTls(privateKeyPem, certificatePem);
return this;
}

@Override
public TelemetryExporterBuilder<LogRecordData> setRetryPolicy(RetryPolicy retryPolicy) {
builder.setRetryPolicy(retryPolicy);
return this;
}

@Override
public TelemetryExporterBuilder<LogRecordData> setChannel(io.grpc.ManagedChannel channel) {
throw new UnsupportedOperationException("Not implemented");
}
return new HttpLogRecordExporterBuilderWrapper(OtlpHttpLogRecordExporter.builder());
}

@Override
public TelemetryExporter<LogRecordData> build() {
return TelemetryExporter.wrap(builder.build());
}
};
@Override
protected TelemetryExporterBuilder<LogRecordData> toBuilder(
TelemetryExporter<LogRecordData> exporter) {
return new HttpLogRecordExporterBuilderWrapper(
((OtlpHttpLogRecordExporter) exporter.unwrap()).toBuilder());
}

@Override
Expand Down
Loading

0 comments on commit d03b485

Please sign in to comment.