From 73fa58abce61ccd484c6aec68a167eca8d385998 Mon Sep 17 00:00:00 2001 From: Paul D'Ambra Date: Sun, 7 Jul 2024 20:38:34 +0100 Subject: [PATCH] feat: send errors one way --- src/extensions/exception-autocapture/index.ts | 21 +++++++++++++------ src/extensions/sentry-integration.ts | 8 ++++++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/extensions/exception-autocapture/index.ts b/src/extensions/exception-autocapture/index.ts index 5fbe682b1..654baca8b 100644 --- a/src/extensions/exception-autocapture/index.ts +++ b/src/extensions/exception-autocapture/index.ts @@ -8,11 +8,11 @@ import { EXCEPTION_CAPTURE_ENABLED_SERVER_SIDE, EXCEPTION_CAPTURE_ENDPOINT } fro import Config from '../../config' // TODO: move this to /x/ as default -const BASE_ENDPOINT = '/e/' +export const BASE_ERROR_ENDPOINT = '/e/' const LOGGER_PREFIX = '[Exception Capture]' export class ExceptionObserver { - private _endpoint: string = BASE_ENDPOINT + private _endpoint: string instance: PostHog remoteEnabled: boolean | undefined private originalOnUnhandledRejectionHandler: Window['onunhandledrejection'] | null | undefined = undefined @@ -22,9 +22,17 @@ export class ExceptionObserver { constructor(instance: PostHog) { this.instance = instance this.remoteEnabled = !!this.instance.persistence?.props[EXCEPTION_CAPTURE_ENABLED_SERVER_SIDE] + + // TODO: once BASE_ERROR_ENDPOINT is no longer /e/ this can be removed + this._endpoint = this.instance.persistence?.props[EXCEPTION_CAPTURE_ENDPOINT] || BASE_ERROR_ENDPOINT + this.startIfEnabled() } + get endpoint() { + return this._endpoint + } + get isEnabled() { return this.remoteEnabled ?? false } @@ -97,14 +105,15 @@ export class ExceptionObserver { // store this in-memory in case persistence is disabled this.remoteEnabled = !!autocaptureExceptionsResponse || false this._endpoint = isObject(autocaptureExceptionsResponse) - ? autocaptureExceptionsResponse.endpoint || BASE_ENDPOINT - : BASE_ENDPOINT + ? autocaptureExceptionsResponse.endpoint || BASE_ERROR_ENDPOINT + : BASE_ERROR_ENDPOINT if (this.instance.persistence) { this.instance.persistence.register({ [EXCEPTION_CAPTURE_ENABLED_SERVER_SIDE]: this.remoteEnabled, }) - // when we come to moving the endpoint to not /e/ we'll want that to persist between startup and decide response + // when we come to moving the endpoint to not /e/ + // we'll want that to persist between startup and decide response // TODO: once BASE_ENDPOINT is no longer /e/ this can be removed this.instance.persistence.register({ [EXCEPTION_CAPTURE_ENDPOINT]: this._endpoint, @@ -132,7 +141,7 @@ export class ExceptionObserver { _noTruncate: true, _batchKey: 'exceptionEvent', _noHeatmaps: true, - _url: this._endpoint, + _url: this.endpoint, }) } } diff --git a/src/extensions/sentry-integration.ts b/src/extensions/sentry-integration.ts index 5c52b762d..06c0f5c9c 100644 --- a/src/extensions/sentry-integration.ts +++ b/src/extensions/sentry-integration.ts @@ -18,6 +18,7 @@ import { PostHog } from '../posthog-core' import { SeverityLevel } from '../types' +import { BASE_ERROR_ENDPOINT } from './exception-autocapture' // NOTE - we can't import from @sentry/types because it changes frequently and causes clashes // We only use a small subset of the types, so we can just define the integration overall and use any for the rest @@ -124,7 +125,12 @@ export function createEventProcessor( '&query=' + event.event_id } - _posthog.capture('$exception', data) + + // we take the URL from the exception observer + // so that when we add error specific URL for ingestion + // these errors are sent there too + _posthog.capture('$exception', data, { _url: _posthog.exceptionObserver?.endpoint || BASE_ERROR_ENDPOINT }) + return event } }