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
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
* Copyright 2012-2024 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.
|
||||
@@ -20,6 +20,7 @@ import java.util.List;
|
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler;
|
||||
import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler.IgnoredMeters;
|
||||
import io.micrometer.core.instrument.observation.MeterObservationHandler;
|
||||
import io.micrometer.observation.GlobalObservationConvention;
|
||||
import io.micrometer.observation.Observation;
|
||||
@@ -131,8 +132,10 @@ public class ObservationAutoConfiguration {
|
||||
static class OnlyMetricsMeterObservationHandlerConfiguration {
|
||||
|
||||
@Bean
|
||||
DefaultMeterObservationHandler defaultMeterObservationHandler(MeterRegistry meterRegistry) {
|
||||
return new DefaultMeterObservationHandler(meterRegistry);
|
||||
DefaultMeterObservationHandler defaultMeterObservationHandler(MeterRegistry meterRegistry,
|
||||
ObservationProperties properties) {
|
||||
return properties.getLongTaskTimer().isEnabled() ? new DefaultMeterObservationHandler(meterRegistry)
|
||||
: new DefaultMeterObservationHandler(meterRegistry, IgnoredMeters.LONG_TASK_TIMER);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -143,8 +146,10 @@ public class ObservationAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
TracingAwareMeterObservationHandler<Observation.Context> tracingAwareMeterObservationHandler(
|
||||
MeterRegistry meterRegistry, Tracer tracer) {
|
||||
DefaultMeterObservationHandler delegate = new DefaultMeterObservationHandler(meterRegistry);
|
||||
MeterRegistry meterRegistry, Tracer tracer, ObservationProperties properties) {
|
||||
DefaultMeterObservationHandler delegate = properties.getLongTaskTimer().isEnabled()
|
||||
? new DefaultMeterObservationHandler(meterRegistry)
|
||||
: new DefaultMeterObservationHandler(meterRegistry, IgnoredMeters.LONG_TASK_TIMER);
|
||||
return new TracingAwareMeterObservationHandler<>(delegate, tracer);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
* Copyright 2012-2024 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.
|
||||
@@ -45,6 +45,8 @@ public class ObservationProperties {
|
||||
*/
|
||||
private Map<String, Boolean> enable = new LinkedHashMap<>();
|
||||
|
||||
private final LongTaskTimer longTaskTimer = new LongTaskTimer();
|
||||
|
||||
public Map<String, Boolean> getEnable() {
|
||||
return this.enable;
|
||||
}
|
||||
@@ -65,6 +67,10 @@ public class ObservationProperties {
|
||||
this.keyValues = keyValues;
|
||||
}
|
||||
|
||||
public LongTaskTimer getLongTaskTimer() {
|
||||
return this.longTaskTimer;
|
||||
}
|
||||
|
||||
public static class Http {
|
||||
|
||||
private final Client client = new Client();
|
||||
@@ -135,4 +141,21 @@ public class ObservationProperties {
|
||||
|
||||
}
|
||||
|
||||
public static class LongTaskTimer {
|
||||
|
||||
/**
|
||||
* Whether to create a LongTaskTimer for every observation.
|
||||
*/
|
||||
private boolean enabled = true;
|
||||
|
||||
public boolean isEnabled() {
|
||||
return this.enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -50,6 +50,7 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
@@ -334,6 +335,46 @@ class ObservationAutoConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldEnableLongTaskTimersByDefault() {
|
||||
this.contextRunner.run((context) -> {
|
||||
DefaultMeterObservationHandler handler = context.getBean(DefaultMeterObservationHandler.class);
|
||||
assertThat(handler).hasFieldOrPropertyWithValue("shouldCreateLongTaskTimer", true);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDisableLongTaskTimerIfPropertyIsSet() {
|
||||
this.contextRunner.withPropertyValues("management.observations.long-task-timer.enabled=false")
|
||||
.run((context) -> {
|
||||
DefaultMeterObservationHandler handler = context.getBean(DefaultMeterObservationHandler.class);
|
||||
assertThat(handler).hasFieldOrPropertyWithValue("shouldCreateLongTaskTimer", false);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void shouldEnableLongTaskTimersForTracingByDefault() {
|
||||
this.tracingContextRunner.run((context) -> {
|
||||
TracingAwareMeterObservationHandler<Observation.Context> tracingHandler = context
|
||||
.getBean(TracingAwareMeterObservationHandler.class);
|
||||
Object delegate = ReflectionTestUtils.getField(tracingHandler, "delegate");
|
||||
assertThat(delegate).hasFieldOrPropertyWithValue("shouldCreateLongTaskTimer", true);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void shouldDisableLongTaskTimerForTracingIfPropertyIsSet() {
|
||||
this.tracingContextRunner.withPropertyValues("management.observations.long-task-timer.enabled=false")
|
||||
.run((context) -> {
|
||||
TracingAwareMeterObservationHandler<Observation.Context> tracingHandler = context
|
||||
.getBean(TracingAwareMeterObservationHandler.class);
|
||||
Object delegate = ReflectionTestUtils.getField(tracingHandler, "delegate");
|
||||
assertThat(delegate).hasFieldOrPropertyWithValue("shouldCreateLongTaskTimer", false);
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class ObservationPredicates {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user