Skip to content

Commit

Permalink
added custom mbean to test server (#120)
Browse files Browse the repository at this point in the history
* added custom mbean to test server

* updated test-server documentation
  • Loading branch information
cristianciutea committed May 30, 2022
1 parent 95055ad commit 4b9f755
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
6 changes: 3 additions & 3 deletions gojmx/gojmx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion test-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -68,5 +74,6 @@ $ ./nrjmx
test:type=Cat,*
{}
```
## Troubleshooting

If registering mBeans fails, you can check the container logs for errors.
19 changes: 19 additions & 0 deletions test-server/src/main/java/org/newrelic/jmx/CustomCat.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
7 changes: 7 additions & 0 deletions test-server/src/main/java/org/newrelic/jmx/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4b9f755

Please sign in to comment.