Skip to content

support dynamic tags in @Counted and @Timed annotations #46007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
package org.springframework.boot.actuate.autoconfigure.metrics;

import io.micrometer.core.aop.CountedAspect;
import io.micrometer.core.aop.CountedMeterTagAnnotationHandler;
import io.micrometer.core.aop.MeterTagAnnotationHandler;
import io.micrometer.core.aop.TimedAspect;
import io.micrometer.core.instrument.MeterRegistry;
import org.aspectj.weaver.Advice;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
Expand All @@ -36,6 +37,7 @@
* aspects.
*
* @author Jonatan Ivanov
* @author Dominique Villard
* @since 3.2.0
*/
@AutoConfiguration(after = { MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class })
Expand All @@ -46,17 +48,40 @@ public class MetricsAspectsAutoConfiguration {

@Bean
@ConditionalOnMissingBean
CountedAspect countedAspect(MeterRegistry registry) {
return new CountedAspect(registry);
CountedAspect countedAspect(MeterRegistry registry,
CountedMeterTagAnnotationHandler countedMeterTagAnnotationHandler) {
CountedAspect countedAspect = new CountedAspect(registry);
countedAspect.setMeterTagAnnotationHandler(countedMeterTagAnnotationHandler);
return countedAspect;
}

@Bean
@ConditionalOnMissingBean
TimedAspect timedAspect(MeterRegistry registry,
ObjectProvider<MeterTagAnnotationHandler> meterTagAnnotationHandler) {
TimedAspect timedAspect(MeterRegistry registry, MeterTagAnnotationHandler meterTagAnnotationHandler) {
TimedAspect timedAspect = new TimedAspect(registry);
meterTagAnnotationHandler.ifAvailable(timedAspect::setMeterTagAnnotationHandler);
timedAspect.setMeterTagAnnotationHandler(meterTagAnnotationHandler);
return timedAspect;
}

@Bean
@ConditionalOnMissingBean
CountedMeterTagAnnotationHandler countedMeterTagAnnotationHandler(BeanFactory beanFactory,
SpelTagValueExpressionResolver metricsTagValueExpressionResolver) {
return new CountedMeterTagAnnotationHandler(beanFactory::getBean,
(ignored) -> metricsTagValueExpressionResolver);
}

@Bean
@ConditionalOnMissingBean
MeterTagAnnotationHandler meterTagAnnotationHandler(BeanFactory beanFactory,
SpelTagValueExpressionResolver meterTagValueExpressionResolver) {
return new MeterTagAnnotationHandler(beanFactory::getBean, (ignored) -> meterTagValueExpressionResolver);
}

@Bean
@ConditionalOnMissingBean
SpelTagValueExpressionResolver meterTagValueExpressionResolver() {
return new SpelTagValueExpressionResolver();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2012-2025 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.actuate.autoconfigure.metrics;

import io.micrometer.common.annotation.ValueExpressionResolver;
import io.micrometer.core.aop.MeterTag;
import io.micrometer.tracing.annotation.SpanTag;

import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.SimpleEvaluationContext;

/**
* Evaluates a Spel expression applied to a parameter for use in {@link MeterTag}
* {@link SpanTag} Micrometer annotations.
*
* @author Dominique Villard
* @since 4.0.0
*/
public class SpelTagValueExpressionResolver implements ValueExpressionResolver {

@Override
public String resolve(String expression, Object parameter) {
try {
SimpleEvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
ExpressionParser expressionParser = new SpelExpressionParser();
Expression expressionToEvaluate = expressionParser.parseExpression(expression);
return expressionToEvaluate.getValue(context, parameter, String.class);
}
catch (Exception ex) {
throw new IllegalStateException("Unable to evaluate SpEL expression '%s'".formatted(expression), ex);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.springframework.boot.actuate.autoconfigure.tracing;

import io.micrometer.common.annotation.ValueExpressionResolver;
import io.micrometer.tracing.Tracer;
import io.micrometer.tracing.annotation.DefaultNewSpanParser;
import io.micrometer.tracing.annotation.ImperativeMethodInvocationProcessor;
Expand All @@ -31,6 +30,7 @@
import org.aspectj.weaver.Advice;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.boot.actuate.autoconfigure.metrics.SpelTagValueExpressionResolver;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
Expand All @@ -41,10 +41,6 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.SimpleEvaluationContext;

/**
* {@link EnableAutoConfiguration Auto-configuration} for the Micrometer Tracing API.
Expand Down Expand Up @@ -113,9 +109,15 @@ DefaultNewSpanParser newSpanParser() {

@Bean
@ConditionalOnMissingBean
SpanTagAnnotationHandler spanTagAnnotationHandler(BeanFactory beanFactory) {
ValueExpressionResolver valueExpressionResolver = new SpelTagValueExpressionResolver();
return new SpanTagAnnotationHandler(beanFactory::getBean, (ignored) -> valueExpressionResolver);
SpelTagValueExpressionResolver spanTagValueExpressionResolver() {
return new SpelTagValueExpressionResolver();
}

@Bean
@ConditionalOnMissingBean
SpanTagAnnotationHandler spanTagAnnotationHandler(BeanFactory beanFactory,
SpelTagValueExpressionResolver spanTagValueExpressionResolver) {
return new SpanTagAnnotationHandler(beanFactory::getBean, (ignored) -> spanTagValueExpressionResolver);
}

@Bean
Expand All @@ -133,21 +135,4 @@ SpanAspect spanAspect(MethodInvocationProcessor methodInvocationProcessor) {

}

private static final class SpelTagValueExpressionResolver implements ValueExpressionResolver {

@Override
public String resolve(String expression, Object parameter) {
try {
SimpleEvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
ExpressionParser expressionParser = new SpelExpressionParser();
Expression expressionToEvaluate = expressionParser.parseExpression(expression);
return expressionToEvaluate.getValue(context, parameter, String.class);
}
catch (Exception ex) {
throw new IllegalStateException("Unable to evaluate SpEL expression '%s'".formatted(expression), ex);
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.boot.actuate.autoconfigure.metrics;

import io.micrometer.core.aop.CountedAspect;
import io.micrometer.core.aop.CountedMeterTagAnnotationHandler;
import io.micrometer.core.aop.MeterTagAnnotationHandler;
import io.micrometer.core.aop.TimedAspect;
import io.micrometer.core.instrument.MeterRegistry;
Expand Down Expand Up @@ -65,12 +66,21 @@ void shouldConfigureAspects() {
@Test
void shouldConfigureMeterTagAnnotationHandler() {
this.contextRunner.withUserConfiguration(MeterTagAnnotationHandlerConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(CountedAspect.class);
assertThat(context).hasSingleBean(TimedAspect.class);
assertThat(ReflectionTestUtils.getField(context.getBean(TimedAspect.class), "meterTagAnnotationHandler"))
.isSameAs(context.getBean(MeterTagAnnotationHandler.class));
});
}

@Test
void shouldConfigureCounterMeterTagAnnotationHandler() {
this.contextRunner.withUserConfiguration(MeterTagAnnotationHandlerConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(CountedAspect.class);
assertThat(ReflectionTestUtils.getField(context.getBean(CountedAspect.class), "meterTagAnnotationHandler"))
.isSameAs(context.getBean(CountedMeterTagAnnotationHandler.class));
});
}

@Test
void shouldNotConfigureAspectsIfMicrometerIsMissing() {
this.contextRunner.withClassLoader(new FilteredClassLoader(MeterRegistry.class)).run((context) -> {
Expand Down Expand Up @@ -128,6 +138,11 @@ MeterTagAnnotationHandler meterTagAnnotationHandler() {
return new MeterTagAnnotationHandler(null, null);
}

@Bean
CountedMeterTagAnnotationHandler countedMeterTagAnnotationHandler() {
return new CountedMeterTagAnnotationHandler(null, null);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2012-2025 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.actuate.autoconfigure.metrics;

import java.util.Map;

import org.junit.jupiter.api.Test;

import org.springframework.data.util.Pair;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;

class SpelTagValueExpressionResolverTests {

final SpelTagValueExpressionResolver resolver = new SpelTagValueExpressionResolver();

@Test
void checkValidExpression() {
var value = Map.of("foo", Pair.of(1, 2));
assertThat(this.resolver.resolve("['foo'].first", value)).isEqualTo("1");
}

@Test
void checkInvalidExpression() {
var value = Map.of("foo", Pair.of(1, 2));
assertThatIllegalStateException().isThrownBy(() -> this.resolver.resolve("['bar'].first", value));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.aspectj.weaver.Advice;
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.SpelTagValueExpressionResolver;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
Expand Down Expand Up @@ -107,6 +108,8 @@ void shouldBackOffOnCustomBeans() {
assertThat(context).hasSingleBean(SpanAspect.class);
assertThat(context).hasBean("customSpanTagAnnotationHandler");
assertThat(context).hasSingleBean(SpanTagAnnotationHandler.class);
assertThat(context).hasBean("customMetricsTagValueExpressionResolver");
assertThat(context).hasSingleBean(SpelTagValueExpressionResolver.class);
});
}

Expand Down Expand Up @@ -239,6 +242,11 @@ SpanTagAnnotationHandler customSpanTagAnnotationHandler() {
(aClass) -> mock(ValueExpressionResolver.class));
}

@Bean
SpelTagValueExpressionResolver customMetricsTagValueExpressionResolver() {
return mock(SpelTagValueExpressionResolver.class);
}

}

@Configuration(proxyBeanMethods = false)
Expand Down