Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toString to OTLP exporters #5686

Merged
merged 3 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ Comparing source compatibility of against
*** 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()
+++ NEW METHOD: PUBLIC(+) java.lang.String toString()
*** 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()
+++ NEW METHOD: PUBLIC(+) java.lang.String toString()
*** 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()
+++ NEW METHOD: PUBLIC(+) java.lang.String toString()
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporterBuilder toBuilder()
+++ NEW METHOD: PUBLIC(+) java.lang.String toString()
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporterBuilder toBuilder()
+++ NEW METHOD: PUBLIC(+) java.lang.String toString()
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporterBuilder toBuilder()
+++ NEW METHOD: PUBLIC(+) java.lang.String toString()
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.function.Supplier;
Expand Down Expand Up @@ -202,6 +203,36 @@
compressionEnabled);
}

public String toString(boolean includePrefixAndSuffix) {
StringJoiner joiner =
includePrefixAndSuffix
? new StringJoiner(", ", "GrpcExporterBuilder{", "}")

Check warning on line 209 in exporters/common/src/main/java/io/opentelemetry/exporter/internal/grpc/GrpcExporterBuilder.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L209 was not covered by tests
: new StringJoiner(", ");
joiner.add("exporterName=" + exporterName);
joiner.add("type=" + type);
joiner.add("endpoint=" + endpoint.toString());
joiner.add("endpointPath=" + grpcEndpointPath);
joiner.add("timeoutNanos=" + timeoutNanos);
joiner.add("compressionEnabled=" + compressionEnabled);
StringJoiner headersJoiner = new StringJoiner(", ", "Headers{", "}");
headers.forEach((key, value) -> headersJoiner.add(key + "=OBFUSCATED"));
joiner.add("headers=" + headersJoiner);
if (retryPolicy != null) {
joiner.add("retryPolicy=" + retryPolicy);
}
if (grpcChannel != null) {
joiner.add("grpcChannel=" + grpcChannel);
}
// Note: omit tlsConfigHelper because we can't log the configuration in any readable way
// Note: omit meterProviderSupplier because we can't log the configuration in any readable way
return joiner.toString();
}

@Override
public String toString() {
return toString(true);

Check warning on line 233 in exporters/common/src/main/java/io/opentelemetry/exporter/internal/grpc/GrpcExporterBuilder.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L233 was not covered by tests
}

// Use an inner class to ensure GrpcExporterBuilder does not have classloading dependencies on
// upstream gRPC.
private class UpstreamGrpcExporterFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.StringJoiner;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.logging.Level;
Expand Down Expand Up @@ -166,6 +167,36 @@
return new HttpExporter<>(exporterName, type, httpSender, meterProviderSupplier, exportAsJson);
}

public String toString(boolean includePrefixAndSuffix) {
StringJoiner joiner =
includePrefixAndSuffix
? new StringJoiner(", ", "HttpExporterBuilder{", "}")

Check warning on line 173 in exporters/common/src/main/java/io/opentelemetry/exporter/internal/http/HttpExporterBuilder.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L173 was not covered by tests
: new StringJoiner(", ");
joiner.add("exporterName=" + exporterName);
joiner.add("type=" + type);
joiner.add("endpoint=" + endpoint);
joiner.add("timeoutNanos=" + timeoutNanos);
joiner.add("compressionEnabled=" + compressionEnabled);
joiner.add("exportAsJson=" + exportAsJson);
if (headers != null) {
StringJoiner headersJoiner = new StringJoiner(", ", "Headers{", "}");
headers.forEach((key, value) -> headersJoiner.add(key + "=OBFUSCATED"));
joiner.add("headers=" + headersJoiner);
}
if (retryPolicy != null) {
joiner.add("retryPolicy=" + retryPolicy);
}
// Note: omit tlsConfigHelper because we can't log the configuration in any readable way
// Note: omit meterProviderSupplier because we can't log the configuration in any readable way
// Note: omit authenticator because we can't log the configuration in any readable way
return joiner.toString();
}

@Override
public String toString() {
return toString(true);

Check warning on line 197 in exporters/common/src/main/java/io/opentelemetry/exporter/internal/http/HttpExporterBuilder.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L197 was not covered by tests
}

