Nightingale is an open-source cloud-native monitoring system. This article explains how to monitor Java programs with Nightingale.
How do you monitor Java programs? Common approaches include:
- Actuator
- Micrometer
- JMX
- Metrics exposed by the component itself
Actuator
If you are running a Spring Boot app, you can use Actuator directly to expose metrics. You can use the following prompt to ask GPT:
如何开启 actuator,暴露 Prometheus 格式的监控指标?
I’m pasting the AI’s response here — it should mostly be correct. Feel free to try it out and submit issues or PRs if you find problems.
To enable Actuator in a Spring Boot project and expose Prometheus monitoring metrics, follow these steps:
1. Add Dependencies
Add the following dependencies to your pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
If you are using Gradle:
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'io.micrometer:micrometer-registry-prometheus'
2. Configure application.properties or application.yml
Ensure that the Actuator endpoints and the Prometheus metrics endpoint are exposed. For example:
management.endpoints.web.exposure.include=*
management.endpoint.prometheus.enabled=true
management.metrics.export.prometheus.enabled=true
Or in YAML format:
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
prometheus:
enabled: true
metrics:
export:
prometheus:
enabled: true
3. Start the Application
After starting your Spring Boot application, the Actuator’s Prometheus endpoint will be exposed by default at /actuator/prometheus.
4. Verify the Metrics Endpoint
Use a browser or curl to access:
http://localhost:8080/actuator/prometheus
You’ll see Prometheus-formatted monitoring metrics, which can be scraped by Prometheus or Categraf.
Notes:
- If you only want to expose specific endpoints (such as
prometheusandhealth), you can adjust the configuration tomanagement.endpoints.web.exposure.include=prometheus,health - For more advanced configurations (such as custom metrics), you can inject
MeterRegistryin your code and register custom metrics.
Micrometer
If you are not running a Spring Boot app, you can also use Micrometer to expose Prometheus-formatted monitoring metrics. Micrometer is a standalone library that can be integrated with any Java application. Actuator is actually built on top of Micrometer. You can use the following prompt to ask GPT:
如果没有使用 actuator,仅仅使用了 micrometer,应该如何把指标导出到 Prometheus?
I’m pasting the AI’s response here — it should mostly be correct. Feel free to try it out and submit issues or PRs if you find problems.
1. Add Micrometer and Prometheus Dependencies
Using Maven as an example:
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
2. Register PrometheusMeterRegistry
In your application code, manually create and register PrometheusMeterRegistry, for example:
import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.micrometer.prometheus.PrometheusConfig;
import io.micrometer.core.instrument.MeterRegistry;
PrometheusMeterRegistry prometheusRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
// 你可以通过 MeterRegistry 来注册自定义指标
// prometheusRegistry.counter("my_custom_counter").increment();
3. Expose the Prometheus Metrics HTTP Endpoint
Micrometer does not automatically expose an HTTP endpoint — you need to implement an HTTP endpoint yourself, for example using Spring MVC or another web framework:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PrometheusController {
private final PrometheusMeterRegistry prometheusRegistry;
public PrometheusController(PrometheusMeterRegistry prometheusRegistry) {
this.prometheusRegistry = prometheusRegistry;
}
@GetMapping("/prometheus")
public String scrape() {
return prometheusRegistry.scrape();
}
}
Alternatively, for non-Spring projects, you can expose the /prometheus path directly using a web server such as Jetty, Undertow, or Netty, returning the content of prometheusRegistry.scrape() as the response. Finally, use Prometheus or Categraf to scrape this endpoint.
JMX
If the Java program you want to monitor is not your own — for example open-source components like Tomcat, Kafka, or Zookeeper — these components usually expose monitoring metrics via JMX. You can use the following prompt to ask GPT:
普通的 Java 中间件,比如 Tomcat、Kafka,应该如何采集其监控指标?
- We recommend using JMX Exporter — a jar package that runs as a javaagent.
- Download the jmx_exporter jar package.
- Locate the component’s startup command and add javaagent-related parameters, for example
-javaagent:/path/to/jmx_prometheus_javaagent-<version>.jar=PORT:/path/to/config.yamlto specify the jmx_exporter jar path, the port to expose metrics on, and the configuration file path.
This will expose Prometheus metrics on the specified port (such as http://localhost:PORT/metrics). You can then use Prometheus or Categraf to scrape this endpoint.
Note that different components require different configuration files. JMX Exporter provides many examples; the specific address is: https://github.com/prometheus/jmx_exporter/tree/main/examples. You can keep asking the AI:
- What do the configuration items in config.yaml mean
- What is a Java MBean
- How to configure JMX Exporter to collect a specific MBean
- Feed jmx_exporter’s sample configuration to the AI and have it analyze the specific meaning of that configuration
Metrics Exposed by the Component Itself
Some components have built-in ways to expose monitoring metrics. For example, Tomcat exposes various metrics at the HTTP endpoint /manager/status/all. Categraf’s tomcat plugin collects metrics based on this endpoint. The configuration method is:
- Modify tomcat-users.xml to add the following — this creates a user to access the
/manager/status/allendpoint:
<role rolename="admin-gui" />
<user username="tomcat" password="s3cret" roles="manager-gui" />
- Comment out the following in
webapps/manager/META-INF/context.xml:
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
- Configure the Tomcat collection address and authentication information in Categraf’s
conf/input.tomcat/tomcat.toml.
Note: The JMX approach is generic, but each component exposes its own metrics differently — the above only uses Tomcat as an example. For other components, you need to consult their documentation separately.