[En] How to activate microprofile metrics in OpenLiberty

In Jakarta EE Microprofile you can use Metrics easily.  No need for Hystrix project.  The simple code in order to show how to use Metrics in the codebase

@GET
@Path("all")
@Metered
public List<Ping> getAllPings() { 
   return pingBean.getAllPings() ;
}

@POST
@Counted(monotonic = true)
public Ping save(Ping ping) {
   return pingBean.save(ping);
}

Project’s pom.xml could be like this :

<dependency>
   <groupId>javax</groupId>
   <artifactId>javaee-api</artifactId>
   <version>8.0</version>
   <scope>provided</scope>
</dependency>

<dependency>
   <groupId>org.eclipse.microprofile</groupId>
   <artifactId>microprofile</artifactId>
   <version>2.2</version>
   <type>pom</type>
   <scope>provided</scope>
</dependency>

In OpenLiberty’s server.xml file, default server.xml can be found under $OPENLIBETY_HOME/usr/servers/defaultServer/

If you add this two lines into server.xml

<feature>mpMetrics-1.1</feature>
<feature>monitor-1.0</feature>

then you could see the metrics in http://localhost:9080/metrics url

Sample project could be found at https://github.com/altuga/airhacks

No Comments

Post a Comment

Comment
Name
Email
Website