Skip to content

Instantly share code, notes, and snippets.

@ogavrisevs
Last active November 13, 2023 01:17
Show Gist options
  • Save ogavrisevs/3deab84ca1966ce896302b8515ecae16 to your computer and use it in GitHub Desktop.
Save ogavrisevs/3deab84ca1966ce896302b8515ecae16 to your computer and use it in GitHub Desktop.
Spring boot sample app with attached prometheus_jmx

Intor

This is a spring boot sample app with attached [prometheus_jmx] (https://github.com/prometheus/jmx_exporter) agent. Agent exposes JVM metrics to 10245 ports. When the app is deployed on k8s cluster Prometheus automatically reads metrics and makes them available for query.

PS (If this gist will collect 100 starts I will make deeper blog post in this topic)

Build app

mvn clean package   
docker build -t prometheus-java-sample .

Test

docker run -d -p 8080:8080 -p 10254:10254 prometheus-java-sample
curl 127.0.0.1:8080
curl 127.0.0.1:10254/metrics 

Push to registry

eval $(aws ecr --profile=prod get-login --no-include-email --region eu-central-1)
docker tag prometheus-java-sample 11122233.dkr.ecr.eu-central-1.amazonaws.com/repo:prometheus-java-sample
docker push 11122233.dkr.ecr.eu-central-1.amazonaws.com/repo:prometheus-java-sample

Deploy to k8s

kubectl create -f app.yaml

Draw Graph

jvm_memory_bytes_used{app="pro-java-sample",area="heap"}
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: pro-java-sample
labels:
commit: latest
spec:
replicas: 3
selector:
matchLabels:
app: pro-java-sample
template:
metadata:
labels:
app: pro-java-sample
commit: latest
spec:
containers:
- name: pro-java-sample
image: 111222333.dkr.ecr.eu-central-1.amazonaws.com/repo:prometheus-java-sample
imagePullPolicy: Always
ports:
- name: http-port
containerPort: 8080
protocol: TCP
- name: jmx-port
containerPort: 10254
---
apiVersion: v1
kind: Service
metadata:
name: pro-java-sample
labels:
app: pro-java-sample
commit: latest
annotations:
prometheus.io/port: "10254"
prometheus.io/scrape: "true"
spec:
ports:
- name: http-port
port: 8080
targetPort: 8080
- name: jmx-port
port: 10254
targetPort: 10254
selector:
app: pro-java-sample
commit: latest
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: pro-java-sample-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: "/"
spec:
rules:
- host:
http:
paths:
- path: /pro-java-sample
backend:
serviceName: pro-java-sample
servicePort: 8080
---
username:
password:
rules:
- pattern: ".*"
FROM openjdk:8-jre-alpine
RUN apk update && apk upgrade && apk --update add curl && rm -rf /tmp/* /var/cache/apk/*
ENV VERSION 0.11.0
ENV JAR jmx_prometheus_javaagent-$VERSION.jar
RUN mkdir -p /opt/jmx_exporter
RUN curl -L https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/$VERSION/$JAR -o /opt/jmx_exporter/$JAR
RUN chmod +x /opt/jmx_exporter/$JAR
COPY config.yaml /opt/jmx_exporter/config.yaml
COPY target/HelloWorld-0.0.1-SNAPSHOT.jar /app.jar
EXPOSE 8080
EXPOSE 10254
CMD /usr/bin/java -Dspring.profiles.active=default -javaagent:/opt/jmx_exporter/$JAR=10254:/opt/jmx_exporter/config.yaml -jar /app.jar
package com.bla.laa;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
@SpringBootApplication
@RestController
public class HelloWorld {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(HelloWorld.class, args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bla.laa</groupId>
<artifactId>HelloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment