Skip to content

Commit 40b7680

Browse files
authored
Do not register error on gRPC client cancel (#6262)
Currently a gRPC client cancellation will be registered as an error but a cancellation is not an error. This change makes it so that OK and CANCELLED statuses are not registered as errors. Closes #6261 Signed-off-by: cfredri4 <[email protected]>
1 parent 158f6ab commit 40b7680

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

micrometer-core/src/main/java/io/micrometer/core/instrument/binder/grpc/ObservationGrpcClientCallListener.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public void onClose(Status status, Metadata trailers) {
5151
GrpcClientObservationContext context = (GrpcClientObservationContext) this.observation.getContext();
5252
context.setStatusCode(status.getCode());
5353
context.setTrailers(trailers);
54-
if (status.getCause() != null) {
54+
if (!Status.Code.OK.equals(status.getCode()) && !Status.Code.CANCELLED.equals(status.getCode())
55+
&& status.getCause() != null) {
5556
observation.error(status.getCause());
5657
}
5758
this.observation.stop();

micrometer-core/src/test/java/io/micrometer/core/instrument/binder/grpc/GrpcObservationTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,28 @@ void bidiStreamingRpc() {
339339
verifyHeaders();
340340
}
341341

342+
@Test
343+
void cancelledRpc() {
344+
SimpleServiceBlockingStub stub = SimpleServiceGrpc.newBlockingStub(channel);
345+
346+
SimpleRequest request = SimpleRequest.newBuilder().setRequestMessage("Hello").build();
347+
io.grpc.Context.CancellableContext context = io.grpc.Context.current().withCancellation();
348+
context.cancel(new RuntimeException("Cancelled!"));
349+
assertThatExceptionOfType(StatusRuntimeException.class)
350+
.isThrownBy(() -> context.run(() -> stub.unaryRpc(request)));
351+
352+
verifyClientContext("grpc.testing.SimpleService", "UnaryRpc", "grpc.testing.SimpleService/UnaryRpc",
353+
MethodType.UNARY);
354+
assertThat(clientHandler.getContext().getStatusCode()).isEqualTo(Code.CANCELLED);
355+
assertThat(clientHandler.getEvents()).containsExactly(GrpcClientEvents.MESSAGE_SENT);
356+
357+
TestObservationRegistryAssert.assertThat(observationRegistry).hasAnObservation(observationContextAssert -> {
358+
observationContextAssert.doesNotHaveError();
359+
observationContextAssert.hasNameEqualTo("grpc.client");
360+
assertCommonKeyValueNames(observationContextAssert);
361+
});
362+
}
363+
342364
private StreamObserver<SimpleResponse> createResponseObserver(List<String> messages, AtomicBoolean completed) {
343365
return new StreamObserver<>() {
344366

0 commit comments

Comments
 (0)