Skip to content

Commit

Permalink
Support metrics with JMX
Browse files Browse the repository at this point in the history
[#151844595]

Fixes #101
  • Loading branch information
acogoluegnes committed Jul 30, 2018
1 parent dff4405 commit 5a2da35
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 11 deletions.
12 changes: 9 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

<rabbitmq.version>5.3.0</rabbitmq.version>
<commons-cli.version>1.1</commons-cli.version>
<metrics.version>4.0.3</metrics.version>
<metrics.version>3.2.6</metrics.version>
<micrometer.version>1.0.6</micrometer.version>
<logback.version>1.2.3</logback.version>
<jetty.version>9.4.11.v20180605</jetty.version>
Expand Down Expand Up @@ -122,14 +122,20 @@
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<artifactId>micrometer-registry-datadog</artifactId>
<version>${micrometer.version}</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-datadog</artifactId>
<artifactId>micrometer-registry-jmx</artifactId>
<version>${micrometer.version}</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>${micrometer.version}</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand Down
12 changes: 11 additions & 1 deletion src/docs/asciidoc/monitoring.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ for more information about tags and dimensions.

PerfTest builds on top https://micrometer.io[Micrometer] to report gathered metrics to various monitoring systems.
Nevertheless, not all systems supported by Micrometer are actually supported by PerfTest.
PerfTest currently supports https://www.datadoghq.com/[Datadog] and https://prometheus.io/[Prometheus].
PerfTest currently supports https://www.datadoghq.com/[Datadog], https://en.wikipedia.org/wiki/Java_Management_Extensions[JMX],
and https://prometheus.io/[Prometheus].
Don't hesitate to
https://github.com/rabbitmq/rabbitmq-perf-test/issues[request support for other monitoring systems].

Expand All @@ -76,6 +77,15 @@ Another useful option is the step size or reporting frequency. The default value
--metrics-datadog-step-size 20
```

==== JMX

JMX support provides a simple way to view metrics locally. Use the `--metrics-jmx` flag to
export metrics to JMX:

```
./runjava com.rabbitmq.perf.PerfTest --metrics-jmx
```

==== Prometheus

Use the `-mpr` or `--metrics-prometheus` flag to enable metrics reporting to Prometheus:
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/rabbitmq/perf/CompositeMetrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public CompositeMetrics() {
metrics.add(new BaseMetrics());
metrics.add(new PrometheusMetrics());
metrics.add(new DatadogMetrics());
metrics.add(new JmxMetrics());
}

@Override
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/rabbitmq/perf/DatadogMetrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.rabbitmq.client.ConnectionFactory;
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import io.micrometer.datadog.DatadogConfig;
import io.micrometer.datadog.DatadogMeterRegistry;
Expand All @@ -36,7 +37,7 @@
*/
public class DatadogMetrics implements Metrics {

private volatile DatadogMeterRegistry registry;
private volatile MeterRegistry registry;

public Options options() {
Options options = new Options();
Expand All @@ -63,6 +64,7 @@ public void configure(CommandLineProxy cmd, CompositeMeterRegistry meterRegistry
dataCfg.put("datadog.uri", strArg(cmd, "mdu", null));

DatadogConfig config = new DatadogConfig() {

@Override
public Duration step() {
return Duration.ofSeconds(Integer.valueOf(dataCfg.get("datadog.step")));
Expand All @@ -83,7 +85,9 @@ public String get(String k) {
}

public void close() {
registry.close();
if (registry != null) {
registry.close();
}
}

@Override
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/com/rabbitmq/perf/JmxMetrics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) 2018 Pivotal Software, Inc. All rights reserved.
//
// This software, the RabbitMQ Java client library, is triple-licensed under the
// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2
// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see
// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL,
// please see LICENSE-APACHE2.
//
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
// either express or implied. See the LICENSE file for specific language governing
// rights and limitations of this software.
//
// If you have any questions regarding licensing, please contact us at
// [email protected].

package com.rabbitmq.perf;

import com.rabbitmq.client.ConnectionFactory;
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import io.micrometer.jmx.JmxConfig;
import io.micrometer.jmx.JmxMeterRegistry;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;

/**
*
*/
public class JmxMetrics implements Metrics {

private volatile MeterRegistry registry;

public Options options() {
Options options = new Options();
options.addOption(new Option("mjx", "metrics-jmx", false, "enable JMX metrics"));
return options;
}

public void configure(CommandLineProxy cmd, CompositeMeterRegistry meterRegistry, ConnectionFactory factory) throws Exception {
if (isEnabled(cmd)) {
registry = new JmxMeterRegistry(
JmxConfig.DEFAULT,
Clock.SYSTEM
);
meterRegistry.add(registry);
}
}

public void close() {
if (registry!= null) {
registry.close();
}
}

@Override
public String toString() {
return "JMX Metrics";
}
}
7 changes: 6 additions & 1 deletion src/main/java/com/rabbitmq/perf/PrometheusMetrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class PrometheusMetrics implements Metrics {

private volatile Server server;

private volatile PrometheusMeterRegistry registry;

public Options options() {
Options options = new Options();
options.addOption(new Option("mpr", "metrics-prometheus", false, "enable Prometheus metrics"));
Expand All @@ -53,7 +55,7 @@ public Options options() {

public void configure(CommandLineProxy cmd, CompositeMeterRegistry meterRegistry, ConnectionFactory factory) throws Exception {
if (isEnabled(cmd)) {
PrometheusMeterRegistry registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
meterRegistry.add(registry);
int prometheusHttpEndpointPort = intArg(cmd, "mpp", 8080);
String prometheusHttpEndpoint = strArg(cmd, "mpe", "metrics");
Expand Down Expand Up @@ -97,6 +99,9 @@ public void close() throws Exception {
if (server != null) {
server.stop();
}
if (registry != null) {
registry.close();
}
}

@Override
Expand Down
6 changes: 2 additions & 4 deletions src/test/java/com/rabbitmq/perf/DatadogMetricsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public void tearDown() throws Exception {
}

@Test
public void datadog() throws Exception {
DatadogMetrics metrics = new DatadogMetrics();
public void metricsShouldBeSentToDatadogHttpEndpoint() throws Exception {
metrics = new DatadogMetrics();
Options options = metrics.options();

CommandLineParser parser = new GnuParser();
Expand All @@ -103,8 +103,6 @@ public void datadog() throws Exception {
assertTrue(content.get().contains("\"metric\":\"dummy\""));
assertTrue(content.get().contains("42.0"));
assertTrue(content.get().contains("\"host\":\"test\""));

metrics.close();
}

private Server startMockDatadogService() throws Exception {
Expand Down
70 changes: 70 additions & 0 deletions src/test/java/com/rabbitmq/perf/JmxMetricsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) 2018 Pivotal Software, Inc. All rights reserved.
//
// This software, the RabbitMQ Java client library, is triple-licensed under the
// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2
// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see
// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL,
// please see LICENSE-APACHE2.
//
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
// either express or implied. See the LICENSE file for specific language governing
// rights and limitations of this software.
//
// If you have any questions regarding licensing, please contact us at
// [email protected].

package com.rabbitmq.perf;

import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Options;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
*
*/
public class JmxMetricsTest {

Metrics metrics = new JmxMetrics();

@AfterEach
public void tearDown() throws Exception {
metrics.close();
}

@Test
public void metricsShouldBeExposedAsMbeans() throws Exception {
Options options = metrics.options();

CommandLineParser parser = new GnuParser();
CommandLine rawCmd = parser.parse(
options,
("--metrics-jmx").split(" ")
);
CommandLineProxy cmd = new CommandLineProxy(options, rawCmd, name -> null);
CompositeMeterRegistry registry = new CompositeMeterRegistry();
AtomicInteger metric = registry.gauge("dummy", new AtomicInteger(0));
metric.set(42);
metrics.configure(cmd, registry, null);

MBeanServer server = ManagementFactory.getPlatformMBeanServer();
Set<ObjectName> objectNames = server.queryNames(new ObjectName("*:name=dummy"), null);
assertEquals(1, objectNames.size());
ObjectName objectName = objectNames.iterator().next();
MBeanInfo info = server.getMBeanInfo(objectName);
Object attribute = server.getAttribute(objectName, info.getAttributes()[0].getName());
assertEquals(metric.get(), Double.valueOf(attribute.toString()).intValue());
}
}
2 changes: 2 additions & 0 deletions src/test/java/com/rabbitmq/perf/MetricsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public void noDuplicateOptionBetweenMetrics() {
Set<String> options = new HashSet<>();
List<Metrics> metrics = new ArrayList<>();
metrics.add(new BaseMetrics());
metrics.add(new DatadogMetrics());
metrics.add(new JmxMetrics());
metrics.add(new PrometheusMetrics());
for (Metrics metric : metrics) {
for (Object optObj : metric.options().getOptions()) {
Expand Down

0 comments on commit 5a2da35

Please sign in to comment.