Skip to content

Commit 5490e73

Browse files
committed
Improve Tags generation for methods names
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".
1 parent 7ad6e44 commit 5490e73

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTags.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import io.micrometer.core.instrument.Tag;
2525

2626
import org.springframework.boot.actuate.metrics.http.Outcome;
27+
import org.springframework.http.HttpMethod;
2728
import org.springframework.http.HttpStatus;
2829
import org.springframework.http.server.reactive.ServerHttpResponse;
2930
import org.springframework.util.StringUtils;
@@ -53,6 +54,8 @@ public final class WebFluxTags {
5354

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

57+
private static final Tag METHOD_UNKNOWN = Tag.of("method", "UNKNOWN");
58+
5659
private static final Pattern FORWARD_SLASHES_PATTERN = Pattern.compile("//+");
5760

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

7683
/**

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import io.micrometer.core.instrument.Tag;
2525

2626
import org.springframework.boot.actuate.metrics.http.Outcome;
27+
import org.springframework.http.HttpMethod;
2728
import org.springframework.http.HttpStatus;
2829
import org.springframework.util.StringUtils;
2930
import org.springframework.web.servlet.HandlerMapping;
@@ -71,7 +72,13 @@ private WebMvcTags() {
7172
* @return the method tag whose value is a capitalized method (e.g. GET).
7273
*/
7374
public static Tag method(HttpServletRequest request) {
74-
return (request != null) ? Tag.of("method", request.getMethod()) : METHOD_UNKNOWN;
75+
if (request != null) {
76+
HttpMethod httpMethod = HttpMethod.resolve(request.getMethod());
77+
if (httpMethod != null) {
78+
return Tag.of("method", httpMethod.name());
79+
}
80+
}
81+
return METHOD_UNKNOWN;
7582
}
7683

7784
/**

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/servlet/WebMvcTagsTests.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -179,4 +179,18 @@ void outcomeTagIsUnknownWhenResponseStatusIsInUnknownSeries() {
179179
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
180180
}
181181

182+
@Test
183+
void methodTagIsWellKnownHttpMethod() {
184+
this.request.setMethod("GET");
185+
Tag tag = WebMvcTags.method(this.request);
186+
assertThat(tag.getValue()).isEqualTo("GET");
187+
}
188+
189+
@Test
190+
void methodTagForUnknownHttpMethods() {
191+
this.request.setMethod("TEST");
192+
Tag tag = WebMvcTags.method(this.request);
193+
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
194+
}
195+
182196
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTagsTests.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.jupiter.api.BeforeEach;
2323
import org.junit.jupiter.api.Test;
2424

25+
import org.springframework.http.HttpMethod;
2526
import org.springframework.http.HttpStatus;
2627
import org.springframework.http.server.reactive.ServerHttpRequest;
2728
import org.springframework.http.server.reactive.ServerHttpResponse;
@@ -130,13 +131,23 @@ void uriTagValueIsUnknownWhenRequestHasNoPatternAndNonRootPathInfo() {
130131
}
131132

132133
@Test
133-
void methodTagToleratesNonStandardHttpMethods() {
134+
void methodTagValueIsHttpMethod() {
134135
ServerWebExchange exchange = mock(ServerWebExchange.class);
135136
ServerHttpRequest request = mock(ServerHttpRequest.class);
136137
given(exchange.getRequest()).willReturn(request);
137-
given(request.getMethodValue()).willReturn("CUSTOM");
138+
given(request.getMethod()).willReturn(HttpMethod.GET);
138139
Tag tag = WebFluxTags.method(exchange);
139-
assertThat(tag.getValue()).isEqualTo("CUSTOM");
140+
assertThat(tag.getValue()).isEqualTo("GET");
141+
}
142+
143+
@Test
144+
void methodTagMarksNonStandardHttpMethodsAsUnknown() {
145+
ServerWebExchange exchange = mock(ServerWebExchange.class);
146+
ServerHttpRequest request = mock(ServerHttpRequest.class);
147+
given(exchange.getRequest()).willReturn(request);
148+
given(request.getMethod()).willReturn(null);
149+
Tag tag = WebFluxTags.method(exchange);
150+
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
140151
}
141152

142153
@Test

0 commit comments

Comments
 (0)