posthog-node 4.2.3 → 4.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Next
2
2
 
3
+ # 4.3.0 - 2024-11-25
4
+
5
+ 1. Add Sentry v8 support to the Sentry integration
6
+
3
7
  # 4.2.3 - 2024-11-21
4
8
 
5
9
  1. fix: identify method allows passing a $set_once object
package/lib/index.cjs.js CHANGED
@@ -4,7 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var rusha = require('rusha');
6
6
 
7
- var version = "4.2.3";
7
+ var version = "4.3.0";
8
8
 
9
9
  var PostHogPersistedProperty;
10
10
  (function (PostHogPersistedProperty) {
@@ -2365,6 +2365,9 @@ class PostHog extends PostHogCoreStateless {
2365
2365
  }
2366
2366
  }
2367
2367
 
2368
+ /**
2369
+ * @file Adapted from [posthog-js](https://github.com/PostHog/posthog-js/blob/8157df935a4d0e71d2fefef7127aa85ee51c82d1/src/extensions/sentry-integration.ts) with modifications for the Node SDK.
2370
+ */
2368
2371
  /**
2369
2372
  * Integrate Sentry with PostHog. This will add a direct link to the person in Sentry, and an $exception event in PostHog.
2370
2373
  *
@@ -2383,59 +2386,96 @@ class PostHog extends PostHogCoreStateless {
2383
2386
  * @param {string} [organization] Optional: The Sentry organization, used to send a direct link from PostHog to Sentry
2384
2387
  * @param {Number} [projectId] Optional: The Sentry project id, used to send a direct link from PostHog to Sentry
2385
2388
  * @param {string} [prefix] Optional: Url of a self-hosted sentry instance (default: https://sentry.io/organizations/)
2389
+ * @param {SeverityLevel[] | '*'} [severityAllowList] Optional: send events matching the provided levels. Use '*' to send all events (default: ['error'])
2386
2390
  */
2387
- class PostHogSentryIntegration {
2388
- constructor(posthog, posthogHost, organization, prefix) {
2389
- this.posthog = posthog;
2390
- this.posthogHost = posthogHost;
2391
- this.organization = organization;
2392
- this.prefix = prefix;
2393
- this.name = 'posthog-node';
2394
- this.posthogHost = posthog.options.host ?? 'https://us.i.posthog.com';
2395
- }
2396
- setupOnce(addGlobalEventProcessor, getCurrentHub) {
2397
- addGlobalEventProcessor(event => {
2398
- if (event.exception?.values === undefined || event.exception.values.length === 0) {
2399
- return event;
2400
- }
2401
- if (!event.tags) {
2402
- event.tags = {};
2403
- }
2404
- const sentry = getCurrentHub();
2405
- // Get the PostHog user ID from a specific tag, which users can set on their Sentry scope as they need.
2406
- const userId = event.tags[PostHogSentryIntegration.POSTHOG_ID_TAG];
2407
- if (userId === undefined) {
2408
- // 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.
2409
- return event;
2410
- }
2411
- event.tags['PostHog Person URL'] = new URL(`/person/${userId}`, this.posthogHost).toString();
2412
- const properties = {
2413
- // PostHog Exception Properties
2414
- $exception_message: event.exception.values[0]?.value,
2415
- $exception_type: event.exception.values[0]?.type,
2416
- $exception_personURL: event.tags['PostHog Person URL'],
2417
- // Sentry Exception Properties
2418
- $sentry_event_id: event.event_id,
2419
- $sentry_exception: event.exception,
2420
- $sentry_exception_message: event.exception.values[0]?.value,
2421
- $sentry_exception_type: event.exception.values[0]?.type,
2422
- $sentry_tags: event.tags
2423
- };
2424
- const projectId = sentry.getClient()?.getDsn()?.projectId;
2425
- if (this.organization !== undefined && projectId !== undefined && event.event_id !== undefined) {
2426
- properties.$sentry_url = `${this.prefix ?? 'https://sentry.io/organizations'}/${this.organization}/issues/?project=${projectId}&query=${event.event_id}`;
2427
- }
2428
- this.posthog.capture({
2429
- event: '$exception',
2430
- distinctId: userId,
2431
- properties
2432
- });
2391
+ const severityLevels = ['fatal', 'error', 'warning', 'log', 'info', 'debug'];
2392
+ const NAME = 'posthog-node';
2393
+ function createEventProcessor(_posthog, {
2394
+ organization,
2395
+ projectId,
2396
+ prefix,
2397
+ severityAllowList = ['error']
2398
+ } = {}) {
2399
+ return event => {
2400
+ const shouldProcessLevel = severityAllowList === '*' || severityAllowList.includes(event.level);
2401
+ if (!shouldProcessLevel) {
2402
+ return event;
2403
+ }
2404
+ if (!event.tags) {
2405
+ event.tags = {};
2406
+ }
2407
+ // Get the PostHog user ID from a specific tag, which users can set on their Sentry scope as they need.
2408
+ const userId = event.tags[PostHogSentryIntegration.POSTHOG_ID_TAG];
2409
+ if (userId === undefined) {
2410
+ // 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.
2433
2411
  return event;
2412
+ }
2413
+ const uiHost = _posthog.options.host ?? 'https://us.i.posthog.com';
2414
+ const personUrl = new URL(`/project/${_posthog.apiKey}/person/${userId}`, uiHost).toString();
2415
+ event.tags['PostHog Person URL'] = personUrl;
2416
+ const exceptions = event.exception?.values || [];
2417
+ exceptions.map(exception => {
2418
+ if (exception.stacktrace) {
2419
+ exception.stacktrace.type = 'raw';
2420
+ }
2421
+ });
2422
+ const properties = {
2423
+ // PostHog Exception Properties,
2424
+ $exception_message: exceptions[0]?.value || event.message,
2425
+ $exception_type: exceptions[0]?.type,
2426
+ $exception_personURL: personUrl,
2427
+ $exception_level: event.level,
2428
+ $exception_list: exceptions,
2429
+ // Sentry Exception Properties
2430
+ $sentry_event_id: event.event_id,
2431
+ $sentry_exception: event.exception,
2432
+ $sentry_exception_message: exceptions[0]?.value || event.message,
2433
+ $sentry_exception_type: exceptions[0]?.type,
2434
+ $sentry_tags: event.tags
2435
+ };
2436
+ if (organization && projectId) {
2437
+ properties['$sentry_url'] = (prefix || 'https://sentry.io/organizations/') + organization + '/issues/?project=' + projectId + '&query=' + event.event_id;
2438
+ }
2439
+ _posthog.capture({
2440
+ event: '$exception',
2441
+ distinctId: userId,
2442
+ properties
2434
2443
  });
2444
+ return event;
2445
+ };
2446
+ }
2447
+ // V8 integration - function based
2448
+ function sentryIntegration(_posthog, options) {
2449
+ const processor = createEventProcessor(_posthog, options);
2450
+ return {
2451
+ name: NAME,
2452
+ processEvent(event) {
2453
+ return processor(event);
2454
+ }
2455
+ };
2456
+ }
2457
+ // V7 integration - class based
2458
+ class PostHogSentryIntegration {
2459
+ constructor(_posthog, organization, prefix, severityAllowList) {
2460
+ this.name = NAME;
2461
+ // setupOnce gets called by Sentry when it intializes the plugin
2462
+ this.name = NAME;
2463
+ this.setupOnce = function (addGlobalEventProcessor, getCurrentHub) {
2464
+ const projectId = getCurrentHub()?.getClient()?.getDsn()?.projectId;
2465
+ addGlobalEventProcessor(createEventProcessor(_posthog, {
2466
+ organization,
2467
+ projectId,
2468
+ prefix,
2469
+ severityAllowList
2470
+ }));
2471
+ };
2435
2472
  }
2436
2473
  }
2437
2474
  PostHogSentryIntegration.POSTHOG_ID_TAG = 'posthog_distinct_id';
2438
2475
 
2439
2476
  exports.PostHog = PostHog;
2440
2477
  exports.PostHogSentryIntegration = PostHogSentryIntegration;
2478
+ exports.createEventProcessor = createEventProcessor;
2479
+ exports.sentryIntegration = sentryIntegration;
2480
+ exports.severityLevels = severityLevels;
2441
2481
  //# sourceMappingURL=index.cjs.js.map