Skip to content

Commit 102e38e

Browse files
committed
Polish Span and Meter Names
Closes spring-projectsgh-12156
1 parent a01d548 commit 102e38e

File tree

7 files changed

+94
-94
lines changed

7 files changed

+94
-94
lines changed

config/src/test/java/org/springframework/security/config/annotation/web/configurers/HttpSecurityObservationTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ public void getWhenUsingObservationRegistryThenObservesRequest() throws Exceptio
7272
ArgumentCaptor<Observation.Context> captor = ArgumentCaptor.forClass(Observation.Context.class);
7373
verify(handler, times(5)).onStart(captor.capture());
7474
Iterator<Observation.Context> contexts = captor.getAllValues().iterator();
75-
assertThat(contexts.next().getContextualName()).isEqualTo("spring.security.http.chains.before");
75+
assertThat(contexts.next().getContextualName()).isEqualTo("security filterchain before");
7676
assertThat(contexts.next().getName()).isEqualTo("spring.security.authentications");
7777
assertThat(contexts.next().getName()).isEqualTo("spring.security.authorizations");
7878
assertThat(contexts.next().getName()).isEqualTo("spring.security.http.secured.requests");
79-
assertThat(contexts.next().getContextualName()).isEqualTo("spring.security.http.chains.after");
79+
assertThat(contexts.next().getContextualName()).isEqualTo("security filterchain after");
8080
}
8181

8282
@EnableWebSecurity

config/src/test/java/org/springframework/security/config/http/HttpConfigTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ public void getWhenUsingObservationRegistryThenObservesRequest() throws Exceptio
122122
ArgumentCaptor<Observation.Context> captor = ArgumentCaptor.forClass(Observation.Context.class);
123123
verify(handler, times(5)).onStart(captor.capture());
124124
Iterator<Observation.Context> contexts = captor.getAllValues().iterator();
125-
assertThat(contexts.next().getContextualName()).isEqualTo("spring.security.http.chains.before");
125+
assertThat(contexts.next().getContextualName()).isEqualTo("security filterchain before");
126126
assertThat(contexts.next().getName()).isEqualTo("spring.security.authentications");
127127
assertThat(contexts.next().getName()).isEqualTo("spring.security.authorizations");
128128
assertThat(contexts.next().getName()).isEqualTo("spring.security.http.secured.requests");
129-
assertThat(contexts.next().getContextualName()).isEqualTo("spring.security.http.chains.after");
129+
assertThat(contexts.next().getContextualName()).isEqualTo("security filterchain after");
130130
}
131131

