Polishing

This commit is contained in:
Sam Brannen 2022-11-03 16:02:19 +01:00
parent 151977259c
commit d492086c76
2 changed files with 58 additions and 40 deletions

View File

@ -21,10 +21,13 @@ import io.micrometer.common.KeyValues;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode; import org.springframework.http.HttpStatusCode;
import org.springframework.http.observation.ServerHttpObservationDocumentation.HighCardinalityKeyNames;
import org.springframework.http.observation.ServerHttpObservationDocumentation.LowCardinalityKeyNames;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
* Default {@link ServerRequestObservationConvention}. * Default {@link ServerRequestObservationConvention}.
*
* @author Brian Clozel * @author Brian Clozel
* @since 6.0 * @since 6.0
*/ */
@ -32,28 +35,30 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
private static final String DEFAULT_NAME = "http.server.requests"; private static final String DEFAULT_NAME = "http.server.requests";
private static final KeyValue METHOD_UNKNOWN = KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.METHOD, "UNKNOWN"); private static final KeyValue METHOD_UNKNOWN = KeyValue.of(LowCardinalityKeyNames.METHOD, "UNKNOWN");
private static final KeyValue STATUS_UNKNOWN = KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.STATUS, "UNKNOWN"); private static final KeyValue STATUS_UNKNOWN = KeyValue.of(LowCardinalityKeyNames.STATUS, "UNKNOWN");
private static final KeyValue HTTP_OUTCOME_SUCCESS = KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.OUTCOME, "SUCCESS"); private static final KeyValue HTTP_OUTCOME_SUCCESS = KeyValue.of(LowCardinalityKeyNames.OUTCOME, "SUCCESS");
private static final KeyValue HTTP_OUTCOME_UNKNOWN = KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.OUTCOME, "UNKNOWN"); private static final KeyValue HTTP_OUTCOME_UNKNOWN = KeyValue.of(LowCardinalityKeyNames.OUTCOME, "UNKNOWN");
private static final KeyValue URI_UNKNOWN = KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.URI, "UNKNOWN"); private static final KeyValue URI_UNKNOWN = KeyValue.of(LowCardinalityKeyNames.URI, "UNKNOWN");
private static final KeyValue URI_ROOT = KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.URI, "root"); private static final KeyValue URI_ROOT = KeyValue.of(LowCardinalityKeyNames.URI, "root");
private static final KeyValue URI_NOT_FOUND = KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.URI, "NOT_FOUND"); private static final KeyValue URI_NOT_FOUND = KeyValue.of(LowCardinalityKeyNames.URI, "NOT_FOUND");
private static final KeyValue URI_REDIRECTION = KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.URI, "REDIRECTION"); private static final KeyValue URI_REDIRECTION = KeyValue.of(LowCardinalityKeyNames.URI, "REDIRECTION");
private static final KeyValue EXCEPTION_NONE = KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.EXCEPTION, KeyValue.NONE_VALUE); private static final KeyValue EXCEPTION_NONE = KeyValue.of(LowCardinalityKeyNames.EXCEPTION, KeyValue.NONE_VALUE);
private static final KeyValue HTTP_URL_UNKNOWN = KeyValue.of(HighCardinalityKeyNames.HTTP_URL, "UNKNOWN");
private static final KeyValue HTTP_URL_UNKNOWN = KeyValue.of(ServerHttpObservationDocumentation.HighCardinalityKeyNames.HTTP_URL, "UNKNOWN");
private final String name; private final String name;
/** /**
* Create a convention with the default name {@code "http.server.requests"}. * Create a convention with the default name {@code "http.server.requests"}.
*/ */
@ -69,6 +74,7 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
this.name = name; this.name = name;
} }
@Override @Override
public String getName() { public String getName() {
return this.name; return this.name;
@ -76,11 +82,11 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
@Override @Override
public String getContextualName(ServerRequestObservationContext context) { public String getContextualName(ServerRequestObservationContext context) {
String httpMethod = context.getCarrier().getMethod().toLowerCase();
if (context.getPathPattern() != null) { if (context.getPathPattern() != null) {
return String.format("http %s %s", context.getCarrier().getMethod().toLowerCase(), return "http %s %s".formatted(httpMethod, context.getPathPattern());
context.getPathPattern());
} }
return "http " + context.getCarrier().getMethod().toLowerCase(); return "http " + httpMethod;
} }
@Override @Override
@ -94,11 +100,15 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
} }
protected KeyValue method(ServerRequestObservationContext context) { protected KeyValue method(ServerRequestObservationContext context) {
return (context.getCarrier() != null) ? KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.METHOD, context.getCarrier().getMethod()) : METHOD_UNKNOWN; return (context.getCarrier() != null) ?
KeyValue.of(LowCardinalityKeyNames.METHOD, context.getCarrier().getMethod()) :
METHOD_UNKNOWN;
} }
protected KeyValue status(ServerRequestObservationContext context) { protected KeyValue status(ServerRequestObservationContext context) {
return (context.getResponse() != null) ? KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.STATUS, Integer.toString(context.getResponse().getStatus())) : STATUS_UNKNOWN; return (context.getResponse() != null) ?
KeyValue.of(LowCardinalityKeyNames.STATUS, Integer.toString(context.getResponse().getStatus())) :
STATUS_UNKNOWN;
} }
protected KeyValue uri(ServerRequestObservationContext context) { protected KeyValue uri(ServerRequestObservationContext context) {
@ -108,7 +118,7 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
if (pattern.isEmpty()) { if (pattern.isEmpty()) {
return URI_ROOT; return URI_ROOT;
} }
return KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.URI, pattern); return KeyValue.of(LowCardinalityKeyNames.URI, pattern);
} }
if (context.getResponse() != null) { if (context.getResponse() != null) {
HttpStatus status = HttpStatus.resolve(context.getResponse().getStatus()); HttpStatus status = HttpStatus.resolve(context.getResponse().getStatus());
@ -129,7 +139,7 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
Throwable error = context.getError(); Throwable error = context.getError();
if (error != null) { if (error != null) {
String simpleName = error.getClass().getSimpleName(); String simpleName = error.getClass().getSimpleName();
return KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.EXCEPTION, return KeyValue.of(LowCardinalityKeyNames.EXCEPTION,
StringUtils.hasText(simpleName) ? simpleName : error.getClass().getName()); StringUtils.hasText(simpleName) ? simpleName : error.getClass().getName());
} }
return EXCEPTION_NONE; return EXCEPTION_NONE;
@ -145,7 +155,7 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
protected KeyValue httpUrl(ServerRequestObservationContext context) { protected KeyValue httpUrl(ServerRequestObservationContext context) {
if (context.getCarrier() != null) { if (context.getCarrier() != null) {
return KeyValue.of(ServerHttpObservationDocumentation.HighCardinalityKeyNames.HTTP_URL, context.getCarrier().getRequestURI()); return KeyValue.of(HighCardinalityKeyNames.HTTP_URL, context.getCarrier().getRequestURI());
} }
return HTTP_URL_UNKNOWN; return HTTP_URL_UNKNOWN;
} }
@ -158,7 +168,7 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
return HTTP_OUTCOME_SUCCESS; return HTTP_OUTCOME_SUCCESS;
} }
else if (statusCode instanceof HttpStatus status) { else if (statusCode instanceof HttpStatus status) {
return KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.OUTCOME, status.series().name()); return KeyValue.of(LowCardinalityKeyNames.OUTCOME, status.series().name());
} }
else { else {
return HTTP_OUTCOME_UNKNOWN; return HTTP_OUTCOME_UNKNOWN;

View File

@ -21,6 +21,8 @@ import io.micrometer.common.KeyValues;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode; import org.springframework.http.HttpStatusCode;
import org.springframework.http.observation.reactive.ServerHttpObservationDocumentation.HighCardinalityKeyNames;
import org.springframework.http.observation.reactive.ServerHttpObservationDocumentation.LowCardinalityKeyNames;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.util.pattern.PathPattern; import org.springframework.web.util.pattern.PathPattern;
@ -34,28 +36,30 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
private static final String DEFAULT_NAME = "http.server.requests"; private static final String DEFAULT_NAME = "http.server.requests";
private static final KeyValue METHOD_UNKNOWN = KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.METHOD, "UNKNOWN"); private static final KeyValue METHOD_UNKNOWN = KeyValue.of(LowCardinalityKeyNames.METHOD, "UNKNOWN");
private static final KeyValue STATUS_UNKNOWN = KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.STATUS, "UNKNOWN"); private static final KeyValue STATUS_UNKNOWN = KeyValue.of(LowCardinalityKeyNames.STATUS, "UNKNOWN");
private static final KeyValue HTTP_OUTCOME_SUCCESS = KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.OUTCOME, "SUCCESS"); private static final KeyValue HTTP_OUTCOME_SUCCESS = KeyValue.of(LowCardinalityKeyNames.OUTCOME, "SUCCESS");
private static final KeyValue HTTP_OUTCOME_UNKNOWN = KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.OUTCOME, "UNKNOWN"); private static final KeyValue HTTP_OUTCOME_UNKNOWN = KeyValue.of(LowCardinalityKeyNames.OUTCOME, "UNKNOWN");
private static final KeyValue URI_UNKNOWN = KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.URI, "UNKNOWN"); private static final KeyValue URI_UNKNOWN = KeyValue.of(LowCardinalityKeyNames.URI, "UNKNOWN");
private static final KeyValue URI_ROOT = KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.URI, "root"); private static final KeyValue URI_ROOT = KeyValue.of(LowCardinalityKeyNames.URI, "root");
private static final KeyValue URI_NOT_FOUND = KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.URI, "NOT_FOUND"); private static final KeyValue URI_NOT_FOUND = KeyValue.of(LowCardinalityKeyNames.URI, "NOT_FOUND");
private static final KeyValue URI_REDIRECTION = KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.URI, "REDIRECTION"); private static final KeyValue URI_REDIRECTION = KeyValue.of(LowCardinalityKeyNames.URI, "REDIRECTION");
private static final KeyValue EXCEPTION_NONE = KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.EXCEPTION, KeyValue.NONE_VALUE); private static final KeyValue EXCEPTION_NONE = KeyValue.of(LowCardinalityKeyNames.EXCEPTION, KeyValue.NONE_VALUE);
private static final KeyValue HTTP_URL_UNKNOWN = KeyValue.of(HighCardinalityKeyNames.HTTP_URL, "UNKNOWN");
private static final KeyValue HTTP_URL_UNKNOWN = KeyValue.of(ServerHttpObservationDocumentation.HighCardinalityKeyNames.HTTP_URL, "UNKNOWN");
private final String name; private final String name;
/** /**
* Create a convention with the default name {@code "http.server.requests"}. * Create a convention with the default name {@code "http.server.requests"}.
*/ */
@ -65,13 +69,13 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
/** /**
* Create a convention with a custom name. * Create a convention with a custom name.
*
* @param name the observation name * @param name the observation name
*/ */
public DefaultServerRequestObservationConvention(String name) { public DefaultServerRequestObservationConvention(String name) {
this.name = name; this.name = name;
} }
@Override @Override
public String getName() { public String getName() {
return this.name; return this.name;
@ -79,11 +83,11 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
@Override @Override
public String getContextualName(ServerRequestObservationContext context) { public String getContextualName(ServerRequestObservationContext context) {
String httpMethod = context.getCarrier().getMethod().name().toLowerCase();
if (context.getPathPattern() != null) { if (context.getPathPattern() != null) {
return String.format("http %s %s", context.getCarrier().getMethod().name().toLowerCase(), return "http %s %s".formatted(httpMethod, context.getPathPattern());
context.getPathPattern().toString());
} }
return "http " + context.getCarrier().getMethod().name().toLowerCase(); return "http " + httpMethod;
} }
@Override @Override
@ -97,7 +101,9 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
} }
protected KeyValue method(ServerRequestObservationContext context) { protected KeyValue method(ServerRequestObservationContext context) {
return (context.getCarrier() != null) ? KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.METHOD, context.getCarrier().getMethod().name()) : METHOD_UNKNOWN; return (context.getCarrier() != null) ?
KeyValue.of(LowCardinalityKeyNames.METHOD, context.getCarrier().getMethod().name()) :
METHOD_UNKNOWN;
} }
protected KeyValue status(ServerRequestObservationContext context) { protected KeyValue status(ServerRequestObservationContext context) {
@ -105,7 +111,8 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
return STATUS_UNKNOWN; return STATUS_UNKNOWN;
} }
return (context.getResponse() != null && context.getResponse().getStatusCode() != null) ? return (context.getResponse() != null && context.getResponse().getStatusCode() != null) ?
KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.STATUS, Integer.toString(context.getResponse().getStatusCode().value())) : STATUS_UNKNOWN; KeyValue.of(LowCardinalityKeyNames.STATUS, Integer.toString(context.getResponse().getStatusCode().value())) :
STATUS_UNKNOWN;
} }
protected KeyValue uri(ServerRequestObservationContext context) { protected KeyValue uri(ServerRequestObservationContext context) {
@ -115,7 +122,7 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
if (pattern.toString().isEmpty()) { if (pattern.toString().isEmpty()) {
return URI_ROOT; return URI_ROOT;
} }
return KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.URI, pattern.toString()); return KeyValue.of(LowCardinalityKeyNames.URI, pattern.toString());
} }
if (context.getResponse() != null && context.getResponse().getStatusCode() != null) { if (context.getResponse() != null && context.getResponse().getStatusCode() != null) {
HttpStatus status = HttpStatus.resolve(context.getResponse().getStatusCode().value()); HttpStatus status = HttpStatus.resolve(context.getResponse().getStatusCode().value());
@ -136,8 +143,8 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
Throwable error = context.getError(); Throwable error = context.getError();
if (error != null) { if (error != null) {
String simpleName = error.getClass().getSimpleName(); String simpleName = error.getClass().getSimpleName();
return KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.EXCEPTION, return KeyValue.of(LowCardinalityKeyNames.EXCEPTION, StringUtils.hasText(simpleName) ?
StringUtils.hasText(simpleName) ? simpleName : error.getClass().getName()); simpleName : error.getClass().getName());
} }
return EXCEPTION_NONE; return EXCEPTION_NONE;
} }
@ -154,11 +161,12 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
protected KeyValue httpUrl(ServerRequestObservationContext context) { protected KeyValue httpUrl(ServerRequestObservationContext context) {
if (context.getCarrier() != null) { if (context.getCarrier() != null) {
return KeyValue.of(ServerHttpObservationDocumentation.HighCardinalityKeyNames.HTTP_URL, context.getCarrier().getPath().toString()); return KeyValue.of(HighCardinalityKeyNames.HTTP_URL, context.getCarrier().getPath().toString());
} }
return HTTP_URL_UNKNOWN; return HTTP_URL_UNKNOWN;
} }
static class HttpOutcome { static class HttpOutcome {
static KeyValue forStatus(HttpStatusCode statusCode) { static KeyValue forStatus(HttpStatusCode statusCode) {
@ -166,7 +174,7 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
return HTTP_OUTCOME_SUCCESS; return HTTP_OUTCOME_SUCCESS;
} }
else if (statusCode instanceof HttpStatus status) { else if (statusCode instanceof HttpStatus status) {
return KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.OUTCOME, status.series().name()); return KeyValue.of(LowCardinalityKeyNames.OUTCOME, status.series().name());
} }
else { else {
return HTTP_OUTCOME_UNKNOWN; return HTTP_OUTCOME_UNKNOWN;