MicroMeter
Micrometer provides a simple facade over the instrumentation clients for the most popular monitoring systems, allowing you to instrument your JVM-based application code without vendor lock-in. Think SLF4J, but for application metrics! Application metrics recorded by Micrometer are intended to be used to observe, alert, and react to the current/recent operational state of your environment. https://micrometer.io/docs
Few concepts in micro meter
Meter is collection of measurements about application .
Meter registry is to held(store) collected measurements
Micrometer packs with a supported set of Meter
primitives including: Timer
, Counter
, Gauge
, DistributionSummary
, LongTaskTimer
, FunctionCounter
, FunctionTimer
, and TimeGauge
. Different meter types result in a different number of time series metrics. Find details for few of it on
Spring boot provides dependency management and auto configuration through actuator . Adding following dependency add required dependencies and auto configuration of beans
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Sprint boot auto configure the micro meter registry based on the registry available on class path .If we add below , spring will auto configure prometheus meter registry
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
Create simple spring boot application to monitor
Create simple web application using https://start.spring.io/
Add simple rest controller ex:
@GetMapping("/monitoring/{name}")
public String sayHello(@PathVariable("name") String name) {
return "Hello " + name + " Greetings from micro meter application.";
}
@GetMapping("/")
public String sayHi() {
return "Micro monitoring applcation ";
}
Create image using docker file using any of the steps mentioned in below links
FROM adoptopenjdk:11-jre-hotspot as builder
WORKDIR application
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract
FROM adoptopenjdk:11-jre-hotspot
WORKDIR application
COPY --from=builder application/dependencies/ ./
COPY --from=builder application/snapshot-dependencies/ ./
COPY --from=builder application/resources/ ./
COPY --from=builder application/application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]
Prometheus yaml to defining that pull data from application using actuator url
# my global config
global:
scrape_interval: 5s
evaluation_interval: 5s
scrape_configs:
# timeseries scraped from this config.
- job_name: 'app_metrics'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['web:8080']
Create docker compose file with prometheus(store data) and Grafana (visualise)
version: "3"
services:
web:
image: web:0.0.2-SNAPSHOT
ports:
- "8080:8080"
hostname: web
prometheus:
image: prom/prometheus
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
hostname: prometheus
grafana:
image: grafana/grafana
depends_on:
- prometheus
ports:
- "3000:3000"
hostname: grafana
Run docker-compose up -d in the same folder having above docker compose file
Once all three apps are up and running you can access promotheus and grafana and app
Prometheus : http://localhost:9090/targets
Grafana : http://localhost:3000/login
Application : http://localhost:8080/actuator/prometheus
Configure grafana data source and create dashboard
Configure Datasource
Use admin and admin as credentials to log in (change default password is optional .. you can skip )
Click on gear icon → choose data source → Add datasource → choose prometheus
only add URL details (should be same as application name defined in docker compose) and use remaining default details .
If url working , you will see below when you click on save
Configure Dashboard
Click on plus symbol ,choose import option and input as 9568 0r 4701 and add datasource as prometheus .
Time to observe metrics
We can hit application rest url using browser or use postman and run in loop .Other easy way to create load and change the metrics and observe the same is using ab — Apache HTTP server benchmarking tool (Available on mac so no need to install but for other operating system we have to install ab tool)
ab -n 500000 -c 50 http://localhost:8080/monitoring/test
Observe the dashboard on grafana
Check below values from the response of http://localhost:8080/actuator/prometheus
http_server_requests_seconds_count
http_server_requests_seconds_sum
Metrics about Outbound http calls
All outbound calls can be monitored if we instantiate rest template client using the rest template builder. Rest template builder uses rest template customiser to register interceptor to collect metrics. Find details in below classes
MetricsClientHttpRequestInterceptor
MetricsRestTemplateCustomizer
Actuator supports following metrics
Supported Metrics
Spring Boot registers the following core metrics when applicable:
- JVM metrics, report utilization of:
- Various memory and buffer pools
- Statistics related to garbage collection
- Threads utilization
- Number of classes loaded/unloaded
- CPU metrics
- File descriptor metrics
- Kafka consumer, producer, and streams metrics
- Log4j2 metrics: record the number of events logged to Log4j2 at each level
- Logback metrics: record the number of events logged to Logback at each level
- Uptime metrics: report a gauge for uptime and a fixed gauge representing the application’s absolute start time
- Tomcat metrics (
server.tomcat.mbeanregistry.enabled
must be set totrue
for all Tomcat metrics to be registered) - Spring integration metrics