Skip to content
Open
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 @@ -139,6 +139,8 @@ public abstract class AbstractGrpcTelemetryExporterTest<T, U extends Message> {

private static volatile String grpcEncodingServerEncoding = "brotli";

private static volatile String grpcEncodingServerStatus = "0";

@RegisterExtension
@Order(1)
static final SelfSignedCertificateExtension certificate = new SelfSignedCertificateExtension();
Expand Down Expand Up @@ -256,7 +258,8 @@ private static <R extends Message> 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
Expand All @@ -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));
});
Expand Down Expand Up @@ -344,6 +347,7 @@ void reset() {
httpRequests.clear();
grpcEncodingServerAttempts.set(0);
grpcEncodingServerEncoding = "brotli";
grpcEncodingServerStatus = "0";
}

@Test
Expand Down Expand Up @@ -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<T> 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<T> 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.
Expand Down Expand Up @@ -794,7 +857,8 @@ void doubleShutdown() {
TelemetryExporter<T> 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.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,19 @@ private void handleResponse(Response response, BiConsumer<GrpcResponse, Boolean>
// 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;
}
Expand Down Expand Up @@ -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]);
Expand Down
Loading