2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.reporting;
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;
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.
21 * The consumers of this class will only be interested in {@code MetricsRegistry}
22 * where metrics for that consumer gets stored.
24 public class MetricsReporter implements AutoCloseable {
26 private static LoadingCache<String, MetricsReporter> METRIC_REPORTERS = CacheBuilder.newBuilder().build(
27 new CacheLoader<String, MetricsReporter>() {
29 public MetricsReporter load(String domainName) {
30 return new MetricsReporter(domainName);
34 private final String domainName;
35 private final JmxReporter jmxReporter;
36 private final MetricRegistry metricRegistry = new MetricRegistry();
38 private MetricsReporter(String domainName) {
39 this.domainName = domainName;
40 jmxReporter = JmxReporter.forRegistry(metricRegistry).inDomain(domainName).build();
44 public static MetricsReporter getInstance(String domainName) {
45 return METRIC_REPORTERS.getUnchecked(domainName);
48 public MetricRegistry getMetricsRegistry() {
49 return metricRegistry;
56 METRIC_REPORTERS.invalidate(domainName);