Make reason tags upper-case

This commit is contained in:
Ravi Khadiwala
2026-03-30 16:35:43 -05:00
committed by ravi-signal
parent 0ee06d83b7
commit 04c4d993a6
2 changed files with 11 additions and 10 deletions

View File

@@ -27,6 +27,7 @@ import io.micrometer.core.instrument.Timer;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Nullable;
@@ -47,9 +48,9 @@ public class MetricServerInterceptor implements ServerInterceptor {
private static final String TAG_REASON = "reason";
@VisibleForTesting
static final String DEFAULT_SUCCESS_REASON = "success";
static final String DEFAULT_SUCCESS_REASON = "SUCCESS";
@VisibleForTesting
static final String DEFAULT_ERROR_REASON = "n/a";
static final String DEFAULT_ERROR_REASON = "N/A";
@VisibleForTesting
static final String REQUEST_MESSAGE_COUNTER_NAME = MetricsUtil.name(MetricServerInterceptor.class, "requestMessage");
@@ -124,7 +125,7 @@ public class MetricServerInterceptor implements ServerInterceptor {
}
Tags responseTags = tags.and(Tag.of(TAG_STATUS_CODE, status.getCode().name()));
if (reason != null) {
responseTags = responseTags.and(TAG_REASON, reason);
responseTags = responseTags.and(TAG_REASON, reason.toUpperCase(Locale.ROOT));
}
meterRegistry.counter(RPC_COUNTER_NAME, responseTags).increment();
super.close(status, responseHeaders);

View File

@@ -198,15 +198,15 @@ public class MetricServerInterceptorTest {
static Stream<Arguments> testUnaryOkResponseReason() {
return Stream.of(
Arguments.argumentSet("Default reason", TagResponse.newBuilder().build(), "success"),
Arguments.argumentSet("No reason", TagResponse.newBuilder().setNoReason(true).build(), "success"),
Arguments.argumentSet("Explicitly set reason", TagResponse.newBuilder().setReason1(true).build(), "reason_1"),
Arguments.argumentSet("Nested reason", TagResponse.newBuilder().setNestedReason(TagResponse.NestedReason.newBuilder().setReason(true)).build(), "nested_reason"));
Arguments.argumentSet("Default reason", TagResponse.newBuilder().build(), "SUCCESS"),
Arguments.argumentSet("No reason", TagResponse.newBuilder().setNoReason(true).build(), "SUCCESS"),
Arguments.argumentSet("Explicitly set reason", TagResponse.newBuilder().setReason1(true).build(), "REASON_1"),
Arguments.argumentSet("Nested reason", TagResponse.newBuilder().setNestedReason(TagResponse.NestedReason.newBuilder().setReason(true)).build(), "NESTED_REASON"));
}
@ParameterizedTest
@MethodSource
void testUnaryOkResponseReason(TagResponse response, String expectedReason) throws InterruptedException {
void testUnaryOkResponseReason(TagResponse response, String expectedReason) {
final TagTestServiceGrpc.TagTestServiceBlockingStub tagTestServiceBlockingStub =
TagTestServiceGrpc.newBlockingStub(channel);
when(tagResponseSupplier.get()).thenReturn(response);
@@ -229,13 +229,13 @@ public class MetricServerInterceptorTest {
// We make no promises if proto fields that have reason tags are present on a message, but this tests for the sane
// behavior that at least one of these tags makes it into the metric.
assertThat(find(Counter.class, MetricServerInterceptor.RPC_COUNTER_NAME).getId().getTag("reason"))
.isIn("duplicate_reason", "reason_1");
.isIn("DUPLICATE_REASON", "REASON_1");
}
@CartesianTest
public void testStatusErrorResponseReason(
@CartesianTest.Enum(mode = CartesianTest.Enum.Mode.EXCLUDE, names = {"OK"}) Status.Code statusCode,
@CartesianTest.Values(strings = {"test", "", "null"}) String reasonParam) {
@CartesianTest.Values(strings = {"TEST", "", "null"}) String reasonParam) {
final String reason, expectedReasonTag;
if (reasonParam.equals("null")) {