Polish Span and Meter Names

Closes gh-12156
This commit is contained in:
Josh Cummings 2022-11-16 13:52:06 -07:00
parent 88e64bac0c
commit e08ed89403
7 changed files with 94 additions and 94 deletions

View File

@ -72,11 +72,11 @@ public class HttpSecurityObservationTests {
ArgumentCaptor<Observation.Context> captor = ArgumentCaptor.forClass(Observation.Context.class); ArgumentCaptor<Observation.Context> captor = ArgumentCaptor.forClass(Observation.Context.class);
verify(handler, times(5)).onStart(captor.capture()); verify(handler, times(5)).onStart(captor.capture());
Iterator<Observation.Context> contexts = captor.getAllValues().iterator(); Iterator<Observation.Context> contexts = captor.getAllValues().iterator();
assertThat(contexts.next().getContextualName()).isEqualTo("spring.security.http.chains.before"); assertThat(contexts.next().getContextualName()).isEqualTo("security filterchain before");
assertThat(contexts.next().getName()).isEqualTo("spring.security.authentications"); assertThat(contexts.next().getName()).isEqualTo("spring.security.authentications");
assertThat(contexts.next().getName()).isEqualTo("spring.security.authorizations"); assertThat(contexts.next().getName()).isEqualTo("spring.security.authorizations");
assertThat(contexts.next().getName()).isEqualTo("spring.security.http.secured.requests"); assertThat(contexts.next().getName()).isEqualTo("spring.security.http.secured.requests");
assertThat(contexts.next().getContextualName()).isEqualTo("spring.security.http.chains.after"); assertThat(contexts.next().getContextualName()).isEqualTo("security filterchain after");
} }
@EnableWebSecurity @EnableWebSecurity

View File

