Sunday, 22 September 2019

Spring boot Actuator metric: @Timed: enable timing on a request url

You can use @Timed annotation to enable time taken measurements for a request uri.

Example
@RequestMapping("/welcome")
@Timed(value = "api.welcome")
public String welcome() {
  helloCounter.increment();
  return "Hello user, have a nice day";
}

Find the below working application.

Step 1: Create a maven project ‘actuatorDemo’.

Step 2: Update pom.xml with maven dependencies.
 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <scope>runtime</scope>
   <optional>true</optional>
  </dependency>

  <dependency>
   <groupId>com.h2database</groupId>
   <artifactId>h2</artifactId>
   <scope>runtime</scope>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>

 </dependencies>


pom.xml
<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>actuatorDemo</groupId>
 <artifactId>actuatorDemo</artifactId>
 <version>1</version>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.6.RELEASE</version>
  <relativePath />
 </parent>

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

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <scope>runtime</scope>
   <optional>true</optional>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
  </dependency>  <dependency>
   <groupId>com.h2database</groupId>
   <artifactId>h2</artifactId>
   <scope>runtime</scope>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>

 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
     <execution>
      <goals>
       <goal>build-info</goal>
      </goals>
     </execution>
    </executions>
   </plugin>

   <plugin>
    <groupId>pl.project13.maven</groupId>
    <artifactId>git-commit-id-plugin</artifactId>

    <executions>
     <execution>
      <id>get-the-git-infos</id>
      <goals>
       <goal>revision</goal>
      </goals>
     </execution>
    </executions>

    <configuration>
     <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
     <prefix>git</prefix>
     <verbose>false</verbose>
     <generateGitPropertiesFile>true</generateGitPropertiesFile>
     <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
     <format>json</format>
     <gitDescribe>
      <skip>false</skip>
      <always>false</always>
      <dirty>-dirty</dirty>
     </gitDescribe>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

Step 3: Create a package ‘com.sample.app’ and define App.java like below.

App.java
package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
 public static void main(String[] args) {
  
  SpringApplication.run(App.class, args);
 }
}

Step 4: Create a package ‘com.sample.app.controller’, define HelloController like below.


HelloController.java
package com.sample.app.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.micrometer.core.annotation.Timed;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;

@RestController
public class HelloController {
  
  private final Counter helloCounter;
  
  public HelloController(MeterRegistry meterRegistry) {
    helloCounter = Counter.builder("api.hello").register(meterRegistry);
  }

  @RequestMapping("/")
  public String home() {
    return "Welcome to spring boot developement";
  }
  
  @RequestMapping("/hello")
  public String hello() {
    helloCounter.increment();
    return "Good Morning";
  }
  
  @RequestMapping("/welcome")
  @Timed(value = "api.welcome")
  public String welcome() {
    helloCounter.increment();
    return "Hello user, have a nice day";
  }

}


application.properties
info.team.name=Alaska
info.team.size=23
info.team.dev=krishna, ram, Sunil, Bikash
info.team.test=hari, pani, panth

info.version=1.23
info.product.owner=Ashwani
info.product.architect=Surav

management.endpoint.health.show-details=always

management.metrics.web.server.auto-time-requests=true
management.endpoints.web.exposure.include=*


Total project structure looks like below.

Run App.java.

Open the url ‘http://localhost:8080/welcome’.

Open the url ‘http://localhost:8080/actuator/metrics/api.welcome’, you will get below response.
{
  "name": "api.welcome",
  "description": null,
  "baseUnit": "seconds",
  "measurements": [
    {
      "statistic": "COUNT",
      "value": 1
    },
    {
      "statistic": "TOTAL_TIME",
      "value": 0.009337399
    },
    {
      "statistic": "MAX",
      "value": 0.009337399
    }
  ],
  "availableTags": [
    {
      "tag": "exception",
      "values": [
        "None"
      ]
    },
    {
      "tag": "method",
      "values": [
        "GET"
      ]
    },
    {
      "tag": "uri",
      "values": [
        "/welcome"
      ]
    },
    {
      "tag": "outcome",
      "values": [
        "SUCCESS"
      ]
    },
    {
      "tag": "status",
      "values": [
        "200"
      ]
    }
  ]
}


You can download complete working application from this link.


Previous                                                    Next                                                    Home

No comments:

Post a Comment