Adjust Tx rate limiter for unused transactions
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / reporting / MetricsReporter.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.reporting;
9
10 import com.codahale.metrics.JmxReporter;
11 import com.codahale.metrics.MetricRegistry;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15
16 /**
17  * Maintains metrics registry that is provided to reporters.
18  * At the moment only one reporter exists {@code JmxReporter}.
19  * More reporters can be added.
20  * <p/>
21  * The consumers of this class will only be interested in {@code MetricsRegistry}
22  * where metrics for that consumer gets stored.
23  */
24 public class MetricsReporter implements AutoCloseable {
25
26     private static LoadingCache<String, MetricsReporter> METRIC_REPORTERS = CacheBuilder.newBuilder().build(
27             new CacheLoader<String, MetricsReporter>() {
28                 @Override
29                 public MetricsReporter load(String domainName) {
30                     return new MetricsReporter(domainName);
31                 }
32             });
33
34     private final String domainName;
35     private final JmxReporter jmxReporter;
36     private final MetricRegistry metricRegistry = new MetricRegistry();
37
38     private MetricsReporter(String domainName) {
39         this.domainName = domainName;
40         jmxReporter = JmxReporter.forRegistry(metricRegistry).inDomain(domainName).build();
41         jmxReporter.start();
42     }
43
44     public static MetricsReporter getInstance(String domainName) {
45         return METRIC_REPORTERS.getUnchecked(domainName);
46     }
47
48     public MetricRegistry getMetricsRegistry() {
49         return metricRegistry;
50     }
51
52     @Override
53     public void close() {
54         jmxReporter.close();
55
56         METRIC_REPORTERS.invalidate(domainName);
57     }
58 }