Skip to content

#12811 - compressing simple class name for observation #12955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -137,6 +139,49 @@ public void doFilter(ServletRequest request, ServletResponse response) throws IO

static final class ObservationFilter implements Filter {

private static final Map<String, String> OBSERVATION_NAMES = new HashMap<>();

static {
OBSERVATION_NAMES.put("DisableEncodeUrlFilter", "session.url-encoding");
OBSERVATION_NAMES.put("ForceEagerSessionCreationFilter", "session.eager-create");
OBSERVATION_NAMES.put("ChannelProcessingFilter", "access.channel");
OBSERVATION_NAMES.put("WebAsyncManagerIntegrationFilter", "context.async");
OBSERVATION_NAMES.put("SecurityContextHolderFilter", "context.holder");
OBSERVATION_NAMES.put("SecurityContextPersistenceFilter", "context.management");
OBSERVATION_NAMES.put("HeaderWriterFilter", "header");
OBSERVATION_NAMES.put("CorsFilter", "cors");
OBSERVATION_NAMES.put("CsrfFilter", "csrf");
OBSERVATION_NAMES.put("LogoutFilter", "logout");
OBSERVATION_NAMES.put("OAuth2AuthorizationRequestRedirectFilter", "oauth2.authnrequest");
OBSERVATION_NAMES.put("Saml2WebSsoAuthenticationRequestFilter", "saml2.authnrequest");
OBSERVATION_NAMES.put("X509AuthenticationFilter", "authentication.x509");
OBSERVATION_NAMES.put("J2eePreAuthenticatedProcessingFilter", "preauthentication.j2ee");
OBSERVATION_NAMES.put("RequestHeaderAuthenticationFilter", "preauthentication.header");
OBSERVATION_NAMES.put("RequestAttributeAuthenticationFilter", "preauthentication.attribute");
OBSERVATION_NAMES.put("WebSpherePreAuthenticatedProcessingFilter", "preauthentication.websphere");
OBSERVATION_NAMES.put("CasAuthenticationFilter", "cas.authentication");
OBSERVATION_NAMES.put("OAuth2LoginAuthenticationFilter", "oauth2.authentication");
OBSERVATION_NAMES.put("Saml2WebSsoAuthenticationFilter", "saml2.authentication");
OBSERVATION_NAMES.put("UsernamePasswordAuthenticationFilter", "authentication.form");
OBSERVATION_NAMES.put("DefaultLoginPageGeneratingFilter", "page.login");
OBSERVATION_NAMES.put("DefaultLogoutPageGeneratingFilter", "page.logout");
OBSERVATION_NAMES.put("ConcurrentSessionFilter", "session.concurrent");
OBSERVATION_NAMES.put("DigestAuthenticationFilter", "authentication.digest");
OBSERVATION_NAMES.put("BearerTokenAuthenticationFilter", "authentication.bearer");
OBSERVATION_NAMES.put("BasicAuthenticationFilter", "authentication.basic");
OBSERVATION_NAMES.put("RequestCacheAwareFilter", "requestcache");
OBSERVATION_NAMES.put("SecurityContextHolderAwareRequestFilter", "context.servlet");
OBSERVATION_NAMES.put("JaasApiIntegrationFilter", "jaas");
OBSERVATION_NAMES.put("RememberMeAuthenticationFilter", "authentication.rememberme");
OBSERVATION_NAMES.put("AnonymousAuthenticationFilter", "authentication.anonymous");
OBSERVATION_NAMES.put("OAuth2AuthorizationCodeGrantFilter", "oauth2.client.code");
OBSERVATION_NAMES.put("SessionManagementFilter", "session.management");
OBSERVATION_NAMES.put("ExceptionTranslationFilter", "access.exceptions");
OBSERVATION_NAMES.put("FilterSecurityInterceptor", "access.request");
OBSERVATION_NAMES.put("AuthorizationFilter", "authorization");
OBSERVATION_NAMES.put("SwitchUserFilter", "authentication.switch");
}

private final ObservationRegistry registry;

private final FilterChainObservationConvention convention = new FilterChainObservationConvention();
Expand All @@ -145,6 +190,8 @@ static final class ObservationFilter implements Filter {

private final String name;

private final String eventName;

private final int position;

private final int size;
Expand All @@ -155,6 +202,12 @@ static final class ObservationFilter implements Filter {
this.name = filter.getClass().getSimpleName();
this.position = position;
this.size = size;
this.eventName = eventName(this.name);
}

private String eventName(String className) {
String eventName = OBSERVATION_NAMES.get(className);
return (eventName != null) ? eventName : className;
}

String getName() {
Expand All @@ -181,15 +234,15 @@ private void wrapFilter(ServletRequest request, ServletResponse response, Filter
parentBefore.setFilterName(this.name);
parentBefore.setChainPosition(this.position);
}
parent.before().event(Observation.Event.of(this.name + ".before", "before " + this.name));
parent.before().event(Observation.Event.of(this.eventName + ".before", "before " + this.name));
this.filter.doFilter(request, response, chain);
parent.start();
if (parent.after().getContext() instanceof FilterChainObservationContext parentAfter) {
parentAfter.setChainSize(this.size);
parentAfter.setFilterName(this.name);
parentAfter.setChainPosition(this.size - this.position + 1);
}
parent.after().event(Observation.Event.of(this.name + ".after", "after " + this.name));
parent.after().event(Observation.Event.of(this.eventName + ".after", "after " + this.name));
}

private AroundFilterObservation parent(HttpServletRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@

package org.springframework.security.web;

import java.io.IOException;
import java.util.List;

import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationHandler;
import io.micrometer.observation.ObservationRegistry;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

Expand Down Expand Up @@ -87,4 +91,32 @@ void decorateFiltersWhenDefaultsThenObserves() throws Exception {
assertThat(events.get(1).getName()).isEqualTo(filter.getClass().getSimpleName() + ".after");
}

@Test
void decorateFiltersWhenDefaultsThenUsesEventName() throws Exception {
ObservationHandler<?> handler = mock(ObservationHandler.class);
given(handler.supportsContext(any())).willReturn(true);
ObservationRegistry registry = ObservationRegistry.create();
registry.observationConfig().observationHandler(handler);
ObservationFilterChainDecorator decorator = new ObservationFilterChainDecorator(registry);
FilterChain chain = mock(FilterChain.class);
Filter filter = new BasicAuthenticationFilter();
FilterChain decorated = decorator.decorate(chain, List.of(filter));
decorated.doFilter(new MockHttpServletRequest("GET", "/"), new MockHttpServletResponse());
ArgumentCaptor<Observation.Event> event = ArgumentCaptor.forClass(Observation.Event.class);
verify(handler, times(2)).onEvent(event.capture(), any());
List<Observation.Event> events = event.getAllValues();
assertThat(events.get(0).getName()).isEqualTo("authentication.basic.before");
assertThat(events.get(1).getName()).isEqualTo("authentication.basic.after");
}

private static class BasicAuthenticationFilter implements Filter {

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
chain.doFilter(request, response);
}

}

}