Skip to content

2026 11 26 selenium flow telemetry design

kubed-io[bot] edited this page Nov 26, 2026 · 7 revisions

selenium-flow's own telemetry, on kubed-mcp's

History. Drafted 2026-11-26 for #65 (programme E4, rescoped), built by PR #217 from its plan. Dr K approved the spec in #65's notes on 2026-11-26. This file finishes that draft against the code; "Found at plan time" says what changed. Its requirements are RUN-32 to RUN-36 in runtime.md, with edits to WS-28 and WS-29 (workspaces-and-sessions.md), GRID-12 and the request table (grid.md), and ERR-30 and ERR-35 (errors-and-logging.md). At plan approval Dr K dropped selenium_flow.grid.sessions ("Found at plan time", 12), took the test dependency as planned (9) and approved the free-slot rule with GRID-12's new fields (10). "Found at plan time" item 3 was wrong about the thread: the last collection runs on the reader's thread, so the gauge stops reading when the server stops (RUN-33). #221 seeds the gauge at 0 for each browser and state, so a Grid with no free slot or no node reports zeros, not no series (RUN-33). #220 timed every MCP tool, open_session and end_browser included, through Calls.timed, without a turn, and RUN-32, RUN-34 and WS-28 say so.

Brief

The operator wants three things from selenium-flow, all through kubed-mcp's telemetry (telemetry.otel, RUN-29):

  • how often each browser action runs, how long the browser takes and how often it fails, on every surface;
  • how busy the Grid is;
  • each call's trace naming the workspace, the tool, the surface and the flow step.

History lives in the operator's backends, never here (programme R12, R7). What every server needs (the OTel SDK and its OTEL_* configuration, FastMCP's spans, mcp.server.operation.duration, trace ids on log records, profiles) is kubed-mcp's since quick-mcp#12, released in 0.3.0. With telemetry.otel on, metrics are pushed over OTLP or scraped from the exporter's own port, and there is no route on the server's port (quick-mcp TEL-17), so the epic's /metrics route is overtaken.

Rulings

  1. The rescope in #65's body (Dr K): only what selenium-flow has is built here.
  2. The call histogram covers all three surfaces (mcp, http, flow), by tool and browser, because it measures the browser's work inside its turn, which FastMCP's request timing does not (Dr K, 2026-11-26).
  3. Claude's recommendations are the calls, the conservative option wherever one was open (Dr K, 2026-11-26). They are marked (call) below. Dr K approved the spec with them.

Non-goals

  • A dashboard or alerting.
  • The workspace as a metric attribute: it is caller-chosen and unbounded (R12).
  • Per-request logs.
  • A /metrics route (TEL-17).
  • OTel SDK setup, FastMCP's spans, call metrics, log correlation and profiles: kubed-mcp's.
  • A new config key (call): everything follows telemetry.otel.

Design

1. The call histogram (RUN-32)

  • selenium_flow.call.duration, a histogram in seconds, fed by a bus subscriber on call.finished (WS-28) named metrics, which records the event's duration.
  • Exactly four attributes: selenium_flow.tool, selenium_flow.surface (mcp|http|flow), selenium_flow.outcome (ok|4xx|5xx) and selenium_flow.browser. Never the workspace, the session key or a Grid id. A browser that is "" on the event is recorded as given.
  • It measures the browser's work inside the session's turn: Calls.begin runs holding the lock, so the wait for it is not counted, and a call refused because a person holds the browser is never begun, so it is never counted.
  • Buckets are mcp.server.operation.duration's, 0.01 to 300 s, given as the API's bucket advisory, so the two histograms compare (call).
  • No separate counter: the histogram's count is the call count.
  • The subscriber runs on the loop and only records, so it never blocks (WS-29).
  • Attribute keys are namespaced selenium_flow.* (call); Prometheus spells them selenium_flow_tool and so on.

2. The Grid gauge (RUN-33) (call: the Grid's numbers, not a count of this server's workspaces)

  • An observable gauge, selenium_flow.grid.slots, by selenium_flow.browser and selenium_flow.slot.state (free|busy).
  • Why the Grid's numbers: they survive a restart, and the monitor's own listing runs only while a watch exists (WS-31). Every replica reports the same Grid, so a dashboard takes max, never sum.
  • One GET /status per collection, 5 s (SHOWN_TIMEOUT) and no retry, in the reader's thread, never on the loop. The interval is OTEL_METRIC_EXPORT_INTERVAL, or one read per scrape with the Prometheus exporter.
  • A failed or slow read observes nothing for that collection and is logged once a streak, by the error's type only (ERR-32: the Grid URL may carry userinfo). It never raises into the reader.

