OpenLiberty / open-liberty

Open Liberty is a highly composable, fast to start, dynamic application server runtime environment
https://openliberty.io
Eclipse Public License 2.0
1.16k stars 597 forks source link

Metrics 5.0 (MicroProfile 6.0) implementation uncorrect behaviours #25032

Closed andreaminutoibm closed 1 year ago

andreaminutoibm commented 1 year ago

Gauge metrics from Metrics 5.0 implementation don't work as expected, at least for my understanding of the specification.

Versions:

Features:

Problems:

  1. Using @Gauge annotation, a gauge metric is not registered at runtime and doesn't show up when called by /metrics API
  2. Using manually registered method, the gauge metric shows up when called by /metrics API but it is shown as a float number even though it has been registered as an Integer.

Scenario: Suppose we are implementing a servlet that must contain a gauge metric to report the number of active requests.

Problem 1: Sample code (the metric is not registered at runtime)

@WebServlet(name = "MyServlet",  urlPatterns = {"/myservlet"})
public class MyServlet extends HttpServlet
{
   @Serial
   private static final long serialVersionUID = 93456419673850054L;

   // Counter
   private final AtomicInteger activeRequests = new AtomicInteger(0);

   @Gauge(name = "myservlet_active_requests", description = "MyServlet active requests", absolute = true, unit = MetricUnits.NONE)
   public Integer getActiveRequests()
   {
      return activeRequests.get();
   }
}

Problem 2: Sample code (the metric is registered but it shows up as a float number)

@WebServlet(name = "MyServlet",  urlPatterns = {"/myservlet"})
public class MyServlet extends HttpServlet
{
   @Serial
   private static final long serialVersionUID = 93456419673850054L;

   // Counter
   private final AtomicInteger activeRequests = new AtomicInteger(0);

   public Integer getActiveRequests()
   {
      return activeRequests.get();
   }

   @Inject
   @RegistryScope(scope= MetricRegistry.APPLICATION_SCOPE)
   MetricRegistry metricRegistry;

   public void init(ServletConfig config) throws ServletException
   {
      Metadata m = Metadata.builder()
                              .withName("myservlet_active_requests")
                              .withDescription("MyServlet active requests")
                              .withUnit(MetricUnits.NONE)
                              .build();

      metricRegistry.gauge(m, this::getActiveRequests);
   }
}

The /metrics?scope=application api reports:

# HELP myservlet_active_requests MyServlet active requests
# TYPE myservlet_active_requests gauge
myservlet_active_requests{mp_scope="application",} 12.0
Channyboy commented 1 year ago

Hello @andreaminutoibm, 1. CDI does not consider all targets on a servlet to be "intercept-able". We recommend to use a RestfulWS (formerly JAX-RS) if you intend to instrument with annotations.

  1. In mpMetrics-5.0 the underlying implementation is now using micrometer metrics. As a consequence of this, the exporting of metrics to /metrics uses the Prometheus client library which formats the metrics into float values.

Channyboy commented 1 year ago

@andreaminutoibm Is this okay to close?

andreaminutoibm commented 1 year ago

It’s ok to close for me. Thank you.

Andrea Minuto

Andrea Minuto Executive IT Specialist IBM Expert Labs Red Hat Certified Architect

39 335 143 3009 Mobile @.**@.> Via Luigi Stipa, 150 00148 ROMA (Italia)

From: David Chan @.> Sent: mercoledì 24 maggio 2023 20:17 To: OpenLiberty/open-liberty @.> Cc: Andrea Minuto @.>; Mention @.> Subject: [EXTERNAL] Re: [OpenLiberty/open-liberty] Metrics 5.0 (MicroProfile 6.0) implementation uncorrect behaviours (Issue #25032)

@andreaminutoibm Is this okay to close? — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned. Message ID: OpenLiberty/open-liberty/issues/25032/1561727205@ github. com ZjQcmQRYFpfptBannerStart This Message Is From an External Sender This message came from outside your organization. ZjQcmQRYFpfptBannerEnd

@andreaminutoibmhttps://github.com/andreaminutoibm Is this okay to close?

— Reply to this email directly, view it on GitHubhttps://github.com/OpenLiberty/open-liberty/issues/25032#issuecomment-1561727205, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ANSHAZZOHQWPT5M5Q42LJDLXHZGCBANCNFSM6AAAAAAXCGV6GQ. You are receiving this because you were mentioned.Message ID: @.***>

Unless otherwise stated above:

IBM Italia S.p.A. Sede Legale: Circonvallazione Idroscalo - 20090 Segrate (MI) Cap. Soc. euro 347.256.998,80 C. F. e Reg. Imprese MI 01442240030 - Partita IVA 10914660153 Società con unico azionista Società soggetta all'attività di direzione e coordinamento di International Business Machines Corporation