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".
This commit is contained in:
parent
7ad6e4470c
commit
5490e73922
@ -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.
|
||||
@ -24,6 +24,7 @@ import java.util.regex.Pattern;
|
||||
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;
|
||||
@ -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<>(
|
||||
@ -70,7 +73,11 @@ public final class 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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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.
|
||||
@ -24,6 +24,7 @@ import javax.servlet.http.HttpServletResponse;
|
||||
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;
|
||||
@ -71,7 +72,13 @@ public final class 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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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.
|
||||
@ -179,4 +179,18 @@ class WebMvcTagsTests {
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ import io.micrometer.core.instrument.Tag;
|
||||
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;
|
||||
@ -130,13 +131,23 @@ class WebFluxTagsTests {
|
||||
}
|
||||
|
||||
@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
|
||||
|
Loading…
x
Reference in New Issue
Block a user