/**
* Resolve the {@link HttpSenderProvider}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,9 @@ public CompletableResultCode flush() {
public CompletableResultCode shutdown() {
return delegate.shutdown();
}

@Override
public String toString() {
return "OtlpHttpLogRecordExporter{" + builder.toString(false) + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,9 @@ public CompletableResultCode flush() {
public CompletableResultCode shutdown() {
return delegate.shutdown();
}

@Override
public String toString() {
return "OtlpHttpMetricExporter{" + builder.toString(false) + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,9 @@ public CompletableResultCode flush() {
public CompletableResultCode shutdown() {
return delegate.shutdown();
}

@Override
public String toString() {
return "OtlpHttpSpanExporter{" + builder.toString(false) + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,9 @@ public CompletableResultCode flush() {
public CompletableResultCode shutdown() {
return delegate.shutdown();
}

@Override
public String toString() {
return "OtlpGrpcLogRecordExporter{" + builder.toString(false) + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,9 @@ public CompletableResultCode flush() {
public CompletableResultCode shutdown() {
return delegate.shutdown();
}

@Override
public String toString() {
return "OtlpGrpcMetricExporter{" + builder.toString(false) + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,9 @@ public CompletableResultCode flush() {
public CompletableResultCode shutdown() {
return delegate.shutdown();
}

@Override
public String toString() {
return "OtlpGrpcSpanExporter{" + builder.toString(false) + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,68 @@ void toBuilderEquality()
}
}

@Test
void stringRepresentation() throws IOException, CertificateEncodingException {
TelemetryExporter<T> telemetryExporter =
exporterBuilder().setEndpoint("http://localhost:4317").build();
try {
assertThat(telemetryExporter.unwrap().toString())
.matches(
"OtlpGrpc[a-zA-Z]*Exporter\\{"
+ "exporterName=otlp, "
+ "type=[a-zA_Z]*, "
+ "endpoint=http://localhost:4317, "
+ "endpointPath=.*, "
+ "timeoutNanos="
+ TimeUnit.SECONDS.toNanos(10)
+ ", "
+ "compressionEnabled=false, "
+ "headers=Headers\\{User-Agent=OBFUSCATED\\}"
+ ".*" // Maybe additional grpcChannel field
+ "\\}");
} finally {
telemetryExporter.shutdown();
}

telemetryExporter =
exporterBuilder()
.setTimeout(Duration.ofSeconds(5))
.setEndpoint("http://example:4317")
.setCompression("gzip")
.addHeader("foo", "bar")
.setTrustedCertificates(certificate.certificate().getEncoded())
.setClientTls(
Files.readAllBytes(clientCertificate.privateKeyFile().toPath()),
Files.readAllBytes(clientCertificate.certificateFile().toPath()))
.setRetryPolicy(
RetryPolicy.builder()
.setMaxAttempts(2)
.setMaxBackoff(Duration.ofSeconds(3))
.setInitialBackoff(Duration.ofMillis(50))
.setBackoffMultiplier(1.3)
.build())
.build();
try {
assertThat(telemetryExporter.unwrap().toString())
.matches(
"OtlpGrpc[a-zA-Z]*Exporter\\{"
+ "exporterName=otlp, "
+ "type=[a-zA_Z]*, "
+ "endpoint=http://example:4317, "
+ "endpointPath=.*, "
+ "timeoutNanos="
+ TimeUnit.SECONDS.toNanos(5)
+ ", "
+ "compressionEnabled=true, "
+ "headers=Headers\\{.*foo=OBFUSCATED.*\\}, "
+ "retryPolicy=RetryPolicy\\{maxAttempts=2, initialBackoff=PT0\\.05S, maxBackoff=PT3S, backoffMultiplier=1\\.3\\}"
+ ".*" // Maybe additional grpcChannel field
+ "\\}");
} finally {
telemetryExporter.shutdown();
}
}

protected abstract TelemetryExporterBuilder<T> exporterBuilder();

protected abstract TelemetryExporterBuilder<T> toBuilder(TelemetryExporter<T> exporter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,65 @@ void toBuilderEquality()
}
}

@Test
void stringRepresentation() throws IOException, CertificateEncodingException {
TelemetryExporter<T> telemetryExporter = exporterBuilder().build();
try {
assertThat(telemetryExporter.unwrap().toString())
.matches(
"OtlpHttp[a-zA-Z]*Exporter\\{"
+ "exporterName=otlp, "
+ "type=[a-zA_Z]*, "
+ "endpoint=http://localhost:4318/v1/[a-zA-Z]*, "
+ "timeoutNanos="
+ TimeUnit.SECONDS.toNanos(10)
+ ", "
+ "compressionEnabled=false, "
+ "exportAsJson=false, "
+ "headers=Headers\\{User-Agent=OBFUSCATED\\}"
+ "\\}");
} finally {
telemetryExporter.shutdown();
}

telemetryExporter =
exporterBuilder()
.setTimeout(Duration.ofSeconds(5))
.setEndpoint("http://example:4318/v1/logs")
.setCompression("gzip")
.addHeader("foo", "bar")
.setTrustedCertificates(certificate.certificate().getEncoded())
.setClientTls(
Files.readAllBytes(clientCertificate.privateKeyFile().toPath()),
Files.readAllBytes(clientCertificate.certificateFile().toPath()))
.setRetryPolicy(
RetryPolicy.builder()
.setMaxAttempts(2)
.setMaxBackoff(Duration.ofSeconds(3))
.setInitialBackoff(Duration.ofMillis(50))
.setBackoffMultiplier(1.3)
.build())
.build();
try {
assertThat(telemetryExporter.unwrap().toString())
.matches(
"OtlpHttp[a-zA-Z]*Exporter\\{"
+ "exporterName=otlp, "
+ "type=[a-zA_Z]*, "
+ "endpoint=http://example:4318/v1/[a-zA-Z]*, "
+ "timeoutNanos="
+ TimeUnit.SECONDS.toNanos(5)
+ ", "
+ "compressionEnabled=true, "
+ "exportAsJson=false, "
+ "headers=Headers\\{.*foo=OBFUSCATED.*\\}, "
+ "retryPolicy=RetryPolicy\\{maxAttempts=2, initialBackoff=PT0\\.05S, maxBackoff=PT3S, backoffMultiplier=1\\.3\\}"
+ "\\}");
} finally {
telemetryExporter.shutdown();
}
}

protected abstract TelemetryExporterBuilder<T> exporterBuilder();

protected abstract TelemetryExporterBuilder<T> toBuilder(TelemetryExporter<T> exporter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public TelemetryExporterBuilder<T> addHeader(String key, String value) {

@Override
public TelemetryExporterBuilder<T> setAuthenticator(Authenticator authenticator) {
delegate.setAuthenticator(authenticator);
return this;
}

Expand All @@ -99,18 +100,21 @@ public TelemetryExporterBuilder<T> setAuthenticator(Authenticator authenticator)

@Override
public TelemetryExporterBuilder<T> setTrustedCertificates(byte[] certificates) {
delegate.setTrustedCertificates(certificates);
tlsConfigHelper.setTrustManagerFromCerts(certificates);
return this;
}

@Override
public TelemetryExporterBuilder<T> setClientTls(byte[] privateKeyPem, byte[] certificatePem) {
delegate.setClientTls(privateKeyPem, certificatePem);
tlsConfigHelper.setKeyManagerFromCerts(privateKeyPem, certificatePem);
return this;
}

@Override
public TelemetryExporterBuilder<T> setRetryPolicy(RetryPolicy retryPolicy) {
delegate.setRetryPolicy(retryPolicy);
String grpcServiceName;
if (delegate instanceof GrpcLogRecordExporterBuilderWrapper) {
grpcServiceName = "opentelemetry.proto.collector.logs.v1.LogsService";
Expand Down Expand Up @@ -167,6 +171,7 @@ public CompletableResultCode shutdown() {
@Override
public TelemetryExporterBuilder<T> setSslContext(
SSLContext sslContext, X509TrustManager trustManager) {
delegate.setSslContext(sslContext, trustManager);
tlsConfigHelper.setSslContext(sslContext, trustManager);
return this;
}
Expand Down