Skip to content

Commit

Permalink
BufferPoolMetricSet support to collect JMX metrics from remote connec…
Browse files Browse the repository at this point in the history
…tion (alibaba#41)
  • Loading branch information
RobertoHuang authored and ralf0131 committed Jun 12, 2019
1 parent 6167d2f commit 05a4bbe
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ public <T extends Metric> T register(String name, T metric) throws IllegalArgume
return register(MetricName.build(name), metric);
}

/**
* 〈unRegister all metrics.〉
*
* eg:
* When collecting remote machine JVM metrics through the JMX port, May be due to network jitter or
* remote process restart, Need to cancel the previously collected metrics and re-register.
*
* @date 2019.06.06 14:35:45
*/
public void unRegisterAll() {
metrics.clear();
}

/**
* Given a {@link Metric}, registers it under the given name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
import org.slf4j.LoggerFactory;

import javax.management.JMException;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -42,10 +43,10 @@ public class BufferPoolMetricSet implements MetricSet {
private static final String[] NAMES = { "count", "used", "capacity" };
private static final String[] POOLS = { "direct", "mapped" };

private final MBeanServer mBeanServer;
private final MBeanServerConnection mBeanServerConnection;

public BufferPoolMetricSet(MBeanServer mBeanServer) {
this.mBeanServer = mBeanServer;
public BufferPoolMetricSet(MBeanServerConnection mBeanServerConnection) {
this.mBeanServerConnection = mBeanServerConnection;
}

@Override
Expand All @@ -55,14 +56,14 @@ public Map<MetricName, Metric> getMetrics() {
for (int i = 0; i < ATTRIBUTES.length; i++) {
final String attribute = ATTRIBUTES[i];
final String name = NAMES[i];

try {
final ObjectName on = new ObjectName("java.nio:type=BufferPool,name=" + pool);
mBeanServer.getMBeanInfo(on);
gauges.put(MetricRegistry.name(pool, name),
new JmxAttributeGauge(mBeanServer, on, attribute));
mBeanServerConnection.getMBeanInfo(on);
gauges.put(MetricRegistry.name(pool, name), new JmxAttributeGauge(mBeanServerConnection, on, attribute));
} catch (JMException ignored) {
LOGGER.debug("Unable to load buffer pool MBeans, possibly running on Java 6");
} catch (IOException ioException) {
LOGGER.debug("Unable to load buffer pool MBeans, an exception occur, cause of", ioException);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.alibaba.metrics.jvm;

import com.alibaba.metrics.Gauge;
import com.alibaba.metrics.MetricName;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Before;
import org.junit.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;

/**
* 〈jmx remote metrics test.〉
*
* @create 2019/6/6
*/
public class JmxRemoteMetricsTest {
private ObjectName mapped;
private final MetricName MAPPED = MetricName.build("mapped");
private final MetricName MAPPED_USED = MAPPED.resolve("used");

/**
* You can get the MBeanServerConnection in the following way:
*
* Map<String, Object> environment = new HashMap<String, Object>();
* environment.put(JMXConnector.CREDENTIALS, new String[]{USERNAME, PASSWORD});
* JMXServiceURL jmxServiceURL = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi");
* MBeanServerConnection mBeanServerConnection = JMXConnectorFactory.connect(jmxServiceURL, environment).getMBeanServerConnection();
**/
private final MBeanServerConnection mBeanServerConnection = mock(MBeanServerConnection.class);

@Before
public void setUp() throws Exception {
this.mapped = new ObjectName("java.nio:type=BufferPool,name=mapped");
}

@Test
public void testCollectionRemoteMetrics() throws Exception {
BufferPoolMetricSet bufferPoolMetricSet = new BufferPoolMetricSet(mBeanServerConnection);
final Gauge gauge = (Gauge) bufferPoolMetricSet.getMetrics().get(MAPPED_USED);
when(mBeanServerConnection.getAttribute(mapped, "MemoryUsed")).thenReturn(100);
assertThat(gauge.getValue()).isEqualTo(100);
}
}

0 comments on commit 05a4bbe

Please sign in to comment.