Skip to content

Commit

Permalink
STruncate Observation Event Name
Browse files Browse the repository at this point in the history
To make events easier for event repositories, remove
the rather common word Filter from the end of the filter-based
event names.

Closes spring-projectsgh-12811
  • Loading branch information
jzheaux committed Mar 7, 2023
1 parent 69606fd commit dea4e92
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,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 @@ -153,6 +155,12 @@ static final class ObservationFilter implements Filter {
this.registry = registry;
this.filter = filter;
this.name = filter.getClass().getSimpleName();
if (this.name.endsWith("Filter")) {
this.eventName = this.name.substring(0, this.name.lastIndexOf("Filter"));
}
else {
this.eventName = this.name;
}
this.position = position;
this.size = size;
}
Expand Down Expand Up @@ -181,15 +189,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.eventName));
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.eventName));
}

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 eventNameWhenEndsInFilterThenTruncates() 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 MyTestFilter();
FilterChain decorated = decorator.decorate(chain, List.of(filter));
decorated.doFilter(new MockHttpServletRequest("GET", "/"), new MockHttpServletResponse());
verify(handler, times(2)).onStart(any());
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("MyTest.before");
assertThat(events.get(1).getName()).isEqualTo("MyTest.after");
}

private static class MyTestFilter implements Filter {

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

}

}

0 comments on commit dea4e92

Please sign in to comment.