Sunday, 22 September 2019

Spring boot actuator: Register new metric


Step 1: Inject MeterRegistry and initialize the Counter.
private final Counter helloCounter;
  
public HelloController(MeterRegistry meterRegistry) {
  helloCounter = Counter.builder("api.hello").register(meterRegistry);
}

Step 2: Use the Counter object (helloCounter) that you defined in step 1 to measure the metrics.
@RequestMapping("/hello")
public String hello() {
  helloCounter.increment();
  return "Good Morning";
}

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.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";
  }

}


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/actuator/metrics/api.hello’ in browser, you will get below response.

{
  "name": "api.hello",
  "description": null,
  "baseUnit": null,
  "measurements": [
    {
      "statistic": "COUNT",
      "value": 0
    }
  ],
  "availableTags": []
}

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

Hit the url ‘http://localhost:8080/actuator/metrics/api.hello’ again, you will get below response.    
{
  "name": "api.hello",
  "description": null,
  "baseUnit": null,
  "measurements": [
    {
      "statistic": "COUNT",
      "value": 1
    }
  ],
  "availableTags": []
}

As you see count is incremented to 1.

You can download complete working application from this link.
 


Previous                                                    Next                                                    Home

No comments:

Post a Comment