Class Metrics
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 Summary
Modifier and TypeMethodDescriptionstatic voidaddSource(String name, String description, String unit, Gauge.Source source) Adds one server's source to the gauge calledname, which reports the sum of every source still registered.static CounterA counter; the same one for the same name.static voiddisableServer(HttpServer server, DataSource pool) A server has stopped: its gauges stop counting it, and once none is left the server instruments stop recording.static voidenableServer(HttpServer server, DataSource pool) Starts recording a server's own metrics.static Gaugegauge(String name, String description, String unit, Gauge.MultiSource source) A gauge with several labelled values.static Gaugegauge(String name, String description, String unit, Gauge.Source source) A gauge read fromsourcewhen metrics are collected.static InstrumentThe instrument calledname, or null.static HistogramA histogram with the default bucket boundaries, which suit milliseconds.static HistogramA histogram with the given bucket boundaries, ascending, and label keys (up to three), or null for either default.static ListEvery instrument, in registration order.static voidA scheduled job has run.static StringEvery instrument in the Prometheus text exposition format.static voidremoveSource(String name, Gauge.Source source) Removes a sourceaddSource(String, String, String, Gauge.Source)added; the gauge goes with its last one.static voidrequestEnded(long started, String method, int status) A request has been answered.static longA request is starting; answers the time to hand torequestEnded(long, String, int).static voidRecords which route template matched, for the request histogram.static Mapsnapshot()Every instrument and its points, for the management endpoint and MCP:name -> {kind, description, unit, points}.static longWhen this process's metrics started counting, for cumulative points.static CounterupDownCounter(String name, String description, String unit) A counter that can go down too -- items in a queue, open sessions.
-
Method Details
-
counter
-
upDownCounter
-
histogram
-
histogram
-
gauge
A gauge read fromsourcewhen 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
A gauge with several labelled values. SeeGauge.MultiSource. -
instruments
Every instrument, in registration order. -
get
The instrument calledname, or null. -
startTimeMillis
public static long startTimeMillis()When this process's metrics started counting, for cumulative points. -
addSource
Adds one server's source to the gauge calledname, 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
Removes a sourceaddSource(String, String, String, Gauge.Source)added; the gauge goes with its last one. -
enableServer
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
A server has stopped: its gauges stop counting it, and once none is left the server instruments stop recording. -
route
Records which route template matched, for the request histogram. -
requestStarted
public static long requestStarted()A request is starting; answers the time to hand torequestEnded(long, String, int). -
requestEnded
A request has been answered. -
jobRan
A scheduled job has run. -
snapshot
Every instrument and its points, for the management endpoint and MCP:name -> {kind, description, unit, points}. -
prometheus
Every instrument in the Prometheus text exposition format.
-