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.MetricRegistry;
11 import com.codahale.metrics.jmx.JmxReporter;
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.
22 * The consumers of this class will only be interested in {@code MetricsRegistry}
23 * where metrics for that consumer gets stored.
25 public class MetricsReporter implements AutoCloseable {
27 private static final LoadingCache<String, MetricsReporter> METRIC_REPORTERS = CacheBuilder.newBuilder().build(
28 new CacheLoader<String, MetricsReporter>() {
30 public MetricsReporter load(final String domainName) {
31 return new MetricsReporter(domainName);
35 private final String domainName;
36 private final JmxReporter jmxReporter;
37 private final MetricRegistry metricRegistry = new MetricRegistry();
39 MetricsReporter(final String domainName) {
40 this.domainName = domainName;
41 jmxReporter = JmxReporter.forRegistry(metricRegistry).inDomain(domainName).build();
45 public static MetricsReporter getInstance(final String domainName) {
46 return METRIC_REPORTERS.getUnchecked(domainName);
49 public MetricRegistry getMetricsRegistry() {
50 return metricRegistry;
57 METRIC_REPORTERS.invalidate(domainName);