Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions docker-compose.cloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ x-backend: &backend
# end-to-end check; leave it empty otherwise.
- SENTRY_DSN=${SENTRY_DSN_BACKEND:-}
- SENTRY_ENVIRONMENT=${SENTRY_ENVIRONMENT:-production}
# 0.5 % of HTTP requests (MCP calls a fifth of that). At 5 % Cloud sent
# about 1 M spans a day: far over the span quota, so Sentry accepted none
# and the SDK spent CPU on traces nobody could see (Nov 2026).
- SENTRY_TRACES_SAMPLE_RATE=${SENTRY_TRACES_SAMPLE_RATE:-0.005}
# Tracing off by default: errors are still reported. At 0.5 % sampling the
# tracing instrumentation still ran on every request and query, about a
# fifth of the backend's CPU (profile of 2026-12-05). Set a rate in .env for
# a time-boxed investigation (MCP calls get a fifth of it).
- SENTRY_TRACES_SAMPLE_RATE=${SENTRY_TRACES_SAMPLE_RATE:-0}
- SENTRY_VERIFY_TOKEN=${SENTRY_VERIFY_TOKEN:-}
# Prometheus /metrics — a separate listener, off by default (docs/operations/observability.md).
# METRICS_TOKEN is required once METRICS_ENABLED=true; the listener refuses to start without it.
Expand Down
2 changes: 2 additions & 0 deletions docs/operations/observability.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ What leaves the process is an allowlist (`packages/backend/src/common/sentry-scr

Set rates between `0` and `1`. `0.1` = 10 %.

With `SENTRY_TRACES_SAMPLE_RATE` and `SENTRY_MCP_TRACES_SAMPLE_RATE` both `0` (the default), the backend does not load Sentry's tracing instrumentation at all. Any rate above `0` loads it for every request, sampled or not, which costs CPU on a busy server: keep tracing for short investigations.

To confirm the pipeline end to end, set `SENTRY_VERIFY_TOKEN` and:

- backend: `curl -H "x-sentry-verify: $TOKEN" https://<host>/health/sentry-verify` → a 500 and an event "Sentry verification (backend)";
Expand Down
2 changes: 2 additions & 0 deletions llms-full.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 24 additions & 1 deletion packages/backend/src/common/sentry-sampler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sampleRateFor } from './sentry-sampler';
import { sampleRateFor, tracingRatesFromEnv } from './sentry-sampler';

const rates = { base: 0.05, mcp: 0.01 };

Expand Down Expand Up @@ -58,3 +58,26 @@ describe('sampleRateFor', () => {
expect(sampleRateFor({ name: 'SELECT "public"."kg_edges"."id" FROM "public"."kg_edges"' }, rates)).toBe(0);
});
});

describe('tracingRatesFromEnv', () => {
it('returns null when nothing is traced, so no tracing instrumentation is loaded', () => {
expect(tracingRatesFromEnv({})).toBeNull();
expect(tracingRatesFromEnv({ SENTRY_TRACES_SAMPLE_RATE: '0' })).toBeNull();
expect(tracingRatesFromEnv({ SENTRY_TRACES_SAMPLE_RATE: '' })).toBeNull();
expect(tracingRatesFromEnv({ SENTRY_TRACES_SAMPLE_RATE: 'abc' })).toBeNull();
expect(tracingRatesFromEnv({ SENTRY_TRACES_SAMPLE_RATE: '0', SENTRY_MCP_TRACES_SAMPLE_RATE: '0' })).toBeNull();
});

it('gives MCP a fifth of the HTTP rate unless set', () => {
expect(tracingRatesFromEnv({ SENTRY_TRACES_SAMPLE_RATE: '0.005' })).toEqual({ base: 0.005, mcp: 0.001 });
expect(tracingRatesFromEnv({ SENTRY_TRACES_SAMPLE_RATE: '0.1', SENTRY_MCP_TRACES_SAMPLE_RATE: '0.5' })).toEqual({ base: 0.1, mcp: 0.5 });
});

it('traces MCP alone when only its rate is set', () => {
expect(tracingRatesFromEnv({ SENTRY_MCP_TRACES_SAMPLE_RATE: '0.01' })).toEqual({ base: 0, mcp: 0.01 });
});

it('ignores rates outside 0..1', () => {
expect(tracingRatesFromEnv({ SENTRY_TRACES_SAMPLE_RATE: '2' })).toBeNull();
});
});
20 changes: 20 additions & 0 deletions packages/backend/src/common/sentry-sampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,23 @@ export function sampleRateFor(ctx: SamplingInput, rates: SamplerRates): number {
if (/^\/mcp(\/|$)/.test(path)) return rates.mcp;
return rates.base;
}

