0

I'm trying to use Quarkus MP-Metrics. In a simple API I have a gauge as follows:

  @Path("/count")
  @GET
  @Gauge(unit = MetricUnits.NONE, name = "customersNumberGauge", description = "Number of customers in the inventory")
  public int getCustomersNumber()
  {
    return customerService.getCustomers().size();
  }

I also have a couple of unit tests, for example:

  @Test
  public void testGetCustomerById()
  {
    get("/customers/1").then().statusCode(200).body("firstName", equalTo("Robert"));
    assertMetricValue("fr.simplex_software.aws.lambda.quarkus.CustomerResource.customerByIdCount", 1);
    get("/customers/1").then().statusCode(200).body("firstName", equalTo("Robert"));
    assertMetricValue("fr.simplex_software.aws.lambda.quarkus.CustomerResource.customerByIdCount", 2);
  }

which fails with the exception:

 java.lang.IllegalArgumentException: A metric with metricID MetricID{name='fr.simplex_software.aws.lambda.quarkus.CustomerResource.customersNumberGauge', tags=[]} already exists

If I comment out the @Gauge annotation everything works fine. It's worth noting that, by doing that, I'm following exactly the Quarkus quick starts.

Many thanks for any help. Kind regards, Seymour

Seymour Glass
  • 81
  • 2
  • 15
  • How does `assertMetricValue` look like? It seems it's registering a "new" metric each time. – Ladicek Sep 22 '20 at 13:52
  • 1
    One possible cause that comes to my mind is that the JAX-RS class is of a CDI scopes that creates multiple instances. You can only use a Gauge in classes which are `@Singleton` or `@ApplicationScoped`. Are you sure it isn't the problem? If you have no scope annotation, the default applied to JAX-RS classes is `@Singleton`. – Jan Martiška Sep 22 '20 at 14:47
  • Thank you very much for shedding light on this issue. While the default CDI scope works, in my case I've forgotten a @RequestScope in the header of my API class. removing it, works of course as expected. Many thanks again. – Seymour Glass Sep 23 '20 at 10:57

0 Answers0