When using the Java programming language, you need to monitor JVM performance. TMP collects the JVM monitoring data exposed by applications and provides an out-of-the-box Grafana dashboard for it.
This document uses deploying a Java application in TKE as an example to describe how to use TMP to monitor the application status.
Note:If you have already used Spring Boot as the development framework, please see Spring Boot Integration.
Note:As a major programming language, Java has a comprehensive ecosystem, where Micrometer has been widely used as a metric timestamping SDK. This document uses Micrometer as an example to describe how to monitor JVM.
Add Maven dependencies to the pom.xml
file and adjust the version as needed as follows:
<dependency>
<groupid>io.prometheus</groupid>
<artifactid>simpleclient</artifactid>
<version>0.9.0</version>
</dependency>
<dependency>
<groupid>io.micrometer</groupid>
<artifactid>micrometer-registry-prometheus</artifactid>
<version>1.1.7</version>
</dependency>
When the project is started, add the corresponding monitoring configuration. In addition, Micrometer also provides the collection of some common metrics, which are in the io.micrometer.core.instrument.binder
package and can be added as needed as follows:
public class Application {
// It can be used in custom monitoring as a global variable
public static final PrometheusMeterRegistry registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
static {
// Add a global Prometheus label. We recommend you add the corresponding application name to it
registry.config().commonTags("application", "java-demo");
}
public static void main(String[] args) throws Exception {
// Add JVM monitoring
new ClassLoaderMetrics().bindTo(registry);
new JvmMemoryMetrics().bindTo(registry);
new JvmGcMetrics().bindTo(registry);
new ProcessorMetrics().bindTo(registry);
new JvmThreadMetrics().bindTo(registry);
new UptimeMetrics().bindTo(registry);
new FileDescriptorMetrics().bindTo(registry);
System.gc(); // Test GC
try {
// Expose the Prometheus HTTP service. If it already exists, you can use the existing HTTP server
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/metrics", httpExchange -> {
String response = registry.scrape();
httpExchange.sendResponseHeaders(200, response.getBytes().length);
try (OutputStream os = httpExchange.getResponseBody()) {
os.write(response.getBytes());
}
});
new Thread(server::start).start();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Note:As monitoring of JVM GC pauses is implemented through the GarbageCollector Notification mechanism, the monitoring data will be generated only after a GC occurs. The above sample actively calls
System.gc()
to make the test more straightforward.
After the application is started locally, you can access the metric data of the Prometheus protocol through http://localhost:8080/metrics
.
If you have already configured a Docker image environment locally, proceed to the next step; otherwise, configure one as instructed in Getting Started.
Dockerfile
in the root directory of the project. Please modify it based on your actual project conditions as follows:FROM openjdk:8-jdk
WORKDIR /java-demo
ADD target/java-demo-*.jar /java-demo/java-demo.jar
CMD ["java","-jar","java-demo.jar"]
namespace
, ImageName
, and image tag
as needed.mvn clean package
docker build . -t ccr.ccs.tencentyun.com/[namespace]/[ImageName]:[image tag]
docker push ccr.ccs.tencentyun.com/[namespace]/[ImageName]:[image tag]
mvn clean package
docker build . -t ccr.ccs.tencentyun.com/prom_spring_demo/java-demo:latest
docker push ccr.ccs.tencentyun.com/prom_spring_demo/-demo:latest
namespace
to deploy the service. Use the following YAML configuration to create the corresponding Deployment:
Note:If you want to create in the console, please see Spring Boot Integration.
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
k8s-app: java-demo
name: java-demo
namespace: spring-demo
spec:
replicas: 1
selector:
matchLabels:
k8s-app: java-demo
template:
metadata:
labels:
k8s-app: java-demo
spec:
containers:
- image: ccr.ccs.tencentyun.com/prom_spring_demo/java-demo
imagePullPolicy: Always
name: java-demo
ports:
- containerPort: 8080
name: metric-port
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
imagePullSecrets:
- name: qcloudregistrykey
restartPolicy: Always
schedulerName: default-scheduler
terminationGracePeriodSeconds: 30
Pod Monitor
to define a Prometheus scrape task. Below is a sample YAML configuration:apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
name: java-demo
namespace: cm-prometheus
spec:
namespaceSelector:
matchNames:
- java-demo
podMetricsEndpoints:
- interval: 30s
path: /metrics
port: metric-port
selector:
matchLabels:
k8s-app: java-demo
Was this page helpful?