Reduce allocations in server conventions
This commit optimizes the default observation conventions to reduce `KeyValues` allocations.
This commit is contained in:
@@ -16,9 +16,14 @@
|
||||
|
||||
package org.springframework.http.server.observation;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import io.micrometer.common.KeyValue;
|
||||
import io.micrometer.common.KeyValues;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.server.observation.ServerHttpObservationDocumentation.HighCardinalityKeyNames;
|
||||
@@ -55,6 +60,8 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
|
||||
|
||||
private static final KeyValue HTTP_URL_UNKNOWN = KeyValue.of(HighCardinalityKeyNames.HTTP_URL, "UNKNOWN");
|
||||
|
||||
private static final Set<String> HTTP_METHODS = Stream.of(HttpMethod.values()).map(HttpMethod::name).collect(Collectors.toUnmodifiableSet());
|
||||
|
||||
|
||||
private final String name;
|
||||
|
||||
@@ -102,9 +109,13 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
|
||||
}
|
||||
|
||||
protected KeyValue method(ServerRequestObservationContext context) {
|
||||
return (context.getCarrier() != null) ?
|
||||
KeyValue.of(LowCardinalityKeyNames.METHOD, context.getCarrier().getMethod()) :
|
||||
METHOD_UNKNOWN;
|
||||
if (context.getCarrier() != null) {
|
||||
String httpMethod = context.getCarrier().getMethod();
|
||||
if (HTTP_METHODS.contains(httpMethod)) {
|
||||
return KeyValue.of(LowCardinalityKeyNames.METHOD, httpMethod);
|
||||
}
|
||||
}
|
||||
return METHOD_UNKNOWN;
|
||||
}
|
||||
|
||||
protected KeyValue status(ServerRequestObservationContext context) {
|
||||
|
||||
@@ -16,9 +16,12 @@
|
||||
|
||||
package org.springframework.http.server.reactive.observation;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import io.micrometer.common.KeyValue;
|
||||
import io.micrometer.common.KeyValues;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.server.reactive.observation.ServerHttpObservationDocumentation.HighCardinalityKeyNames;
|
||||
@@ -55,6 +58,8 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
|
||||
|
||||
private static final KeyValue HTTP_URL_UNKNOWN = KeyValue.of(HighCardinalityKeyNames.HTTP_URL, "UNKNOWN");
|
||||
|
||||
private static final Set<HttpMethod> HTTP_METHODS = Set.of(HttpMethod.values());
|
||||
|
||||
|
||||
private final String name;
|
||||
|
||||
@@ -102,9 +107,13 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
|
||||
}
|
||||
|
||||
protected KeyValue method(ServerRequestObservationContext context) {
|
||||
return (context.getCarrier() != null) ?
|
||||
KeyValue.of(LowCardinalityKeyNames.METHOD, context.getCarrier().getMethod().name()) :
|
||||
METHOD_UNKNOWN;
|
||||
if (context.getCarrier() != null) {
|
||||
HttpMethod method = context.getCarrier().getMethod();
|
||||
if (HTTP_METHODS.contains(method)) {
|
||||
return KeyValue.of(LowCardinalityKeyNames.METHOD, method.name());
|
||||
}
|
||||
}
|
||||
return METHOD_UNKNOWN;
|
||||
}
|
||||
|
||||
protected KeyValue status(ServerRequestObservationContext context) {
|
||||
|
||||
Reference in New Issue
Block a user