/**
* The sampling rates from the environment, or null when nothing is traced.
*
* Null matters: Sentry installs its tracing instrumentation (OpenTelemetry for
* http, express, Prisma, pg...) whenever a `tracesSampler` is passed, even one
* that always returns 0. That instrumentation runs on every request and every
* query whether or not the span is kept: on Cloud on 2026-12-05 it was about a
* fifth of the backend's CPU time while 0.5 % of requests were sampled. With
* both rates 0 instrument.ts passes no sampler, so only error reporting runs.
*/
export function tracingRatesFromEnv(env: NodeJS.ProcessEnv = process.env): SamplerRates | null {
const sample = (raw: string | undefined, fallback: number) => {
const n = raw === undefined || raw === '' ? NaN : Number(raw);
return Number.isFinite(n) && n >= 0 && n <= 1 ? n : fallback;
};
const base = sample(env.SENTRY_TRACES_SAMPLE_RATE, 0);
const mcp = sample(env.SENTRY_MCP_TRACES_SAMPLE_RATE, base / 5);
return base > 0 || mcp > 0 ? { base, mcp } : null;
}
13 changes: 7 additions & 6 deletions packages/backend/src/instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import * as Sentry from '@sentry/nestjs';
import { APP_VERSION } from './common/app-version';
import { IGNORED_ERRORS, isMcpTransportError, scrubBreadcrumb, scrubEvent, scrubSpan } from './common/sentry-scrub';
import { sampleRateFor } from './common/sentry-sampler';
import { sampleRateFor, tracingRatesFromEnv } from './common/sentry-sampler';

const dsn = process.env.SENTRY_DSN;

Expand All @@ -22,6 +22,7 @@ if (dsn) {
const n = raw === undefined ? NaN : Number(raw);
return Number.isFinite(n) && n >= 0 && n <= 1 ? n : fallback;
};
const tracing = tracingRatesFromEnv(process.env);

Sentry.init({
dsn,
Expand All @@ -35,11 +36,11 @@ if (dsn) {
// SENTRY_TRACES_SAMPLE_RATE applies to HTTP requests; MCP calls take
// SENTRY_MCP_TRACES_SAMPLE_RATE (default: a fifth of it); background work
// and health probes are not traced. See ./common/sentry-sampler.ts.
tracesSampler: (() => {
const base = sample(process.env.SENTRY_TRACES_SAMPLE_RATE, 0.0);
const rates = { base, mcp: sample(process.env.SENTRY_MCP_TRACES_SAMPLE_RATE, base / 5) };
return (ctx: Parameters<typeof sampleRateFor>[0]) => sampleRateFor(ctx, rates);
})(),
// With both rates 0 no sampler is passed at all: Sentry then does not
// install its tracing instrumentation (tracingRatesFromEnv explains why).
...(tracing
? { tracesSampler: (ctx: Parameters<typeof sampleRateFor>[0]) => sampleRateFor(ctx, tracing) }
: {}),
// SDK v11 renamed profilesSampleRate. Like before, it has no effect
// until nodeProfilingIntegration is added to the integrations below.
profileSessionSampleRate: sample(process.env.SENTRY_PROFILES_SAMPLE_RATE, 0.0),
Expand Down
Loading