πͺ’ Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
refactor(observability): move beforeSend checks into named predicates (LFE-10974) (#15276)
Nikita Kabardina3803be91bee
diff --git a/.agents/skills/sentry-instrumentation/SKILL.md b/.agents/skills/sentry-instrumentation/SKILL.mdindex f75d1253fa..6446c8bdc9 100644--- a/.agents/skills/sentry-instrumentation/SKILL.md+++ b/.agents/skills/sentry-instrumentation/SKILL.md@@ -59,9 +59,9 @@ properly (below).only `Sentry.init` in the codebase β `beforeSend`,`captureConsoleIntegration`, `denyUrls`, replay masking). Add every new filteras a named predicate there, never as an ad-hoc inline check. `beforeSend`- still carries a couple of legacy inline checks (invalid-href, React-devtools)- that predate this convention and read only the exception value β migrate those- into named, all-field predicates; do not add more inline checks.+ now holds NO inline checks β every filter routes through a named predicate+ (`isNoisyHttpClientPollEvent`, `isDenylistedNoiseEvent`,+ `isReactDevtoolsInternalEvent`). Keep it that way.- **Classify tRPC errors at the seam,** not per call site: `handleTrpcError`([`web/src/utils/api.ts`](../../../web/src/utils/api.ts)) is the singlechokepoint every query/mutation error flows through β the place to drop@@ -108,9 +108,8 @@ properly (below).(a second `https://` β repeated `//`) passes validation and `<Link>`'s routerstill rejects it. PR #15245 renders these as a native `<a>` (which never runsthe router's href validation), removing the family at the source β no new- filter needed. (The older inline `beforeSend` invalid-href check is redundant- and never fired anyway β it read the wrong field, Rule 6 β so it can be- retired.)+ filter needed. (The older inline `beforeSend` invalid-href check β which+ never fired anyway, wrong field, Rule 6 β was removed in #15276.)6. **Every `beforeSend` / denylist rule: narrow signature + written rationale +a NEGATIVE fixture proving a real error still passes.** This is the MANDATORYdiff --git a/.agents/skills/sentry-instrumentation/references/sentry-capture-contract.md b/.agents/skills/sentry-instrumentation/references/sentry-capture-contract.mdindex 5b42704430..edb479747c 100644--- a/.agents/skills/sentry-instrumentation/references/sentry-capture-contract.md+++ b/.agents/skills/sentry-instrumentation/references/sentry-capture-contract.md@@ -24,11 +24,11 @@ the detail behind its rules.reaches Sentry; it does not. (Treat "Sentry = frontend-only" until a decisionsays otherwise.)- **Filter predicates** live in [`web/src/utils/sentryFilters.ts`](../../../../web/src/utils/sentryFilters.ts)- with unit tests in `sentryFilters.clienttest.ts`; `beforeSend` calls them.- `beforeSend` also still carries two legacy inline checks (invalid-href,- React-devtools) that predate this convention and read only- `event.exception.values[0].value` β a known gap to migrate into named,- all-field predicates. Add new filters only as predicates here, never inline.+ with unit tests in `sentryFilters.clienttest.ts`; `beforeSend` only calls+ these named predicates and holds no inline checks (the former invalid-href+ and React-devtools inline checks were removed / migrated to+ `isReactDevtoolsInternalEvent` in #15276). Add new filters only as predicates+ here, never inline.- **The tRPC seam** is `handleTrpcError` in [`web/src/utils/api.ts`](../../../../web/src/utils/api.ts):every query/mutation error flows through it β the single place to classify.PR #15243 drops expected `data.code`s (`NOT_FOUND` / `FORBIDDEN` /diff --git a/web/instrumentation-client.ts b/web/instrumentation-client.tsindex 02563a6ec0..615c293d3d 100644--- a/web/instrumentation-client.ts+++ b/web/instrumentation-client.ts@@ -2,6 +2,7 @@ import * as Sentry from "@sentry/nextjs";import {isDenylistedNoiseEvent,isNoisyHttpClientPollEvent,+ isReactDevtoolsInternalEvent,} from "@/src/utils/sentryFilters";const isEuOrUsRegionNonHipaa =@@ -15,21 +16,10 @@ Sentry.init({release: process.env.NEXT_PUBLIC_BUILD_ID,beforeSend(event) {- // const error = hint.originalException;- const errorValue = event.exception?.values?.[0]?.value || "";-- // Filter invalid href errors - these are from user-inputted data containing malformed URLs- // The Next.js router correctly rejects them, no need to log as errors- if (- errorValue.includes("Invalid href") &&- errorValue.includes("passed to next/router")- ) {- return null;- }-- // Filter React DevTools internal errors - these are benign errors from DevTools- // trying to access internal React properties- if (errorValue.includes("__reactContextDevtoolDebugId")) {+ // Drop React DevTools' internal probes against React's private fiber+ // properties - benign, DevTools-only noise. See isReactDevtoolsInternalEvent+ // for the rationale.+ if (isReactDevtoolsInternalEvent(event)) {return null;}diff --git a/web/src/utils/sentryFilters.clienttest.ts b/web/src/utils/sentryFilters.clienttest.tsindex ce0032b509..19553f164f 100644--- a/web/src/utils/sentryFilters.clienttest.ts+++ b/web/src/utils/sentryFilters.clienttest.ts@@ -3,6 +3,7 @@ import { type ErrorEvent } from "@sentry/nextjs";import {isDenylistedNoiseEvent,isNoisyHttpClientPollEvent,+ isReactDevtoolsInternalEvent,} from "@/src/utils/sentryFilters";/**@@ -534,3 +535,72 @@ describe("isDenylistedNoiseEvent", () => {});});});++describe("isReactDevtoolsInternalEvent", () => {+ describe("drops React DevTools internal probes", () => {+ it("drops a message-event probe", () => {+ expect(+ isReactDevtoolsInternalEvent(+ messageEvent(+ "Cannot read properties of undefined (reading '__reactContextDevtoolDebugId')",+ ),+ ),+ ).toBe(true);+ });++ it("drops an exception-value variant", () => {+ expect(+ isReactDevtoolsInternalEvent(+ exceptionEvent(+ "Cannot read properties of undefined (reading '__reactContextDevtoolDebugId')",+ "TypeError",+ ),+ ),+ ).toBe(true);+ });++ it("drops it when the text lives on event.logentry.message", () => {+ expect(+ isReactDevtoolsInternalEvent(+ logentryEvent(+ "Cannot read properties of null (reading '__reactContextDevtoolDebugId')",+ ),+ ),+ ).toBe(true);+ });++ it("drops a mixed event: EMPTY exception value but text on message", () => {+ // An empty-string exception value is not nullish, so a naive `??` chain+ // would keep it and never look at `message`. `eventText` treats it as+ // absent, so this still matches.+ expect(+ isReactDevtoolsInternalEvent({+ exception: { values: [{ value: "" }] },+ message:+ "Cannot read properties of undefined (reading '__reactContextDevtoolDebugId')",+ } as unknown as Parameters<typeof isReactDevtoolsInternalEvent>[0]),+ ).toBe(true);+ });+ });++ // The safety contract: a suppression predicate must not swallow real errors.+ describe("KEEPS real errors (never masks a genuine app error)", () => {+ it("keeps a real thrown TypeError unrelated to DevTools", () => {+ expect(+ isReactDevtoolsInternalEvent(+ exceptionEvent("TypeError: cannot read x", "TypeError"),+ ),+ ).toBe(false);+ });++ it("keeps an unrelated message event", () => {+ expect(+ isReactDevtoolsInternalEvent(messageEvent("some unrelated message")),+ ).toBe(false);+ });++ it("keeps an event with no exception/message/logentry text", () => {+ expect(isReactDevtoolsInternalEvent({} as ErrorEvent)).toBe(false);+ });+ });+});diff --git a/web/src/utils/sentryFilters.ts b/web/src/utils/sentryFilters.tsindex 33f3230e2d..f7df54be27 100644--- a/web/src/utils/sentryFilters.ts+++ b/web/src/utils/sentryFilters.ts@@ -143,6 +143,40 @@ function coreMessage(value: string): string {.trim();}+/**+ * True for React DevTools' internal probes against React's private fiber+ * properties (`__reactContextDevtoolDebugId` and similar). These are benign:+ * DevTools reads properties React does not guarantee exist, and the resulting+ * failure is DevTools' own instrumentation, not a Langfuse app bug β it fires+ * only when the extension is attached and installs its own probes.+ *+ * Matched against ALL text fields (exception value, message-event text, and+ * the `logentry` fallback) because these can arrive as either an exception or+ * a message event depending on how DevTools triggers the failure.+ */+/**+ * The event's first NON-EMPTY text field β the exception value, else the+ * message-event text, else the `logentry` fallback. An empty-string exception+ * value is treated as absent (not nullish, so `??` alone would keep it), so a+ * "mixed" event β empty exception value but real text on `message` β still+ * matches on the message rather than being silently skipped.+ */+function eventText(event: ErrorEvent): string {+ const exceptionValue = event.exception?.values?.[0]?.value;+ return (+ (typeof exceptionValue === "string" && exceptionValue.length > 0+ ? exceptionValue+ : undefined) ??+ event.message ??+ event.logentry?.message ??+ ""+ );+}++export function isReactDevtoolsInternalEvent(event: ErrorEvent): boolean {+ return eventText(event).includes("__reactContextDevtoolDebugId");+}+/*** True for known-benign CLIENT-side noise that cannot be a real Langfuse app* bug: browser-level network/transport failures, transient framework/vendor@@ -179,17 +213,13 @@ export function isDenylistedNoiseEvent(event: ErrorEvent): boolean {const exception = event.exception?.values?.[0];const exceptionType = exception?.type;const exceptionValue = exception?.value;- const hasExceptionValue =- typeof exceptionValue === "string" && exceptionValue.length > 0;- // The message-signature rules run against the exception value when present,- // otherwise the message-event text (console-origin noise has no exception).- const messageText =- (hasExceptionValue ? exceptionValue : undefined) ??- event.message ??- event.logentry?.message;+ // Message-signature rules run against the first non-empty text field+ // (exception value β message β logentry); `eventText` treats an empty+ // exception value as absent so mixed events still match on the message.+ const messageText = eventText(event);- if (typeof messageText === "string" && messageText.length > 0) {+ if (messageText.length > 0) {const core = coreMessage(messageText);// --- A. Transport / connectivity (whole-message match after unwrapping) ---