Skip to content

feat(api): replace Sentry with optional OpenTelemetry OTLP export - #4555

Merged
mogery merged 11 commits into
mainfrom
mogery/otel-collector-drop-sentry
Sep 9, 2026
Merged

mogery merged 11 commits into
mainfrom
mogery/otel-collector-drop-sentry

Conversation

@mogery

@mogery mogery commented Sep 6, 2026 •

Copy link
Copy Markdown
Member

Summary

Removes Sentry from the API entirely and replaces the span wrapper with a real OpenTelemetry setup that optionally exports to an OTLP collector.

  • Sentry is gone: @sentry/node, @sentry/core, @sentry/cli, Sentry.init, the Express error handler, every captureException site, services/sentry.ts, and the SENTRY_DSN / SENTRY_TRACE_SAMPLE_RATE / SENTRY_ERROR_SAMPLE_RATE config. Error paths keep their existing logger.error calls, and the paths that only had Sentry capture (v1 scrape, endpoint feedback, the scrape pipeline's handled failures) now record the error on a span instead.
  • SENTRY_ENVIRONMENT is untouched: it is an environment marker with its own consumers (colorized logs and uncached DNS under dev), unrelated to the Sentry SDK.
  • lib/otel-tracer.ts now sits on @opentelemetry/sdk-trace-node. withSpan / setSpanAttributes / SpanKind keep their signatures; spans are always ended, errors are recorded as span events with ERROR status.
  • Export is optional and standard: src/otel.ts is imported first by every entry point and registers a provider only when OTEL_EXPORTER_OTLP_ENDPOINT (or OTEL_EXPORTER_OTLP_TRACES_ENDPOINT) is set. Export is http/protobuf via @opentelemetry/exporter-trace-otlp-proto, which honors the standard OTEL_EXPORTER_OTLP_* variables; OTEL_SERVICE_NAME / OTEL_RESOURCE_ATTRIBUTES set the resource. Without an endpoint, withSpan is a no-op wrapper. Every entry point flushes pending spans on graceful shutdown.
  • No sampling: ParentBased(AlwaysOn), so everything is exported and continued traces keep the parent's decision.
  • Zero-data-retention spans never leave the process:
    1. withSpan(..., { zeroDataRetention }) / withZeroDataRetention() flag the OTel context; a sampler makes every span started under it non-recording, including library spans (AI SDK).
    2. A span processor drops any span that ends carrying a zero_data_retention / *.zero_data_retention attribute equal to true. Setting that attribute on a live recording span also flags its trace, so descendants started afterwards are not recorded and in-flight spans of the trace are dropped.
      The flag is passed at every root where ZDR is known: v1 and v2 scrape, v2 parse and parse-upload requests (resolved before the root span from the team flags and raw body), v2 search (ZDR / anon), endpoint feedback (per endpoint), scrape.pipeline, worker.scrape.process and the queue bookkeeping that follows it, NuQ enqueues, crawl Redis persistence, GCS job persistence.
  • Queue propagation switched to W3C traceparent/tracestate/baggage on job.data.traceContext. Legacy Sentry-shaped contexts on in-flight jobs are ignored (fresh trace), so the deploy is safe.
  • AI SDK telemetry keeps its defaults: the existing experimental_telemetry blocks are unchanged, so model, usage, inputs and outputs are recorded on the AI spans (the Sentry integration used to force inputs/outputs off).

Deploy notes

  • Production Helm values still point OTEL_EXPORTER_OTLP_ENDPOINT at a hostname that no longer resolves and set the Sentry DSN. The companion infra change repoints it at the ClickStack OTel collector gateway over http/protobuf and drops the Sentry DSN and sample rate; merge that first so the API never tries the dead endpoint.
  • OTEL_SERVICE_NAME should be set per deployment (production already does: app, nuq-worker, ...); without it every process reports as firecrawl-api.

Test plan

  • pnpm exec tsc --noEmit, pnpm exec knip, audit-ci clean
  • src/lib/otel-tracer.test.ts: export of attributes/kind/status/resource, exception recording, nesting of library spans, ZDR context suppression (own and library spans), attribute-based drop, late-flagged trace suppression (descendants and in-flight spans), W3C round trip with inherited sampling, ZDR parent stays unrecorded, legacy/missing contexts, empty context outside a span
  • Existing tests whose Sentry mocks were removed pass (log_job, batch_billing, llm-failure-fallback, search-developer-ledger, services/index)
  • Smoke run of the bootstrap: with an endpoint the root span records and a ZDR span does not; without an endpoint nothing records
  • CI

🤖 Generated with Claude Code

Remove @sentry/node entirely (error capture, express handler, tracing
wrapper) and rebuild lib/otel-tracer on the OpenTelemetry SDK:

- Tracing is off unless OTEL_EXPORTER_OTLP_ENDPOINT (or the traces
  endpoint) is set; spans then go to the collector over http/protobuf
  with 100% sampling (parent-based for continued traces).
- Zero-data-retention spans never leave the process: a sampler makes
  every span started under a ZDR context non-recording, and a span
  processor drops anything ending with a *.zero_data_retention=true
  attribute. Root spans (v2 scrape/parse/upload requests, scrape
  pipeline, worker job and its queue bookkeeping, NuQ enqueues,
  crawl/GCS persistence) pass the flag.
- Queue trace propagation uses W3C traceparent/tracestate/baggage;
  legacy Sentry-shaped contexts on in-flight jobs start a fresh trace.
- AI SDK telemetry keeps recordInputs/recordOutputs off, previously
  enforced by the Sentry integration.
- Workers flush pending spans on graceful shutdown.
- The SENTRY_ENVIRONMENT=dev local override (colored logs, uncached
  DNS under ENV=production) goes away with the variable.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

@superagent-security superagent-security Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superagent found 2 security concern(s).

Comment thread apps/api/src/scraper/scrapeURL/index.ts Resolved
Comment thread apps/api/src/otel.ts Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 78 files

Confidence score: 3/5

  • In apps/api/src/lib/gcs-jobs.ts, keyless job saves can send the client IP embedded in params.team_id to the OTLP collector, creating a concrete privacy exposure in telemetry; apply the keyless-team pseudonymizer to this attribute while retaining the raw team ID.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="apps/api/src/lib/gcs-jobs.ts">

<violation number="1" location="apps/api/src/lib/gcs-jobs.ts:100">
P1: When a keyless request saves a job, this span exports the client IP embedded in `params.team_id` to the OTLP collector. Apply the keyless-team pseudonymizer to this telemetry attribute while retaining the raw team ID for GCS metadata and storage logic.</violation>
</file>

Tip: instead of fixing issues one by one fix them all with cubic

Re-trigger cubic

Comment thread apps/api/src/lib/otel-tracer.ts Resolved
Comment thread apps/api/src/lib/gcs-jobs.ts Resolved
Comment thread apps/api/src/controllers/v2/search.ts Resolved
Comment thread apps/api/src/otel.ts Resolved
Comment thread apps/api/src/controllers/v1/scrape.ts Resolved
Comment thread apps/api/src/lib/otel-tracer.test.ts Resolved
Follow-ups from review:

- The scrape worker applies the zero-data-retention context whether or not
  a trace context arrived with the job, so nothing between the worker entry
  and the pipeline records for ZDR jobs.
- ZDR / anonymous searches flag the context before the controller body runs.
- The parse upload root span and the crawl lookup span carry the ZDR flag.
- scrape.pipeline records handled unexpected failures as ERROR spans instead
  of ending OK.
- Every entry point flushes buffered spans on graceful shutdown.
- OTLP endpoints are validated as URLs and empty OTEL_* values count as unset.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

@superagent-security superagent-security Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superagent found 1 security concern(s).

Comment thread apps/api/src/lib/otel-tracer.ts Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 15 files (changes from recent commits).

Confidence score: 3/5

  • apps/api/src/lib/otel-tracer.ts records request-derived error messages and stack traces in OTLP-exported spans, which could expose sensitive request data to telemetry systems; sanitize or omit these fields before calling recordException.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="apps/api/src/lib/otel-tracer.ts">

<violation number="1" location="apps/api/src/lib/otel-tracer.ts:281">
P1: Sanitize or omit request-derived error messages and stack traces before calling `recordException`; this span event is exported with OTLP and may contain sensitive request data.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Comment thread apps/api/src/lib/otel-tracer.ts Resolved
…d feedback

- Flagging a live span with a *.zero_data_retention attribute now marks its
  trace: descendants started afterwards are not recorded and spans of that
  trace still in flight are dropped when they end (bounded registry), so the
  attribute path no longer depends on children starting after the end-time
  filter. Covered by tests.
- v1 scrape requests and endpoint feedback recording run inside a span, and
  their handled failures are recorded as ERROR spans with the error id, so
  error visibility that used to come from Sentry capture has an OTel path.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 4 files (changes from recent commits).

Confidence score: 2/5

  • apps/api/src/lib/otel-tracer.ts can FIFO-evict an older trace ID while work is still running once more than 10,000 ZDR traces are marked, allowing later child or library spans to be recorded and exported incorrectly; fix the lifecycle-aware eviction handling before merging.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="apps/api/src/lib/otel-tracer.ts">

<violation number="1" location="apps/api/src/lib/otel-tracer.ts:75">
P1: When more than 10,000 ZDR traces are marked while an older trace still has work running, this FIFO evicts the older trace ID. A later child or library span from that trace can then be recorded and exported; use lifecycle-aware retention or propagate ZDR context instead of evicting active traces.</violation>
</file>

Tip: Review your code locally with the cubic CLI to iterate faster.

Fix all with cubic | Re-trigger cubic

Comment thread apps/api/src/lib/otel-tracer.ts Resolved
… feedback ZDR

- The ZDR trace registry only takes entries from recording spans (a
  non-recording span already suppresses its descendants through
  parent-based sampling), so it only sees the rare late-discovery case;
  cap raised to 100k as a pure memory guard.
- Feedback recording derives its ZDR flag from the endpoint the feedback
  refers to (search forced-zdr/forced-anon or scrape/parse forced), and
  flags the span when the job itself turns out to be ZDR so it is dropped.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 2 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Fix all with cubic | Re-trigger cubic

Map feedback is not a ZDR endpoint, so its span stays recorded even for
teams forced into scrape ZDR, matching shouldSkipPersistenceForForcedZdr.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 issues found across 1 file (changes from recent commits).

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Requires human review: Auto-approval blocked by 2 unresolved issues from previous reviews.

Re-trigger cubic

@superagent-security superagent-security Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superagent found 1 security concern(s).

Comment thread apps/api/src/lib/crawl-redis.ts Resolved
mogery and others added 2 commits September 9, 2026 00:32
The variable is a deployment-environment marker with its own consumers
(colorized logs and uncached DNS when set to "dev"); it stays exactly as
it was. Only SENTRY_DSN and SENTRY_TRACE_SAMPLE_RATE go with Sentry.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Drop the recordInputs/recordOutputs=false carried over from the Sentry
integration; AI SDK telemetry keeps its defaults.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 12 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Fix all with cubic | Re-trigger cubic

Comment thread apps/api/src/lib/branding/llm.ts Resolved
mogery and others added 2 commits September 9, 2026 00:52
…-drop-sentry

# Conflicts:
#	apps/api/src/controllers/v2/parse-upload.ts
#	apps/api/src/lib/crawl-redis.ts
#	apps/api/src/services/logging/log_job.test.ts
#	apps/api/src/services/logging/log_job.ts
Those two calls feed the model the page screenshot and raw page chunks;
recording them as span attributes would bloat OTLP batches. Outputs stay
recorded; every other AI call keeps the SDK defaults.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@mogery

mogery commented Sep 8, 2026

Copy link
Copy Markdown
Member Author

@cubic-dev-ai review

All previous threads are addressed or resolved; please re-review the whole PR at the current head.

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review

All previous threads are addressed or resolved; please re-review the whole PR at the current head.

@mogery I have started the AI code review. It will take a few minutes to complete.

@superagent-security superagent-security Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superagent found 1 security concern(s).

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 70 files

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Comment thread apps/api/src/lib/otel-tracer.ts Resolved
Comment thread apps/api/src/lib/otel-tracer.ts Resolved
Comment thread apps/api/src/lib/crawl-redis.ts Resolved
Comment thread apps/api/src/controllers/v2/search.ts Resolved
Search requests now get an api.search.request SERVER span (inside the
ZDR context for zdr/anon searches), matching scrape and parse.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

@superagent-security superagent-security Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superagent found 1 security concern(s).

Comment thread apps/api/src/lib/branding/llm.ts Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 1 file (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Fix all with cubic | Re-trigger cubic

Comment thread apps/api/src/controllers/v2/search.ts Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 issues found across 1 file (changes from recent commits).

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Re-trigger cubic

@mogery
mogery merged commit bca5645 into main Sep 9, 2026
11 of 13 checks passed
@mogery
mogery deleted the mogery/otel-collector-drop-sentry branch September 9, 2026 12:30
edenbuilds added a commit to edenbuilds/firecrawl that referenced this pull request Sep 10, 2026
Upstream removed Sentry entirely in favor of OpenTelemetry (firecrawl#4555), which
is why this hunk conflicted even though upstream never touched this
function: the base state on both sides had no Sentry usage. Resolving
in favor of this branch's closedDuringAuth/isSocketOpen guard would have
kept an unimported `Sentry.captureException(err)` call — a leftover from
before this repo dropped the Sentry dependency, and something that would
throw ReferenceError on the exact disconnect-during-auth path this PR
hardens. Dropped it; the existing logger.error() call below already
reports the full error.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant