Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(observability): symbolicate web and native errors and drop browser noise (#8213)

teo07a780f83109
diff --git a/.agents/rules/web/startup-bundle.md b/.agents/rules/web/startup-bundle.md
index 5916013b48..32d6ed1087 100644
--- a/.agents/rules/web/startup-bundle.md
+++ b/.agents/rules/web/startup-bundle.md
@@ -30,3 +30,7 @@ alwaysApply: false
caches, and unregister; it must not intercept requests.
- The build fails if any emitted file exceeds CloudFront's 10 MB compression
limit; split it rather than raising the limit.
+- The build also fails if a chunk has no source map or a map exceeds PostHog's
+ 25 MB symbolication limit. `startupChunks()` in `vite.base.ts` splits the
+ code the entry loads into `app`, `vendor`, and `api-schemas` chunks so each
+ map stays under it; it moves only modules the entry already imports.
diff --git a/apps/web/docs/ios-releases.md b/apps/web/docs/ios-releases.md
index 96b8462196..0768a86632 100644
--- a/apps/web/docs/ios-releases.md
+++ b/apps/web/docs/ios-releases.md
@@ -214,6 +214,11 @@ ios-release --config prd -- just ios-release` covers them), and
`scripts/ios-app-store.mjs` for the underlying App Store Connect commands
(`wait-build`, `submit`, `status`).
+`just ios-build` and `just ios-release` export frontend traces and logs to the
+production OTLP proxy
+(`ios_production_telemetry` in `scripts/ios-native-env.sh`); set
+`VITE_OTEL_EXPORTER_URL` and `VITE_OTEL_ENV` to send them elsewhere.
+
## One-time repository setup
- **Labels**: create `ios-update` and `skip-ios-update`.
diff --git a/apps/web/scripts/ios-build.sh b/apps/web/scripts/ios-build.sh
index 9d0f9807d5..67cc67e5a9 100644
--- a/apps/web/scripts/ios-build.sh
+++ b/apps/web/scripts/ios-build.sh
@@ -21,6 +21,7 @@ source "$script_dir/ios-native-env.sh"
ios_resolve_toolchain aarch64-apple-ios
ios_require_posthog_key
+ios_production_telemetry
# The web/WASM build keeps its original toolchain. Switch to Apple tools only
# for the native build, and skip Tauri's hook because it has already run here.
diff --git a/apps/web/scripts/ios-native-env.sh b/apps/web/scripts/ios-native-env.sh
index fcb7402561..3636cad7c0 100644
--- a/apps/web/scripts/ios-native-env.sh
+++ b/apps/web/scripts/ios-native-env.sh
@@ -62,3 +62,9 @@ ios_require_posthog_key() {
fi
export VITE_POSTHOG_API_KEY
}
+
+ios_production_telemetry() {
+ # Without an exporter the app sends no traces or logs to Datadog.
+ export VITE_OTEL_EXPORTER_URL="${VITE_OTEL_EXPORTER_URL:-https://macro-prox-prod.macroverse.workers.dev/i/otlp/v1/traces}"
+ export VITE_OTEL_ENV="${VITE_OTEL_ENV:-prod}"
+}
diff --git a/apps/web/scripts/ios-release.sh b/apps/web/scripts/ios-release.sh
index b74f00db41..70c9a88df2 100755
--- a/apps/web/scripts/ios-release.sh
+++ b/apps/web/scripts/ios-release.sh
@@ -115,6 +115,7 @@ just ios-prepare
# skipped because it has already run here.
step "Building the frontend bundle"
ios_require_posthog_key
+ios_production_telemetry
just build-tauri
step "Archiving and exporting IPA (this takes a while)"
diff --git a/apps/web/src/lib/analytics/analytics.test.ts b/apps/web/src/lib/analytics/analytics.test.ts
index c6c073a0a7..f9be05ec94 100644
--- a/apps/web/src/lib/analytics/analytics.test.ts
+++ b/apps/web/src/lib/analytics/analytics.test.ts
@@ -5,6 +5,7 @@ const mocks = vi.hoisted(() => ({
touch: false,
capture: vi.fn(),
identify: vi.fn(),
+ init: vi.fn(),
gtag: vi.fn(),
fbq: vi.fn(),
}));
@@ -24,6 +25,7 @@ vi.mock('posthog-js', () => ({
PostHog: class {
capture = mocks.capture;
identify = mocks.identify;
+ init = mocks.init;
},
}));
@@ -127,3 +129,98 @@ describe('Meta pixel platform support', () => {
}
);
});
+
+describe('PostHog exception frames', () => {
+ async function beforeSendException(value: string, filenames: string[]) {
+ vi.stubEnv('VITE_POSTHOG_API_KEY', 'test-key');
+ await import('./analytics');
+ const beforeSend = mocks.init.mock.calls[0][1].before_send;
+ return beforeSend({
+ event: '$exception',
+ properties: {
+ $exception_values: [value],
+ $exception_list: [
+ {
+ type: 'Error',
+ value,
+ stacktrace: {
+ type: 'raw',
+ frames: filenames.map((filename) => ({
+ platform: 'web:javascript',
+ filename,
+ lineno: 27,
+ colno: 40048,
+ })),
+ },
+ },
+ ],
+ },
+ });
+ }
+
+ async function sendException(filenames: string[]) {
+ const event = await beforeSendException('boom', filenames);
+ return event.properties.$exception_list[0].stacktrace.frames.map(
+ (frame: { filename: string }) => frame.filename
+ );
+ }
+
+ it('drops the ResizeObserver loop warning, which browsers report with a trailing period', async () => {
+ expect(
+ await beforeSendException(
+ 'ResizeObserver loop completed with undelivered notifications.',
+ ['https://macro.com/app/app-gifhAVYj.js']
+ )
+ ).toBeNull();
+ });
+
+ it('drops errors thrown by browser extensions', async () => {
+ expect(
+ await beforeSendException(
+ "Cannot read properties of undefined (reading 'M_ID')",
+ [
+ 'https://macro.com/app/app-gifhAVYj.js',
+ 'chrome-extension://eppiocemhmnlbhjplcgkofciiegomcon/content.js',
+ ]
+ )
+ ).toBeNull();
+ });
+
+ it('keeps app errors that only pass through an extension frame', async () => {
+ expect(
+ await beforeSendException('boom', [
+ 'chrome-extension://eppiocemhmnlbhjplcgkofciiegomcon/content.js',
+ 'https://macro.com/app/app-gifhAVYj.js',
+ ])
+ ).not.toBeNull();
+ });
+
+ it.each(['ios', 'android', 'desktop'] as const)(
+ 'points %s bundle frames at the public web bundle so PostHog can fetch its source map',
+ async (platform) => {
+ mocks.platform = platform;
+
+ expect(
+ await sendException([
+ 'tauri://localhost/app/app-xMZRw8gs.js',
+ 'https://tauri.localhost/app/chunk-AbC123.js',
+ 'http://tauri.localhost/app/chunk-AbC123.js',
+ 'tauri://localhost',
+ 'https://cdn.example.com/app/vendor.js',
+ ])
+ ).toEqual([
+ 'https://macro.com/app/app-xMZRw8gs.js',
+ 'https://macro.com/app/chunk-AbC123.js',
+ 'https://macro.com/app/chunk-AbC123.js',
+ 'tauri://localhost',
+ 'https://cdn.example.com/app/vendor.js',
+ ]);
+ }
+ );
+
+ it('leaves web frames untouched', async () => {
+ expect(
+ await sendException(['https://macro.com/app/app-gifhAVYj.js'])
+ ).toEqual(['https://macro.com/app/app-gifhAVYj.js']);
+ });
+});
diff --git a/apps/web/src/lib/analytics/analytics.ts b/apps/web/src/lib/analytics/analytics.ts
index ec5ac393d5..daee6953e5 100644
--- a/apps/web/src/lib/analytics/analytics.ts
+++ b/apps/web/src/lib/analytics/analytics.ts
@@ -14,8 +14,10 @@ import {
redactCallLinkProperties,
redactCallLinkTokens,
} from '@core/util/telemetryUrl';
+import { getWebOrigin } from '@core/util/webOrigin';
import { type CaptureOptions, PostHog } from 'posthog-js';
import { match } from 'ts-pattern';
+import { isIgnorableException, rewriteNativeFrameUrls } from './exceptionList';
import { getPlanAnalyticsProperties } from './planProperties';
/**
@@ -111,11 +113,6 @@ export type MetaStandardEvent = (typeof META_STANDARD_EVENT_NAMES)[number];
const META_STANDARD_EVENTS: Set<string> = new Set(META_STANDARD_EVENT_NAMES);
-const IGNORABLE_ERRORS = [
- // This error is reported very frequently but is not an actual issue.
- 'ResizeObserver loop completed with undelivered notifications',
-];
-
// Privacy filter lists block the upstream filename; the proxy maps this opaque alias back.
const POSTHOG_RECORDER_SCRIPT_NAME = 'posthog-recorder.js';
const POSTHOG_RECORDER_PROXY_SCRIPT_NAME = 'runtime.js';
@@ -161,12 +158,10 @@ const initializePosthog = (instance: PostHog) => {
if (cr?.event !== '$exception') return cr;
- const exceptionValues = cr.properties.$exception_values;
-
- if (!exceptionValues || !Array.isArray(exceptionValues)) return cr;
+ if (isIgnorableException(cr.properties.$exception_list)) return null;
- if (IGNORABLE_ERRORS.some((e) => exceptionValues.includes(e))) {
- return null;
+ if (isTauri()) {
+ rewriteNativeFrameUrls(cr.properties.$exception_list, getWebOrigin());
}
return cr;
diff --git a/apps/web/src/lib/analytics/exceptionList.ts b/apps/web/src/lib/analytics/exceptionList.ts
new file mode 100644
index 0000000000..9cc1ff939e
--- /dev/null
+++ b/apps/web/src/lib/analytics/exceptionList.ts
@@ -0,0 +1,41 @@
+import { isIgnorableError } from '@core/util/ignorableErrors';
+
+/** PostHog's `$exception_list`: the thrown error first, then its causes. */
+type ExceptionList = {
+ value?: string;
+ stacktrace?: { frames?: { filename?: string }[] };
+}[];
+
+/** Frames run outermost first, so the last one is where the error was thrown. */
+export function isIgnorableException(
+ exceptionList: ExceptionList | undefined
+): boolean {
+ const thrown = exceptionList?.[0];
+ if (!thrown) return false;
+ return isIgnorableError({
+ message: thrown.value,
+ source: thrown.stacktrace?.frames?.at(-1)?.filename,
+ });
+}
+
+const NATIVE_ASSET_ORIGIN =
+ /^(?:tauri:\/\/localhost|https?:\/\/tauri\.localhost)(?=\/app\/)/;
+
+/**
+ * Native apps run the same hashed bundle files the web app serves under
+ * `<web origin>/app/`, but load them from the webview's private origin, which
+ * PostHog cannot fetch. Point frames at the public copy so PostHog can
+ * download the file and its source map.
+ */
+export function rewriteNativeFrameUrls(
+ exceptionList: ExceptionList | undefined,
+ webOrigin: string
+): void {
+ for (const exception of exceptionList ?? []) {
+ for (const frame of exception.stacktrace?.frames ?? []) {
+ if (frame.filename) {
+ frame.filename = frame.filename.replace(NATIVE_ASSET_ORIGIN, webOrigin);
+ }
+ }
+ }
+}
diff --git a/apps/web/src/lib/core/util/ignorableErrors.ts b/apps/web/src/lib/core/util/ignorableErrors.ts
new file mode 100644
index 0000000000..90d1f758a8
--- /dev/null
+++ b/apps/web/src/lib/core/util/ignorableErrors.ts
@@ -0,0 +1,23 @@
+const IGNORABLE_MESSAGES = [
+ // Browsers report this benign layout warning as an uncaught error.
+ 'ResizeObserver loop completed with undelivered notifications',
+ 'ResizeObserver loop limit exceeded',
+];
+
+const EXTENSION_SCRIPT = /^(?:chrome|moz|safari(?:-web)?)-extension:\/\//;
+
+/**
+ * Browser noise that is not a Macro bug, dropped before it reaches PostHog or
+ * Datadog. `source` is the URL of the script that threw.
+ */
+export function isIgnorableError(error: {
+ message?: string;
+ source?: string;
+}): boolean {
+ const { message, source } = error;
+ return (
+ (source !== undefined && EXTENSION_SCRIPT.test(source)) ||
+ (message !== undefined &&
+ IGNORABLE_MESSAGES.some((ignored) => message.includes(ignored)))
+ );
+}
diff --git a/apps/web/src/observability/browser.test.ts b/apps/web/src/observability/browser.test.ts
index eccf05d872..7980cef3f3 100644
--- a/apps/web/src/observability/browser.test.ts
+++ b/apps/web/src/observability/browser.test.ts
@@ -72,6 +72,42 @@ it('explicit native recording overrides the normal enablement gate and exporter
});
});
+it('drops browser and extension noise from window errors but reports app errors', async () => {
+ vi.mocked(readDesktopDiagnostics).mockResolvedValue(undefined);
+ const addEventListener = vi.spyOn(window, 'addEventListener');
+ await initializeBrowserObservability();
+ const onError = addEventListener.mock.calls.find(
+ ([type]) => type === 'error'
+ )?.[1] as (event: ErrorEvent) => void;
+ addEventListener.mockRestore();
+ const appError = new Error('boom');
+
+ onError(
+ new ErrorEvent('error', {
+ message: 'ResizeObserver loop completed with undelivered notifications.',
+ })
+ );
+ onError(
+ new ErrorEvent('error', {
+ message: "Cannot read properties of undefined (reading 'M_ID')",
+ filename:
+ 'chrome-extension://eppiocemhmnlbhjplcgkofciiegomcon/content.js',
+ })
+ );
+ onError(
+ new ErrorEvent('error', {
+ message: 'boom',
+ filename: 'https://macro.com/app/app-gifhAVYj.js',
+ error: appError,
+ })
+ );
+
+ expect(Telemetry.error).toHaveBeenCalledOnce();
+ expect(Telemetry.error).toHaveBeenCalledWith(appError, {
+ 'error.source': 'window',
+ });
+});
+
it('an older binary without diagnostics still obeys the normal telemetry gate', async () => {
vi.mocked(readDesktopDiagnostics).mockResolvedValue(undefined);
await initializeBrowserObservability();
diff --git a/apps/web/src/observability/browser.ts b/apps/web/src/observability/browser.ts
index 9f09066782..08f78d9474 100644
--- a/apps/web/src/observability/browser.ts
+++ b/apps/web/src/observability/browser.ts
@@ -1,3 +1,4 @@
+import { isIgnorableError } from '@core/util/ignorableErrors';
import { getPlatform, isTauri } from '@core/util/platform';
import { recordBrowserTursoCacheNavigation } from '@graphql-cache/rollout-observability';
import { Telemetry } from '@macro-inc/observability';
@@ -110,6 +111,9 @@ export async function initializeBrowserObservability(): Promise<void> {
window.addEventListener('pagehide', () => void Telemetry.flush());
window.addEventListener('error', (event) => {
+ if (isIgnorableError({ message: event.message, source: event.filename })) {
+ return;
+ }
Telemetry.error(event.error ?? event.message, {
'error.source': 'window',
});
diff --git a/apps/web/vite.base.ts b/apps/web/vite.base.ts
index 37f43a9191..a84e72d69d 100644
--- a/apps/web/vite.base.ts
+++ b/apps/web/vite.base.ts
@@ -3,7 +3,7 @@ import { unwatchFile, watchFile } from 'node:fs';
import { resolve } from 'node:path';
import tailwind from '@tailwindcss/vite';
import { Features } from 'lightningcss';
-import type { Plugin, UserConfigFn } from 'vite';
+import type { Plugin, Rollup, UserConfigFn } from 'vite';
import solid from 'vite-plugin-solid';
import solidSvg from 'vite-plugin-solid-svg';
import wasm from 'vite-plugin-wasm';
@@ -130,6 +130,97 @@ function cloudFrontCompressionLimit(): Plugin {
};
}
+/**
+ * PostHog symbolicates every platform's stack frames by fetching the public
+ * `.map` next to each chunk, and gives up on maps over 25,000,000 bytes. The
+ * entry chunk's map once crossed that and every frame in it stayed minified,
+ * so fail the build on a missing or oversized map.
+ */
+function posthogSourceMapLimit(): Plugin {
+ const limitBytes = 25_000_000;
+ return {
+ name: 'posthog-source-map-limit',
+ apply: 'build',
+ generateBundle(_options, bundle) {
+ const problems = Object.values(bundle).flatMap((file) => {
+ if (file.type === 'chunk') {
+ return file.map ? [] : [`${file.fileName} has no source map`];
+ }
+ if (!file.fileName.endsWith('.map')) return [];
+ const bytes =
+ typeof file.source === 'string'
+ ? Buffer.byteLength(file.source)
+ : file.source.byteLength;
+ return bytes > limitBytes ? [`${file.fileName} is ${bytes} bytes`] : [];
+ });
+ if (problems.length === 0) return;
+ this.error(
+ `PostHog cannot symbolicate these chunks (source maps are required and must be at most ${limitBytes} bytes): ${problems.join(', ')}. Split the chunk behind lazy() boundaries or in startupChunks().`
+ );
+ },
+ };
+}
+
+const isNodeModule = (id: string) => id.includes('/node_modules/');
+const isGeneratedSource = (id: string) =>
+ !isNodeModule(id) && /\/generated\//.test(id);
+
+/**
+ * Splits the entry chunk so each source map stays under PostHog's limit: one
+ * map with every startup module was 44 MB, most of it library code and the
+ * embedded text of generated API clients.
+ *
+ * Only modules the entry already imports statically move, so startup loads
+ * the same code in three files and lazy code stays lazy. A generated module
+ * moves only if it imports nothing but libraries and other generated modules;
+ * otherwise the chunks would import each other and could read bindings before
+ * they are initialized.
+ */
+function startupChunks(): Rollup.ManualChunksOption {
+ const plans = new WeakMap<Rollup.GetModuleInfo, Map<string, string>>();
+ const planChunks = (
+ getModuleInfo: Rollup.GetModuleInfo,
+ moduleIds: Iterable<string>
+ ) => {
+ const imports = (id: string) => getModuleInfo(id)?.importedIds ?? [];
+ const startup = new Set<string>();
+ const pending = [...moduleIds].filter((id) => getModuleInfo(id)?.isEntry);
+ for (let id = pending.pop(); id !== undefined; id = pending.pop()) {
+ if (startup.has(id)) continue;
+ startup.add(id);
+ pending.push(...imports(id));
+ }
+ const schemas = new Set([...startup].filter(isGeneratedSource));
+ let changed = true;
+ while (changed) {
+ changed = false;
+ for (const id of schemas) {
+ const importsAppCode = imports(id).some(
+ (imported) => !isNodeModule(imported) && !schemas.has(imported)
+ );
+ if (importsAppCode) {
+ schemas.delete(id);
+ changed = true;
+ }
+ }
+ }
+ const plan = new Map<string, string>();
+ for (const id of startup) {
+ if (isNodeModule(id)) plan.set(id, 'vendor');
+ else if (schemas.has(id)) plan.set(id, 'api-schemas');
+ }
+ return plan;
+ };
+ return (id, { getModuleInfo, getModuleIds }) => {
+ let plan = plans.get(getModuleInfo);
+ if (!plan) {
+ plan = planChunks(getModuleInfo, getModuleIds());
+ plans.set(getModuleInfo, plan);
+ }
+ return plan.get(id);
+ };
+}
+
/**
* loro-crdt's `browser` export downloads its 3 MB WASM with a synchronous
* XMLHttpRequest and compiles it on the main thread, freezing startup for the
@@ -237,6 +328,7 @@ export const createAppViteConfig = (): UserConfigFn => {
}),
gitBranchHmrPlugin(),
cloudFrontCompressionLimit(),
+ posthogSourceMapLimit(),
],
define: defineEnv(ENV_MODE, command),
clearScreen: false,
@@ -246,6 +338,7 @@ export const createAppViteConfig = (): UserConfigFn => {
tsconfigpaths({
root: './',
}),
+ posthogSourceMapLimit(),
],
rollupOptions: {
output: {
@@ -273,9 +366,9 @@ export const createAppViteConfig = (): UserConfigFn => {
app: resolve(__dirname, 'index.html'),
},
// KaTeX and PDF.js are now reachable through lazy boundaries. Let
- // Rollup place them naturally; forcing named chunks hoists shared
- // CommonJS helpers into those chunks and makes the entry preload
- // otherwise-lazy code.
+ // Rollup place them naturally; forcing lazy code into named chunks
+ // hoists shared CommonJS helpers into those chunks and makes the
+ // entry preload otherwise-lazy code.
output: NO_MINIFY
? {
// remove hashes from output paths
@@ -283,11 +376,13 @@ export const createAppViteConfig = (): UserConfigFn => {
entryFileNames: `assets/[name].js`,
chunkFileNames: `assets/[name].js`,
assetFileNames: `assets/[name].[ext]`,
+ manualChunks: startupChunks(),
}
: {
format: 'es',
chunkFileNames: '[name]-[hash].js',
entryFileNames: '[name]-[hash].js',
+ manualChunks: startupChunks(),
},
},
assetsInlineLimit: (filePath) => {