Skip to content
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

@MetricOptions to allow filtering AbstractMethodTagger beans to apply to metrics #764

Merged
merged 4 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
@@ -0,0 +1,43 @@
/*
* Copyright 2017-2019 original 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 io.micronaut.configuration.metrics.annotation;

import io.micronaut.configuration.metrics.aggregator.AbstractMethodTagger;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Holds metadata about metric options to apply
*
* @author Haiden Rothwell
* @since 5.6.0
*/
@Documented
@Retention(RUNTIME)
@Target({METHOD})
public @interface MetricOptions {
hrothwell marked this conversation as resolved.
Show resolved Hide resolved
/**
* TODO if empty or null should this do nothing? no filtering happening unless something defined? but then
* what if you really truly don't want anything applied? Null is not an option for default, some secondary annotation value?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can add a containers check for the annotation member and if it is empty then disable all taggers I guess

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I follow what you are referring to, but it sounds like disable all taggers if taggers is empty?

The idea I am going back and forth on is how do we differentiate "use all taggers" without needing to specify all additional taggers? Ideally what I was hoping for was something like:

taggers == null --> no filtering done, all taggers get appliedf
taggers == empty array --> filtering is enabled, this filters out all taggers effectively disabling them
taggers == populated array --> filters just to taggers in populated array

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a helper annotation value as a different option also, not sure what is most ideal really

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok looks fine can you update the javadoc and remove the TODO

* @return array of {@link io.micronaut.configuration.metrics.aggregator.AbstractMethodTagger} to apply to metrics for method
*/
Class<? extends AbstractMethodTagger>[] taggers() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.micronaut.aop.MethodInterceptor;
import io.micronaut.aop.MethodInvocationContext;
import io.micronaut.configuration.metrics.aggregator.AbstractMethodTagger;
import io.micronaut.configuration.metrics.annotation.MetricOptions;
import io.micronaut.configuration.metrics.annotation.RequiresMetrics;
import io.micronaut.core.annotation.AnnotationMetadata;
import io.micronaut.core.annotation.Nullable;
Expand All @@ -34,6 +35,7 @@
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -148,13 +150,15 @@ public Object intercept(MethodInvocationContext<Object, Object> context) {
}

private void doCount(AnnotationMetadata metadata, String metricName, @Nullable Throwable e, MethodInvocationContext<Object, Object> context) {
List<Class<? extends AbstractMethodTagger>> taggers = Arrays.asList(metadata.classValues(MetricOptions.class, "taggers"));
Counter.builder(metricName)
.tags(metadata.stringValues(Counted.class, "extraTags"))
.tags(
methodTaggers.isEmpty() ? Collections.emptyList() :
methodTaggers
.stream()
.flatMap(b -> b.getTags(context).stream())
.filter(t -> taggers.isEmpty() || taggers.contains(t.getClass()))
.flatMap(t -> t.getTags(context).stream())
.toList()
)
.description(metadata.stringValue(Counted.class, "description").orElse(null))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.micronaut.aop.MethodInterceptor;
import io.micronaut.aop.MethodInvocationContext;
import io.micronaut.configuration.metrics.aggregator.AbstractMethodTagger;
import io.micronaut.configuration.metrics.annotation.MetricOptions;
import io.micronaut.configuration.metrics.annotation.RequiresMetrics;
import io.micronaut.core.annotation.AnnotationMetadata;
import io.micronaut.core.annotation.AnnotationValue;
Expand All @@ -43,6 +44,7 @@
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -212,6 +214,7 @@ private void stopTimed(String metricName, Timer.Sample sample,
try {
final String description = metadata.stringValue("description").orElse(null);
final String[] tags = metadata.stringValues("extraTags");
List<Class<? extends AbstractMethodTagger>> taggers = Arrays.asList(context.getAnnotationMetadata().classValues(MetricOptions.class, "taggers"));
final double[] percentiles = metadata.doubleValues("percentiles");
final boolean histogram = metadata.isTrue("histogram");
final Timer timer = Timer.builder(metricName)
Expand All @@ -221,7 +224,8 @@ private void stopTimed(String metricName, Timer.Sample sample,
methodTaggers.isEmpty() ? Collections.emptyList() :
methodTaggers
.stream()
.flatMap(b -> b.getTags(context).stream())
.filter(t -> taggers.isEmpty() || taggers.contains(t.getClass()))
.flatMap(b -> b.getTags(context).stream())
.toList()
)
.tags(EXCEPTION_TAG, exceptionClass)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package io.micronaut.configuration.metrics.annotation

import io.micrometer.core.instrument.MeterRegistry
import io.micrometer.core.instrument.search.MeterNotFoundException
import io.micronaut.context.ApplicationContext
import spock.lang.Specification
import spock.util.concurrent.PollingConditions

import java.util.function.Consumer

import static java.util.concurrent.TimeUnit.MILLISECONDS

class CountedAnnotationSpec extends Specification {

void "test counted annotation usage"() {
Expand Down Expand Up @@ -86,4 +89,28 @@ class CountedAnnotationSpec extends Specification {
cleanup:
ctx.close()
}

void "taggers are filtered if filter present"(){
given:
ApplicationContext ctx = ApplicationContext.run()
CountedTarget tt = ctx.getBean(CountedTarget)
MeterRegistry registry = ctx.getBean(MeterRegistry)

when:
Integer result = tt.maxWithOptions(4, 10)
registry.get("counted.test.maxWithOptions.blocking").tags("method", "maxWithOptions", "parameters", "a b").counter()

then:
thrown(MeterNotFoundException)

when:
def timer = registry.get("counted.test.maxWithOptions.blocking").tags("method", "maxWithOptions").counter()

then:
result == 10
timer.count() == 1

cleanup:
ctx.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.micronaut.configuration.metrics.annotation

import io.micrometer.core.instrument.MeterRegistry
import io.micrometer.core.instrument.Tag
import io.micrometer.core.instrument.search.MeterNotFoundException
import io.micronaut.context.ApplicationContext
import spock.lang.Specification
import spock.util.concurrent.PollingConditions
Expand Down Expand Up @@ -99,4 +100,29 @@ class TimeAnnotationSpec extends Specification {
cleanup:
ctx.close()
}

void "taggers are filtered if filter present"(){
given:
ApplicationContext ctx = ApplicationContext.run()
TimedTarget tt = ctx.getBean(TimedTarget)
MeterRegistry registry = ctx.getBean(MeterRegistry)

when:
Integer result = tt.maxWithOptions(4, 10)
registry.get("timed.test.maxWithOptions.blocking").tags("method", "maxWithOptions", "parameters", "a b").timer()

then:
thrown(MeterNotFoundException)

when:
def timer = registry.get("timed.test.maxWithOptions.blocking").tags("method", "maxWithOptions").timer()

then:
result == 10
timer.count() == 1
timer.totalTime(MILLISECONDS) > 0

cleanup:
ctx.close()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.micronaut.configuration.metrics.annotation;

import io.micrometer.core.annotation.Counted;
import io.micronaut.configuration.metrics.aggregator.MethodTaggerExample;
import jakarta.inject.Singleton;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand All @@ -15,6 +16,12 @@ Integer max(int a, int b) {
return Math.max(a, b);
}

@Counted("counted.test.maxWithOptions.blocking")
@MetricOptions(taggers = {MethodTaggerExample.class})
Integer maxWithOptions(int a, int b) {
return Math.max(a, b);
}

@Counted("counted.test.max.blocking")
Integer error(int a, int b) {
throw new NumberFormatException("cannot");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.micronaut.configuration.metrics.annotation;

import io.micrometer.core.annotation.Timed;
import io.micronaut.configuration.metrics.aggregator.MethodTaggerExample;
import jakarta.inject.Singleton;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand All @@ -15,6 +16,12 @@ Integer max(int a, int b) {
return Math.max(a, b);
}

@Timed("timed.test.maxWithOptions.blocking")
@MetricOptions(taggers = {MethodTaggerExample.class})
Integer maxWithOptions(int a, int b) {
return Math.max(a, b);
}

@Timed("timed.test.repeated1")
@Timed("timed.test.repeated2")
Integer repeated(int a, int b) {
Expand Down
Loading