posthog-js icon indicating copy to clipboard operation
posthog-js copied to clipboard

Type 'SentryIntegration' is not assignable to type 'Integration'

Open mozeryansky opened this issue 1 year ago • 1 comments

Following the instructions from here: https://posthog.com/docs/libraries/sentry#installation

The SentryIntegration class is not type compatible. Force casting throws causing an error from sentry for the same reason.

Type 'SentryIntegration' is not assignable to type 'Integration'.
  Types of property 'setupOnce' are incompatible.
    Type '(addGlobalEventProcessor: (callback: any) => void, getCurrentHub: () => any) => void' is not assignable to type '() => void'.
      Target signature provides too few arguments. Expected 2 or more, but got 0.ts(2322)

Turns out Sentry recently changed the signature: https://github.com/getsentry/sentry-javascript/pull/11238

mozeryansky avatar May 25 '24 23:05 mozeryansky

I am able to get it to work by doing this after Sentry.init:

const postHogIntegration = new posthog.SentryIntegration(
    posthog,
    'your organization',
    project-id,
    undefined, // optional: but necessary if you want to set a severity allowlist
    ['error', 'info'] // optional: here is set to handle captureMessage (info) and captureException (error)
)

postHogIntegration.setupOnce(Sentry.addEventProcessor, () => {})

Essentially passing the handler inside setupOnce to the addEventProcessor.

However, another issue is that the urls being generated are incorrect. The config token here is the API key, but Sentry uses a the project ID (i.e. 80397).

const personUrl = _posthog.requestRouter.endpointFor(
    'ui',
    `/project/${_posthog.config.token}/person/${_posthog.get_distinct_id()}` // <--- wrong url
)

mozeryansky avatar May 25 '24 23:05 mozeryansky

@benjackwhite Does your PR fix this?

mozeryansky avatar Dec 12 '24 07:12 mozeryansky

@benjackwhite also wanted to follow up - ty :)

pellicceama avatar Dec 31 '24 11:12 pellicceama

It should, and how you use it is to replace new SentryIntegration... with sentryIntegration() FYI. based on the discussion there the "new" v8 compatible version is a function that is camelCased whereas the "old" v7 version is a class that is "PascalCased"

osdiab avatar Jan 08 '25 06:01 osdiab