Skip to content

Commit dff84c6

Browse files
authored
feat: optionally exclude exception capture from sentry integration (#2312)
1 parent 12cc350 commit dff84c6

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

.changeset/pink-birds-switch.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'posthog-js': patch
3+
'posthog-node': patch
4+
---
5+
6+
chore: allow PostHog exception capture to be skipped in Sentry integration

packages/browser/src/extensions/sentry-integration.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* @param {Number} [projectId] Optional: The Sentry project id, used to send a direct link from PostHog to Sentry
1616
* @param {string} [prefix] Optional: Url of a self-hosted sentry instance (default: https://sentry.io/organizations/)
1717
* @param {SeverityLevel[] | '*'} [severityAllowList] Optional: send events matching the provided levels. Use '*' to send all events (default: ['error'])
18+
* @param {boolean} [sendExceptionsToPostHog] Optional: capture exceptions as events in PostHog (default: true)
1819
*/
1920

2021
import { PostHog } from '../posthog-core'
@@ -63,13 +64,20 @@ export type SentryIntegrationOptions = {
6364
projectId?: number
6465
prefix?: string
6566
severityAllowList?: SeverityLevel[] | '*'
67+
sendExceptionsToPostHog?: boolean
6668
}
6769

6870
const NAME = 'posthog-js'
6971

7072
export function createEventProcessor(
7173
_posthog: PostHog,
72-
{ organization, projectId, prefix, severityAllowList = ['error'] }: SentryIntegrationOptions = {}
74+
{
75+
organization,
76+
projectId,
77+
prefix,
78+
severityAllowList = ['error'],
79+
sendExceptionsToPostHog = true,
80+
}: SentryIntegrationOptions = {}
7381
): (event: _SentryEvent) => _SentryEvent {
7482
return (event) => {
7583
const shouldProcessLevel = severityAllowList === '*' || severityAllowList.includes(event.level as SeverityLevel)
@@ -135,7 +143,9 @@ export function createEventProcessor(
135143
event.event_id
136144
}
137145

138-
_posthog.exceptions.sendExceptionEvent(data)
146+
if (sendExceptionsToPostHog) {
147+
_posthog.exceptions.sendExceptionEvent(data)
148+
}
139149

140150
return event
141151
}
@@ -165,13 +175,20 @@ export class SentryIntegration implements _SentryIntegrationClass {
165175
organization?: string,
166176
projectId?: number,
167177
prefix?: string,
168-
severityAllowList?: SeverityLevel[] | '*'
178+
severityAllowList?: SeverityLevel[] | '*',
179+
sendExceptionsToPostHog?: boolean
169180
) {
170181
// setupOnce gets called by Sentry when it intializes the plugin
171182
this.name = NAME
172183
this.setupOnce = function (addGlobalEventProcessor: (callback: _SentryEventProcessor) => void) {
173184
addGlobalEventProcessor(
174-
createEventProcessor(_posthog, { organization, projectId, prefix, severityAllowList })
185+
createEventProcessor(_posthog, {
186+
organization,
187+
projectId,
188+
prefix,
189+
severityAllowList,
190+
sendExceptionsToPostHog: sendExceptionsToPostHog ?? true,
191+
})
175192
)
176193
}
177194
}

packages/node/src/extensions/sentry-integration.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* @param {Number} [projectId] Optional: The Sentry project id, used to send a direct link from PostHog to Sentry
2121
* @param {string} [prefix] Optional: Url of a self-hosted sentry instance (default: https://sentry.io/organizations/)
2222
* @param {SeverityLevel[] | '*'} [severityAllowList] Optional: send events matching the provided levels. Use '*' to send all events (default: ['error'])
23+
* @param {boolean} [sendExceptionsToPostHog] Optional: capture exceptions as events in PostHog (default: true)
2324
*/
2425

2526
import { SeverityLevel } from './error-tracking/types'
@@ -70,13 +71,20 @@ export type SentryIntegrationOptions = {
7071
projectId?: number
7172
prefix?: string
7273
severityAllowList?: SeverityLevel[] | '*'
74+
sendExceptionsToPostHog?: boolean
7375
}
7476

7577
const NAME = 'posthog-node'
7678

7779
export function createEventProcessor(
7880
_posthog: PostHogBackendClient,
79-
{ organization, projectId, prefix, severityAllowList = ['error'] }: SentryIntegrationOptions = {}
81+
{
82+
organization,
83+
projectId,
84+
prefix,
85+
severityAllowList = ['error'],
86+
sendExceptionsToPostHog = true,
87+
}: SentryIntegrationOptions = {}
8088
): (event: _SentryEvent) => _SentryEvent {
8189
return (event) => {
8290
const shouldProcessLevel = severityAllowList === '*' || severityAllowList.includes(event.level)
@@ -147,7 +155,9 @@ export function createEventProcessor(
147155
event.event_id
148156
}
149157

150-
_posthog.capture({ event: '$exception', distinctId: userId, properties })
158+
if (sendExceptionsToPostHog) {
159+
_posthog.capture({ event: '$exception', distinctId: userId, properties })
160+
}
151161

152162
return event
153163
}
@@ -182,7 +192,8 @@ export class PostHogSentryIntegration implements _SentryIntegrationClass {
182192
_posthog: PostHogBackendClient,
183193
organization?: string,
184194
prefix?: string,
185-
severityAllowList?: SeverityLevel[] | '*'
195+
severityAllowList?: SeverityLevel[] | '*',
196+
sendExceptionsToPostHog?: boolean
186197
) {
187198
// setupOnce gets called by Sentry when it intializes the plugin
188199
this.name = NAME
@@ -197,6 +208,7 @@ export class PostHogSentryIntegration implements _SentryIntegrationClass {
197208
projectId,
198209
prefix,
199210
severityAllowList,
211+
sendExceptionsToPostHog: sendExceptionsToPostHog ?? true,
200212
})
201213
)
202214
}

0 commit comments

Comments
 (0)