This repository has been archived by the owner on Dec 14, 2017. It is now read-only.
forked from dropwizard/metrics
/
JmxReporter.java
161 lines (138 loc) · 5.5 KB
/
JmxReporter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright (c) 2012. Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.yammer.metrics.reporting.jmx;
import com.yammer.metrics.core.*;
import com.yammer.metrics.reporting.AbstractReporter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.management.*;
import java.lang.management.ManagementFactory;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* User: gorzell
* Date: 3/16/12
* Time: 1:27 PM
*/
public class JmxReporter extends AbstractReporter implements MetricsRegistryListener,
MetricProcessor<JmxReporter.Context> {
private static final Logger LOGGER = LoggerFactory.getLogger(JmxReporter.class);
private final Map<MetricName, ObjectName> registeredBeans;
private final MBeanServer server;
static final class Context {
private final MetricName metricName;
private final MetricsRegistry registry;
public Context(final MetricName metricName, final MetricsRegistry registry) {
this.metricName = metricName;
this.registry = registry;
}
MetricName getMetricName() {
return metricName;
}
MetricsRegistry getRegistry(){
return registry;
}
}
/**
* Creates a new {@link JmxReporter} for the given registry.
*
* @param registry a {@link com.yammer.metrics.core.MetricsRegistry}
*/
public JmxReporter(MetricsRegistry registry) {
super(registry);
this.registeredBeans = new ConcurrentHashMap<MetricName, ObjectName>(100);
this.server = ManagementFactory.getPlatformMBeanServer();
registry.addListener(this);
}
@Override
public void onMetricAdded(MetricsRegistry registry, MetricName name, Metric metric) {
if (metric != null) {
try {
metric.processWith(this, name, new Context(name, registry));
} catch (Exception e) {
LOGGER.warn("Error processing {}", name, e);
}
}
}
@Override
public void onMetricRemoved(MetricsRegistry registry, MetricName name) {
unregisterBean(name);
}
@Override
public void processMeter(MetricName name, Metered meter, Context context) throws Exception {
registerBean(context.getRegistry().getName(), name, meter);
}
@Override
public void processCounter(MetricName name, Counter counter, Context context) throws Exception {
registerBean(context.getRegistry().getName(), name, counter);
}
@Override
public void processHistogram(MetricName name, Histogram histogram, Context context) throws Exception {
registerBean(context.getRegistry().getName(), name, histogram);
}
@Override
public void processTimer(MetricName name, Timer timer, Context context) throws Exception {
registerBean(context.getRegistry().getName(), name, timer);
}
@Override
public void processGauge(MetricName name, Gauge<?> gauge, Context context) throws Exception {
registerBean(context.getRegistry().getName(), name, gauge);
}
@Override
public void shutdown() {
getMetricsRegistry().removeListener(this);
for (MetricName name : registeredBeans.keySet()) {
unregisterBean(name);
}
registeredBeans.clear();
}
/**
* Starts the reporter.
*/
public final void start() {
getMetricsRegistry().addListener(this);
}
private void registerBean(String registryName, MetricName name, Metric metric)
throws MBeanRegistrationException, OperationsException {
try{
JmxMetric jmxMetric = new JmxMetric(registryName, name, metric);
//TODO Not sure this matters
if(server.isRegistered(jmxMetric.getObjectName())){
server.unregisterMBean(jmxMetric.getObjectName());
}
server.registerMBean(jmxMetric, jmxMetric.getObjectName());
server.registerMBean(jmxMetric.getMetadataMBean(), jmxMetric.getMetadataMBean().getObjectName());
registeredBeans.put(name, jmxMetric.getObjectName());
} catch (Exception e){
throw new MBeanRegistrationException(e, "Problem creating mBean.");
}
}
private void unregisterBean(MetricName name) {
final ObjectName objectName = registeredBeans.remove(name);
if (objectName != null) {
try {
server.unregisterMBean(objectName);
} catch (InstanceNotFoundException e) {
// This is often thrown when the process is shutting down. An application with lots of
// metrics will often begin unregistering metrics *after* JMX itself has cleared,
// resulting in a huge dump of exceptions as the process is exiting.
LOGGER.trace("Error unregistering {}", name, e);
} catch (MBeanRegistrationException e) {
LOGGER.debug("Error unregistering {}", name, e);
}
}
}
}