Skip to content

Load Sentry tracing only when traces are sampled - #1997

Merged
helpcodeai merged 3 commits into
mainfrom
perf/sentry-tracing-off
Dec 5, 2026
Merged

helpcodeai merged 3 commits into
mainfrom
perf/sentry-tracing-off

Conversation

@helpcodeai

Copy link
Copy Markdown
Contributor

Summary

A CPU profile of the live Cloud backend (60 s, 2026-12-05 21:37 UTC) puts about 22 % of its busy CPU time in Sentry and OpenTelemetry code: span attributes, SQL sanitising, uuid generation, span start logging. That happens on every request and every Prisma/pg query. Yet only 0.5 % of HTTP requests are sampled, and the spans were over quota anyway.

The cause: instrument.ts always passes a tracesSampler, even when the rates are 0. With any sampler, @sentry/node 11 adds its tracing integrations (getDefaultIntegrations → hasSpansEnabled(options) ? getTracingIntegrations() : []). Unsampled spans are still built.

Changes

  • common/sentry-sampler.ts: tracingRatesFromEnv() returns the rates, or null when both are 0.
  • instrument.ts: passes tracesSampler only when there is something to sample. Error reporting is unchanged: DSN, scrubbers, httpIntegration with no request bodies.
  • docker-compose.cloud.yml: the Cloud default SENTRY_TRACES_SAMPLE_RATE goes from 0.005 to 0. A rate in .env turns tracing back on for an investigation.
  • docs/operations/observability.md: explains that any rate above 0 loads the instrumentation for every request.

Type

  • Performance fix

Testing

  • Added unit tests (sentry-sampler.spec.ts, tracingRatesFromEnv): returns null for unset, 0, empty, invalid or out-of-range rates; MCP gets a fifth of the HTTP rate by default; MCP alone can be traced.
  • In packages/backend after prisma generate:
    • npx tsc --noEmit -p tsconfig.json: clean.
    • npx eslint on the changed files: clean.
    • npx jest --maxWorkers=4 src/common/sentry: 18 passed.
  • Checked in node_modules/@sentry/node (11.16.0) that tracing integrations depend on hasSpansEnabled(options). No backend code calls startSpan/getActiveSpan.

Checklist

  • My code follows the existing code style
  • I have tested my changes locally
  • I have updated documentation (if applicable)
  • All existing tests pass (npm test): full suite in CI
  • This PR has a descriptive title

Cloud: errors still go to Sentry; no performance traces unless a rate is set in .env.
Self-hosted: the default rate was already 0, so a self-hosted instance with a DSN now also skips the tracing instrumentation it never used. No database migration. No change under ee/.

After the deploy I will compare backend CPU and /mcp latency at the same hours.

Related Issues

Daily check 2026-12-05 (backend slow at peak hours).

@helpcodeai
helpcodeai merged commit 61518ee into main Dec 5, 2026
16 checks passed
@helpcodeai
helpcodeai deleted the perf/sentry-tracing-off branch December 5, 2026 22:00
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 5, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant