-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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 415d736
Showing
3 changed files
with
89 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
74 changes: 74 additions & 0 deletions
74
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,74 @@ | ||
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.annotation.NoOpValueResolver; | ||
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(annotation, 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); | ||
} | ||
|
||
/* | ||
* Precedence copied from MeterTagAnnotationHandler | ||
*/ | ||
private static String resolveTagValue(MeterTag annotation, Object parameterValue) { | ||
if (annotation.resolver() != NoOpValueResolver.class) { | ||
return "?"; | ||
} else if (StringUtils.isNotBlank(annotation.expression())) { | ||
return "?"; | ||
} else if (parameterValue != null) { | ||
return parameterValue.toString(); | ||
} else { | ||
return ""; | ||
} | ||
} | ||
|
||
/* | ||
* Precedence copied from MeterTagAnnotationHandler | ||
*/ | ||
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; | ||
} | ||
} | ||
} |