Angara kilkiri
Angara kilkiri

Reputation: 167

How to count number of requests in spring boot app?

I have a spring boot application whose metrics I want to later send to gcp. But for now I just need to find a way to count the total number of requests my application is handling either with actuator or with micrometer. Does anybody know how I could do something like this? Thanks In advance

Upvotes: 2

Views: 9887

Answers (3)

Paulo Berezini
Paulo Berezini

Reputation: 200

U can add Spring Boot Actuator and Micrometer dependencies

application.properties

management.metrics.enable.http.server.requests=true
management.endpoints.web.exposure.include=*

Maven:

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
  <dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-core</artifactId>
  </dependency>
  <!-- Include a Micrometer registry of your choice -->
  <dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
  </dependency>
</dependencies>

Gradle:

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter-actuator'
  implementation 'io.micrometer:micrometer-core'
  implementation 'io.micrometer:micrometer-registry-prometheus'
}

If your runs on localhost:8080 use this

http://localhost:8080/actuator/metrics

"application.ready.time", "application.started.time", "disk.free", "disk.total", "executor.active", "executor.completed", "executor.pool.core", "executor.pool.max", "executor.pool.size",

http://localhost:8080/actuator/prometheus 

spring_data_repository_invocations_seconds_count{exception="None",method="findFirstByTitle",repository="NewsArticleRepository",state="SUCCESS",} 10.0 spring_data_repository_invocations_seconds_sum{exception="None",method="findFirstByTitle",repository="NewsArticleRepository",state="SUCCESS",} 0.0931051 spring_data_repository_invocations_seconds_count{exception="None",method="findByOrderByPublishedAtDesc",repository="NewsArticleRepository",state="SUCCESS",} 1.0 and .....

http://localhost:8080/actuator/metrics/http.server.requests

"name": "http.server.requests", "baseUnit": "seconds", "measurements": [ { "statistic": "COUNT", "value": 20.0 }, { "statistic": "TOTAL_TIME", "value": 2.0688337 }, { "statistic": "MAX", "value": 0.0 } ],

Upvotes: 0

PatrickChen
PatrickChen

Reputation: 1420

A spring-actuator example:

//add pom
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

using the actuator

@Bean
public SecurityWebFilterChain securityWebFilterChain(
  ServerHttpSecurity http) {
    return http.authorizeExchange()
      .pathMatchers("/actuator/**").permitAll()
      .anyExchange().authenticated()
      .and().build();
}

Then you can get the data from http://localhost:8080/metrics

check more from the official documentation

Upvotes: 1

Bal&#225;zs N&#233;meth
Bal&#225;zs N&#233;meth

Reputation: 6637

Have a look at Spring Boot Actuator - HttpTrace

It's not enabled in the latest versions by default but you can do so by implementing HttpTraceRepository.

In this repository you can use a counter to measure the number of requests, then add it to the MetricRegistry. Another option would be to use a filter and then use a counter there.

Upvotes: 2

Related Questions