Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

How can I put a new key after init in metricSet? #203

Open
finira opened this issue Jun 26, 2017 · 3 comments
Open

How can I put a new key after init in metricSet? #203

finira opened this issue Jun 26, 2017 · 3 comments

Comments

@finira
Copy link

finira commented Jun 26, 2017

I make the simple xml spring config like this:

<!-- Creates a MetricRegistry bean -->
    <metrics:metric-registry id="metricRegistry" />

    <!-- Creates a HealthCheckRegistry bean (Optional) -->
    <metrics:health-check-registry id="health" />

    <!-- Registers BeanPostProcessors with Spring which proxy beans and capture metrics -->
    <!-- Include this once per context (once in the parent context and in any subcontexts) -->
    <metrics:annotation-driven metric-registry="metricRegistry" />

    <!-- Example reporter definiton. Supported reporters include jmx, slf4j, graphite, and others. -->
    <!-- Reporters should be defined only once, preferably in the parent context -->
    <metrics:reporter type="console" metric-registry="metricRegistry" period="5s" />

    <!-- Register metric beans (Optional) -->
    <!-- The metrics in this example require metrics-jvm -->
    <metrics:register metric-registry="metricRegistry">
        <bean metrics:name="jvm.count" id="advices" class="cn.com.servyou.alpha.aop.TheSets" />
    </metrics:register>

register a set class TheSets implements the MetricSet,the class is like this:

public class TheSets implements MetricSet {


    public static Map<String, Metric> metricMap = new LinkedHashMap<String, Metric>();
    public Map<String, Metric> getMetrics() {
        System.out.println("the set class is :" + this);
        System.out.println("set map size is "+ metricMap.size());
        metricMap.put("cc", new Counter());
        return metricMap;
    }

    public static void increase(String key) {
        if (metricMap.get(key) != null) {
            Counter counter1 = (Counter)metricMap.get(key);
            counter1.inc();
        }else {
            Counter counter2 = new Counter();
            counter2.inc();
            metricMap.put(key, counter2);
        }
        System.out.println("invoked map size is "+ metricMap.size());
    }
}

and I invoke the increase method in another class , just like TheSets.increase("cc");.
this works.
but ,when I want to invoke the method with another arg , which like TheSets.increase("dd");. the result is I can't get the counter named with "dd",it seems like the put method on metricMap only works in the getMetrics method.
now I need to put some dynamic keys in the map, how can I make it? @ryantenney
thank you .

@ryantenney
Copy link
Owner

I'm afraid that can't be done. The getMetrics method on a MetricSet is only ever invoked once. What you need to do is get a reference to the MetricRegistry and then replace the body of the increase method with the code: MetricRegistry.counter(key).inc();.

@finira
Copy link
Author

finira commented Jul 11, 2017

Thank you. that means I should init my map before getMetrics

@finira
Copy link
Author

finira commented Aug 31, 2017

it works MetricRegistry.counter(key).inc(); thanks a lot

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants