Skip to content

Commit

Permalink
Improve Tags generation for methods names
Browse files Browse the repository at this point in the history
This commit optimizes the `Tag` generation for method names by only
allocating new `Tag` instances for well-known method names. Others will
be marked as "UNKNOWN".
  • Loading branch information
bclozel committed Nov 22, 2023
1 parent 7ad6e44 commit 5490e73
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@
import io.micrometer.core.instrument.Tag;

import org.springframework.boot.actuate.metrics.http.Outcome;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.util.StringUtils;
Expand Down Expand Up @@ -53,6 +54,8 @@ public final class WebFluxTags {

private static final Tag EXCEPTION_NONE = Tag.of("exception", "None");

private static final Tag METHOD_UNKNOWN = Tag.of("method", "UNKNOWN");

private static final Pattern FORWARD_SLASHES_PATTERN = Pattern.compile("//+");

private static final Set<String> DISCONNECTED_CLIENT_EXCEPTIONS = new HashSet<>(
Expand All @@ -70,7 +73,11 @@ private WebFluxTags() {
* @return the method tag whose value is a capitalized method (e.g. GET).
*/
public static Tag method(ServerWebExchange exchange) {
return Tag.of("method", exchange.getRequest().getMethodValue());
HttpMethod httpMethod = exchange.getRequest().getMethod();
if (httpMethod != null) {
return Tag.of("method", httpMethod.name());
}
return METHOD_UNKNOWN;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@
import io.micrometer.core.instrument.Tag;

import org.springframework.boot.actuate.metrics.http.Outcome;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerMapping;
Expand Down Expand Up @@ -71,7 +72,13 @@ private WebMvcTags() {
* @return the method tag whose value is a capitalized method (e.g. GET).
*/
public static Tag method(HttpServletRequest request) {
return (request != null) ? Tag.of("method", request.getMethod()) : METHOD_UNKNOWN;
if (request != null) {
HttpMethod httpMethod = HttpMethod.resolve(request.getMethod());
if (httpMethod != null) {
return Tag.of("method", httpMethod.name());
}
}
return METHOD_UNKNOWN;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -179,4 +179,18 @@ void outcomeTagIsUnknownWhenResponseStatusIsInUnknownSeries() {
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
}

@Test
void methodTagIsWellKnownHttpMethod() {
this.request.setMethod("GET");
Tag tag = WebMvcTags.method(this.request);
assertThat(tag.getValue()).isEqualTo("GET");
}

@Test
void methodTagForUnknownHttpMethods() {
this.request.setMethod("TEST");
Tag tag = WebMvcTags.method(this.request);
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
Expand Down Expand Up @@ -130,13 +131,23 @@ void uriTagValueIsUnknownWhenRequestHasNoPatternAndNonRootPathInfo() {
}

@Test
void methodTagToleratesNonStandardHttpMethods() {
void methodTagValueIsHttpMethod() {
ServerWebExchange exchange = mock(ServerWebExchange.class);
ServerHttpRequest request = mock(ServerHttpRequest.class);
given(exchange.getRequest()).willReturn(request);
given(request.getMethodValue()).willReturn("CUSTOM");
given(request.getMethod()).willReturn(HttpMethod.GET);
Tag tag = WebFluxTags.method(exchange);
assertThat(tag.getValue()).isEqualTo("CUSTOM");
assertThat(tag.getValue()).isEqualTo("GET");
}

@Test
void methodTagMarksNonStandardHttpMethodsAsUnknown() {
ServerWebExchange exchange = mock(ServerWebExchange.class);
ServerHttpRequest request = mock(ServerHttpRequest.class);
given(exchange.getRequest()).willReturn(request);
given(request.getMethod()).willReturn(null);
Tag tag = WebFluxTags.method(exchange);
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
}

@Test
Expand Down

0 comments on commit 5490e73

Please sign in to comment.