PaulNUK
PaulNUK

Reputation: 5209

Spring Boot Public Metric not exported by JmxMetricWriter

I have a metric that doesn't easily fit into a counter or a gauge (it's just the size of a particular cache I want to expose).

So I've amended an existing class to implement PublicMetrics:

@Component
public class MyMetric implements PublicMetrics 

and then expose the metric:

@Override
public Collection<Metric<?>> metrics() {
    Collection<Metric<?>> metrics = new TreeSet<Metric<?>>();
    Metric<Integer> cacheSize = new Metric<Integer>("gauge.cacheSize",cacheSize);
    metrics.add(cacheSize);
    return metrics;
}

The Spring Boot docs state :

"To add additional metrics that are computed every time the metrics endpoint is invoked, simply register additional PublicMetrics implementation bean(s). By default, all such beans are gathered by the endpoint."

So in order to expose these additional metrics via JMX,I thought that this would export them along with the out of the box metrics:

@Bean
@ExportMetricWriter
MetricWriter metricWriter(MBeanExporter exporter) {
    return new JmxMetricWriter(exporter);
}

But my metrics aren't appearing when I look at the mbeans in JConsole or VisualVM.

What am I doing wrong here in terms of exporting them, and is there an easier way of exposing some state (I didn't inject a counter or gauge service as I don't have an appropriate method to inject these into)?

Update :

If I call the org.springframework.boot.Endpoint.metricsEndpoint data() operation my gauge is correctly returned with the correct values, it's only the JMX exporter that's not exporting it as an mbean.

Upvotes: 0

Views: 818

Answers (1)

PaulNUK
PaulNUK

Reputation: 5209

It turns out that this is the solution (see here)

The push and pull metrics are automatically exposed via the /metrics endpoint. But out of the box only the pull metrics are exported automatically through MetricWriters. To export the pull metrics (e.g. PublicMetrics) you have to define a MetricsEndpointMetricReader bean like this:

@Bean 
public MetricsEndpointMetricReader metricsEndpointMetricReader(MetricsEndpoint metricsEndpoint) { 
  return new MetricsEndpointMetricReader(metricsEndpoint); 
}

Upvotes: 2

Related Questions