Skip to content

Commit 491e12a

Browse files
committed
Add property to disable Spring Security observations
Setting 'management.observations.spring-security.enabled' installs an ObservationPredicate, which prevents all observations starting with 'spring.security.' to be created. Closes gh-34802
1 parent 6eede82 commit 491e12a

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationAutoConfiguration.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
4141
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
4242
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
43+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
4344
import org.springframework.boot.context.properties.EnableConfigurationProperties;
4445
import org.springframework.context.annotation.Bean;
4546
import org.springframework.context.annotation.Configuration;
@@ -82,6 +83,12 @@ PropertiesObservationFilter propertiesObservationFilter(ObservationProperties pr
8283
return new PropertiesObservationFilter(properties);
8384
}
8485

86+
@Bean
87+
@ConditionalOnProperty(name = "management.observations.spring-security.enabled", havingValue = "false")
88+
ObservationPredicate springSecurityObservationsDisabler() {
89+
return (name, context) -> !name.startsWith("spring.security.");
90+
}
91+
8592
@Configuration(proxyBeanMethods = false)
8693
@ConditionalOnClass(MeterRegistry.class)
8794
@ConditionalOnMissingClass("io.micrometer.tracing.Tracer")

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,6 +2038,12 @@
20382038
"level": "error"
20392039
}
20402040
},
2041+
{
2042+
"name": "management.observations.spring-security.enabled",
2043+
"description": "Whether to enable observations for Spring Security",
2044+
"type": "java.lang.Boolean",
2045+
"defaultValue": true
2046+
},
20412047
{
20422048
"name": "management.otlp.tracing.compression",
20432049
"defaultValue": "none"

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationAutoConfigurationTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,28 @@ void autoConfiguresObservationHandlerWhenTracingIsActive() {
291291
});
292292
}
293293

294+
@Test
295+
void shouldNotDisableSpringSecurityObservationsByDefault() {
296+
this.contextRunner.run((context) -> {
297+
ObservationRegistry observationRegistry = context.getBean(ObservationRegistry.class);
298+
Observation.start("spring.security.filterchains", observationRegistry).stop();
299+
MeterRegistry meterRegistry = context.getBean(MeterRegistry.class);
300+
assertThat(meterRegistry.get("spring.security.filterchains").timer().count()).isOne();
301+
});
302+
}
303+
304+
@Test
305+
void shouldDisableSpringSecurityObservationsIfPropertyIsSet() {
306+
this.contextRunner.withPropertyValues("management.observations.spring-security.enabled=false")
307+
.run((context) -> {
308+
ObservationRegistry observationRegistry = context.getBean(ObservationRegistry.class);
309+
Observation.start("spring.security.filterchains", observationRegistry).stop();
310+
MeterRegistry meterRegistry = context.getBean(MeterRegistry.class);
311+
assertThatThrownBy(() -> meterRegistry.get("spring.security.filterchains").timer())
312+
.isInstanceOf(MeterNotFoundException.class);
313+
});
314+
}
315+
294316
@Configuration(proxyBeanMethods = false)
295317
static class ObservationPredicates {
296318

spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/observability.adoc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,16 @@ Commons key-values are applied to all observations as low cardinality key-values
3737

3838
The preceding example adds `region` and `stack` key-values to all observations with a value of `us-east-1` and `prod`, respectively.
3939

40+
[[actuator.observability.preventing-observations]]
41+
=== Preventing Observations
42+
43+
If you'd like to prevent some observations from being reported, you can register beans of type `ObservationPredicate`.
44+
Observations are only reported if all the `ObservationPredicate` beans return `true` for that observation.
45+
46+
include::code:MyObservationPredicate[]
47+
48+
The preceding example will prevent all observations with a name starting with "denied.prefix.".
49+
50+
TIP: If you want to prevent Spring Security from reporting observations, set the property configprop:management.observations.spring-security.enabled[] to `false`.
51+
4052
The next sections will provide more details about logging, metrics and traces.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.actuator.observability.preventingobservations;
18+
19+
import io.micrometer.observation.Observation.Context;
20+
import io.micrometer.observation.ObservationPredicate;
21+
22+
import org.springframework.stereotype.Component;
23+
24+
@Component
25+
class MyObservationPredicate implements ObservationPredicate {
26+
27+
@Override
28+
public boolean test(String name, Context context) {
29+
return !name.startsWith("denied.prefix.");
30+
}
31+
32+
}

0 commit comments

Comments
 (0)