Skip to content
WebMCP Agents
Esc
↑↓navigate↵open⌘Jpreview
On this page

The relay

observability.relay: the /api/telemetry endpoint the page posts its spans to, what it checks, and the guard that limits it.

observability.relay() builds the POST handler the page’s spans arrive at. A backend’s API key must never reach the page, so the page posts its runs and tool calls to your own endpoint, /api/telemetry, and the server forwards them through the same layer as its own spans: the same exporters, or the app’s own provider. Mount it once, next to the chat route.

Import

The relay is a method of the layer, so it comes with it:

import { defineObservability } from "@webmcp-agents/telemetry/server";
import type { TelemetryRelayOptions } from "@webmcp-agents/telemetry/server";

Example

Everything the relay accepts reaches your backend, so give it a per-IP limit through guard, as for the chat:

import { observability } from "../src/lib/observability";

// The page's spans arrive here. The relay checks and redacts them,
// then sends them where the server's spans go. It refuses a request
// from another origin, or with no Origin at all (a script, not a page).

// Everything it accepts reaches your backend, so limit each visitor.
// Here in memory, per instance; in production count in a shared store
// (Upstash, Redis), as for the chat: see Limits and budgets.
const seen = new Map<string, { since: number; count: number }>();
const perIp = (req: Request) => {
  const who = req.headers.get("x-forwarded-for") ?? "local";
  const now = Date.now();
  const last = seen.get(who);
  const entry =
    last && now - last.since < 60_000 ? last : { since: now, count: 0 };
  entry.count++;
  seen.set(who, entry);
  // about one batch per run: 60 a minute is plenty for a person
  if (entry.count <= 60) return undefined;
  return new Response(null, { status: 429 });
};

export const POST = observability.relay({ guard: perIp });
import { observability } from "@/lib/observability";

// The page's spans arrive here. The relay checks and redacts them,
// then sends them where the server's spans go. It refuses a request
// from another origin, or with no Origin at all (a script, not a page).

// Everything it accepts reaches your backend, so limit each visitor.
// Here in memory, per instance; in production count in a shared store
// (Upstash, Redis), as for the chat: see Limits and budgets.
const seen = new Map<string, { since: number; count: number }>();
const perIp = (req: Request) => {
  const who = req.headers.get("x-forwarded-for") ?? "local";
  const now = Date.now();
  const last = seen.get(who);
  const entry =
    last && now - last.since < 60_000 ? last : { since: now, count: 0 };
  entry.count++;
  seen.set(who, entry);
  // about one batch per run: 60 a minute is plenty for a person
  if (entry.count <= 60) return undefined;
  return new Response(null, { status: 429 });
};

export const POST = observability.relay({ guard: perIp });
import { createFileRoute } from "@tanstack/react-router";

import { observability } from "@/lib/observability";

// The page's spans arrive here. The relay checks and redacts them,
// then sends them where the server's spans go. It refuses a request
// from another origin, or with no Origin at all (a script, not a page).

// Everything it accepts reaches your backend, so limit each visitor.
// Here in memory, per instance; in production count in a shared store
// (Upstash, Redis), as for the chat: see Limits and budgets.
const seen = new Map<string, { since: number; count: number }>();
const perIp = (req: Request) => {
  const who = req.headers.get("x-forwarded-for") ?? "local";
  const now = Date.now();
  const last = seen.get(who);
  const entry =
    last && now - last.since < 60_000 ? last : { since: now, count: 0 };
  entry.count++;
  seen.set(who, entry);
  // about one batch per run: 60 a minute is plenty for a person
  if (entry.count <= 60) return undefined;
  return new Response(null, { status: 429 });
};

const relay = observability.relay({ guard: perIp });

export const Route = createFileRoute("/api/telemetry")({
  server: {
    handlers: {
      POST: ({ request }) => relay(request),
    },
  },
});
import type { APIRoute } from "astro";

import { observability } from "@/lib/observability";

export const prerender = false;

// The page's spans arrive here. The relay checks and redacts them,
// then sends them where the server's spans go. It refuses a request
// from another origin, or with no Origin at all (a script, not a page).

// Everything it accepts reaches your backend, so limit each visitor.
// Here in memory, per instance; in production count in a shared store
// (Upstash, Redis), as for the chat: see Limits and budgets.
const seen = new Map<string, { since: number; count: number }>();
const perIp = (req: Request) => {
  const who = req.headers.get("x-forwarded-for") ?? "local";
  const now = Date.now();
  const last = seen.get(who);
  const entry =
    last && now - last.since < 60_000 ? last : { since: now, count: 0 };
  entry.count++;
  seen.set(who, entry);
  // about one batch per run: 60 a minute is plenty for a person
  if (entry.count <= 60) return undefined;
  return new Response(null, { status: 429 });
};

const relay = observability.relay({ guard: perIp });

export const POST: APIRoute = ({ request }) => relay(request);

The counts here live in memory, per instance. In production keep them in a shared store, as for the chat (Limits and budgets); on Vercel, createVercelTelemetryRoute does that.

Options

PropType
guard?(req: Request) => Promise<Response | undefined> | Response | undefined

Runs first (after the cross-site check): return a `Response` to refuse the request (a rate limit, a session check), or nothing to let it through. A throw refuses with 500 and is logged once.

Type(req: Request) => Promise<Response | undefined> | Response | undefined
maxBytes?number

The largest body accepted, in bytes; larger is refused with 413. Default 262144 (256 KB).

Typenumber
allowMissingOrigin?boolean

Accept a `POST` with no `Origin` header. Default `false`: a browser always sends one (`fetch`, `keepalive`, `sendBeacon`), so a request without it is a script (`curl`, a bot) and is refused with 403. Set it only behind a proxy that strips `Origin`, and give the relay a `guard` then: anything that reaches it is forwarded to the backend.

Typeboolean
attributes?(req: Request) => Record<string, string>

Resource attributes added on the server (a release, a region), never trusted from the browser. Through an app's own provider, where the resource is the app's, they are set on each span instead.

Type(req: Request) => Record<string, string>

Returns

observability.relay(options?) returns (req: Request, ...host) => Promise<Response>. Every answer has no body:

Status When
202 Accepted (or dropped quietly)
400 Not this library’s OTLP JSON
403 Cross-site, or no Origin
405 Not a POST
413 Over maxBytes
415 Not application/json
500 The guard threw

Notes

What the relay checks

It does not trust the page. It refuses cross-site requests and requests with no Origin (a script, not a page), caps the body at 256 KB, accepts only OTLP JSON from this library’s instrumentation scope, redacts every attribute again, replaces the page’s resource with the server’s, and lets the server’s sampling decide. The cross-site rule is the chat’s own (isCrossSite).

A 202 reveals nothing

The relay also answers 202 when telemetry is off (OTEL_SDK_DISABLED), when the server’s sampling said no, or when the backend failed. Nothing about an upstream error reaches the page.

A 404 stops the page

A page whose relay answers 404 stops sending, so a page without a relay costs one request.

Serverless hosts

An argument after the request with waitUntil (a Worker’s ctx), or Vercel’s request context, keeps the function alive until the batch is sent; the 202 never waits for it.

Next

Was this page helpful?