From c308e4665a284eb5e631983bd0f44e2a433fb619 Mon Sep 17 00:00:00 2001 From: Josh Cummings Date: Fri, 6 Jan 2023 11:12:42 -0700 Subject: [PATCH] Polish Event Name Provide a name with no spaces separate from the human-friendly one with spaces. Closes gh-12490 --- .../web/ObservationFilterChainDecorator.java | 6 ++-- .../ObservationFilterChainDecoratorTests.java | 28 ++++++++++++++++++- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/web/src/main/java/org/springframework/security/web/ObservationFilterChainDecorator.java b/web/src/main/java/org/springframework/security/web/ObservationFilterChainDecorator.java index 0c709b50e2..f3b361266f 100644 --- a/web/src/main/java/org/springframework/security/web/ObservationFilterChainDecorator.java +++ b/web/src/main/java/org/springframework/security/web/ObservationFilterChainDecorator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -182,7 +182,7 @@ public final class ObservationFilterChainDecorator implements FilterChainProxy.F parentBefore.setFilterName(this.name); parentBefore.setChainPosition(this.position); } - parent.before().event(Observation.Event.of(this.name + " before")); + parent.before().event(Observation.Event.of(this.name + ".before", "before " + this.name)); this.filter.doFilter(request, response, chain); parent.start(); if (parent.after().getContext() instanceof FilterChainObservationContext parentAfter) { @@ -190,7 +190,7 @@ public final class ObservationFilterChainDecorator implements FilterChainProxy.F parentAfter.setFilterName(this.name); parentAfter.setChainPosition(this.size - this.position + 1); } - parent.after().event(Observation.Event.of(this.name + " after")); + parent.after().event(Observation.Event.of(this.name + ".after", "after " + this.name)); } private AroundFilterObservation parent(HttpServletRequest request) { diff --git a/web/src/test/java/org/springframework/security/web/ObservationFilterChainDecoratorTests.java b/web/src/test/java/org/springframework/security/web/ObservationFilterChainDecoratorTests.java index caf7c71ba1..47fe1c1b5b 100644 --- a/web/src/test/java/org/springframework/security/web/ObservationFilterChainDecoratorTests.java +++ b/web/src/test/java/org/springframework/security/web/ObservationFilterChainDecoratorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,17 +16,24 @@ package org.springframework.security.web; +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 org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; @@ -61,4 +68,23 @@ public class ObservationFilterChainDecoratorTests { verifyNoInteractions(handler); } + @Test + void decorateFiltersWhenDefaultsThenObserves() 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 = mock(Filter.class); + FilterChain decorated = decorator.decorate(chain, List.of(filter)); + decorated.doFilter(new MockHttpServletRequest("GET", "/"), new MockHttpServletResponse()); + verify(handler, times(2)).onStart(any()); + ArgumentCaptor event = ArgumentCaptor.forClass(Observation.Event.class); + verify(handler, times(2)).onEvent(event.capture(), any()); + List events = event.getAllValues(); + assertThat(events.get(0).getName()).isEqualTo(filter.getClass().getSimpleName() + ".before"); + assertThat(events.get(1).getName()).isEqualTo(filter.getClass().getSimpleName() + ".after"); + } + }