How to use Microprofile-Metrics API 4.0

16 views Asked by At

It seems that the API for Counter Metrics in Eclipse Microprofile has changed a lot from version 2.0 ... 4.0

I did not find an actual example how to count a custom metric. For example I have a method that should increase a custom counter:

@Singleton
@Startup
public class BackupService {

  @Metric(name = "eventProcessed")
  private Counter pullCounter;

  public ItemCollection pullSnapshot()  {
    ....
    // inc counter
    pullCounter.inc();
  }

Next I wrote a CDI Bean Controller to show the current count:

@Named
@RequestScoped
public class BackupController implements Serializable {

    public long getPullCount() {

        // find counter by name
        SortedMap<MetricID, Counter> allCounters = metricRegistry.getCounters();

        for (Map.Entry<MetricID, Counter> entry : allCounters.entrySet()) {

            MetricID metricID = entry.getKey();
            if (metricID.getName().endsWith("eventProcessed")) {
                return entry.getValue().getCount();
            }
        }
        logger.warning("Metric Counter : " + name + " not found!");
        return 0;
    }
}

But the injected counter is always null. What is wrong with this code? Did I need to register the counter first?

I am using Microprofile 6.0 on Wildfly 25

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

Maybe someone can provide a correct example code or a tutorial page for Microprofile 6.0 ?

0

There are 0 answers