From 4b9f75586d8dc593324557a62de37b908971ac38 Mon Sep 17 00:00:00 2001 From: Cristian Ciutea Date: Mon, 30 May 2022 10:11:48 +0200 Subject: [PATCH] added custom mbean to test server (#120) * added custom mbean to test server * updated test-server documentation --- gojmx/gojmx_test.go | 6 +++--- test-server/README.md | 9 ++++++++- .../main/java/org/newrelic/jmx/CustomCat.java | 19 +++++++++++++++++++ .../main/java/org/newrelic/jmx/Service.java | 7 +++++++ 4 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 test-server/src/main/java/org/newrelic/jmx/CustomCat.java diff --git a/gojmx/gojmx_test.go b/gojmx/gojmx_test.go index 44f79e3c..2365314c 100644 --- a/gojmx/gojmx_test.go +++ b/gojmx/gojmx_test.go @@ -865,14 +865,14 @@ func TestProcessExits(t *testing.T) { close(waitToStart) }() - + // For troubleshooting purposes. defer func() { stdoutBytes, _ := io.ReadAll(&stdout) - fmt.Println(stdoutBytes) + fmt.Println(fmt.Sprintf("[DEBUG] Stdout: '%s'", stdoutBytes)) stderrBytes, _ := io.ReadAll(&stderr) - fmt.Println(stderrBytes) + fmt.Println(fmt.Sprintf("[DEBUG] Stderr: '%s'", stderrBytes)) }() <-waitToStart diff --git a/test-server/README.md b/test-server/README.md index 530adb99..67109a6a 100644 --- a/test-server/README.md +++ b/test-server/README.md @@ -49,6 +49,12 @@ In the port `4567`: * `POST /cat` * BODY: `{"name":"Isidoro"}` would register in JMX a cat named Isidoro to the registry name + +* `POST /custom_cat` + * BODY: `{"name": "TombstoneScannedHistogram", "mBeanName":"org.apache.cassandra.metrics:type=Table,keyspace=test2,scope=test2,name=TombstoneScannedHistogram","floatValue":1.1,"doubleValue":3.2,"timeout": 60000}` + * would register in JMX a custom mBean named 'org.apache.cassandra.metrics:type=Table,keyspace=test2,scope=test2,name=TombstoneScannedHistogram' + * notice the 'timeout' attribute, it allows specifying a delay before the mBean will be returned by JMX endpoint. + * `PUT /clear` * Will clear all the cats from JMX @@ -68,5 +74,6 @@ $ ./nrjmx test:type=Cat,* {} ``` +## Troubleshooting - +If registering mBeans fails, you can check the container logs for errors. \ No newline at end of file diff --git a/test-server/src/main/java/org/newrelic/jmx/CustomCat.java b/test-server/src/main/java/org/newrelic/jmx/CustomCat.java new file mode 100644 index 00000000..ac4e7ce6 --- /dev/null +++ b/test-server/src/main/java/org/newrelic/jmx/CustomCat.java @@ -0,0 +1,19 @@ +package org.newrelic.jmx; + +import javax.management.MBeanRegistration; +import javax.management.MBeanServer; +import javax.management.ObjectName; + +public class CustomCat extends Cat { + private String mBeanName; + + public CustomCat(String mBeanName, String name, Double doubleValue, Float floatValue, Boolean boolValue, Number numberValue, Integer timeout, long dateValue) { + super(name, doubleValue, floatValue, boolValue, numberValue, timeout, dateValue); + this.mBeanName = mBeanName; + } + + @Override + public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception { + return new ObjectName(this.mBeanName); + } +} diff --git a/test-server/src/main/java/org/newrelic/jmx/Service.java b/test-server/src/main/java/org/newrelic/jmx/Service.java index 661b9668..e1a066e7 100644 --- a/test-server/src/main/java/org/newrelic/jmx/Service.java +++ b/test-server/src/main/java/org/newrelic/jmx/Service.java @@ -50,6 +50,13 @@ public static void main(String[] args) throws Exception { return "ok!\n"; }); + post("/custom_cat", (req, res) -> { + CustomCat cat = gson.fromJson(req.body(), CustomCat.class); + log.info("registering {}", cat); + server.registerMBean(cat, null); + return "ok!\n"; + }); + final ObjectName queryObject = new ObjectName("*:type=Cat,*"); // Removes all registered MBean cats