Class Metrics
- Author:
- jamesdbloom
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classComposite key (scenario + run_id) for the load gauge readers.static enum -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidclear()static voidclear(Metrics.Name name) static voidstatic voidstatic voidvoiddecrement(Metrics.Name name) voiddecrement(Action.Type type) static voidevictLoadRun(String runId) Evict every durablemock_server_load_*series whoserun_idlabel equals the given run id, bounding accumulation of completed-run series.static Integerget(Metrics.Name name) Per-action-type count of currently-active expectations.Per-fault-type count of currently-active service-scoped chaos profiles.static longgetAsyncMessageConsumedCount(String channel) Return the current async-messages-consumed count for the given channel, or 0 if metrics are disabled.static longgetAsyncMessagePublishedCount(String channel) Return the current async-messages-published count for the given channel, or 0 if metrics are disabled.static longReturn the current chaos auto-halt count, or 0 if metrics are disabled.static intCurrent cluster member count, backing themock_server_cluster_membersgauge.static longReturn the current dropped-log-events count, or 0 if metrics are disabled.static longgetExpectationMatchedCount(String expectationId) Return the current per-expectation match count for the given expectation id, or 0 if the per-expectation counter is not registered.static longgetForwardRequestCount(String upstreamHost, String statusClass) Return the current forward-request count for the given upstream host and status class, or 0 if metrics are disabled.static longgetHttpChaosInjectedCount(String faultType) Return the current chaos-injected count for the given fault type, or 0 if metrics are disabled.static longReturn the current LLM cost-budget tripped count, or 0 if metrics are disabled.static doublegetLlmCostUsd(String provider, String model) Return the current cumulative LLM cost in USD for the given provider and model, or 0.0 if LLM metrics are disabled.static doubleReturn the aggregate cumulative LLM cost in USD across all providers and models.static longgetLlmInputTokens(String provider, String model) Return the current LLM input token count for the given provider and model, or 0 if LLM metrics are disabled.static longgetLlmOutputTokens(String provider, String model) Return the current LLM output token count for the given provider and model, or 0 if LLM metrics are disabled.static Map<Metrics.LoadGaugeKey,Integer> Live readers for the OTEL load observable gauges (active VUs / in-flight).static longgetLoadErrorCount(String scenario, String runId, String kind) static Map<Metrics.LoadGaugeKey,Integer> static longgetLoadIterationCount(String scenario, String runId) static longgetLoadRequestCount(String scenario, String runId, String step, String route, String method, String statusClass) Read a per-request load counter value for a fixed-label combination (custom labels empty).static longgetLoadThrottledCount(String scenario, String runId, String reason) static longgetMcpToolCallCount(String toolName) Return the current MCP tool call count for the given tool name, or 0 if metrics are disabled.static intNumber of upstreams whose forward/proxy circuit breaker is currently open, backing themock_server_upstream_circuit_opengauge.static longReturn the current slow-request count, or 0 if metrics are disabled.voidincrement(Metrics.Name name) voidincrement(Action.Type type) static voidincrementAsyncMessageConsumed(String channel) Increment the async-messages-consumed counter for the given channel.static voidincrementAsyncMessagePublished(String channel) Increment the async-messages-published counter for the given channel.static voidIncrement the chaos auto-halt counter.static voidIncrement the dropped-log-events counter (event-log ring buffer full).static voidincrementExpectationMatched(String expectationId) Increment the per-expectation match counter for the given stable expectation id.static voidincrementHttpChaosInjected(String faultType) Increment the HTTP chaos injected counter for the given fault type.static voidIncrement the LLM cost-budget circuit-breaker tripped counter.static voidincrementLlmTokens(String provider, String model, long inputTokens, long outputTokens, Double costUsd) Increment the LLM token and cost counters for a served or forwarded completion.static voidincrementLoadError(String scenario, String runId, String kind) Increment the error counter for the given kind.static voidincrementLoadIteration(String scenario, String runId) Increment the completed-iteration counter (labels scenario, run_id).static voidincrementLoadThrottled(String scenario, String runId, String reason) Increment the throttled-dispatch counter for the given reason.static voidincrementMcpToolCall(String toolName) Increment the MCP tool call counter for the given tool name.static voidIncrement the slow request counter.static booleanReturn true if the per-upstream forward metrics are registered (i.e. metrics are enabled).static booleanReturn true if LLM token/cost counters are registered (i.e. both metricsEnabled and llmMetricsEnabled are true).static booleanTrue if the load metric family is registered (i.e. metrics are enabled).static booleanReturn true if the per-expectation match counter is registered (i.e. both metricsEnabled and perExpectationMetricsEnabled are on).static longloadLatencyPercentileMillis(String scenario, String runId, int pct) Derive a latency percentile (millis) for the given fixed-label combination from the load histogram's classic buckets — bounded memory, no reservoir.static voidobserveForwardRequest(String upstreamHost, Integer statusCode, double latencySeconds) Record observability for a single forwarded/proxied request: its latency (seconds) in the per-upstream histogram and a count in the per-upstream, per-status-class counter.static voidobserveLoadRequest(String scenario, String runId, String step, String route, String method, Integer statusCode, double latencySeconds, long requestBytes, long responseBytes, String traceId, Map<String, String> customLabels) Record one completed load-scenario request: its duration in the histogram (with an optional trace_id exemplar), the request/response body byte counts, and a request count — all labeled by the fixed structured labels plus the allowlisted custom labels.static voidobserveRequestDurationByMethodSeconds(double seconds, String method) Record a request-handling duration (seconds) in the per-method labeled histogram.static voidobserveRequestDurationSeconds(double seconds) Record a request-handling duration (seconds) in the latency histogram.static voidregisterOtelLoadInstruments(io.opentelemetry.api.metrics.DoubleHistogram duration, io.opentelemetry.api.metrics.LongCounter requests, io.opentelemetry.api.metrics.LongCounter requestBytes, io.opentelemetry.api.metrics.LongCounter responseBytes, io.opentelemetry.api.metrics.LongCounter iterations, io.opentelemetry.api.metrics.LongCounter throttled, io.opentelemetry.api.metrics.LongCounter errors) Install the OTel load instruments.static voidregisterOtelRequestDurationHistogram(io.opentelemetry.api.metrics.DoubleHistogram histogram) Register an OTel histogram for request duration.static voidReset the one-shot registration guard and null all lazily-registered metrics so that a subsequentnew Metrics(configuration)call re-registers them.voidset(Metrics.Name name, Integer value) static voidsetActiveExpectationsSupplier(Supplier<List<Expectation>> supplier) Set the supplier of active expectations.static voidsetClusterMemberCountSupplier(Supplier<Integer> supplier) Set the supplier of the current cluster member count.static voidsetLoadGaugeReaders(Supplier<Map<Metrics.LoadGaugeKey, Integer>> activeVusReader, Supplier<Map<Metrics.LoadGaugeKey, Integer>> inflightReader) Register the live readers for the active-VU and in-flight load gauges.
-
Constructor Details
-
Metrics
-
-
Method Details
-
resetAdditionalMetricsForTesting
public static void resetAdditionalMetricsForTesting()Reset the one-shot registration guard and null all lazily-registered metrics so that a subsequentnew 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
-
set
-
get
-
registerOtelRequestDurationHistogram
public static void registerOtelRequestDurationHistogram(io.opentelemetry.api.metrics.DoubleHistogram histogram) Register an OTel histogram for request duration. Called byOtelMetricsExporterwhen 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
Record a request-handling duration (seconds) in the per-method labeled histogram. No-op unless route labels are enabled.- Parameters:
seconds- duration in secondsmethod- 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 byOtelMetricsExporterto mirror the Prometheus counter via OTLP. -
incrementSlowRequestTotal
public static void incrementSlowRequestTotal()Increment the slow request counter. No-op unless metrics are enabled. -
incrementDroppedLogEvents
public static void incrementDroppedLogEvents()Increment the dropped-log-events counter (event-log ring buffer full). No-op unless metrics are enabled (the counter is null otherwise). The authoritative, always-available count is maintained onMockServerEventLog.getDroppedLogEventCount(); this mirrors it to Prometheus when metrics are on. -
getDroppedLogEventCount
public static long getDroppedLogEventCount()Return the current dropped-log-events count, or 0 if metrics are disabled. -
observeForwardRequest
public static void observeForwardRequest(String upstreamHost, Integer statusCode, double latencySeconds) Record observability for a single forwarded/proxied request: its latency (seconds) in the per-upstream histogram and a count in the per-upstream, per-status-class counter. No-op unless metrics are enabled (both metrics are null until then), so the forward path pays nothing when metrics are off.Cardinality is bounded by the number of distinct upstream hosts (the
upstream_hostlabel is the host only — never the full URL/path) and the five status classes (1xx..5xx). A null/blank host is recorded as"unknown".- Parameters:
upstreamHost- the resolved upstream host (no port, no path)statusCode- the upstream response status code, or null if unknownlatencySeconds- the forward latency in seconds (negative values are clamped to 0)
-
getForwardRequestCount
Return the current forward-request count for the given upstream host and status class, or 0 if metrics are disabled. -
isForwardMetricsActive
public static boolean isForwardMetricsActive()Return true if the per-upstream forward metrics are registered (i.e. metrics are enabled). -
getHttpChaosInjectedCount
Return the current chaos-injected count for the given fault type, or 0 if metrics are disabled. Used byOtelMetricsExporterto mirror the Prometheus counter via OTLP. -
incrementHttpChaosInjected
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", "graphql", or "rateLimit"
-
incrementChaosAutoHalt
public static void incrementChaosAutoHalt()Increment the chaos auto-halt counter. Called byChaosAutoHaltMonitorwhen the circuit-breaker triggers. No-op when metrics are disabled (counter not registered). -
getChaosAutoHaltCount
public static long getChaosAutoHaltCount()Return the current chaos auto-halt count, or 0 if metrics are disabled. -
incrementMcpToolCall
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
Return the current MCP tool call count for the given tool name, or 0 if metrics are disabled. -
incrementAsyncMessagePublished
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
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
Return the current async-messages-published count for the given channel, or 0 if metrics are disabled. -
getAsyncMessageConsumedCount
Return the current async-messages-consumed count for the given channel, or 0 if metrics are disabled. -
incrementLlmTokens
public static void incrementLlmTokens(String provider, String model, long inputTokens, long outputTokens, Double costUsd) Increment the LLM token and cost counters for a served or forwarded completion. No-op when LLM metrics are not registered (llmMetricsEnabled is false or metrics are disabled). Fail-soft: a null or unresolvable provider/model is recorded as "unknown".- Parameters:
provider- the LLM provider name (e.g. "ANTHROPIC", "OPENAI")model- the model name (e.g. "claude-opus-4")inputTokens- number of input tokens (may be 0)outputTokens- number of output tokens (may be 0)costUsd- estimated cost in USD (may be 0.0; null means unknown and is skipped)
-
getLlmInputTokens
Return the current LLM input token count for the given provider and model, or 0 if LLM metrics are disabled. -
getLlmOutputTokens
Return the current LLM output token count for the given provider and model, or 0 if LLM metrics are disabled. -
getLlmCostUsd
Return the current cumulative LLM cost in USD for the given provider and model, or 0.0 if LLM metrics are disabled. -
getLlmCostUsdTotal
public static double getLlmCostUsdTotal()Return the aggregate cumulative LLM cost in USD across all providers and models. Used by the cost-budget circuit-breaker. Returns 0.0 if LLM metrics are disabled. -
isLlmMetricsActive
public static boolean isLlmMetricsActive()Return true if LLM token/cost counters are registered (i.e. both metricsEnabled and llmMetricsEnabled are true). -
incrementLlmCostBudgetTripped
public static void incrementLlmCostBudgetTripped()Increment the LLM cost-budget circuit-breaker tripped counter. No-op when metrics are disabled (counter not registered). -
getLlmCostBudgetTrippedCount
public static long getLlmCostBudgetTrippedCount()Return the current LLM cost-budget tripped count, or 0 if metrics are disabled. -
incrementExpectationMatched
Increment the per-expectation match counter for the given stable expectation id.No-op unless per-expectation metrics are enabled (
Configuration.perExpectationMetricsEnabled()) AND metrics are enabled (the counter is null otherwise), or whenexpectationIdis null. Labeled by the stable expectation id (not the request path) so cardinality is bounded by the number of distinct expectations.- Parameters:
expectationId- the stable id of the matched expectation
-
getExpectationMatchedCount
Return the current per-expectation match count for the given expectation id, or 0 if the per-expectation counter is not registered. -
isPerExpectationMetricsActive
public static boolean isPerExpectationMetricsActive()Return true if the per-expectation match counter is registered (i.e. both metricsEnabled and perExpectationMetricsEnabled are on). -
setActiveExpectationsSupplier
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
Per-action-type count of currently-active expectations. Backs themock_server_expectations_by_typePrometheus gauge; reads the live expectation list at scrape time via the registered supplier. -
getActiveServiceChaosCountByFaultType
Per-fault-type count of currently-active service-scoped chaos profiles. Backs themock_server_active_service_chaosPrometheus gauge and its OTLP mirror; reads the registry directly (not gated onmetricsEnabled, the gauge is only registered when metrics are on). -
setClusterMemberCountSupplier
Set the supplier of the current cluster member count. Called by HttpState at startup so themock_server_cluster_membersGaugeWithCallback can read live membership from the StateBackend at scrape time without Metrics depending on the state package.- Parameters:
supplier- returns the current number of cluster members
-
getClusterMemberCount
public static int getClusterMemberCount()Current cluster member count, backing themock_server_cluster_membersgauge. Returns 1 (single local node) when no supplier is registered or the supplier fails/returns a non-positive value. -
getOpenUpstreamCircuitCount
public static int getOpenUpstreamCircuitCount()Number of upstreams whose forward/proxy circuit breaker is currently open, backing themock_server_upstream_circuit_opengauge. Reads the liveForwardCircuitBreakerregistry at scrape time; returns 0 when the breaker is disabled or no upstream is open. -
isLoadMetricsActive
public static boolean isLoadMetricsActive()True if the load metric family is registered (i.e. metrics are enabled). -
evictLoadRun
Evict every durablemock_server_load_*series whoserun_idlabel equals the given run id, bounding accumulation of completed-run series. Each load run uses a fresh UUIDrun_idlabel; without eviction the Prometheus client retains those datapoints in the registry forever (memory growth, slower scrapes andloadLatencyPercentileMillis). The orchestrator calls this for the previous run when a new run starts, so the most-recent completed run stays scrapeable while accumulation is bounded to at most one completed run.The Prometheus client 1.8.0 exposes
removeIf(Function<List<String>, Boolean>)on stateful metrics, keyed by label values in registration order.run_idis index 1 in every load metric's label schema (the fixed structured labels beginscenario, run_id, …and the scalar counters arescenario, run_id, …). The gauges are deliberately untouched — they self-clear via the orchestrator's empty-callback readers. No-op when metrics are disabled (the metrics are null) orrunIdis null. -
observeLoadRequest
public static void observeLoadRequest(String scenario, String runId, String step, String route, String method, Integer statusCode, double latencySeconds, long requestBytes, long responseBytes, String traceId, Map<String, String> customLabels) Record one completed load-scenario request: its duration in the histogram (with an optional trace_id exemplar), the request/response body byte counts, and a request count — all labeled by the fixed structured labels plus the allowlisted custom labels. The full custom-label map is mirrored to OTLP as attributes (arbitrary keys). No-op when load metrics are off.- Parameters:
scenario- the scenario namerunId- the stable per-run idstep- the step label (step name or index)route- the low-cardinality templatised route labelmethod- the HTTP methodstatusCode- the upstream response status code, or nulllatencySeconds- the request latency in seconds (negative clamped to 0)requestBytes- request body byte countresponseBytes- response body byte counttraceId- the request's trace id for the histogram exemplar, or nullcustomLabels- merged scenario+step custom labels (may be null/empty)
-
incrementLoadIteration
Increment the completed-iteration counter (labels scenario, run_id). No-op when off. -
incrementLoadThrottled
Increment the throttled-dispatch counter for the given reason.- Parameters:
reason- one of "inflight_cap" or "rate_limit"
-
incrementLoadError
Increment the error counter for the given kind.- Parameters:
kind- one of "timeout", "connection", "render", "http_5xx", "null_response"
-
setLoadGaugeReaders
public static void setLoadGaugeReaders(Supplier<Map<Metrics.LoadGaugeKey, Integer>> activeVusReader, Supplier<Map<Metrics.LoadGaugeKey, Integer>> inflightReader) Register the live readers for the active-VU and in-flight load gauges. Called by the orchestrator on start (and cleared with null on stop), so the gauges read the running scenario at scrape time. Mirrors the chaos GaugeWithCallback pattern. -
registerOtelLoadInstruments
public static void registerOtelLoadInstruments(io.opentelemetry.api.metrics.DoubleHistogram duration, io.opentelemetry.api.metrics.LongCounter requests, io.opentelemetry.api.metrics.LongCounter requestBytes, io.opentelemetry.api.metrics.LongCounter responseBytes, io.opentelemetry.api.metrics.LongCounter iterations, io.opentelemetry.api.metrics.LongCounter throttled, io.opentelemetry.api.metrics.LongCounter errors) Install the OTel load instruments. Called byOtelMetricsExporterwhen OTLP export is enabled; passing null on stop clears them so the load path stops mirroring to OTLP. -
getLoadActiveVus
Live readers for the OTEL load observable gauges (active VUs / in-flight). -
getLoadInflightRequests
-
getLoadRequestCount
public static long getLoadRequestCount(String scenario, String runId, String step, String route, String method, String statusClass) Read a per-request load counter value for a fixed-label combination (custom labels empty). Test helper. -
getLoadThrottledCount
-
getLoadErrorCount
-
getLoadIterationCount
-
loadLatencyPercentileMillis
Derive a latency percentile (millis) for the given fixed-label combination from the load histogram's classic buckets — bounded memory, no reservoir. Returns the upper bound of the bucket in which the requested rank falls (the standard histogram-quantile interpolation point for classic buckets), or 0 when there are no observations. Any percentile is also directly queryable from the histogram in Prometheus viahistogram_quantile.- Parameters:
pct- percentile in 0..100
-
increment
-
increment
-
decrement
-
decrement
-
clearRequestAndExpectationMetrics
public static void clearRequestAndExpectationMetrics() -
clearActionMetrics
public static void clearActionMetrics() -
clearWebSocketMetrics
public static void clearWebSocketMetrics()
-