Skip to content

Commit c662e9b

Browse files
committed
Add configuration property to disable long timers in Micrometer Observations
Set management.observations.long-task-timer.enabled = false to disable the LongTaskTimer creation. Closes gh-39618
1 parent 2d9b1ad commit c662e9b

File tree

3 files changed

+75
-6
lines changed

3 files changed

+75
-6
lines changed

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020

2121
import io.micrometer.core.instrument.MeterRegistry;
2222
import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler;
23+
import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler.IgnoredMeters;
2324
import io.micrometer.core.instrument.observation.MeterObservationHandler;
2425
import io.micrometer.observation.GlobalObservationConvention;
2526
import io.micrometer.observation.Observation;
@@ -131,8 +132,10 @@ static class MeterObservationHandlerConfiguration {
131132
static class OnlyMetricsMeterObservationHandlerConfiguration {
132133

133134
@Bean
134-
DefaultMeterObservationHandler defaultMeterObservationHandler(MeterRegistry meterRegistry) {
135-
return new DefaultMeterObservationHandler(meterRegistry);
135+
DefaultMeterObservationHandler defaultMeterObservationHandler(MeterRegistry meterRegistry,
136+
ObservationProperties properties) {
137+
return properties.getLongTaskTimer().isEnabled() ? new DefaultMeterObservationHandler(meterRegistry)
138+
: new DefaultMeterObservationHandler(meterRegistry, IgnoredMeters.LONG_TASK_TIMER);
136139
}
137140

138141
}
@@ -143,8 +146,10 @@ static class TracingAndMetricsObservationHandlerConfiguration {
143146

144147
@Bean
145148
TracingAwareMeterObservationHandler<Observation.Context> tracingAwareMeterObservationHandler(
146-
MeterRegistry meterRegistry, Tracer tracer) {
147-
DefaultMeterObservationHandler delegate = new DefaultMeterObservationHandler(meterRegistry);
149+
MeterRegistry meterRegistry, Tracer tracer, ObservationProperties properties) {
150+
DefaultMeterObservationHandler delegate = properties.getLongTaskTimer().isEnabled()
151+
? new DefaultMeterObservationHandler(meterRegistry)
152+
: new DefaultMeterObservationHandler(meterRegistry, IgnoredMeters.LONG_TASK_TIMER);
148153
return new TracingAwareMeterObservationHandler<>(delegate, tracer);
149154
}
150155

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

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,6 +45,8 @@ public class ObservationProperties {
4545
*/
4646
private Map<String, Boolean> enable = new LinkedHashMap<>();
4747

48+
private final LongTaskTimer longTaskTimer = new LongTaskTimer();
49+
4850
public Map<String, Boolean> getEnable() {
4951
return this.enable;
5052
}
@@ -65,6 +67,10 @@ public void setKeyValues(Map<String, String> keyValues) {
6567
this.keyValues = keyValues;
6668
}
6769

70+
public LongTaskTimer getLongTaskTimer() {
71+
return this.longTaskTimer;
72+
}
73+
6874
public static class Http {
6975

7076
private final Client client = new Client();
@@ -135,4 +141,21 @@ public void setName(String name) {
135141

136142
}
137143

144+
public static class LongTaskTimer {
145+
146+
/**
147+
* Whether to create a LongTaskTimer for every observation.
148+
*/
149+
private boolean enabled = true;
150+
151+
public boolean isEnabled() {
152+
return this.enabled;
153+
}
154+
155+
public void setEnabled(boolean enabled) {
156+
this.enabled = enabled;
157+
}
158+
159+
}
160+
138161
}

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

+41
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.springframework.context.annotation.Configuration;
5151
import org.springframework.context.annotation.Import;
5252
import org.springframework.core.annotation.Order;
53+
import org.springframework.test.util.ReflectionTestUtils;
5354

5455
import static org.assertj.core.api.Assertions.assertThat;
5556
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -334,6 +335,46 @@ void shouldDisableSpringSecurityObservationsIfPropertyIsSet() {
334335
});
335336
}
336337

338+
@Test
339+
void shouldEnableLongTaskTimersByDefault() {
340+
this.contextRunner.run((context) -> {
341+
DefaultMeterObservationHandler handler = context.getBean(DefaultMeterObservationHandler.class);
342+
assertThat(handler).hasFieldOrPropertyWithValue("shouldCreateLongTaskTimer", true);
343+
});
344+
}
345+
346+
@Test
347+
void shouldDisableLongTaskTimerIfPropertyIsSet() {
348+
this.contextRunner.withPropertyValues("management.observations.long-task-timer.enabled=false")
349+
.run((context) -> {
350+
DefaultMeterObservationHandler handler = context.getBean(DefaultMeterObservationHandler.class);
351+
assertThat(handler).hasFieldOrPropertyWithValue("shouldCreateLongTaskTimer", false);
352+
});
353+
}
354+
355+
@Test
356+
@SuppressWarnings("unchecked")
357+
void shouldEnableLongTaskTimersForTracingByDefault() {
358+
this.tracingContextRunner.run((context) -> {
359+
TracingAwareMeterObservationHandler<Observation.Context> tracingHandler = context
360+
.getBean(TracingAwareMeterObservationHandler.class);
361+
Object delegate = ReflectionTestUtils.getField(tracingHandler, "delegate");
362+
assertThat(delegate).hasFieldOrPropertyWithValue("shouldCreateLongTaskTimer", true);
363+
});
364+
}
365+
366+
@Test
367+
@SuppressWarnings("unchecked")
368+
void shouldDisableLongTaskTimerForTracingIfPropertyIsSet() {
369+
this.tracingContextRunner.withPropertyValues("management.observations.long-task-timer.enabled=false")
370+
.run((context) -> {
371+
TracingAwareMeterObservationHandler<Observation.Context> tracingHandler = context
372+
.getBean(TracingAwareMeterObservationHandler.class);
373+
Object delegate = ReflectionTestUtils.getField(tracingHandler, "delegate");
374+
assertThat(delegate).hasFieldOrPropertyWithValue("shouldCreateLongTaskTimer", false);
375+
});
376+
}
377+
337378
@Configuration(proxyBeanMethods = false)
338379
static class ObservationPredicates {
339380

0 commit comments

Comments
 (0)