Skip to content

Commit

Permalink
order and sortAbstractMethodTagger before applying Tags (#812)
Browse files Browse the repository at this point in the history
A use case might be that multiple taggers might use the same tag name, when determining which takes precedence in those situations felt like some sort of order / sorting would be needed
  • Loading branch information
hrothwell authored Oct 25, 2024
1 parent 6e0ec33 commit 244e642
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.micronaut.aop.MethodInvocationContext;
import io.micronaut.core.annotation.Indexed;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.order.Ordered;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -32,7 +33,8 @@
* @since 5.5.0
*/
@Indexed(AbstractMethodTagger.class)
public abstract class AbstractMethodTagger {
public abstract class AbstractMethodTagger implements Ordered {

private static final Logger LOGGER = LoggerFactory.getLogger(AbstractMethodTagger.class);

private final Class<? extends AbstractMethodTagger> implClass = this.getClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.async.publisher.Publishers;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.order.OrderUtil;
import io.micronaut.core.util.StringUtils;
import jakarta.inject.Inject;
import org.reactivestreams.Publisher;
Expand Down Expand Up @@ -157,6 +158,7 @@ private void doCount(AnnotationMetadata metadata, String metricName, @Nullable T
methodTaggers.isEmpty() ? Collections.emptyList() :
methodTaggers
.stream()
.sorted(OrderUtil.ORDERED_COMPARATOR)
.filter(t -> !filter || taggers.contains(t.getClass()))
.flatMap(t -> t.getTags(context).stream())
.toList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.micronaut.core.annotation.TypeHint;
import io.micronaut.core.async.publisher.Publishers;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.order.OrderUtil;
import io.micronaut.core.util.CollectionUtils;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
Expand Down Expand Up @@ -225,6 +226,7 @@ private void stopTimed(String metricName, Timer.Sample sample,
methodTaggers.isEmpty() ? Collections.emptyList() :
methodTaggers
.stream()
.sorted(OrderUtil.ORDERED_COMPARATOR)
.filter(t -> !filter || taggers.contains(t.getClass()))
.flatMap(b -> b.getTags(context).stream())
.toList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,28 @@ class CountedAnnotationSpec extends Specification {
cleanup:
ctx.close()
}

void "taggers are applied in order"(){
given:
ApplicationContext ctx = ApplicationContext.run()
CountedTarget cc = ctx.getBean(CountedTarget)
MeterRegistry registry = ctx.getBean(MeterRegistry)

when:
Integer result = cc.max(4, 10)
registry.get("counted.test.max.blocking").tags("ordered", "1", "parameters", "a b").timer()

then:
thrown(MeterNotFoundException)

when:
def counter = registry.get("counted.test.max.blocking").tags("ordered", "2", "parameters", "a b").counter()

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

cleanup:
ctx.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,29 @@ class TimeAnnotationSpec extends Specification {
cleanup:
ctx.close()
}

void "taggers are applied in order"(){
given:
ApplicationContext ctx = ApplicationContext.run()
TimedTarget tt = ctx.getBean(TimedTarget)
MeterRegistry registry = ctx.getBean(MeterRegistry)

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

then:
thrown(MeterNotFoundException)

when:
def timer = registry.get("timed.test.max.blocking").tags("ordered", "2", "parameters", "a b").timer()

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

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

import io.micrometer.core.instrument.Tag;
import io.micronaut.aop.MethodInvocationContext;
import jakarta.inject.Singleton;

import java.util.List;

@Singleton
public class OrderedMethodTagger extends AbstractMethodTagger{
@Override
public int getOrder() {
return 1;
}

@Override
public List<Tag> buildTags(MethodInvocationContext<Object, Object> context) {
return List.of(Tag.of("ordered", String.valueOf(getOrder())));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.micronaut.configuration.metrics.aggregator;

import io.micrometer.core.instrument.Tag;
import io.micronaut.aop.MethodInvocationContext;
import jakarta.inject.Singleton;

import java.util.List;

@Singleton
public class OrderedMethodTagger2 extends AbstractMethodTagger{
@Override
public int getOrder() {
return 2;
}

@Override
public List<Tag> buildTags(MethodInvocationContext<Object, Object> context) {
return List.of(Tag.of("ordered", String.valueOf(getOrder())));
}
}

0 comments on commit 244e642

Please sign in to comment.