Skip to content

telemetry

kubed-io[bot] edited this page Nov 28, 2026 · 27 revisions

Telemetry

What a server reports about itself while it runs: continuous profiles to Pyroscope, and traces and metrics through OpenTelemetry, configured by OpenTelemetry's standard OTEL_* variables. Each is off until the server's config switches it on, and none of it is importable until the telemetry extra is installed. A server built on QuickMCP gets all of it without code of its own, and an install that never asks for it carries nothing.

Use cases

  • An operator profiles a server in production, CPU and memory, by setting one Pyroscope address in its config. The server's code does not change.
  • An operator follows a tool call through its trace in any OTLP backend, and opens the profile of a slow call from its span.
  • An operator charts calls, durations and failures per tool in any metrics backend, pushed over OTLP, and the server adds counters of its own beside them.
  • A log line written during a traced call carries that call's trace id in its text, so the backend joins the two.
  • An operator opens a flame graph's frame at its source line: in the server's own code at the commit its image was built from, or in kubed-mcp, FastMCP or the standard library at the version the image installed.
  • Someone installs a server from PyPI without the extra: no telemetry package is installed or imported, and the server runs as before. Their image builds can opt in with kubed-mcp[telemetry].

Neighbours

TEL-1. The telemetry extra and what it holds are the package's (PKG-22). The server's lifespan and its shutdown are the app's (APP-5). Which deployment sets which address, and what sits between the server and its backends, belong to that deployment.

Settings

TEL-2. The built-in telemetry section (CMP-4), TelemetrySettings, is registered in every server, and QuickMCP reads no attribute of a server's object for it. With its defaults every signal is off:

Field Type Default env arg Meaning
profiles SecretStr | None None TELEMETRY_PROFILES --telemetry-profiles Pyroscope server address. Profiles are on when it is set
profile_cpu bool True TELEMETRY_PROFILE_CPU --telemetry-profile-cpu Sample CPU while profiles are on
profile_memory bool True TELEMETRY_PROFILE_MEMORY --telemetry-profile-memory Sample allocations while profiles are on
otel bool False TELEMETRY_OTEL --telemetry-otel Run OpenTelemetry: traces and metrics, as the OTEL_* variables configure them (TEL-11)
tags dict[str, str] {} TELEMETRY_TAGS --telemetry-tags Labels on every profile, and resource attributes on every trace and metric (TEL-12)

profiles is a secret, so it's shown as *** and read with get_secret_value() (CFG-13). A server's subclass of TelemetrySettings under telemetry replaces it (CFG-3), and code reads it as Inject[TelemetrySettings] (CFG-16).

TEL-3. kubed.mcp exports TelemetrySettings, a Config with those fields and defaults, each described in one sentence. It refuses unknown keys, so a typo inside a server's telemetry section stops the boot. Importing it imports no FastMCP (facade, FAC-11).

TEL-4. A signal switched on whose package is missing logs one warning naming the telemetry extra, and the server serves without that signal. A signal switched off imports nothing.

Lifetime

TEL-5. Every signal that is on starts once per process, when the telemetry component starts (CMP-3), before the server serves, and stops after the server's lifespan ends, before the container's shutdown (APP-5). It stops also when the lifespan fails. The signals belong to the process, so the first QuickMCP to start them owns them: its settings apply, and they stop when its lifespan ends. When its lifespan is entered again before an earlier entry has exited, each entry runs the start, and the signals stop when the last of those entries exits. A later QuickMCP in the same process starts nothing that's already running. OpenTelemetry's providers can be set only once per process. Once the owner's have stopped, a later QuickMCP with otel on logs that OpenTelemetry runs once per process, and serves without it. Stopping waits a few seconds at most for each provider, so a collector that's down delays a shutdown by seconds, not minutes.

TEL-6. A signal that fails to start logs the error's type, never its text or a traceback, and the server serves without it. Telemetry never stops a server from serving.

Profiles

TEL-7. Profiles use pyroscope-io. They push to the profiles address under the server's service name: OTEL_SERVICE_NAME when it's set, otherwise the name QuickMCP was built with. Profiles and traces therefore share one name, which is what a trace backend matches to open a span's profile.