132132
private String xml(String configName) {

core/src/main/java/org/springframework/security/authentication/AuthenticationObservationConvention.java

+15
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ public String getName() {
4343
return OBSERVATION_NAME;
4444
}
4545

46+
@Override
47+
public String getContextualName(AuthenticationObservationContext context) {
48+
if (context.getAuthenticationRequest() != null) {
49+
String authenticationType = context.getAuthenticationRequest().getClass().getSimpleName();
50+
if (authenticationType.endsWith("Token")) {
51+
authenticationType = authenticationType.substring(0, authenticationType.lastIndexOf("Token"));
52+
}
53+
if (authenticationType.endsWith("Authentication")) {
54+
authenticationType = authenticationType.substring(0, authenticationType.lastIndexOf("Authentication"));
55+
}
56+
return "authenticate " + authenticationType.toLowerCase();
57+
}
58+
return "authenticate";
59+
}
60+
4661
/**
4762
* {@inheritDoc}
4863
*/

core/src/main/java/org/springframework/security/authorization/AuthorizationObservationConvention.java

+27-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import io.micrometer.common.KeyValues;
2020
import io.micrometer.observation.Observation;
2121
import io.micrometer.observation.ObservationConvention;
22+
import org.aopalliance.intercept.MethodInvocation;
23+
24+
import org.springframework.security.authorization.method.MethodInvocationResult;
2225

2326
/**
2427
* An {@link ObservationConvention} for translating authorizations into {@link KeyValues}.
@@ -39,23 +42,28 @@ public String getName() {
3942
return OBSERVATION_NAME;
4043
}
4144

45+
@Override
46+
public String getContextualName(AuthorizationObservationContext<?> context) {
47+
return "authorize " + getObjectType(context);
48+
}
49+
4250
/**
4351
* {@inheritDoc}
4452
*/
4553
@Override
4654
public KeyValues getLowCardinalityKeyValues(AuthorizationObservationContext<?> context) {
47-
return KeyValues.of("authentication.type", getAuthenticationType(context))
48-
.and("object.type", getObjectType(context))
49-
.and("authorization.decision", getAuthorizationDecision(context));
55+
return KeyValues.of("spring.security.authentication.type", getAuthenticationType(context))
56+
.and("spring.security.object", getObjectType(context))
57+
.and("spring.security.authorization.decision", getAuthorizationDecision(context));
5058
}
5159

5260
/**
5361
* {@inheritDoc}
5462
*/
5563
@Override
5664
public KeyValues getHighCardinalityKeyValues(AuthorizationObservationContext<?> context) {
57-
return KeyValues.of("authentication.authorities", getAuthorities(context)).and("authorization.decision.details",
58-
getDecisionDetails(context));
65+
return KeyValues.of("spring.security.authentication.authorities", getAuthorities(context))
66+
.and("spring.security.authorization.decision.details", getDecisionDetails(context));
5967
}
6068

6169
@Override
@@ -74,7 +82,20 @@ private String getObjectType(AuthorizationObservationContext<?> context) {
7482
if (context.getObject() == null) {
7583
return "unknown";
7684
}
77-
return context.getObject().getClass().getSimpleName();
85+
if (context.getObject() instanceof MethodInvocation) {
86+
return "method";
87+
}
88+
if (context.getObject() instanceof MethodInvocationResult) {
89+
return "method";
90+
}
91+
String className = context.getObject().getClass().getSimpleName();
92+
if (className.contains("Request")) {
93+
return "request";
94+
}
95+
if (className.contains("Message")) {
96+
return "message";
97+
}
98+
return className;
7899
}
79100

80101
private String getAuthorizationDecision(AuthorizationObservationContext<?> context) {

core/src/main/java/org/springframework/security/core/context/ObservationSecurityContextChangedListener.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333
*/
3434
public final class ObservationSecurityContextChangedListener implements SecurityContextChangedListener {
3535

36-
static final String SECURITY_CONTEXT_CREATED = "security.context.created";
36+
static final String SECURITY_CONTEXT_CREATED = "spring.security.context.created";
3737

38-
static final String SECURITY_CONTEXT_CHANGED = "security.context.changed";
38+
static final String SECURITY_CONTEXT_CHANGED = "spring.security.context.changed";
3939

40-
static final String SECURITY_CONTEXT_CLEARED = "security.context.cleared";
40+
static final String SECURITY_CONTEXT_CLEARED = "spring.security.context.cleared";
4141

4242
private final ObservationRegistry registry;
4343

web/src/main/java/org/springframework/security/web/ObservationFilterChainDecorator.java

+24-44
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@
3737
import org.apache.commons.logging.LogFactory;
3838

3939
import org.springframework.core.log.LogMessage;
40-
import org.springframework.security.web.util.UrlUtils;
4140

4241
/**
43-
* A {@link org.springframework.security.web.server.FilterChainProxy.FilterChainDecorator}
44-
* that wraps the chain in before and after observations
42+
* A {@link org.springframework.security.web.FilterChainProxy.FilterChainDecorator} that
43+
* wraps the chain in before and after observations
4544
*
4645
* @author Josh Cummings
4746
* @since 6.0
@@ -75,14 +74,16 @@ public FilterChain decorate(FilterChain original, List<Filter> filters) {
7574
private FilterChain wrapSecured(FilterChain original) {
7675
return (req, res) -> {
7776
AroundFilterObservation parent = observation((HttpServletRequest) req);
78-
Observation observation = Observation.createNotStarted(SECURED_OBSERVATION_NAME, this.registry);
77+
Observation observation = Observation.createNotStarted(SECURED_OBSERVATION_NAME, this.registry)
78+
.contextualName("secured request");
7979
parent.wrap(FilterObservation.create(observation).wrap(original)).doFilter(req, res);
8080
};
8181
}
8282

8383
private FilterChain wrapUnsecured(FilterChain original) {
8484
return (req, res) -> {
85-
Observation observation = Observation.createNotStarted(UNSECURED_OBSERVATION_NAME, this.registry);
85+
Observation observation = Observation.createNotStarted(UNSECURED_OBSERVATION_NAME, this.registry)
86+
.contextualName("unsecured request");
8687
FilterObservation.create(observation).wrap(original).doFilter(req, res);
8788
};
8889
}
@@ -189,8 +190,8 @@ private void wrapFilter(ServletRequest request, ServletResponse response, Filter
189190
}
190191

191192
private AroundFilterObservation parent(HttpServletRequest request) {
192-
FilterChainObservationContext beforeContext = FilterChainObservationContext.before(request);
193-
FilterChainObservationContext afterContext = FilterChainObservationContext.after(request);
193+
FilterChainObservationContext beforeContext = FilterChainObservationContext.before();
194+
FilterChainObservationContext afterContext = FilterChainObservationContext.after();
194195
Observation before = Observation.createNotStarted(this.convention, () -> beforeContext, this.registry);
195196
Observation after = Observation.createNotStarted(this.convention, () -> afterContext, this.registry);
196197
AroundFilterObservation parent = AroundFilterObservation.create(before, after);
@@ -409,8 +410,6 @@ public FilterChain wrap(FilterChain chain) {
409410

410411
static final class FilterChainObservationContext extends Observation.Context {
411412

412-
private final ServletRequest request;
413-
414413
private final String filterSection;
415414

416415
private String filterName;
@@ -419,29 +418,17 @@ static final class FilterChainObservationContext extends Observation.Context {
419418

420419
private int chainSize;
421420

422-
private FilterChainObservationContext(ServletRequest request, String filterSection) {
421+
private FilterChainObservationContext(String filterSection) {
423422
this.filterSection = filterSection;
424-
this.request = request;
425-
}
426-
427-
static FilterChainObservationContext before(ServletRequest request) {
428-
return new FilterChainObservationContext(request, "before");
429-
}
430-
431-
static FilterChainObservationContext after(ServletRequest request) {
432-
return new FilterChainObservationContext(request, "after");
423+
setContextualName("security filterchain " + filterSection);
433424
}
434425

435-
@Override
436-
public void setName(String name) {
437-
super.setName(name);
438-
if (name != null) {
439-
setContextualName(name + "." + this.filterSection);
440-
}
426+
static FilterChainObservationContext before() {
427+
return new FilterChainObservationContext("before");
441428
}
442429

443-
String getRequestLine() {
444-
return requestLine((HttpServletRequest) this.request);
430+
static FilterChainObservationContext after() {
431+
return new FilterChainObservationContext("after");
445432
}
446433

447434
String getFilterSection() {
@@ -472,32 +459,31 @@ void setChainSize(int chainSize) {
472459
this.chainSize = chainSize;
473460
}
474461

475-
private static String requestLine(HttpServletRequest request) {
476-
return request.getMethod() + " " + UrlUtils.buildRequestUrl(request);
477-
}
478-
479462
}
480463

481464
static final class FilterChainObservationConvention
482465
implements ObservationConvention<FilterChainObservationContext> {
483466

484-
static final String CHAIN_OBSERVATION_NAME = "spring.security.http.chains";
485-
486-
private static final String REQUEST_LINE_NAME = "request.line";
467+
static final String CHAIN_OBSERVATION_NAME = "spring.security.filterchains";
487468

488-
private static final String CHAIN_POSITION_NAME = "chain.position";
469+
private static final String CHAIN_POSITION_NAME = "spring.security.filterchain.position";
489470

490-
private static final String CHAIN_SIZE_NAME = "chain.size";
471+
private static final String CHAIN_SIZE_NAME = "spring.security.filterchain.size";
491472

492-
private static final String FILTER_SECTION_NAME = "filter.section";
473+
private static final String FILTER_SECTION_NAME = "security.security.reached.filter.section";
493474

494-
private static final String FILTER_NAME = "current.filter.name";
475+
private static final String FILTER_NAME = "spring.security.reached.filter.name";
495476

496477
@Override
497478
public String getName() {
498479
return CHAIN_OBSERVATION_NAME;
499480
}
500481

482+
@Override
483+
public String getContextualName(FilterChainObservationContext context) {
484+
return "security filterchain " + context.getFilterSection();
485+
}
486+
501487
@Override
502488
public KeyValues getLowCardinalityKeyValues(FilterChainObservationContext context) {
503489
KeyValues kv = KeyValues.of(CHAIN_SIZE_NAME, String.valueOf(context.getChainSize()))
@@ -509,12 +495,6 @@ public KeyValues getLowCardinalityKeyValues(FilterChainObservationContext contex
509495
return kv;
510496
}
511497

512-
@Override
513-
public KeyValues getHighCardinalityKeyValues(FilterChainObservationContext context) {
514-
String requestLine = context.getRequestLine();
515-
return KeyValues.of(REQUEST_LINE_NAME, requestLine);
516-
}
517-
518498
@Override
519499
public boolean supportsContext(Observation.Context context) {
520500
return context instanceof FilterChainObservationContext;

web/src/main/java/org/springframework/security/web/server/ObservationWebFilterChainDecorator.java

+21-37
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,16 @@ private static AroundWebFilterObservation observation(ServerWebExchange exchange
7575
private WebFilterChain wrapSecured(WebFilterChain original) {
7676
return (exchange) -> {
7777
AroundWebFilterObservation parent = observation(exchange);
78-
Observation observation = Observation.createNotStarted(SECURED_OBSERVATION_NAME, this.registry);
78+
Observation observation = Observation.createNotStarted(SECURED_OBSERVATION_NAME, this.registry)
79+
.contextualName("secured request");
7980
return parent.wrap(WebFilterObservation.create(observation).wrap(original)).filter(exchange);
8081
};
8182
}
8283

8384
private WebFilterChain wrapUnsecured(WebFilterChain original) {
8485
return (exchange) -> {
85-
Observation observation = Observation.createNotStarted(UNSECURED_OBSERVATION_NAME, this.registry);
86+
Observation observation = Observation.createNotStarted(UNSECURED_OBSERVATION_NAME, this.registry)
87+
.contextualName("unsecured request");
8688
return WebFilterObservation.create(observation).wrap(original).filter(exchange);
8789
};
8890
}
@@ -210,8 +212,8 @@ private Mono<Void> wrapFilter(ServerWebExchange exchange, WebFilterChain chain)
210212
}
211213

212214
private AroundWebFilterObservation parent(ServerWebExchange exchange) {
213-
WebFilterChainObservationContext beforeContext = WebFilterChainObservationContext.before(exchange);
214-
WebFilterChainObservationContext afterContext = WebFilterChainObservationContext.after(exchange);
215+
WebFilterChainObservationContext beforeContext = WebFilterChainObservationContext.before();
216+
WebFilterChainObservationContext afterContext = WebFilterChainObservationContext.after();
215217
Observation before = Observation.createNotStarted(this.convention, () -> beforeContext, this.registry);
216218
Observation after = Observation.createNotStarted(this.convention, () -> afterContext, this.registry);
217219
AroundWebFilterObservation parent = AroundWebFilterObservation.create(before, after);
@@ -419,8 +421,6 @@ public WebFilterChain wrap(WebFilterChain chain) {
419421

420422
static final class WebFilterChainObservationContext extends Observation.Context {
421423

422-
private final ServerWebExchange exchange;
423-
424424
private final String filterSection;
425425

426426
private String filterName;
@@ -429,29 +429,16 @@ static final class WebFilterChainObservationContext extends Observation.Context
429429

430430
private int chainSize;
431431

432-
private WebFilterChainObservationContext(ServerWebExchange exchange, String filterSection) {
433-
this.exchange = exchange;
432+
private WebFilterChainObservationContext(String filterSection) {
434433
this.filterSection = filterSection;
435434
}
436435

437-
static WebFilterChainObservationContext before(ServerWebExchange exchange) {
438-
return new WebFilterChainObservationContext(exchange, "before");
439-
}
440-
441-
static WebFilterChainObservationContext after(ServerWebExchange exchange) {
442-
return new WebFilterChainObservationContext(exchange, "after");
443-
}
444-
445-
@Override
446-
public void setName(String name) {
447-
super.setName(name);
448-
if (name != null) {
449-
setContextualName(name + "." + this.filterSection);
450-
}
436+
static WebFilterChainObservationContext before() {
437+
return new WebFilterChainObservationContext("before");
451438
}
452439

453-
String getRequestLine() {
454-
return this.exchange.getRequest().getPath().toString();
440+
static WebFilterChainObservationContext after() {
441+
return new WebFilterChainObservationContext("after");
455442
}
456443

457444
String getFilterSection() {
@@ -487,23 +474,26 @@ void setChainSize(int chainSize) {
487474
static final class WebFilterChainObservationConvention
488475
implements ObservationConvention<WebFilterChainObservationContext> {
489476

490-
static final String CHAIN_OBSERVATION_NAME = "spring.security.http.chains";
477+
static final String CHAIN_OBSERVATION_NAME = "spring.security.filterchains";
491478

492-
private static final String REQUEST_LINE_NAME = "request.line";
479+
private static final String CHAIN_POSITION_NAME = "spring.security.filterchain.position";
493480

494-
private static final String CHAIN_POSITION_NAME = "chain.position";
481+
private static final String CHAIN_SIZE_NAME = "spring.security.filterchain.size";
495482

496-
private static final String CHAIN_SIZE_NAME = "chain.size";
483+
private static final String FILTER_SECTION_NAME = "spring.security.reached.filter.section";
497484

498-
private static final String FILTER_SECTION_NAME = "filter.section";
499-
500-
private static final String FILTER_NAME = "current.filter.name";
485+
private static final String FILTER_NAME = "spring.security.reached.filter.name";
501486

502487
@Override
503488
public String getName() {
504489
return CHAIN_OBSERVATION_NAME;
505490
}
506491

492+
@Override
493+
public String getContextualName(WebFilterChainObservationContext context) {
494+
return "security filterchain " + context.getFilterSection();
495+
}
496+
507497
@Override
508498
public KeyValues getLowCardinalityKeyValues(WebFilterChainObservationContext context) {
509499
KeyValues kv = KeyValues.of(CHAIN_SIZE_NAME, String.valueOf(context.getChainSize()))
@@ -515,12 +505,6 @@ public KeyValues getLowCardinalityKeyValues(WebFilterChainObservationContext con
515505
return kv;
516506
}
517507

518-
@Override
519-
public KeyValues getHighCardinalityKeyValues(WebFilterChainObservationContext context) {
520-
String requestLine = context.getRequestLine();
521-
return KeyValues.of(REQUEST_LINE_NAME, requestLine);
522-
}
523-
524508
@Override
525509
public boolean supportsContext(Observation.Context context) {
526510
return context instanceof WebFilterChainObservationContext;

0 commit comments

Comments
 (0)