---
title: Send traces to Sentry
description: "Six steps to agent traces and AI conversations in Sentry, through Sentry's own OpenTelemetry provider."
# The page ends with its own "Next" card; the sidebar neighbours would say something else.
pagination: false
sidebar:
  label: Sentry
---

<RememberBackend name="Sentry" />

:::note[Not run by us against Sentry]
Written from Sentry's docs on 2026-11-25. We checked offline, with `@sentry/node` 11.17.0 and a placeholder DSN, that the layer finds Sentry's provider when the option in step 3 is set, and finds none without it.
:::

Six steps, from the packages to one trace per turn in Sentry: the same six on every backend's page. The framework
picker above the code picks your routes; the backend stays picked for the pages after this one.

<div class="install-steps">

## Install the packages

The telemetry layer and its OpenTelemetry packages, beside the Sentry SDK your server already has. `@opentelemetry/sdk-trace` is not needed when the app registers a tracer provider, as Sentry does.

```package-install
npm install @webmcp-agents/telemetry @opentelemetry/api @opentelemetry/sdk-trace @ai-sdk/otel
```

## Set the environment

```bash title=".env"
SENTRY_DSN=https://<key>@o<org>.ingest.sentry.io/<project>
```

## Add the server layer

If your server already initializes Sentry, add one option. `enableOpenTelemetrySetup: true` makes Sentry register its OpenTelemetry provider, which the layer then finds and uses ([rule 3](/docs/observability/where-spans-go)). Without it, most of Sentry's server SDKs register none, and the layer stays off. `@sentry/nextjs` and `@sentry/sveltekit` turn it on by default.

```ts title="instrument.ts"
import * as Sentry from "@sentry/node";

// Your Sentry setup, run before the app (instrument.ts, or
// instrumentation.ts in Next.js). The one addition: register
// Sentry's OpenTelemetry provider, which the chat then finds.
Sentry.init({
  dsn: process.env.SENTRY_DSN,
  tracesSampleRate: 1,
  enableOpenTelemetrySetup: true,
});
```

Then the layer needs only the key Sentry groups conversations by:

```ts title="lib/observability.ts"
import { defineObservability } from "@webmcp-agents/telemetry/server";

// Nothing to pass: Sentry's provider is found and used.
export const observability = defineObservability({
  // Sentry groups AI spans into conversations by this key.
  metadataMapping: {
    "webmcp.conversation.id": "gen_ai.conversation.id",
  },
});
```

In Astro, `process.env` does not hold `.env.local` in development: read the variables with `getSecret` from `astro:env/server` and pass them as `env`, as the Astro view of the [generic layer](/docs/observability/installation/otlp#add-the-server-layer) does. The chat route passes the layer to the handler:

**Vite**

```ts title="api/chat.ts" {3,6}
import { createChatHandler } from "@webmcp-agents/chat/server";

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

const handler = createChatHandler({ agents, observability });

export const GET = handler;
export const POST = handler;
```

**Next.js**

```ts title="app/api/chat/route.ts" {4,6}
import { createChatHandler } from "@webmcp-agents/chat/server";

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

const handler = createChatHandler({ agents, observability });

export const GET = handler;
export const POST = handler;
```

**TanStack Start**

```ts title="src/routes/api/chat.ts" {5,7}
import { createFileRoute } from "@tanstack/react-router";
import { createChatHandler } from "@webmcp-agents/chat/server";

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

const handler = createChatHandler({ agents, observability });

export const Route = createFileRoute("/api/chat")({
  server: {
    handlers: {
      GET: ({ request }) => handler(request),
      POST: ({ request }) => handler(request),
    },
  },
});
```

**Astro**

```ts title="src/pages/api/chat.ts" {6,12}
import type { APIRoute } from "astro";
import { getSecret } from "astro:env/server";
import { createChatHandler } from "@webmcp-agents/chat/server";

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

export const prerender = false;

const handler = createChatHandler({
  agents,
  observability,
  env: {
    ANTHROPIC_API_KEY: getSecret("ANTHROPIC_API_KEY"),
    WEB_AGENTS_MODEL: getSecret("WEB_AGENTS_MODEL"),
  },
});

export const GET: APIRoute = ({ request }) => handler(request);
export const POST: APIRoute = ({ request }) => handler(request);
```

:::note[An app with its own provider]
Sentry never replaces a provider that is already registered: if your app has one, follow Sentry's custom setup instead (its OTLP endpoint in your provider), and the layer uses your provider.
:::

## Add the relay route

The page's spans come to your own endpoint, so the backend's key never reaches the browser. The relay refuses other origins and requests with no `Origin` (a script, not a page); the `guard` is the per-IP limit for the rest. Keep its counts in a shared store in production, as for the chat ([Limits and budgets](/docs/security/limits)).

**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);
```

## Observe the page

One component turns each run and tool call on the page into spans and posts them to the relay. Render it once, inside `<WebAgentsProvider>`, next to the chat. Without React, call [`observeAgents()`](/docs/observability/observe-agents) once instead.

**Vite**

```tsx title="src/components/observe-runs.tsx"
import { ObserveAgents } from "@webmcp-agents/telemetry/react";

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