TEL-8. profile_cpu turns on CPU sampling (the SDK's cpu_enabled), and profile_memory turns on allocation sampling (mem_enabled), which yields allocated and in-use objects and bytes. Either runs without the other. With both off, profiles start nothing.

What profiling costs a server, measured, and how to lower it with these settings, is on the Telemetry Guide.

TEL-9. Each profile is labelled with tags. It also gets namespace and pod when the POD_NAMESPACE and POD_NAME environment variables are set, as the Kubernetes downward API sets them. A key in tags overrides the one from the environment.

TEL-10. Profiles carry no per-call labels. The profiler's labels belong to an OS thread, and an async server runs many calls on one, so a tool's name would land on other tools' samples.

TEL-25. Each profile is also labelled with the source its image was built from:

  • service_repository comes from SERVICE_REPOSITORY, the repository's GitHub URL, such as https://github.com/example/server.
  • service_git_ref comes from SERVICE_GIT_REF, the commit the image was built from.

A server's image sets both at build. Pyroscope then opens a frame's source at that commit, through the file TEL-26 writes. A variable that is unset or empty adds no label, and a key in tags overrides either one, as it does TEL-9's. These two variables label profiles only. Unlike POD_NAMESPACE and POD_NAME, they add nothing to the resource on traces and metrics (TEL-12).

OpenTelemetry

TEL-11. With otel on, QuickMCP runs OpenTelemetry's standard configuration before serving, the one opentelemetry-instrument runs (opentelemetry.instrumentation.auto_instrumentation.initialize() with opentelemetry-distro). Which signals export, where to and how all come from the standard OTEL_* variables: OTEL_TRACES_EXPORTER, OTEL_METRICS_EXPORTER, OTEL_LOGS_EXPORTER, OTEL_EXPORTER_OTLP_*, OTEL_SERVICE_NAME and OTEL_RESOURCE_ATTRIBUTES. FastMCP's own spans for tools, prompts, resources and tasks are exported through it.

TEL-12. Before it configures, QuickMCP sets these defaults, each only when the variable is unset or empty:

Variable Default Why
OTEL_SERVICE_NAME the name QuickMCP was built with traces and metrics are named after the server
OTEL_EXPORTER_OTLP_PROTOCOL http/protobuf the extra carries the HTTP exporter, not gRPC
OTEL_LOGS_EXPORTER none logs go where the server already sends them
OTEL_PYTHON_LOG_CORRELATION true log records carry the trace they were written in (TEL-15)

tags are added to OTEL_RESOURCE_ATTRIBUTES, and so are k8s.namespace.name and k8s.pod.name from POD_NAMESPACE and POD_NAME when they're set (TEL-9). A key already there wins. Profiles and traces from one pod can then be joined. A tag's key is stripped of surrounding spaces, and a tag whose key is blank or holds , or = is left off traces, with a warning: the variable can't carry it. A value arrives as given, spaces and commas included.

TEL-13. An OpenTelemetry provider that's already set, by opentelemetry-instrument or by the server, is kept. With a tracer provider set, QuickMCP doesn't configure OpenTelemetry a second time: a meter provider nobody set stays unset, and the call metrics (TEL-16) record nowhere until the server sets one. With a meter or logger provider set and no tracer provider, it configures the rest, and builds no second exporter for the ones it keeps. It still adds the span processor (TEL-14) to the tracer provider when that provider is the SDK's, and the call metrics record to the global meter provider, whoever set it. When the signals stop, only the providers QuickMCP set are shut down: one that was already set keeps running, and the span processor goes quiet. Logging instrumentation that's already on is kept, and stopping removes only what QuickMCP added.

TEL-14. With profiles on as well, pyroscope-otel's span processor is added to the tracer provider, so a profile carries the id of the request span it was sampled in, and a trace backend opens a span's profile. The link is exact while calls don't overlap. The processor labels the thread, and an async server runs overlapping calls on one, so samples taken during an overlap can carry another call's span. Work in a worker thread carries none. If the processor can't be added, or fails later, it logs once, by the error's type, and the server serves without it.

TEL-15. Log records written inside a span carry otelTraceID and otelSpanID, through OpenTelemetry's logging instrumentation. Records are exported over OTLP only when OTEL_LOGS_EXPORTER says so. Turning otel on doesn't change a server's logging: QuickMCP records root's handlers and level before it configures OpenTelemetry or instruments logging, and restores both after, so root's handlers, their formats and its level stay as the server set them, and records only gain the correlation fields. The one exception is an OTEL_LOGS_EXPORTER that exports records: OpenTelemetry's handler then stays on root. On a root the server left bare it is the only handler, so warnings reach only the collector, and Python's lastResort no longer prints them.

TEL-16. With otel on, QuickMCP adds FastMCP's UsageMiddleware, extended for resource reads (TEL-21) and listings, first in the server's middleware, ahead of everything that can answer a request, with user_id="omit". It then records tools/call, prompts/get, resources/read (TEL-21), and the listings tools/list, resources/list, resources/templates/list, prompts/list, server/discover, skills/list and agents/list on mcp.server.operation.duration, named and labelled by the OpenTelemetry MCP conventions. A listing carries mcp.method.name and no component name, cursor or URI, so its series are bounded by its method. A listing a tool makes through the server's middleware is recorded too. skills/get, agents/get and resources/directory/read are not recorded. Unknown component names are left off, so the series stay bounded. A prompt render is labelled by the prompt's name, bounded by the prompts served, a plugin's prefixed name (PLUG-63) included, and carries no plugin label. Because it runs first, it records each request as the client sent it. That includes requests a server's own middleware rewrites further in, such as an address it strips a prefix from, and requests that middleware refuses. A resource read's label is the exception: TEL-21 takes it from the URI the providers saw.

TEL-17. Metrics leave the server as OTEL_METRICS_EXPORTER says when QuickMCP configures the meter provider (TEL-13): pushed over OTLP by default, or served for scraping on a port of their own with prometheus. QuickMCP adds no route to the server. A server's own instruments, from OpenTelemetry's global meter provider, leave the same way.

TEL-18. With otel on, a log line the server writes inside a span ends with trace_id=<32 hex> span_id=<16 hex>, OpenTelemetry's own names, whatever format the server's handlers use. So a backend that reads only the line, such as Loki reading stdout, joins it to its trace. The ids are added to what the record's getMessage() returns, unless that message already holds them. Its msg and args, and every handler and formatter, are left as they are, so a formatter that prints record.msg raw doesn't show them. A line formatted later, on another thread, shows the span it was written in, and shows the ids once, even when a traceback follows them. Outside a span, a line is unchanged. TEL-15's record fields stay. When OTEL_LOGS_EXPORTER exports, the record's body carries the ids too. The stop puts back the record factory it found, unless something replaced it since, which then stays.

An operator who reads these lines in Loki adds a derived field with the matcher trace_id=(\w+), pointing at the trace backend, and the line links to its trace. A format that prints record.msg rather than the formatted message shows no ids.

TEL-19. Pyroscope unreachable, checked on the push interval: with profiles on, the server checks the profiles address every 10 seconds, the interval it pushes on. It sends GET <address>/ready with the address's basic auth, follows no redirect, and goes through the environment's proxies. The check is reachability, not the push itself: pyroscope-io gives Python no status for a push.

  • A connection refused, a DNS, TLS or timeout failure, any other connection failure, a 401 or 403, or a 5xx makes the address unreachable. Any other answer, a 404 included, is reachable, since some hosted endpoints serve no /ready.
  • An address that isn't a URL, a non-numeric port included, is unreachable as invalid address. So is one whose userinfo can't be split without guessing: an @ after a ? or #, or a userinfo holding a / or \. Neither is ever probed.
  • The first failure logs one warning on kubed.mcp.telemetry, profiles: pyroscope at <host> unreachable: <kind>, and nothing more until the address is reachable again. Then it logs one info line, profiles: pyroscope at <host> reachable again.
  • <host> is shown as TEL-20 shows it, so an address with no host shows an address with no host. No line shows a credential, the path, the query or an error's text.
  • The first check runs at start, so a server whose Pyroscope is down at boot says so within the first interval, not only at exit. The check runs on a thread of its own, and starts and stops with profiles.

TEL-20. At start, the telemetry component logs one info line on kubed.mcp.telemetry naming the signals this start began and where each one goes, after the config's boot line and before the server's components start (CMP-3):

telemetry: profiles → pyroscope.example.com:4040; otel → traces otlp (collector.example:4318), metrics otlp (default), logs none
  • Each signal that started is one part, and a signal that didn't start, or is off, has none.
  • Profiles go to the host of the profiles address, with its port, and never its userinfo, path or query. An address without :// is read as //<address>, so user:pw@pyroscope:4040 shows pyroscope:4040, and an IPv6 host is in brackets.
  • Userinfo ends at the address's last @, so a password holding an unencoded / never shows: http://admin:4321/x@host:4040 shows host:4040. An @ after a ? or # shows an address with no host: the text before it may be a password holding an unencoded ?, and the text after it is the query's.
  • A host shows only when it is an IP address, or is shaped as a hostname (letters, digits, dots, hyphens and underscores, starting and ending with a letter, digit or underscore) and the address has a scheme, a valid port or a dot in the host, so a Compose service's pyro_scope:4040 shows. A port that doesn't parse is left off. Any other address shows an address with no host, so a bare token never prints.
  • OpenTelemetry's exporters are OTEL_TRACES_EXPORTER, OTEL_METRICS_EXPORTER and OTEL_LOGS_EXPORTER as set once TEL-12's defaults apply, trimmed, and one that's unset or empty shows otlp, or none for logs.
  • Each OTLP exporter, a name starting with otlp such as otlp or otlp_proto_http, is followed by where it sends, in parentheses. That's OTEL_EXPORTER_OTLP_<SIGNAL>_ENDPOINT, else OTEL_EXPORTER_OTLP_ENDPOINT, as the exporter resolves them, so an empty variable counts as unset. It's shown as profiles' host is: host and port only, so a credential, path or query never shows. When neither is set it shows default, the exporter's own fallback. A list such as otlp,console is shown entry by entry, each trimmed and joined by ,, and only its OTLP entries show an endpoint. Logs show one only when they export over OTLP.

If this start began none, because none is on, none could start or another start already owns the process's signals (TEL-5), it logs telemetry: off. The line never shows a credential. Every server built on QuickMCP gets it, with no line of its own.

TEL-21. Call metrics (TEL-16) also record resources/read on mcp.server.operation.duration, labelled mcp.resource.template.

  • The plugin layer labels a read first, from the request's build. A read of a served prompt or agent file, or of a URI below a served skill's root or below a folder of prompt or agent files, is labelled by a pattern and two more attributes, never by its URI:

    • kubed.plugin.library: the first segment of the plugin's namespace, grafana for grafana/grafana-k6;
    • kubed.plugin.name: the plugin's name, grafana-k6.

    In Prometheus these are mcp_resource_template, kubed_plugin_library and kubed_plugin_name. A skill's name is never a label.

  • The plugin is a served file's own, or the skill's for anything below the skill's root, or, for anything else below a folder of prompt or agent files, its index included, the first admitted plugin with a file in the folder (PLUG-68). {path} is every segment between the library and the file's name:

    A read of mcp.resource.template
    a skill's SKILL.md skill://{library}/{path}/SKILL.md
    a skill's _manifest skill://{library}/{path}/_manifest
    a skill folder's index, the skill's own root included skill://{library}/{path}/_index.md, skill://{library}/{path}/_index.json
    any other file of a skill skill://{library}/{path}
    a prompt or agent file prompts://{library}/{path}, agents://{library}/{path}
    a prompt or agent folder's index prompts://{library}/{path}/_index.md, prompts://{library}/{path}/_index.json, and the same for agents://

    The root's SKILL.md and _manifest take their own patterns. A SKILL.md or _manifest in a subfolder is any other file.

  • The lookup reads the build the request already pinned, never a file, so whether the file is there doesn't decide the labels. It never pins: a read that pinned no build, a server's with no plugin served among them, isn't the layer's.

  • Every other read is labelled from the template lookup. A read whose URI matches a template is labelled by that template's uri_template, a static resource included when its URI also matches a template. A successful read that matches no template is labelled by its static URI. A server's own resources outside those are such reads, mcp-kb's skill://<library>/_index.md among them, and are the server's to label.

  • The URI is the one the server's providers saw: never with a gateway prefix (GW-4), and never a templated URI's values, so the series stay bounded.

  • A failed read keeps the layer's labels, or else the label of the template its URI matches. A failed read that is neither the layer's nor a template's, a static resource's included, carries no label, only the error attributes the other methods get. A read FastMCP counts as a routing error, a missing file's among them, carries none of the three.

  • The read's span keeps the exact URI as mcp.resource.uri.

  • The recorded duration of a read includes the lookups that label it.

  • A read through the read_resource tool records both a tools/call and a resources/read point.

TEL-22. A subscriptions/listen stream is the client's connection, not an operation.

  • It has no SERVER span and no mcp.server.operation.duration point, whether otel is on or the tracer provider is someone else's. Trace context still propagates.
  • Anything on the stream that is an operation of its own keeps its span.
  • Every other method is traced and timed as before.
  • If FastMCP's server has changed so the stream can't be left out, one warning is logged, and the server serves (TEL-6).

TEL-23. With otel on, the server measures its garbage collections, so a stall of the event loop either lines up with a collection or rules one out.

  • Two instruments, from OpenTelemetry's global meter provider, whoever set it (TEL-13), leave as TEL-17 says. Both carry cpython.gc.generation, the collection's generation as an int, 0, 1 or 2:

    Instrument Kind Unit What it holds
    cpython.gc.collections counter {collection} collections run
    cpython.gc.duration histogram s how long each collection paused the process

    The histogram's buckets are 0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5 seconds. In Prometheus they are cpython_gc_collections_total and cpython_gc_duration_seconds_bucket, _sum and _count, labelled cpython_gc_generation.

  • A collection of 0.5 s or more also logs one warning on kubed.mcp.telemetry, naming its generation and its duration in seconds. When a span was current as it ran, the line carries that span's trace id, as TEL-15 and TEL-18 put it on a line: the trace it stalled.

  • The callback Python runs inside a collection does no I/O, no logging and no recording on an instrument. It reads the clock and, for a collection of 0.5 s or more, the current span, and puts the collection on a bounded queue, which drops its oldest entries when full. A task on the event loop drains the queue every second, recording each collection and logging each warning. Collections in other threads are queued too, and the next drain records them.

  • It starts and stops with the signals and is owned like them (TEL-5): it starts with OpenTelemetry and stops before OpenTelemetry's providers shut down, after one last drain. It never raises: a failure to start logs the error's type, and OpenTelemetry runs without it (TEL-6). With no running event loop at start, nothing is registered and no collection is captured.

TEL-24. A build made or waited on (PLUG-55) is a span kubed.mcp.build, from OpenTelemetry's global tracer provider, whoever set it. A kept build has none: that is every request's.

Attribute Type What it holds
kubed.build.generation int the generation built
kubed.build.prefixed bool whether the build's key holds a prefix (GW-6)
kubed.build.outcome string made; waited for a reader of a key another is building; cancelled for a build cancelled before it was made
kubed.build.skills, kubed.build.prompts, kubed.build.agents int on made: how many the build serves

A rewritten build's span covers its wait for the rewritten builds asked for before it, so a trace shows each request's wait.

Source links

TEL-26. python -m kubed.mcp.pyroscope, run inside the server's built image, writes the .pyroscope.yaml that Pyroscope reads from the server's repository at service_git_ref (TEL-25). It also checks a committed copy against the image. Only the image's paths and versions are true, so the command takes no paths or versions as flags.

  • --write <distribution> prints the file on stdout. <distribution> is the server's own distribution, such as kubed-mcp-kb, spelled any way pip accepts.
  • --check <distribution> [<file>] reads the committed file from <file>, or from stdin when there is no <file> or it is -. It then builds the image's file.
    • It exits 0 when both have the same version and map the same prefixes to the same language and source, whatever their order, grouping or comments.
    • Otherwise, including when the committed file isn't YAML of that shape, it prints a unified diff from the committed file to the image's on stdout and one line on stderr saying to regenerate it, and exits 1.
    • Empty input exits 2, with one line on stderr saying it read nothing: docker run without -i passes no stdin. So does a <file> that can't be read.
  • A server's CI runs it in the image it just built: docker run -i --rm <image> python -m kubed.mcp.pyroscope --check <distribution> < .pyroscope.yaml.

It maps these, in this order, each with language: python:

Installed Source
each package of the named distribution local, at its import path: kubed/mcp_kb for kubed.mcp_kb
kubed-mcp's kubed/mcp/ github: the repository its Project-URL names, at its ref (TEL-27), path kubed/mcp
fastmcp-slim's fastmcp/ github: the repository its Project-URL names, at v<version>, path fastmcp_slim/fastmcp
the standard library, sysconfig.get_paths()["stdlib"] github: python/cpython at v<Python version>, such as v3.14.10, path Lib
  • Packages. A distribution's package is a directory in its installed files that holds an __init__.py while no directory above it does. So a kubed.* package is kubed/mcp_kb, never kubed/, and fastmcp/contrib/mcp_mixin is part of fastmcp. The packages of the named distribution are in import-path order.
  • FastMCP. The fastmcp distribution installs no code of its own: fastmcp/ comes from fastmcp-slim, kept in FastMCP's repository under fastmcp_slim/.
  • Prefixes. Each mapping's path lists the installed directory, ending in /, twice: with its leading / and without it. The directory is the one the interpreter imports from, with symlinks left unresolved. The distribution is the installed one, looked up on the interpreter's path without the working directory, so a source checkout the image was built in doesn't stand in for it. A longer prefix wins in Pyroscope, so an image without a venv, whose site-packages sits inside the standard library's directory, still maps each package to its own source.
  • Each distribution is mapped once, by its first row. So --write kubed-mcp maps kubed-mcp's package to a local path.
  • The header. The file opens with a comment naming the command that wrote it, with the distribution's Name from its metadata. The comment says to regenerate the file in the image rather than edit it. The same image writes the same bytes.
  • What it can't map.
    • Exit 2, with one line on stderr naming the distribution:
      • the named distribution isn't installed, or its files hold no package;
      • kubed-mcp's or fastmcp-slim's Project-URL names no GitHub repository;
      • kubed-mcp's version has no ref.
    • Left out, with one line on stderr:
      • fastmcp-slim isn't installed;
      • kubed-mcp's or fastmcp-slim's files hold no package, as in an editable install.

TEL-27. The refs and repositories that TEL-26 maps to:

  • kubed-mcp's ref, from its installed version:
    • a final X.Y.Z is the tag vX.Y.Z;
    • a release candidate, which pip reads as X.Y.ZrcN, is the tag vX.Y.Z-rc.N, as its release tagged it;
    • a development build has a local part starting +g<sha>, as in setuptools_scm's X.Y.Z.devN+g<sha>, with a .dYYYYMMDD after it when the tree was dirty. Its ref is the full commit, the vcs_info.commit_id of the distribution's direct_url.json, when it was installed from git. Otherwise it is the <sha>: GitHub resolves a short commit as a ref;
    • any other version has no ref.
  • fastmcp-slim's ref is v<version>.
  • The standard library's ref is v and the interpreter's platform.python_version().
  • A repository is the first Project-URL whose label, lowercased and stripped of spaces and punctuation, is source, repository, sourcecode or github, the labels PEP 753 gives for source code. Its URL is https://github.com/<owner>/<repo>, which may end in .git or /.

Not now

  • The build's commit on traces and metrics. vcs.repository.url.full and vcs.ref.head.revision as resource attributes are a later change. Profiles are what source links need (TEL-25).
  • service_root_path. It would come from SERVICE_ROOT_PATH and is a later change.
  • Mapping more distributions, such as pydantic, starlette, uvicorn, anyio and mcp. Their tag schemes vary, and a wrong ref shows the wrong lines.
  • A repository whose packages sit below a folder, such as src/. A local path is the package's import path, relative to the repository's root.
  • eBPF profiling. It needs a privileged agent on every node, and Dr K ruled that out.
  • Per-call profile labels. See TEL-10.
  • A collector in front of the backends. The addresses are config, so a deployment can put one in without a change here.
  • MCP instrumentation libraries and observability bootstrap libraries. FastMCP traces itself natively and records its own call metrics, and starting and stopping the signals is this area's own job.
  • A metrics route on the server's port. Metrics are pushed, or scraped from the exporter's own port (TEL-17).
  • A user label on metrics. It adds a series per user and needs a secret salt to hash safely (TEL-16). Spans still carry enduser.id when the server has auth.

Clone this wiki locally