-
Notifications
You must be signed in to change notification settings - Fork 998
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added missing tests for parameter handling
- Loading branch information
1 parent
7c85c65
commit 7fa9545
Showing
3 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
micrometer-observation/src/test/java/io/micrometer/observation/aop/HighCardinality.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,53 @@ | ||
/** | ||
* Copyright 2022 the original author or 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.micrometer.observation.aop; | ||
|
||
import io.micrometer.common.annotation.NoOpValueResolver; | ||
import io.micrometer.common.annotation.ValueResolver; | ||
|
||
import java.lang.annotation.*; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Inherited | ||
@Target(ElementType.PARAMETER) | ||
@interface HighCardinality { | ||
|
||
/** | ||
* The name of the key of the tag which should be created. | ||
* @return the tag key | ||
*/ | ||
String value() default ""; | ||
|
||
/** | ||
* The name of the key of the tag which should be created. | ||
* @return the tag value | ||
*/ | ||
String key() default ""; | ||
|
||
/** | ||
* Execute this expression to calculate the tag value. Will be analyzed if no value of | ||
* the {@link HighCardinality#resolver()} was set. | ||
* @return an expression | ||
*/ | ||
String expression() default ""; | ||
|
||
/** | ||
* Use this bean to resolve the tag value. Has the highest precedence. | ||
* @return {@link ValueResolver} bean | ||
*/ | ||
Class<? extends ValueResolver> resolver() default NoOpValueResolver.class; | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
...rvation/src/test/java/io/micrometer/observation/aop/HighCardinalityAnnotationHandler.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,70 @@ | ||
/* | ||
* Copyright 2023 VMware, Inc. | ||
* | ||
* 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.micrometer.observation.aop; | ||
|
||
import io.micrometer.common.KeyValue; | ||
import io.micrometer.common.annotation.AnnotationHandler; | ||
import io.micrometer.common.annotation.NoOpValueResolver; | ||
import io.micrometer.common.annotation.ValueExpressionResolver; | ||
import io.micrometer.common.annotation.ValueResolver; | ||
import io.micrometer.common.util.StringUtils; | ||
|
||
import io.micrometer.observation.Observation; | ||
|
||
import java.util.function.Function; | ||
|
||
/** | ||
* Taken from Tracing for testing. | ||
*/ | ||
class HighCardinalityAnnotationHandler extends AnnotationHandler<Observation> { | ||
|
||
public HighCardinalityAnnotationHandler( | ||
Function<Class<? extends ValueResolver>, ? extends ValueResolver> resolverProvider, | ||
Function<Class<? extends ValueExpressionResolver>, ? extends ValueExpressionResolver> expressionResolverProvider) { | ||
super((keyValue, observation) -> observation.highCardinalityKeyValue(keyValue), resolverProvider, | ||
expressionResolverProvider, HighCardinality.class, (annotation, o) -> { | ||
if (!(annotation instanceof HighCardinality)) { | ||
return null; | ||
} | ||
HighCardinality highCardinality = (HighCardinality) annotation; | ||
return KeyValue.of(resolveTagKey(highCardinality), | ||
resolveTagValue(highCardinality, o, resolverProvider, expressionResolverProvider)); | ||
}); | ||
} | ||
|
||
private static String resolveTagKey(HighCardinality annotation) { | ||
return StringUtils.isNotBlank(annotation.value()) ? annotation.value() : annotation.key(); | ||
} | ||
|
||
static String resolveTagValue(HighCardinality annotation, Object argument, | ||
Function<Class<? extends ValueResolver>, ? extends ValueResolver> resolverProvider, | ||
Function<Class<? extends ValueExpressionResolver>, ? extends ValueExpressionResolver> expressionResolverProvider) { | ||
String value = null; | ||
if (annotation.resolver() != NoOpValueResolver.class) { | ||
ValueResolver ValueResolver = resolverProvider.apply(annotation.resolver()); | ||
value = ValueResolver.resolve(argument); | ||
} | ||
else if (StringUtils.isNotBlank(annotation.expression())) { | ||
value = expressionResolverProvider.apply(ValueExpressionResolver.class) | ||
.resolve(annotation.expression(), argument); | ||
} | ||
else if (argument != null) { | ||
value = argument.toString(); | ||
} | ||
return value == null ? "" : value; | ||
} | ||
|
||
} |
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