ryantenney / metrics-spring

Spring integration for Metrics
http://www.ryantenney.com/metrics-spring/
Apache License 2.0
702 stars 226 forks source link

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

Open finira opened 7 years ago

finira commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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

finira commented 7 years ago

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