---
title: The relay
description: "observability.relay: the /api/telemetry endpoint the page posts its spans to, what it checks, and the guard that limits it."
sidebar:
  label: "The relay"
related:
  - /observability/observe-agents
  - /security/limits
  - /observability/spans
---

`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:

```ts
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:

**Vite**

```ts title="api/telemetry.ts"
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 });
```

**Next.js**

```ts title="app/api/telemetry/route.ts"
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 });
```

**TanStack Start**

```ts title="src/routes/api/telemetry.ts"
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),
    },
  },
});
```

**Astro**

```ts title="src/pages/api/telemetry.ts"
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](/docs/security/limits)); on Vercel,
[`createVercelTelemetryRoute`](/docs/reference/vercel-api/create-vercel-telemetry-route) does that.

## Options

<AutoTypeTable path="../../packages/telemetry/src/server/types.ts" name="TelemetryRelayOptions" />

## 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`](/docs/reference/chat/chat-request-schema)).

**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.

:::warning[The `Origin` check is not a limit]
It stops other sites and bare scripts, not a script that sends a matching `Origin`. Keep the `guard`, with its counts
in a shared store. Set `allowMissingOrigin` only behind a proxy that strips `Origin`, and keep the `guard` then too.
:::

## Next

**[relayThrough](/docs/observability/relay-through)**

Where relayed spans go when your app has its own provider.

**[Span names and types](/docs/observability/spans)**

Every span and attribute name as a constant, and the OpenTelemetry types the options take.