@ -122,11 +122,11 @@ public class HttpConfigTests {
ArgumentCaptor<Observation.Context> captor = ArgumentCaptor.forClass(Observation.Context.class); ArgumentCaptor<Observation.Context> captor = ArgumentCaptor.forClass(Observation.Context.class);
verify(handler, times(5)).onStart(captor.capture()); verify(handler, times(5)).onStart(captor.capture());
Iterator<Observation.Context> contexts = captor.getAllValues().iterator(); Iterator<Observation.Context> contexts = captor.getAllValues().iterator();
assertThat(contexts.next().getContextualName()).isEqualTo("spring.security.http.chains.before"); assertThat(contexts.next().getContextualName()).isEqualTo("security filterchain before");
assertThat(contexts.next().getName()).isEqualTo("spring.security.authentications"); assertThat(contexts.next().getName()).isEqualTo("spring.security.authentications");
assertThat(contexts.next().getName()).isEqualTo("spring.security.authorizations"); assertThat(contexts.next().getName()).isEqualTo("spring.security.authorizations");
assertThat(contexts.next().getName()).isEqualTo("spring.security.http.secured.requests"); assertThat(contexts.next().getName()).isEqualTo("spring.security.http.secured.requests");
assertThat(contexts.next().getContextualName()).isEqualTo("spring.security.http.chains.after"); assertThat(contexts.next().getContextualName()).isEqualTo("security filterchain after");
} }
private String xml(String configName) { private String xml(String configName) {

View File

@ -43,6 +43,21 @@ public final class AuthenticationObservationConvention
return OBSERVATION_NAME; return OBSERVATION_NAME;
} }
@Override
public String getContextualName(AuthenticationObservationContext context) {
if (context.getAuthenticationRequest() != null) {
String authenticationType = context.getAuthenticationRequest().getClass().getSimpleName();
if (authenticationType.endsWith("Token")) {
authenticationType = authenticationType.substring(0, authenticationType.lastIndexOf("Token"));
}
if (authenticationType.endsWith("Authentication")) {
authenticationType = authenticationType.substring(0, authenticationType.lastIndexOf("Authentication"));
}
return "authenticate " + authenticationType.toLowerCase();
}
return "authenticate";
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View File

@ -19,6 +19,9 @@ package org.springframework.security.authorization;
import io.micrometer.common.KeyValues; import io.micrometer.common.KeyValues;
import io.micrometer.observation.Observation; import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationConvention; import io.micrometer.observation.ObservationConvention;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.security.authorization.method.MethodInvocationResult;
/** /**
* An {@link ObservationConvention} for translating authorizations into {@link KeyValues}. * An {@link ObservationConvention} for translating authorizations into {@link KeyValues}.
@ -39,14 +42,19 @@ public final class AuthorizationObservationConvention
return OBSERVATION_NAME; return OBSERVATION_NAME;
} }
@Override
public String getContextualName(AuthorizationObservationContext<?> context) {
return "authorize " + getObjectType(context);
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public KeyValues getLowCardinalityKeyValues(AuthorizationObservationContext<?> context) { public KeyValues getLowCardinalityKeyValues(AuthorizationObservationContext<?> context) {
return KeyValues.of("authentication.type", getAuthenticationType(context)) return KeyValues.of("spring.security.authentication.type", getAuthenticationType(context))
.and("object.type", getObjectType(context)) .and("spring.security.object", getObjectType(context))
.and("authorization.decision", getAuthorizationDecision(context)); .and("spring.security.authorization.decision", getAuthorizationDecision(context));
} }
/** /**
@ -54,8 +62,8 @@ public final class AuthorizationObservationConvention
*/ */
@Override @Override
public KeyValues getHighCardinalityKeyValues(AuthorizationObservationContext<?> context) { public KeyValues getHighCardinalityKeyValues(AuthorizationObservationContext<?> context) {
return KeyValues.of("authentication.authorities", getAuthorities(context)).and("authorization.decision.details", return KeyValues.of("spring.security.authentication.authorities", getAuthorities(context))
getDecisionDetails(context)); .and("spring.security.authorization.decision.details", getDecisionDetails(context));
} }
@Override @Override
@ -74,7 +82,20 @@ public final class AuthorizationObservationConvention
if (context.getObject() == null) { if (context.getObject() == null) {
return "unknown"; return "unknown";
} }
return context.getObject().getClass().getSimpleName(); if (context.getObject() instanceof MethodInvocation) {
return "method";
}
if (context.getObject() instanceof MethodInvocationResult) {
return "method";
}
String className = context.getObject().getClass().getSimpleName();
if (className.contains("Request")) {
return "request";
}
if (className.contains("Message")) {
return "message";
}
return className;
} }
private String getAuthorizationDecision(AuthorizationObservationContext<?> context) { private String getAuthorizationDecision(AuthorizationObservationContext<?> context) {

View File

@ -33,11 +33,11 @@ import org.springframework.security.core.Authentication;
*/ */
public final class ObservationSecurityContextChangedListener implements SecurityContextChangedListener { public final class ObservationSecurityContextChangedListener implements SecurityContextChangedListener {
static final String SECURITY_CONTEXT_CREATED = "security.context.created"; static final String SECURITY_CONTEXT_CREATED = "spring.security.context.created";
static final String SECURITY_CONTEXT_CHANGED = "security.context.changed"; static final String SECURITY_CONTEXT_CHANGED = "spring.security.context.changed";
static final String SECURITY_CONTEXT_CLEARED = "security.context.cleared"; static final String SECURITY_CONTEXT_CLEARED = "spring.security.context.cleared";
private final ObservationRegistry registry; private final ObservationRegistry registry;

View File

@ -37,11 +37,10 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.core.log.LogMessage; import org.springframework.core.log.LogMessage;
import org.springframework.security.web.util.UrlUtils;
/** /**
* A {@link org.springframework.security.web.server.FilterChainProxy.FilterChainDecorator} * A {@link org.springframework.security.web.FilterChainProxy.FilterChainDecorator} that
* that wraps the chain in before and after observations * wraps the chain in before and after observations
* *
* @author Josh Cummings * @author Josh Cummings
* @since 6.0 * @since 6.0
@ -75,14 +74,16 @@ public final class ObservationFilterChainDecorator implements FilterChainProxy.F
private FilterChain wrapSecured(FilterChain original) { private FilterChain wrapSecured(FilterChain original) {
return (req, res) -> { return (req, res) -> {
AroundFilterObservation parent = observation((HttpServletRequest) req); AroundFilterObservation parent = observation((HttpServletRequest) req);
Observation observation = Observation.createNotStarted(SECURED_OBSERVATION_NAME, this.registry); Observation observation = Observation.createNotStarted(SECURED_OBSERVATION_NAME, this.registry)
.contextualName("secured request");
parent.wrap(FilterObservation.create(observation).wrap(original)).doFilter(req, res); parent.wrap(FilterObservation.create(observation).wrap(original)).doFilter(req, res);
}; };
} }
private FilterChain wrapUnsecured(FilterChain original) { private FilterChain wrapUnsecured(FilterChain original) {
return (req, res) -> { return (req, res) -> {
Observation observation = Observation.createNotStarted(UNSECURED_OBSERVATION_NAME, this.registry); Observation observation = Observation.createNotStarted(UNSECURED_OBSERVATION_NAME, this.registry)
.contextualName("unsecured request");
FilterObservation.create(observation).wrap(original).doFilter(req, res); FilterObservation.create(observation).wrap(original).doFilter(req, res);
}; };
} }
@ -189,8 +190,8 @@ public final class ObservationFilterChainDecorator implements FilterChainProxy.F
} }
private AroundFilterObservation parent(HttpServletRequest request) { private AroundFilterObservation parent(HttpServletRequest request) {
FilterChainObservationContext beforeContext = FilterChainObservationContext.before(request); FilterChainObservationContext beforeContext = FilterChainObservationContext.before();
FilterChainObservationContext afterContext = FilterChainObservationContext.after(request); FilterChainObservationContext afterContext = FilterChainObservationContext.after();
Observation before = Observation.createNotStarted(this.convention, () -> beforeContext, this.registry); Observation before = Observation.createNotStarted(this.convention, () -> beforeContext, this.registry);
Observation after = Observation.createNotStarted(this.convention, () -> afterContext, this.registry); Observation after = Observation.createNotStarted(this.convention, () -> afterContext, this.registry);
AroundFilterObservation parent = AroundFilterObservation.create(before, after); AroundFilterObservation parent = AroundFilterObservation.create(before, after);
@ -409,8 +410,6 @@ public final class ObservationFilterChainDecorator implements FilterChainProxy.F
static final class FilterChainObservationContext extends Observation.Context { static final class FilterChainObservationContext extends Observation.Context {
private final ServletRequest request;
private final String filterSection; private final String filterSection;
private String filterName; private String filterName;
@ -419,29 +418,17 @@ public final class ObservationFilterChainDecorator implements FilterChainProxy.F
private int chainSize; private int chainSize;
private FilterChainObservationContext(ServletRequest request, String filterSection) { private FilterChainObservationContext(String filterSection) {
this.filterSection = filterSection; this.filterSection = filterSection;
this.request = request; setContextualName("security filterchain " + filterSection);
} }
static FilterChainObservationContext before(ServletRequest request) { static FilterChainObservationContext before() {
return new FilterChainObservationContext(request, "before"); return new FilterChainObservationContext("before");
} }
static FilterChainObservationContext after(ServletRequest request) { static FilterChainObservationContext after() {
return new FilterChainObservationContext(request, "after"); return new FilterChainObservationContext("after");
}
@Override
public void setName(String name) {
super.setName(name);
if (name != null) {
setContextualName(name + "." + this.filterSection);
}
}
String getRequestLine() {
return requestLine((HttpServletRequest) this.request);
} }
String getFilterSection() { String getFilterSection() {
@ -472,32 +459,31 @@ public final class ObservationFilterChainDecorator implements FilterChainProxy.F
this.chainSize = chainSize; this.chainSize = chainSize;
} }
private static String requestLine(HttpServletRequest request) {
return request.getMethod() + " " + UrlUtils.buildRequestUrl(request);
}
} }
static final class FilterChainObservationConvention static final class FilterChainObservationConvention
implements ObservationConvention<FilterChainObservationContext> { implements ObservationConvention<FilterChainObservationContext> {
static final String CHAIN_OBSERVATION_NAME = "spring.security.http.chains"; static final String CHAIN_OBSERVATION_NAME = "spring.security.filterchains";
private static final String REQUEST_LINE_NAME = "request.line"; private static final String CHAIN_POSITION_NAME = "spring.security.filterchain.position";
private static final String CHAIN_POSITION_NAME = "chain.position"; private static final String CHAIN_SIZE_NAME = "spring.security.filterchain.size";
private static final String CHAIN_SIZE_NAME = "chain.size"; private static final String FILTER_SECTION_NAME = "security.security.reached.filter.section";
private static final String FILTER_SECTION_NAME = "filter.section"; private static final String FILTER_NAME = "spring.security.reached.filter.name";
private static final String FILTER_NAME = "current.filter.name";
@Override @Override
public String getName() { public String getName() {
return CHAIN_OBSERVATION_NAME; return CHAIN_OBSERVATION_NAME;
} }
@Override
public String getContextualName(FilterChainObservationContext context) {
return "security filterchain " + context.getFilterSection();
}
@Override @Override
public KeyValues getLowCardinalityKeyValues(FilterChainObservationContext context) { public KeyValues getLowCardinalityKeyValues(FilterChainObservationContext context) {
KeyValues kv = KeyValues.of(CHAIN_SIZE_NAME, String.valueOf(context.getChainSize())) KeyValues kv = KeyValues.of(CHAIN_SIZE_NAME, String.valueOf(context.getChainSize()))
@ -509,12 +495,6 @@ public final class ObservationFilterChainDecorator implements FilterChainProxy.F
return kv; return kv;
} }
@Override
public KeyValues getHighCardinalityKeyValues(FilterChainObservationContext context) {
String requestLine = context.getRequestLine();
return KeyValues.of(REQUEST_LINE_NAME, requestLine);
}
@Override @Override
public boolean supportsContext(Observation.Context context) { public boolean supportsContext(Observation.Context context) {
return context instanceof FilterChainObservationContext; return context instanceof FilterChainObservationContext;

View File

@ -75,14 +75,16 @@ public final class ObservationWebFilterChainDecorator implements WebFilterChainP
private WebFilterChain wrapSecured(WebFilterChain original) { private WebFilterChain wrapSecured(WebFilterChain original) {
return (exchange) -> { return (exchange) -> {
AroundWebFilterObservation parent = observation(exchange); AroundWebFilterObservation parent = observation(exchange);
Observation observation = Observation.createNotStarted(SECURED_OBSERVATION_NAME, this.registry); Observation observation = Observation.createNotStarted(SECURED_OBSERVATION_NAME, this.registry)
.contextualName("secured request");
return parent.wrap(WebFilterObservation.create(observation).wrap(original)).filter(exchange); return parent.wrap(WebFilterObservation.create(observation).wrap(original)).filter(exchange);
}; };
} }
private WebFilterChain wrapUnsecured(WebFilterChain original) { private WebFilterChain wrapUnsecured(WebFilterChain original) {
return (exchange) -> { return (exchange) -> {
Observation observation = Observation.createNotStarted(UNSECURED_OBSERVATION_NAME, this.registry); Observation observation = Observation.createNotStarted(UNSECURED_OBSERVATION_NAME, this.registry)
.contextualName("unsecured request");
return WebFilterObservation.create(observation).wrap(original).filter(exchange); return WebFilterObservation.create(observation).wrap(original).filter(exchange);
}; };
} }
@ -210,8 +212,8 @@ public final class ObservationWebFilterChainDecorator implements WebFilterChainP
} }
private AroundWebFilterObservation parent(ServerWebExchange exchange) { private AroundWebFilterObservation parent(ServerWebExchange exchange) {
WebFilterChainObservationContext beforeContext = WebFilterChainObservationContext.before(exchange); WebFilterChainObservationContext beforeContext = WebFilterChainObservationContext.before();
WebFilterChainObservationContext afterContext = WebFilterChainObservationContext.after(exchange); WebFilterChainObservationContext afterContext = WebFilterChainObservationContext.after();
Observation before = Observation.createNotStarted(this.convention, () -> beforeContext, this.registry); Observation before = Observation.createNotStarted(this.convention, () -> beforeContext, this.registry);
Observation after = Observation.createNotStarted(this.convention, () -> afterContext, this.registry); Observation after = Observation.createNotStarted(this.convention, () -> afterContext, this.registry);
AroundWebFilterObservation parent = AroundWebFilterObservation.create(before, after); AroundWebFilterObservation parent = AroundWebFilterObservation.create(before, after);
@ -419,8 +421,6 @@ public final class ObservationWebFilterChainDecorator implements WebFilterChainP
static final class WebFilterChainObservationContext extends Observation.Context { static final class WebFilterChainObservationContext extends Observation.Context {
private final ServerWebExchange exchange;
private final String filterSection; private final String filterSection;
private String filterName; private String filterName;
@ -429,29 +429,16 @@ public final class ObservationWebFilterChainDecorator implements WebFilterChainP
private int chainSize; private int chainSize;
private WebFilterChainObservationContext(ServerWebExchange exchange, String filterSection) { private WebFilterChainObservationContext(String filterSection) {
this.exchange = exchange;
this.filterSection = filterSection; this.filterSection = filterSection;
} }
static WebFilterChainObservationContext before(ServerWebExchange exchange) { static WebFilterChainObservationContext before() {
return new WebFilterChainObservationContext(exchange, "before"); return new WebFilterChainObservationContext("before");
} }
static WebFilterChainObservationContext after(ServerWebExchange exchange) { static WebFilterChainObservationContext after() {
return new WebFilterChainObservationContext(exchange, "after"); return new WebFilterChainObservationContext("after");
}
@Override
public void setName(String name) {
super.setName(name);
if (name != null) {
setContextualName(name + "." + this.filterSection);
}
}
String getRequestLine() {
return this.exchange.getRequest().getPath().toString();
} }
String getFilterSection() { String getFilterSection() {
@ -487,23 +474,26 @@ public final class ObservationWebFilterChainDecorator implements WebFilterChainP
static final class WebFilterChainObservationConvention static final class WebFilterChainObservationConvention
implements ObservationConvention<WebFilterChainObservationContext> { implements ObservationConvention<WebFilterChainObservationContext> {
static final String CHAIN_OBSERVATION_NAME = "spring.security.http.chains"; static final String CHAIN_OBSERVATION_NAME = "spring.security.filterchains";
private static final String REQUEST_LINE_NAME = "request.line"; private static final String CHAIN_POSITION_NAME = "spring.security.filterchain.position";
private static final String CHAIN_POSITION_NAME = "chain.position"; private static final String CHAIN_SIZE_NAME = "spring.security.filterchain.size";
private static final String CHAIN_SIZE_NAME = "chain.size"; private static final String FILTER_SECTION_NAME = "spring.security.reached.filter.section";
private static final String FILTER_SECTION_NAME = "filter.section"; private static final String FILTER_NAME = "spring.security.reached.filter.name";
private static final String FILTER_NAME = "current.filter.name";
@Override @Override
public String getName() { public String getName() {
return CHAIN_OBSERVATION_NAME; return CHAIN_OBSERVATION_NAME;
} }
@Override
public String getContextualName(WebFilterChainObservationContext context) {
return "security filterchain " + context.getFilterSection();
}
@Override @Override
public KeyValues getLowCardinalityKeyValues(WebFilterChainObservationContext context) { public KeyValues getLowCardinalityKeyValues(WebFilterChainObservationContext context) {
KeyValues kv = KeyValues.of(CHAIN_SIZE_NAME, String.valueOf(context.getChainSize())) KeyValues kv = KeyValues.of(CHAIN_SIZE_NAME, String.valueOf(context.getChainSize()))
@ -515,12 +505,6 @@ public final class ObservationWebFilterChainDecorator implements WebFilterChainP
return kv; return kv;
} }
@Override
public KeyValues getHighCardinalityKeyValues(WebFilterChainObservationContext context) {
String requestLine = context.getRequestLine();
return KeyValues.of(REQUEST_LINE_NAME, requestLine);
}
@Override @Override
public boolean supportsContext(Observation.Context context) { public boolean supportsContext(Observation.Context context) {
return context instanceof WebFilterChainObservationContext; return context instanceof WebFilterChainObservationContext;