3. The call span (RUN-34)

  • Calls.begin starts a span and Calls.end ends it, on every surface and for every flow step. Begun gains a span field.
  • begin starts it with the current context as its parent: FastMCP's tools/call span over MCP, the route span (item 4) over HTTP, and for a flow step the context the run was called in. The span is not made current (call): nothing is attached across begin and end, and nothing inside the call parents to it.
  • Name: call <tool>, such as call navigate (call).
  • Attributes: selenium_flow.workspace (the workspace part of the key, sessions.split(key)[0]), selenium_flow.sub_session (true for a sub-session's key), selenium_flow.tool, selenium_flow.browser, and, set at end, selenium_flow.surface and selenium_flow.outcome.
  • Never on a span: a sub-session's name (WS-49 (3), sessions.logged), a Grid session id (WS-33), an argument, a URL or a value (programme R6, ERR-35).
  • A failed call's span has status ERROR with no description, and error.type, the exception's class name. record_exception is never used (call): an exception's text and traceback may carry a value.
  • end still never raises; a span API failure is logged by type.
  • The workspace stays off FastMCP's own tools/call span (call). The child span carries it, and TraceQL finds a trace by any of its spans.

4. The HTTP route span (RUN-35)

  • An ASGI middleware in http_middleware(), so it sits under the mount and at the root alike. One SERVER span per HTTP request.
  • Name: <METHOD> <route template>, the template from RouteTemplates (ERR-29), so a workspace name, ?workspace= and a signature never reach a span.
  • Attributes: http.request.method, http.route and http.response.status_code, and nothing else from the request: no query, no headers, no client address (call).
  • Parent: the global propagator's context from the request headers (traceparent), so a gateway's trace continues. A 5xx sets ERROR.
  • Skipped (call): /mcp (FastMCP's spans own it), the four probes (/health, /started, /ready, /info), and WebSocket scopes (the live view).

5. Scope, placement and the switch (RUN-36)

  • One tracer and one meter, named kubed.selenium_flow, with the package's version. Their providers can be injected and default to the global ones, so the unit tests use in-memory providers without setting globals (call).
  • kubed/selenium_flow/telemetry.py holds the subscriber, the gauge, the tracer and meter, and the span helpers. It imports only the OpenTelemetry API and kernel modules, so workspace/calls.py can import it; it joins NO_PROTOCOL in tests/test_boundaries.py (call). The middleware is http/tracing.py, beside RouteTemplates.
  • server.py wires all of it when settings.telemetry.otel is on. Off, nothing is subscribed, registered or added.
  • opentelemetry-api comes with fastmcp-slim[mcp], so importing it needs no guard and is a no-op until an SDK is configured.

Found at plan time

What reading the code changed in the approved draft, each the conservative reading:

  1. Grid.status() waits 30 s and retries (_json("get", "/status")). The gauge's read is Grid.status(timeout=SHOWN_TIMEOUT, retry=False), two keywords whose defaults keep every other caller as it is.
  2. Two gauges are two callbacks per collection. The read was to be shared, one read serving both for 1 s. With the sessions gauge dropped (12) one callback is left, so a collection reads once and nothing is shared.
  3. kubed-mcp shuts the meter provider down at the lifespan's exit, on the loop, and a shutdown runs a last collection. A collection on the event loop's thread reads nothing and observes nothing, so the loop never waits on the Grid.
  4. The instruments are made at build, and kubed-mcp sets the global providers in the lifespan, after it (RUN-29). The API's proxy tracer, meter and instruments delegate once the providers are set, so they record; a test in a fresh interpreter holds it.
  5. http_middleware() takes nothing, and run() builds the access log's RouteTemplates from an app of its own. access_log.quiet returns the filter it installs, and http_middleware(telemetry, templates=, prefix=) hands the same one to the route spans. That settles the draft's "how the middleware learns the routes".
  6. A path no route serves is named by its method alone, with no http.route (OTel's HTTP convention). RouteTemplates.template's /<first segment>/… fallback is a log line's; a span takes nothing from a path it cannot name. RouteTemplates.matched says which.
  7. Flow steps need no carried context. run_for runs every step in the one worker thread it is handed (anyio.to_thread.run_sync), and anyio copies the caller's context into it, which core/cancel.py already relies on. A test asserts the parentage. A run that became a Docket task parents its steps to whatever context its worker has.
  8. The unit suite must not start kubed-mcp's SDK. SeleniumMCP(telemetry=) takes a Telemetry built on in-memory providers, used whatever telemetry.otel says; without one, telemetry.otel builds it on the global providers. Tests inject with otel off.
  9. The SDK the tests need is not in [test], and CI installs .[test] only. When opentelemetry.sdk does not already import there, test gains opentelemetry-sdk, and #199's test_the_telemetry_extra_is_kubed_mcps_and_nothing_else_carries_it (whose substring check reads opentelemetry as telemetry) says what it means: the runtime dependencies carry no telemetry package, and nothing but the extra carries kubed-mcp[telemetry] or pyroscope.
  10. A free slot is one that can take a session. A slot is busy with a session, and free without one only on a node whose availability is UP and whose busy slots are below its maxSessions. Other slots are not counted. The browser is the slot's stereotype.browserName. A node without availability counts as up, one without maxSessions has no cap. GRID-12 gains these fields.
  11. telemetry.py takes the workspace and the sub-session flag as arguments (calls.py splits the key), so it imports nothing of workspace/ and no import cycle forms.
  12. selenium_flow.grid.sessions equals selenium_flow.grid.slots{state="busy"} on a Grid 4, where a slot holds at most one session. Dr K dropped it at plan approval: it only adds a series.

Not now

  • A bus-drops metric (Subscription.dropped).
  • A count of this server's own live browsers.
  • The workspace on FastMCP's span.
  • A hashed workspace.
  • Spans for the live view's WebSockets.

Clone this wiki locally