// Render it once, inside <WebAgentsProvider>. Each run the person
// agreed to becomes spans, posted to /api/telemetry.
export function ObserveRuns() {
  return <ObserveAgents enabled={() => consent.analytics} />;
}
```

**Next.js**

```tsx title="components/observe-runs.tsx"
"use client";

import { ObserveAgents } from "@webmcp-agents/telemetry/react";

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

// Render it once, inside <WebAgentsProvider>. Each run the person
// agreed to becomes spans, posted to /api/telemetry.
export function ObserveRuns() {
  return <ObserveAgents enabled={() => consent.analytics} />;
}
```

**TanStack Start**

```tsx title="src/components/observe-runs.tsx"
import { ObserveAgents } from "@webmcp-agents/telemetry/react";

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

// Render it once, inside <WebAgentsProvider>. Each run the person
// agreed to becomes spans, posted to /api/telemetry.
export function ObserveRuns() {
  return <ObserveAgents enabled={() => consent.analytics} />;
}
```

**Astro**

```tsx title="src/components/observe-runs.tsx"
import { ObserveAgents } from "@webmcp-agents/telemetry/react";

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

// Render it once, inside <WebAgentsProvider>. Each run the person
// agreed to becomes spans, posted to /api/telemetry.
export function ObserveRuns() {
  return <ObserveAgents enabled={() => consent.analytics} />;
}
```

## Check it in Sentry

Start the app and send one message in the chat. Sentry groups AI spans into conversations by `gen_ai.conversation.id`, which the layer maps; set the person with `Sentry.setUser`. Sentry's own instrumentations, such as its `vercelAIIntegration`, are Sentry's to configure ([Privacy and redaction](/docs/observability/privacy)).

</div>

**Sources, and what was not confirmed**

- [Using OpenTelemetry APIs](https://docs.sentry.io/platforms/javascript/guides/node/opentelemetry/using-opentelemetry-apis/): `enableOpenTelemetrySetup`
- [Custom OpenTelemetry setup](https://docs.sentry.io/platforms/javascript/guides/node/opentelemetry/custom-setup/)
- [Vercel AI integration](https://docs.sentry.io/platforms/javascript/guides/node/configuration/integrations/vercelai/)
- [AI conversations](https://docs.sentry.io/ai/observability/conversations/): `gen_ai.conversation.id`
- [OTLP traces](https://docs.sentry.io/concepts/otlp/direct/traces): the direct OTLP endpoint, which drops span events

## Next

**[The telemetry APIs](/docs/observability/apis?backend=sentry)**

What you just set up, one API per page: the layer, where spans go, the relay, the page's observer, sampling and
the conversation key, each showing what differs for Sentry.
