Sampling
Keep a share of turns, or decide per turn: the server decides from the trace id, for its spans and the page's, so a turn is kept or dropped whole.
sampling decides which turns are exported. Keep a share of them when the volume costs too much, or decide per turn:
by agent, or by a consent cookie when the model steps must not be exported without the person’s agreement. The server
decides from the trace id, for its own spans and for the ones the page relays, so a turn is kept or dropped whole.
Example
Keep a turn only when the person agreed to analytics:
import { defineObservability } from "@webmcp-agents/telemetry/server";
// Keep a turn only when the person agreed to analytics: the sampler
// gets the request, so it can read your consent cookie. The relay
// asks the same sampler, so a turn is kept or dropped whole.
export const observability = defineObservability({
sampling: {
type: "custom",
sampler: ({ request }) =>
request?.headers.get("cookie")?.includes("analytics=yes") ??
false,
},
});
To keep a share instead, sampling: { type: "ratio", probability: 0.1 }, or set OTEL_TRACES_SAMPLER=traceidratio
with OTEL_TRACES_SAMPLER_ARG=0.1 and write no code.
Options
sampling?Sampling
Which requests are exported: always, never, ratio (by trace id, so a trace is kept whole) or custom (a throw drops it).
Sampling{ type: "always" }agent?string
The agent's id, when the run has one.
stringconversation?string
The conversation the run belongs to.
stringrequest?Request
The request (server side: the chat request, or the relay's).
RequestThe table above is what a custom sampler receives. Every other option is on
defineObservability.
Notes
The default comes from the environment
With no sampling, the layer reads OTEL_TRACES_SAMPLER (and OTEL_TRACES_SAMPLER_ARG), else keeps every turn.
The page's own sampling
observeAgents takes a sampling too, decided when each run starts. The page’s consent belongs in its enabled
option instead: a run the person did not agree to produces no span and no request
(Privacy and redaction).
Off entirely
WEB_AGENTS_OTEL_ENABLED=false keeps this library’s spans off without touching the app’s own.