Class Metrics

java.lang.Object
com.codename1.backend.metrics.Metrics

public final class Metrics extends Object

The server's metrics: every Instrument by name, and the ones the server records about itself.

  private static final Counter ORDERS = Metrics.counter("orders.placed",
          "Orders accepted", "{order}");
  ...
  ORDERS.increment();

Asking for a name twice answers the same instrument, so a static field initialized in two classes, or a server restarted in one process, shares it. Asking for an existing name as a different kind is refused.

What reads them: the OTLP exporter, when the build enables OpenTelemetry; the management endpoint's metrics and prometheus views; and the development MCP server. None of them is linked into a server whose build does not ask for it, and an instrument nothing reads costs its own recording and nothing more.

What the server records

Once enableServer(HttpServer, DataSource) has run -- the generated entry point calls it when metrics are on -- every request is measured in http.server.request.duration (milliseconds, by route template, method and status), and the server's counters, the database pool, the task executors and the process's memory are published as gauges.

  • Method Details

    • counter

      public static Counter counter(String name, String description, String unit)
      A counter; the same one for the same name.
    • upDownCounter

      public static Counter upDownCounter(String name, String description, String unit)
      A counter that can go down too -- items in a queue, open sessions.
    • histogram

      public static Histogram histogram(String name, String description, String unit)
      A histogram with the default bucket boundaries, which suit milliseconds.
    • histogram

      public static Histogram histogram(String name, String description, String unit, double[] bounds, String[] labels)
      A histogram with the given bucket boundaries, ascending, and label keys (up to three), or null for either default.
    • gauge

      public static Gauge gauge(String name, String description, String unit, Gauge.Source source)
      A gauge read from source when metrics are collected. A gauge registered again under the same name REPLACES the old one, since its source is usually an object that has been replaced too -- a restarted server's pool.
    • gauge

      public static Gauge gauge(String name, String description, String unit, Gauge.MultiSource source)
      A gauge with several labelled values. See Gauge.MultiSource.
    • instruments

      public static List instruments()
      Every instrument, in registration order.
    • get

      public static Instrument get(String name)
      The instrument called name, or null.
    • startTimeMillis

      public static long startTimeMillis()
      When this process's metrics started counting, for cumulative points.
    • addSource

      public static void addSource(String name, String description, String unit, Gauge.Source source)
      Adds one server's source to the gauge called name, which reports the sum of every source still registered. A server's managed-resource gauges go through here and are removed when it stops, so a stopped server's bean is never read again and a second live server adds to the gauge instead of silently replacing the first one's.
    • removeSource

      public static void removeSource(String name, Gauge.Source source)
      Removes a source addSource(String, String, String, Gauge.Source) added; the gauge goes with its last one.
    • enableServer

      public static void enableServer(HttpServer server, DataSource pool)

      Starts recording a server's own metrics. Called once the server is listening; the pool may be null.

      The instruments are the process's, as an OpenTelemetry meter's are, so with two servers in one process each built-in gauge reports the SUM over the servers still running, and the request histogram counts both. Each server used to register its own source under the same name, so the last one started silently replaced the others' -- and a stopped server's stayed.

    • disableServer

      public static void disableServer(HttpServer server, DataSource pool)
      A server has stopped: its gauges stop counting it, and once none is left the server instruments stop recording.
    • route

      public static void route(String template)
      Records which route template matched, for the request histogram.
    • requestStarted

      public static long requestStarted()
      A request is starting; answers the time to hand to requestEnded(long, String, int).
    • requestEnded

      public static void requestEnded(long started, String method, int status)
      A request has been answered.
    • jobRan

      public static void jobRan(String job, long millis, boolean failed)
      A scheduled job has run.
    • snapshot

      public static Map snapshot()
      Every instrument and its points, for the management endpoint and MCP: name -> {kind, description, unit, points}.
    • prometheus

      public static String prometheus()
      Every instrument in the Prometheus text exposition format.