forked from micrometer-metrics/micrometer
-
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.
Closes micrometer-metricsgh-4725
- Loading branch information
Showing
7 changed files
with
321 additions
and
41 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
57 changes: 57 additions & 0 deletions
57
micrometer-core/src/main/java/io/micrometer/core/aop/CountedMeterTagAnnotationHandler.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,57 @@ | ||
/* | ||
* Copyright 2024 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.core.aop; | ||
|
||
import io.micrometer.common.KeyValue; | ||
import io.micrometer.common.annotation.AnnotationHandler; | ||
import io.micrometer.common.annotation.ValueExpressionResolver; | ||
import io.micrometer.common.annotation.ValueResolver; | ||
import io.micrometer.core.instrument.Counter; | ||
|
||
import java.util.function.Function; | ||
|
||
/** | ||
* Annotation handler for {@link MeterTag}. To add support for {@link MeterTag} on | ||
* {@link CountedAspect} check the | ||
* {@link CountedAspect#setMeterTagAnnotationHandler(CountedMeterTagAnnotationHandler)} | ||
* method. | ||
* | ||
* @author Marcin Grzejszczak | ||
* @author Johnny Lim | ||
*/ | ||
public class CountedMeterTagAnnotationHandler extends AnnotationHandler<Counter.Builder> { | ||
|
||
/** | ||
* Creates a new instance of {@link CountedMeterTagAnnotationHandler}. | ||
* @param resolverProvider function to retrieve a {@link ValueResolver} | ||
* @param expressionResolverProvider function to retrieve a | ||
* {@link ValueExpressionResolver} | ||
*/ | ||
public CountedMeterTagAnnotationHandler( | ||
Function<Class<? extends ValueResolver>, ? extends ValueResolver> resolverProvider, | ||
Function<Class<? extends ValueExpressionResolver>, ? extends ValueExpressionResolver> expressionResolverProvider) { | ||
super((keyValue, builder) -> builder.tag(keyValue.getKey(), keyValue.getValue()), resolverProvider, | ||
expressionResolverProvider, MeterTag.class, (annotation, o) -> { | ||
if (!(annotation instanceof MeterTag)) { | ||
return null; | ||
} | ||
MeterTag meterTag = (MeterTag) annotation; | ||
return KeyValue.of(MeterTagSupport.resolveTagKey(meterTag), | ||
MeterTagSupport.resolveTagValue(meterTag, o, resolverProvider, expressionResolverProvider)); | ||
}); | ||
} | ||
|
||
} |
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
55 changes: 55 additions & 0 deletions
55
micrometer-core/src/main/java/io/micrometer/core/aop/MeterTagSupport.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,55 @@ | ||
/* | ||
* Copyright 2024 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.core.aop; | ||
|
||
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 java.util.function.Function; | ||
|
||
/** | ||
* Support for {@link MeterTag}. | ||
* | ||
* @author Marcin Grzejszczak | ||
* @author Johnny Lim | ||
*/ | ||
final class MeterTagSupport { | ||
|
||
static String resolveTagKey(MeterTag annotation) { | ||
return StringUtils.isNotBlank(annotation.value()) ? annotation.value() : annotation.key(); | ||
} | ||
|
||
static String resolveTagValue(MeterTag 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
Oops, something went wrong.