forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59bc85b
commit c553f96
Showing
3 changed files
with
74 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
extensions/micrometer/runtime/src/main/java/io/quarkus/micrometer/runtime/TagsSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package io.quarkus.micrometer.runtime; | ||
|
||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Parameter; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import io.micrometer.common.util.StringUtils; | ||
import io.micrometer.core.aop.MeterTag; | ||
import io.micrometer.core.instrument.Tag; | ||
import io.micrometer.core.instrument.Tags; | ||
import io.quarkus.arc.ArcInvocationContext; | ||
|
||
class TagsSupport { | ||
|
||
static Tags getTags(ArcInvocationContext context) { | ||
return getCommonTags(context) | ||
.and(getMeterTags(context)); | ||
} | ||
|
||
private static Tags getMeterTags(ArcInvocationContext context) { | ||
List<Tag> tags = new ArrayList<>(); | ||
Method method = context.getMethod(); | ||
Parameter[] parameters = method.getParameters(); | ||
for (int i = 0; i < parameters.length; i++) { | ||
Parameter methodParameter = parameters[i]; | ||
MeterTag annotation = methodParameter.getAnnotation(MeterTag.class); | ||
if (annotation != null) { | ||
Object parameterValue = context.getParameters()[i]; | ||
|
||
tags.add(Tag.of( | ||
resolveTagKey(annotation, methodParameter.getName()), | ||
resolveTagValue(parameterValue))); | ||
} | ||
} | ||
return Tags.of(tags); | ||
} | ||
|
||
private static Tags getCommonTags(ArcInvocationContext context) { | ||
Method method = context.getMethod(); | ||
String className = method.getDeclaringClass().getName(); | ||
String methodName = method.getName(); | ||
return Tags.of("class", className, "method", methodName); | ||
} | ||
|
||
private static String resolveTagValue(Object parameterValue) { | ||
return String.valueOf(parameterValue); | ||
} | ||
|
||
private static String resolveTagKey(MeterTag annotation, String parameterName) { | ||
if (StringUtils.isNotBlank(annotation.value())) { | ||
return annotation.value(); | ||
} else if (StringUtils.isNotBlank(annotation.key())) { | ||
return annotation.key(); | ||
} else { | ||
return parameterName; | ||
} | ||
} | ||
} |