Skip to content

Commit

Permalink
Add docs for multiple MeterTag annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatan-ivanov committed Nov 7, 2024
1 parent 09810cc commit d9c031d
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 13 deletions.
4 changes: 4 additions & 0 deletions docs/modules/ROOT/pages/concepts/counters.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ include::{include-java}/metrics/CountedAspectTest.java[tags=example_value_resolv
// Example for calling the provided <ValueExpressionResolver>
include::{include-java}/metrics/CountedAspectTest.java[tags=example_value_spel,indent=0]
// Example for using multiple @MeterTag annotations on the same parameter
// @MeterTags({ @MeterTag(...), @MeterTag(...) }) can be also used
include::{include-java}/metrics/CountedAspectTest.java[tags=example_multi_annotations,indent=0]
-----

== Function-tracking Counters
Expand Down
4 changes: 4 additions & 0 deletions docs/modules/ROOT/pages/concepts/timers.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ include::{include-java}/metrics/TimedAspectTest.java[tags=example_value_resolver
// Example for calling the provided <ValueExpressionResolver>
include::{include-java}/metrics/TimedAspectTest.java[tags=example_value_spel,indent=0]
// Example for using multiple @MeterTag annotations on the same parameter
// @MeterTags({ @MeterTag(...), @MeterTag(...) }) can be also used
include::{include-java}/metrics/TimedAspectTest.java[tags=example_multi_annotations,indent=0]
-----

== Function-tracking Timers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,30 @@ void meterTagsWithExpression(AnnotatedTestClass annotatedClass) {
// end::example_value_spel[]
}

@ParameterizedTest
@EnumSource(AnnotatedTestClass.class)
void multipleMeterTagsWithExpression(AnnotatedTestClass annotatedClass) {
MeterRegistry registry = new SimpleMeterRegistry();
CountedAspect countedAspect = new CountedAspect(registry);
countedAspect.setMeterTagAnnotationHandler(
new CountedMeterTagAnnotationHandler(aClass -> valueResolver, aClass -> valueExpressionResolver));

AspectJProxyFactory pf = new AspectJProxyFactory(annotatedClass.newInstance());
pf.addAspect(countedAspect);

MeterTagClassInterface service = pf.getProxy();

// tag::example_multi_annotations[]
service.getMultipleAnnotationsForTagValueExpression(new DataHolder("zxe", "qwe"));

assertThat(registry.get("method.counted")
.tag("value1", "value1: zxe")
.tag("value2", "value2: qwe")
.counter()
.count()).isEqualTo(1);
// end::example_multi_annotations[]
}

enum AnnotatedTestClass {

CLASS_WITHOUT_INTERFACE(MeterTagClass.class), CLASS_WITH_INTERFACE(MeterTagClassChild.class);
Expand Down Expand Up @@ -142,6 +166,11 @@ void getAnnotationForTagValueExpression(
@Counted
void getAnnotationForArgumentToString(@MeterTag("test") Long param);

@Counted
void getMultipleAnnotationsForTagValueExpression(
@MeterTag(key = "value1", expression = "'value1: ' + value1") @MeterTag(key = "value2",
expression = "'value2: ' + value2") DataHolder param);

}
// end::interface[]

Expand All @@ -164,6 +193,13 @@ public void getAnnotationForTagValueExpression(
public void getAnnotationForArgumentToString(@MeterTag("test") Long param) {
}

@Counted
@Override
public void getMultipleAnnotationsForTagValueExpression(
@MeterTag(key = "value1", expression = "'value1: ' + value1") @MeterTag(key = "value2",
expression = "'value2: ' + value2") DataHolder param) {
}

}

static class MeterTagClassChild implements MeterTagClassInterface {
Expand All @@ -183,6 +219,33 @@ public void getAnnotationForTagValueExpression(String test) {
public void getAnnotationForArgumentToString(Long param) {
}

@Counted
@Override
public void getMultipleAnnotationsForTagValueExpression(
@MeterTag(key = "value2", expression = "'value2: ' + value2") DataHolder param) {
}

}

static class DataHolder {

private final String value1;

private final String value2;

private DataHolder(String value1, String value2) {
this.value1 = value1;
this.value2 = value2;
}

public String getValue1() {
return value1;
}

public String getValue2() {
return value2;
}

}

}
61 changes: 61 additions & 0 deletions docs/src/test/java/io/micrometer/docs/metrics/TimedAspectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,28 @@ void meterTagsWithExpression(AnnotatedTestClass annotatedClass) {
// end::example_value_spel[]
}

@ParameterizedTest
@EnumSource(AnnotatedTestClass.class)
void multipleMeterTagsWithExpression(AnnotatedTestClass annotatedClass) {
MeterRegistry registry = new SimpleMeterRegistry();
TimedAspect timedAspect = new TimedAspect(registry);
timedAspect.setMeterTagAnnotationHandler(
new MeterTagAnnotationHandler(aClass -> valueResolver, aClass -> valueExpressionResolver));

AspectJProxyFactory pf = new AspectJProxyFactory(annotatedClass.newInstance());
pf.addAspect(timedAspect);

MeterTagClassInterface service = pf.getProxy();

// tag::example_multi_annotations[]
service.getMultipleAnnotationsForTagValueExpression(new DataHolder("zxe", "qwe"));

assertThat(
registry.get("method.timed").tag("value1", "value1: zxe").tag("value2", "value2: qwe").timer().count())
.isEqualTo(1);
// end::example_multi_annotations[]
}

enum AnnotatedTestClass {

CLASS_WITHOUT_INTERFACE(MeterTagClass.class), CLASS_WITH_INTERFACE(MeterTagClassChild.class);
Expand Down Expand Up @@ -142,6 +164,11 @@ void getAnnotationForTagValueExpression(
@Timed
void getAnnotationForArgumentToString(@MeterTag("test") Long param);

@Timed
void getMultipleAnnotationsForTagValueExpression(
@MeterTag(key = "value1", expression = "'value1: ' + value1") @MeterTag(key = "value2",
expression = "'value2: ' + value2") DataHolder param);

}
// end::interface[]

Expand All @@ -164,6 +191,13 @@ public void getAnnotationForTagValueExpression(
public void getAnnotationForArgumentToString(@MeterTag("test") Long param) {
}

@Timed
@Override
public void getMultipleAnnotationsForTagValueExpression(
@MeterTag(key = "value1", expression = "'value1: ' + value1") @MeterTag(key = "value2",
expression = "'value2: ' + value2") DataHolder param) {
}

}

static class MeterTagClassChild implements MeterTagClassInterface {
Expand All @@ -183,6 +217,33 @@ public void getAnnotationForTagValueExpression(String test) {
public void getAnnotationForArgumentToString(Long param) {
}

@Timed
@Override
public void getMultipleAnnotationsForTagValueExpression(
@MeterTag(key = "value2", expression = "'value2: ' + value2") DataHolder param) {
}

}

static class DataHolder {

private final String value1;

private final String value2;

private DataHolder(String value1, String value2) {
this.value1 = value1;
this.value2 = value2;
}

public String getValue1() {
return value1;
}

public String getValue2() {
return value2;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
import io.micrometer.common.annotation.ValueResolver;
import io.micrometer.core.Issue;
import io.micrometer.core.annotation.Counted;
import io.micrometer.core.aop.CountedAspect;
import io.micrometer.core.aop.CountedMeterTagAnnotationHandler;
import io.micrometer.core.aop.MeterTag;
import io.micrometer.core.aop.*;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
Expand Down Expand Up @@ -496,6 +494,49 @@ void meterTagsWithExpression(AnnotatedTestClass annotatedClass) {
assertThat(registry.get("method.counted").tag("test", "hello characters").counter().count()).isEqualTo(1);
}

@ParameterizedTest
@EnumSource(AnnotatedTestClass.class)
void multipleMeterTagsWithExpression(AnnotatedTestClass annotatedClass) {
MeterRegistry registry = new SimpleMeterRegistry();
CountedAspect countedAspect = new CountedAspect(registry);
countedAspect.setMeterTagAnnotationHandler(meterTagAnnotationHandler);

AspectJProxyFactory pf = new AspectJProxyFactory(annotatedClass.newInstance());
pf.addAspect(countedAspect);

MeterTagClassInterface service = pf.getProxy();

service.getMultipleAnnotationsForTagValueExpression(new DataHolder("zxe", "qwe"));

assertThat(registry.get("method.counted")
.tag("value1", "value1: zxe")
.tag("value2", "value2.overridden: qwe")
.counter()
.count()).isEqualTo(1);
}

@ParameterizedTest
@EnumSource(AnnotatedTestClass.class)
void multipleMeterTagsWithinContainerWithExpression(AnnotatedTestClass annotatedClass) {
MeterRegistry registry = new SimpleMeterRegistry();
CountedAspect countedAspect = new CountedAspect(registry);
countedAspect.setMeterTagAnnotationHandler(meterTagAnnotationHandler);

AspectJProxyFactory pf = new AspectJProxyFactory(annotatedClass.newInstance());
pf.addAspect(countedAspect);

MeterTagClassInterface service = pf.getProxy();

service.getMultipleAnnotationsWithContainerForTagValueExpression(new DataHolder("zxe", "qwe"));

assertThat(registry.get("method.counted")
.tag("value1", "value1: zxe")
.tag("value2", "value2: qwe")
.tag("value3", "value3.overridden: ZXEQWE")
.counter()
.count()).isEqualTo(1);
}

@Test
void meterTagOnPackagePrivateMethod() {
MeterRegistry registry = new SimpleMeterRegistry();
Expand Down Expand Up @@ -547,6 +588,17 @@ void getAnnotationForTagValueExpression(
@Counted
void getAnnotationForArgumentToString(@MeterTag("test") Long param);

@Counted
void getMultipleAnnotationsForTagValueExpression(
@MeterTag(key = "value1", expression = "'value1: ' + value1") @MeterTag(key = "value2",
expression = "'value2: ' + value2") DataHolder param);

@Counted
void getMultipleAnnotationsWithContainerForTagValueExpression(@MeterTags({
@MeterTag(key = "value1", expression = "'value1: ' + value1"),
@MeterTag(key = "value2", expression = "'value2: ' + value2"), @MeterTag(key = "value3",
expression = "'value3: ' + value1.toUpperCase + value2.toUpperCase") }) DataHolder param);

}

static class MeterTagClass implements MeterTagClassInterface {
Expand All @@ -572,6 +624,21 @@ public void getAnnotationForArgumentToString(@MeterTag("test") Long param) {
void getAnnotationForPackagePrivateMethod(@MeterTag("foo") String foo) {
}

@Counted
@Override
public void getMultipleAnnotationsForTagValueExpression(
@MeterTag(key = "value1", expression = "'value1: ' + value1") @MeterTag(key = "value2",
expression = "'value2.overridden: ' + value2") DataHolder param) {
}

@Counted
@Override
public void getMultipleAnnotationsWithContainerForTagValueExpression(@MeterTags({
@MeterTag(key = "value1", expression = "'value1: ' + value1"),
@MeterTag(key = "value2", expression = "'value2: ' + value2"), @MeterTag(key = "value3",
expression = "'value3.overridden: ' + value1.toUpperCase + value2.toUpperCase") }) DataHolder param) {
}

}

static class MeterTagClassChild implements MeterTagClassInterface {
Expand All @@ -591,6 +658,39 @@ public void getAnnotationForTagValueExpression(String test) {
public void getAnnotationForArgumentToString(Long param) {
}

@Counted
@Override
public void getMultipleAnnotationsForTagValueExpression(
@MeterTag(key = "value2", expression = "'value2.overridden: ' + value2") DataHolder param) {
}

@Counted
@Override
public void getMultipleAnnotationsWithContainerForTagValueExpression(@MeterTag(key = "value3",
expression = "'value3.overridden: ' + value1.toUpperCase + value2.toUpperCase") DataHolder param) {
}

}

static class DataHolder {

private final String value1;

private final String value2;

private DataHolder(String value1, String value2) {
this.value1 = value1;
this.value2 = value2;
}

public String getValue1() {
return value1;
}

public String getValue2() {
return value2;
}

}

}
Expand Down
Loading

0 comments on commit d9c031d

Please sign in to comment.