Skip to content

Example Spring application (not Spring Boot) showing how to collect metrics and scrape with Prometheus

Notifications You must be signed in to change notification settings

goeh/spring-metrics-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Example - Add metrics to Spring application

This project is a minimal example on how to add application metrics to a Spring Application (not Spring Boot) using Micrometer and Prometheus.

Step 1: Add Micrometer+Prometheus dependency.

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

Step 2: Create a PrometheusMeterRegistry bean.

@Bean
public PrometheusMeterRegistry prometheusMeterRegistry() {
    final PrometheusMeterRegistry registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
    new JvmMemoryMetrics().bindTo(registry);
    return registry;
}

Step 3: Inject bean into target class and add custom metrics.

Counter.builder("http.requests.total")
    .description("Http Request Total")
    .tags("method","GET","handler","/hello","status","200")
    .register(registry).increment();

Step 4: Configure Prometheus to scrape the /metrics endpoint.

scrape_configs:
  - job_name: myapp
    static_configs:
      - targets: ['localhost:8080']
    metrics_path: /metrics

Done!

You should now be able to view your application's metrics in Prometheus.

About

Example Spring application (not Spring Boot) showing how to collect metrics and scrape with Prometheus

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages