Skip to content

Commit

Permalink
feat: capture more sentry levels (#1104)
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra authored Apr 4, 2024
1 parent ba223d4 commit e804970
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/extensions/sentry-integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { PostHog } from '../posthog-core'
// EventProcessor as _SentryEventProcessor,
// Hub as _SentryHub,
// Integration as _SentryIntegration,
// SeverityLevel as _SeverityLevel,
// } from '@sentry/types'

// Uncomment the above and comment the below to get type checking for development
Expand All @@ -39,6 +40,11 @@ interface _SentryIntegration {
setupOnce(addGlobalEventProcessor: (callback: _SentryEventProcessor) => void, getCurrentHub: () => _SentryHub): void
}

// levels copied from Sentry to avoid relying on a frequently changing @sentry/types dependency
// but provided as an array of literal types, so we can constrain the level below
const severityLevels = ['fatal', 'error', 'warning', 'log', 'info', 'debug'] as const
declare type _SeverityLevel = typeof severityLevels[number]

interface SentryExceptionProperties {
$sentry_event_id: any
$sentry_exception: any
Expand All @@ -56,12 +62,27 @@ export class SentryIntegration implements _SentryIntegration {
getCurrentHub: () => _SentryHub
) => void

constructor(_posthog: PostHog, organization?: string, projectId?: number, prefix?: string) {
constructor(
_posthog: PostHog,
organization?: string,
projectId?: number,
prefix?: string,
/**
* By default, only errors are sent to PostHog. You can set this to '*' to send all events.
* Or to an error of SeverityLevel to only send events matching the provided levels.
* e.g. ['error', 'fatal'] to send only errors and fatals
* e.g. ['error'] to send only errors -- the default when omitted
* e.g. '*' to send all events
*/
severityAllowList: _SeverityLevel[] | '*' = ['error']
) {
// setupOnce gets called by Sentry when it intializes the plugin
this.name = 'posthog-js'
this.setupOnce = function (addGlobalEventProcessor: (callback: _SentryEventProcessor) => void) {
addGlobalEventProcessor((event: _SentryEvent) => {
if (event.level !== 'error' || !_posthog.__loaded) return event
const shouldProcessLevel =
severityAllowList === '*' || severityAllowList.includes(event.level as _SeverityLevel)
if (!shouldProcessLevel || !_posthog.__loaded) return event
if (!event.tags) event.tags = {}

const personUrl = _posthog.requestRouter.endpointFor('ui', '/person/' + _posthog.get_distinct_id())
Expand All @@ -78,17 +99,19 @@ export class SentryIntegration implements _SentryIntegration {
$exception_message: any
$exception_type: any
$exception_personURL: string
$level: _SeverityLevel
} = {
// PostHog Exception Properties,
$exception_message: exceptions[0]?.value,
$exception_message: exceptions[0]?.value || event.message,
$exception_type: exceptions[0]?.type,
$exception_personURL: personUrl,
// Sentry Exception Properties
$sentry_event_id: event.event_id,
$sentry_exception: event.exception,
$sentry_exception_message: exceptions[0]?.value,
$sentry_exception_message: exceptions[0]?.value || event.message,
$sentry_exception_type: exceptions[0]?.type,
$sentry_tags: event.tags,
$level: event.level,
}

if (organization && projectId)
Expand Down

0 comments on commit e804970

Please sign in to comment.