Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@MetricOptions process based on condition #897

Merged

Conversation

hrothwell
Copy link
Contributor

@hrothwell hrothwell commented Dec 2, 2024

Opened in relation to #896

Opening as draft initially for asking a few questions. No longer draft as of Dec 3rd 2024

Plan + thoughts:

  • Add new value onto MetricOptions annotation that takes in classes used to filter
    • Question 1: I looked into using Condition as I was thinking this might work similarly to the Requires annotation, but I was unsure if it was possible to retrieve this in these metric interceptors as things currently are. Is there a way to retrieve a ConditionContext while in these interceptors?
    • Question 2: Right now these are classes that are intended to be initialized (which I also could not find how Condition impls were being initialized / used), but if needing access to the environment (Beans, active environments, etc) these may need to follow the pattern that AbstractMethodTagger does right now where it is a bean, injected into the metric interceptors, filtered, etc. Is there a way to fetch any context related information without defining a Bean or anything of the sort?
  • Use new classes to determine if metric will be calculated/processed/etc in TimedInterceptor and CountedInterceptor
    • Only did TimedInterceptor prior to opening due to potential for things to change based on feedback.

Edit December 3rd 2024:

  • using expressions to evaluate condition, thank you for the reminder/recommendation @graemerocher
  • Create MetricOptionsUtil to share means of fetching the condition between CountedInterceptor and TimedInterceptor
    • Thought that maybe the methods of fetching the other properties from the annotation could also be shared here, but wanted to limit changes in this PR. If approved / merged I am planning to do a follow-up PR to do that additional change
  • Tests showing that evaluated condition prevents metric from being published / calculated when false

@hrothwell hrothwell changed the title Metric option specific env @MetricOptions process based on condition Dec 2, 2024
@hrothwell hrothwell changed the title @MetricOptions process based on condition @MetricOptions process based on condition Dec 2, 2024
@graemerocher
Copy link
Contributor

@hrothwell
Copy link
Contributor Author

have you considered using expressions for this? See https://micronaut-projects.github.io/micronaut-cache/latest/guide/#conditional and https://github.com/micronaut-projects/micronaut-cache/blob/6a3e54f85b6a468c4f50265ff6a35fde218e0c71/cache-core/src/main/java/io/micronaut/cache/interceptor/CacheInterceptor.java#L537

I had not, in fact I actually forgot that those existed / not thought about it. I will take a look at using that to build this out 👍

@hrothwell hrothwell marked this pull request as ready for review December 3, 2024 19:01
@hrothwell
Copy link
Contributor Author

@graemerocher thank you for that suggestion!

@graemerocher graemerocher added the type: enhancement New feature or request label Dec 3, 2024
Copy link
Contributor

@graemerocher graemerocher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks could you add some documentation somewhere?

* @since 5.10.0
* @author Haiden Rothwell
*/
public class MetricOptionsUtil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be final and probably doesn't need to be public

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when not public it is not callable from the interceptors due to differing packages


private static final Logger LOG = LoggerFactory.getLogger(MetricOptionsUtil.class);

public static boolean evaluateCondition(MethodInvocationContext<?, ?> context) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add javadoc and make not public

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar situation as the whole class being not public: if public is removed it is unable to be called where it currently is

@graemerocher
Copy link
Contributor

could you document @MetricOptions and the conditional support here https://github.com/micronaut-projects/micronaut-micrometer/edit/5.9.x/src/main/docs/guide/metricsAnnotations.adoc ?

@graemerocher graemerocher merged commit 196a787 into micronaut-projects:5.10.x Dec 5, 2024
6 checks passed
@graemerocher
Copy link
Contributor

Thanks for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants