-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Build more tags using method context (#753)
- Loading branch information
Showing
11 changed files
with
240 additions
and
21 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
...ore/src/main/java/io/micronaut/configuration/metrics/aggregator/AbstractMethodTagger.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,56 @@ | ||
/* | ||
* Copyright 2017-2019 original 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.micronaut.configuration.metrics.aggregator; | ||
|
||
import io.micrometer.core.instrument.Tag; | ||
import io.micronaut.aop.MethodInvocationContext; | ||
import io.micronaut.core.annotation.Indexed; | ||
import io.micronaut.core.annotation.NonNull; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* Appends additional {@link io.micrometer.core.instrument.Tag} to metrics annotated with {@link io.micrometer.core.annotation.Timed} and {@link io.micrometer.core.annotation.Counted}. | ||
* | ||
* @author Haiden Rothwell | ||
* @since 5.5.0 | ||
*/ | ||
@Indexed(AbstractMethodTagger.class) | ||
public abstract class AbstractMethodTagger { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractMethodTagger.class); | ||
|
||
private final Class<? extends AbstractMethodTagger> implClass = this.getClass(); | ||
|
||
/** | ||
* Build list of tags using {@link io.micronaut.aop.MethodInvocationContext} which will be included on published metric. | ||
* @param context Context of the method annotated | ||
* @return List of {@link io.micrometer.core.instrument.Tag} which will be included in the metric | ||
*/ | ||
@NonNull protected abstract List<Tag> buildTags(@NonNull MethodInvocationContext<Object, Object> context); | ||
|
||
@NonNull public final List<Tag> getTags(@NonNull MethodInvocationContext<Object, Object> context) { | ||
List<Tag> tags = buildTags(context); | ||
if (tags != null) { | ||
return tags; | ||
} else { | ||
LOGGER.error("{} returned null list of tags and will not include additional tags on metric", implClass); | ||
return Collections.emptyList(); | ||
} | ||
} | ||
} |
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
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
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
17 changes: 17 additions & 0 deletions
17
micrometer-core/src/test/groovy/io/micronaut/docs/MethodNameTagger.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,17 @@ | ||
package io.micronaut.docs; | ||
|
||
import io.micrometer.core.instrument.Tag; | ||
import io.micronaut.aop.MethodInvocationContext; | ||
import io.micronaut.configuration.metrics.aggregator.AbstractMethodTagger; | ||
import jakarta.inject.Singleton; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
@Singleton | ||
public class MethodNameTagger extends AbstractMethodTagger { | ||
@Override | ||
public List<Tag> buildTags(MethodInvocationContext<Object, Object> context) { | ||
return Collections.singletonList(Tag.of("method", context.getMethodName())); | ||
} | ||
} |
Oops, something went wrong.