Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!(node): support sentry>=8 #243

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions posthog-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@
},
"devDependencies": {
"@types/node": "^18.0.0",
"@sentry/types": "^8.17.0",
"commander": "^9.3.0"
},
"peerDependencies": {
"@sentry/types": ">=8"
},
songkeys marked this conversation as resolved.
Show resolved Hide resolved
"keywords": [
"posthog",
"stats",
Expand Down
43 changes: 10 additions & 33 deletions posthog-node/src/extensions/sentry-integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,12 @@
*/
import { type PostHog } from '../posthog-node'

// 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

// import {
// Event as _SentryEvent,
// EventProcessor as _SentryEventProcessor,
// Exception as _SentryException,
// Hub as _SentryHub,
// Integration as _SentryIntegration,
// Primitive as _SentryPrimitive,
// } from '@sentry/types'

// Uncomment the above and comment the below to get type checking for development

type _SentryEvent = any
type _SentryEventProcessor = any
type _SentryHub = any
type _SentryException = any
type _SentryPrimitive = any

interface _SentryIntegration {
name: string
setupOnce(addGlobalEventProcessor: (callback: _SentryEventProcessor) => void, getCurrentHub: () => _SentryHub): void
}
import {
type Exception as _SentryException,
type Integration as _SentryIntegration,
type Primitive as _SentryPrimitive,
type Client,
} from '@sentry/types'

interface PostHogSentryExceptionProperties {
$sentry_event_id?: string
Expand Down Expand Up @@ -73,11 +55,8 @@ export class PostHogSentryIntegration implements _SentryIntegration {
this.posthogHost = posthog.options.host ?? 'https://us.i.posthog.com'
}

public setupOnce(
addGlobalEventProcessor: (callback: _SentryEventProcessor) => void,
getCurrentHub: () => _SentryHub
): void {
addGlobalEventProcessor((event: _SentryEvent): _SentryEvent => {
setup(client: Client): void {
client.addEventProcessor((event) => {
if (event.exception?.values === undefined || event.exception.values.length === 0) {
return event
}
Expand All @@ -86,10 +65,8 @@ export class PostHogSentryIntegration implements _SentryIntegration {
event.tags = {}
}

const sentry = getCurrentHub()

// Get the PostHog user ID from a specific tag, which users can set on their Sentry scope as they need.
const userId = event.tags[PostHogSentryIntegration.POSTHOG_ID_TAG]
const userId = event.tags[PostHogSentryIntegration.POSTHOG_ID_TAG]?.toString()
if (userId === undefined) {
// If we can't find a user ID, don't bother linking the event. We won't be able to send anything meaningful to PostHog without it.
return event
Expand All @@ -110,7 +87,7 @@ export class PostHogSentryIntegration implements _SentryIntegration {
$sentry_tags: event.tags,
}

const projectId = sentry.getClient()?.getDsn()?.projectId
const projectId = client.getDsn()?.projectId
if (this.organization !== undefined && projectId !== undefined && event.event_id !== undefined) {
properties.$sentry_url = `${this.prefix ?? 'https://sentry.io/organizations'}/${
this.organization
Expand Down
18 changes: 8 additions & 10 deletions posthog-node/test/extensions/sentry-integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,18 @@ describe('PostHogSentryIntegration', () => {
it('should forward sentry exceptions to posthog', async () => {
expect(mockedFetch).toHaveBeenCalledTimes(0)

let processorFunction: any
const mockSentry = {
getClient: () => ({
getDsn: () => ({
projectId: 123,
}),
getDsn: () => ({
projectId: 123,
}),
addEventProcessor: (fn: any) => {
processorFunction = fn
},
}

let processorFunction: any

posthogSentry.setupOnce(
(fn) => (processorFunction = fn),
() => mockSentry
)
// @ts-expect-error - we're mocking the Sentry integration
posthogSentry.setup(mockSentry)

processorFunction(createMockSentryException())

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2627,6 +2627,11 @@
component-type "^1.2.1"
join-component "^1.1.0"

"@sentry/types@^8.17.0":
version "8.17.0"
resolved "https://registry.yarnpkg.com/@sentry/types/-/types-8.17.0.tgz#0100b5decb2bac55a6c6a231de1db2c79bad7f89"
integrity sha512-v0nI0+ajiGTijhF1W/ryn2+zFVFr6VPn6lao3W4qKj9MlltIHa4/uuGzTaiCFwoPw7g5bZ1Q09SStpDXVMkz2A==

"@sideway/address@^4.1.3":
version "4.1.4"
resolved "https://registry.npmjs.org/@sideway/address/-/address-4.1.4.tgz"
Expand Down