Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with using the JMX reporter with platform MBean server #2839

Closed
rohit2395 opened this issue Sep 19, 2022 · 1 comment
Closed

Issue with using the JMX reporter with platform MBean server #2839

rohit2395 opened this issue Sep 19, 2022 · 1 comment

Comments

@rohit2395
Copy link

rohit2395 commented Sep 19, 2022

Hi. I am using the latest metrics-jmx (4.2.12) library.

Here is a snippet of my code

Initialize the metrics registry:

MetricRegistry registry = new MetricRegistry();
final JmxReporter reporter = JmxReporter.forRegistry(registry).build();
reporter.start();

Register metrics:

String metricKey = "metric.key";

// metrics map
HashMap<String,Metric> metricMap = new HashMap<String,Metric>(6);

..
..
registry.register(metricKey,metricMap.get(metricKey));
..
..

Get the count using ManagementFactory.getPlatformMBeanServer()

MBeanServerConnection server = ManagementFactory.getPlatformMBeanServer()
String metricKey = "metric.key";
ObjectName metric = new ObjectName("metrics:name=" + metricKey);
 if (server.isRegistered(metric)){
      Object object = server.getAttribute(metric, "Count");
      result = (Long) object;
}else {
      System.out.println("Metric is not registered : "+ metric);
}

The issue is it says that the metric is not registered i.e server.isRegistered(metric) returns false.

This works with metrics-jmx (4.0.7) but not metrics-jmx (4.1.0)

Can someone help me if I need to do something else to achieve this? or is it an issue in the library?

@the-thing
Copy link
Contributor

When registering a new metric with JmxReporter the name is generated via com.codahale.metrics.jmx.ObjectNameFactory which might be slightly different between versions.

server.isRegistered(new ObjectName("metrics:name=" + metricKey)) // returns false
server.isRegistered(new ObjectName("metrics:name=" + metricKey + ",type=counters")) // returns true when registered metric is a counter

com.codahale.metrics.jmx.ObjectNameFactory is package private in JmxReporter, but can provide custom one with a domain name via com.codahale.metrics.jmx.JmxReporter.Builder#Builder.

Unfortunately you also need to know the metric types which are hardcoded in the implementation (see usages of com.codahale.metrics.jmx.JmxReporter.JmxListener#createName).

String domain = "metrics";
ObjectNameFactory objectNameFactory = new DefaultObjectNameFactory();

MetricRegistry registry = new MetricRegistry();
JmxReporter reporter = JmxReporter.forRegistry(registry).createsObjectNamesWith(objectNameFactory).inDomain(domain).build();
reporter.start();

String metricKey = "metric.key";
registry.register(metricKey, new Counter());

MBeanServer server = ManagementFactory.getPlatformMBeanServer();
System.out.println(server.isRegistered(objectNameFactory.createName("counters", domain, metricKey)));

@dropwizard dropwizard locked and limited conversation to collaborators Jan 24, 2023
@joschi joschi converted this issue into discussion #3120 Jan 24, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants