Class Metrics

java.lang.Object
org.mockserver.metrics.Metrics

public class Metrics extends Object
Author:
jamesdbloom
  • Constructor Details

  • Method Details

    • resetAdditionalMetricsForTesting

      public static void resetAdditionalMetricsForTesting()
      Reset the one-shot registration guard and null all lazily-registered metrics so that a subsequent new Metrics(configuration) call re-registers them. Also clears the default Prometheus registry.

      Public for cross-package test access (e.g. chaos injection tests); intended for test use only to guarantee deterministic test ordering.

    • clear

      public static void clear()
    • clear

      public static void clear(Metrics.Name name)
    • set

      public void set(Metrics.Name name, Integer value)
    • get

      public static Integer get(Metrics.Name name)
    • registerOtelRequestDurationHistogram

      public static void registerOtelRequestDurationHistogram(io.opentelemetry.api.metrics.DoubleHistogram histogram)
      Register an OTel histogram for request duration. Called by OtelMetricsExporter when OTLP export is enabled.
    • observeRequestDurationSeconds

      public static void observeRequestDurationSeconds(double seconds)
      Record a request-handling duration (seconds) in the latency histogram. No-op unless metrics are enabled (the histogram is null until then), so a caller on the request hot path pays nothing when metrics are off.
    • observeRequestDurationByMethodSeconds

      public static void observeRequestDurationByMethodSeconds(double seconds, String method)
      Record a request-handling duration (seconds) in the per-method labeled histogram. No-op unless route labels are enabled.
      Parameters:
      seconds - duration in seconds
      method - the HTTP method (e.g. "GET", "POST")
    • getSlowRequestCount

      public static long getSlowRequestCount()
      Return the current slow-request count, or 0 if metrics are disabled. Used by OtelMetricsExporter to mirror the Prometheus counter via OTLP.
    • incrementSlowRequestTotal

      public static void incrementSlowRequestTotal()
      Increment the slow request counter. No-op unless metrics are enabled.
    • getHttpChaosInjectedCount

      public static long getHttpChaosInjectedCount(String faultType)
      Return the current chaos-injected count for the given fault type, or 0 if metrics are disabled. Used by OtelMetricsExporter to mirror the Prometheus counter via OTLP.
    • incrementHttpChaosInjected

      public static void incrementHttpChaosInjected(String faultType)
      Increment the HTTP chaos injected counter for the given fault type. No-op when metrics are disabled (counter not registered) or faultType is null.
      Parameters:
      faultType - one of "drop", "error", "latency", "truncate", "malformed", "slow", "quota", or "graphql"
    • incrementMcpToolCall

      public static void incrementMcpToolCall(String toolName)
      Increment the MCP tool call counter for the given tool name. No-op when metrics are disabled (counter not registered) or toolName is null. Counts each completed tool invocation (called after the tool handler returns).
      Parameters:
      toolName - the name of the MCP tool invoked (from the bounded tool registry)
    • getMcpToolCallCount

      public static long getMcpToolCallCount(String toolName)
      Return the current MCP tool call count for the given tool name, or 0 if metrics are disabled.
    • incrementAsyncMessagePublished

      public static void incrementAsyncMessagePublished(String channel)
      Increment the async-messages-published counter for the given channel. No-op when metrics are disabled (counter not registered) or channel is null. Called by the mockserver-async publish path (one increment per message published to a broker).
      Parameters:
      channel - the broker channel/topic the message was published to
    • incrementAsyncMessageConsumed

      public static void incrementAsyncMessageConsumed(String channel)
      Increment the async-messages-consumed counter for the given channel. No-op when metrics are disabled (counter not registered) or channel is null. Called by the mockserver-async subscriber record path (one increment per message recorded from a broker).
      Parameters:
      channel - the broker channel/topic the message was consumed from
    • getAsyncMessagePublishedCount

      public static long getAsyncMessagePublishedCount(String channel)
      Return the current async-messages-published count for the given channel, or 0 if metrics are disabled.
    • getAsyncMessageConsumedCount

      public static long getAsyncMessageConsumedCount(String channel)
      Return the current async-messages-consumed count for the given channel, or 0 if metrics are disabled.
    • setActiveExpectationsSupplier

      public static void setActiveExpectationsSupplier(Supplier<List<Expectation>> supplier)
      Set the supplier of active expectations. Called by HttpState at startup so the expectations-by-type GaugeWithCallback can read live state at scrape time without a core-to-netty dependency.
      Parameters:
      supplier - returns the list of currently-active expectations
    • getActiveExpectationCountByType

      public static Map<String,Integer> getActiveExpectationCountByType()
      Per-action-type count of currently-active expectations. Backs the mock_server_expectations_by_type Prometheus gauge; reads the live expectation list at scrape time via the registered supplier.
    • getActiveServiceChaosCountByFaultType

      public static Map<String,Integer> getActiveServiceChaosCountByFaultType()
      Per-fault-type count of currently-active service-scoped chaos profiles. Backs the mock_server_active_service_chaos Prometheus gauge and its OTLP mirror; reads the registry directly (not gated on metricsEnabled, the gauge is only registered when metrics are on).
    • increment

      public void increment(Metrics.Name name)
    • increment

      public void increment(Action.Type type)
    • decrement

      public void decrement(Metrics.Name name)
    • decrement

      public void decrement(Action.Type type)
    • clearRequestAndExpectationMetrics

      public static void clearRequestAndExpectationMetrics()
    • clearActionMetrics

      public static void clearActionMetrics()
    • clearWebSocketMetrics

      public static void clearWebSocketMetrics()