diff --git a/exporters/otlp/testing-internal/src/main/java/io/opentelemetry/exporter/otlp/testing/internal/AbstractGrpcTelemetryExporterTest.java b/exporters/otlp/testing-internal/src/main/java/io/opentelemetry/exporter/otlp/testing/internal/AbstractGrpcTelemetryExporterTest.java index 14487429248..72f279d4883 100644 --- a/exporters/otlp/testing-internal/src/main/java/io/opentelemetry/exporter/otlp/testing/internal/AbstractGrpcTelemetryExporterTest.java +++ b/exporters/otlp/testing-internal/src/main/java/io/opentelemetry/exporter/otlp/testing/internal/AbstractGrpcTelemetryExporterTest.java @@ -139,6 +139,8 @@ public abstract class AbstractGrpcTelemetryExporterTest { private static volatile String grpcEncodingServerEncoding = "brotli"; + private static volatile String grpcEncodingServerStatus = "0"; + @RegisterExtension @Order(1) static final SelfSignedCertificateExtension certificate = new SelfSignedCertificateExtension(); @@ -256,7 +258,8 @@ private static void handleExport( } // A minimal server that returns a configurable grpc-encoding to test encoding handling. - // The encoding is controlled via grpcEncodingServerEncoding (defaults to "brotli"). + // The encoding is controlled via grpcEncodingServerEncoding (defaults to "brotli") and the status + // via grpcEncodingServerStatus (defaults to "0"). // Both OkHttpGrpcSender (our code) and UpstreamGrpcSender (grpc-java framework) reject unknown // encodings and fail the export with INTERNAL status. @RegisterExtension @@ -275,7 +278,7 @@ protected void configure(ServerBuilder sb) { ResponseHeaders.builder(HttpStatus.OK) .contentType(MediaType.parse("application/grpc+proto")) .add("grpc-encoding", grpcEncodingServerEncoding) - .add("grpc-status", "0") + .add("grpc-status", grpcEncodingServerStatus) .build(), HttpData.wrap(frame)); }); @@ -344,6 +347,7 @@ void reset() { httpRequests.clear(); grpcEncodingServerAttempts.set(0); grpcEncodingServerEncoding = "brotli"; + grpcEncodingServerStatus = "0"; } @Test @@ -471,6 +475,65 @@ void unsupportedGrpcMessageEncoding() { } } + // Verifies that a response compressed with the request's own encoding, which the sender can't + // decompress, resolves from grpc-status instead of failing with INTERNAL. grpc-go servers (such + // as the OpenTelemetry Collector) respond with the request's encoding by default. + @Test + void responseInRequestGrpcMessageEncoding() { + // grpc-java rejects a response encoding missing from its DecompressorRegistry + assumeThat(exporter.unwrap()) + .extracting("delegate.grpcSender") + .matches(sender -> sender.getClass().getSimpleName().equals("OkHttpGrpcSender")); + + grpcEncodingServerEncoding = "base64"; + try (TelemetryExporter testExporter = + exporterBuilder() + .setEndpoint(grpcEncodingServer.httpUri().toString()) + .setCompression("base64") + .setRetryPolicy(null) + .build()) { + CompletableResultCode result = + testExporter + .export(Collections.singletonList(generateFakeTelemetry())) + .join(10, TimeUnit.SECONDS); + assertThat(result.isSuccess()).isTrue(); + assertThat(grpcEncodingServerAttempts).hasValue(1); + } + } + + // Verifies that the grpc-status of a response compressed with the request's own encoding is + // still honored when it is an error. + @Test + @SuppressLogger(GrpcExporter.class) + void responseInRequestGrpcMessageEncodingWithErrorStatus() { + assumeThat(exporter.unwrap()) + .extracting("delegate.grpcSender") + .matches(sender -> sender.getClass().getSimpleName().equals("OkHttpGrpcSender")); + + grpcEncodingServerEncoding = "base64"; + grpcEncodingServerStatus = "7"; + try (TelemetryExporter testExporter = + exporterBuilder() + .setEndpoint(grpcEncodingServer.httpUri().toString()) + .setCompression("base64") + .setRetryPolicy(null) + .build()) { + CompletableResultCode result = + testExporter + .export(Collections.singletonList(generateFakeTelemetry())) + .join(10, TimeUnit.SECONDS); + assertThat(result.isSuccess()).isFalse(); + assertThat(result.getFailureThrowable()) + .isInstanceOfSatisfying( + FailedExportException.GrpcExportException.class, + ex -> + assertThat(requireNonNull(ex.getResponse())) + .satisfies(grpcResponse -> + assertThat(grpcResponse.getStatusCode()) + .isEqualTo(GrpcStatusCode.PERMISSION_DENIED))); + } + } + // Verifies that a response with flag=1 and grpc-encoding: identity is rejected with INTERNAL. // Although identity is advertised in grpc-accept-encoding (meaning "no encoding applied"), // a server setting the compressed flag to 1 is contradictory and should not be accepted. @@ -794,7 +857,8 @@ void doubleShutdown() { TelemetryExporter exporter = exporterBuilder().setEndpoint(server.httpUri().toString()).build(); assertThat(exporter.shutdown().join(10, TimeUnit.SECONDS).isSuccess()).isTrue(); - assertThat(logs.getEvents()).isEmpty(); + // Not isEmpty(): the captured events can include those of the test that ran before this one + logs.assertDoesNotContain("Calling shutdown() multiple times."); assertThat(exporter.shutdown().join(10, TimeUnit.SECONDS).isSuccess()).isTrue(); logs.assertContains("Calling shutdown() multiple times."); } diff --git a/exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpGrpcSender.java b/exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpGrpcSender.java index f17355cea43..20a072de441 100644 --- a/exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpGrpcSender.java +++ b/exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpGrpcSender.java @@ -372,6 +372,19 @@ private void handleResponse(Response response, BiConsumer // Compressed: validate the encoding and decompress with a post-decompression size limit String encoding = response.header("grpc-encoding"); if (!"gzip".equalsIgnoreCase(encoding)) { + if (isRequestEncoding(encoding)) { + // Servers may compress the response with the request's encoding (grpc-go does by + // default). Compressor has no way to decompress, so the body, which only carries + // partial success details, is dropped and the status is read from the response. + logger.log( + Level.FINE, + "Ignoring gRPC response body compressed with the request encoding: " + encoding); + onResponse.accept( + ImmutableGrpcResponse.create( + grpcStatus(response), grpcMessage(response), new byte[0]), + true); + return; + } onResponse.accept(responseUnsupportedGrpcEncoding(encoding), false); return; } @@ -406,6 +419,10 @@ private static GrpcResponse responseMessageTooLarge(long maxResponseBodySize) { new byte[0]); } + private boolean isRequestEncoding(@Nullable String encoding) { + return compressor != null && compressor.getEncoding().equalsIgnoreCase(encoding); + } + private static GrpcResponse responseUnsupportedGrpcEncoding(@Nullable String encoding) { return ImmutableGrpcResponse.create( GrpcStatusCode.INTERNAL, "Unsupported gRPC message encoding: " + encoding, new byte[0]);