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/lib/index.d.ts CHANGED
@@ -435,40 +435,32 @@ declare class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
435
435
  * @file Adapted from [posthog-js](https://github.com/PostHog/posthog-js/blob/8157df935a4d0e71d2fefef7127aa85ee51c82d1/src/extensions/sentry-integration.ts) with modifications for the Node SDK.
436
436
  */
437
437
 
438
+ declare const severityLevels: readonly ["fatal", "error", "warning", "log", "info", "debug"];
439
+ declare type SeverityLevel = (typeof severityLevels)[number];
440
+ type _SentryEvent = any;
438
441
  type _SentryEventProcessor = any;
439
442
  type _SentryHub = any;
440
443
  interface _SentryIntegration {
444
+ name: string;
445
+ processEvent(event: _SentryEvent): _SentryEvent;
446
+ }
447
+ interface _SentryIntegrationClass {
441
448
  name: string;
442
449
  setupOnce(addGlobalEventProcessor: (callback: _SentryEventProcessor) => void, getCurrentHub: () => _SentryHub): void;
443
450
  }
444
- /**
445
- * Integrate Sentry with PostHog. This will add a direct link to the person in Sentry, and an $exception event in PostHog.
446
- *
447
- * ### Usage
448
- *
449
- * Sentry.init({
450
- * dsn: 'https://example',
451
- * integrations: [
452
- * new PostHogSentryIntegration(posthog)
453
- * ]
454
- * })
455
- *
456
- * Sentry.setTag(PostHogSentryIntegration.POSTHOG_ID_TAG, 'some distinct id');
457
- *
458
- * @param {Object} [posthog] The posthog object
459
- * @param {string} [organization] Optional: The Sentry organization, used to send a direct link from PostHog to Sentry
460
- * @param {Number} [projectId] Optional: The Sentry project id, used to send a direct link from PostHog to Sentry
461
- * @param {string} [prefix] Optional: Url of a self-hosted sentry instance (default: https://sentry.io/organizations/)
462
- */
463
- declare class PostHogSentryIntegration implements _SentryIntegration {
464
- private readonly posthog;
465
- private readonly posthogHost?;
466
- private readonly organization?;
467
- private readonly prefix?;
451
+ type SentryIntegrationOptions = {
452
+ organization?: string;
453
+ projectId?: number;
454
+ prefix?: string;
455
+ severityAllowList?: SeverityLevel[] | '*';
456
+ };
457
+ declare function createEventProcessor(_posthog: PostHog, { organization, projectId, prefix, severityAllowList }?: SentryIntegrationOptions): (event: _SentryEvent) => _SentryEvent;
458
+ declare function sentryIntegration(_posthog: PostHog, options?: SentryIntegrationOptions): _SentryIntegration;
459
+ declare class PostHogSentryIntegration implements _SentryIntegrationClass {
468
460
  readonly name = "posthog-node";
469
461
  static readonly POSTHOG_ID_TAG = "posthog_distinct_id";
470
- constructor(posthog: PostHog, posthogHost?: string | undefined, organization?: string | undefined, prefix?: string | undefined);
471
- setupOnce(addGlobalEventProcessor: (callback: _SentryEventProcessor) => void, getCurrentHub: () => _SentryHub): void;
462
+ setupOnce: (addGlobalEventProcessor: (callback: _SentryEventProcessor) => void, getCurrentHub: () => _SentryHub) => void;
463
+ constructor(_posthog: PostHog, organization?: string, prefix?: string, severityAllowList?: SeverityLevel[] | '*');
472
464
  }
473
465
 
474
- export { PostHog, PostHogOptions, PostHogSentryIntegration };
466
+ export { PostHog, PostHogOptions, PostHogSentryIntegration, SentryIntegrationOptions, SeverityLevel, createEventProcessor, sentryIntegration, severityLevels };
package/lib/index.esm.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createHash } from 'rusha';
2
2
 
3
- var version = "4.2.3";
3
+ var version = "4.3.0";
4
4
 
5
5
  var PostHogPersistedProperty;
6
6
  (function (PostHogPersistedProperty) {
@@ -2361,6 +2361,9 @@ class PostHog extends PostHogCoreStateless {
2361
2361
  }
2362
2362
  }
2363
2363
 
2364
+ /**
2365
+ * @file Adapted from [posthog-js](https://github.com/PostHog/posthog-js/blob/8157df935a4d0e71d2fefef7127aa85ee51c82d1/src/extensions/sentry-integration.ts) with modifications for the Node SDK.
2366
+ */
2364
2367
  /**
2365
2368
  * Integrate Sentry with PostHog. This will add a direct link to the person in Sentry, and an $exception event in PostHog.
2366
2369
  *
@@ -2379,58 +2382,92 @@ class PostHog extends PostHogCoreStateless {
2379
2382
  * @param {string} [organization] Optional: The Sentry organization, used to send a direct link from PostHog to Sentry
2380
2383
  * @param {Number} [projectId] Optional: The Sentry project id, used to send a direct link from PostHog to Sentry
2381
2384
  * @param {string} [prefix] Optional: Url of a self-hosted sentry instance (default: https://sentry.io/organizations/)
2385
+ * @param {SeverityLevel[] | '*'} [severityAllowList] Optional: send events matching the provided levels. Use '*' to send all events (default: ['error'])
2382
2386
  */
2383
- class PostHogSentryIntegration {
2384
- constructor(posthog, posthogHost, organization, prefix) {
2385
- this.posthog = posthog;
2386
- this.posthogHost = posthogHost;
2387
- this.organization = organization;
2388
- this.prefix = prefix;
2389
- this.name = 'posthog-node';
2390
- this.posthogHost = posthog.options.host ?? 'https://us.i.posthog.com';
2391
- }
2392
- setupOnce(addGlobalEventProcessor, getCurrentHub) {
2393
- addGlobalEventProcessor(event => {
2394
- if (event.exception?.values === undefined || event.exception.values.length === 0) {
2395
- return event;
2396
- }
2397
- if (!event.tags) {
2398
- event.tags = {};
2399
- }
2400
- const sentry = getCurrentHub();
2401
- // Get the PostHog user ID from a specific tag, which users can set on their Sentry scope as they need.
2402
- const userId = event.tags[PostHogSentryIntegration.POSTHOG_ID_TAG];
2403
- if (userId === undefined) {
2404
- // 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.
2405
- return event;
2406
- }
2407
- event.tags['PostHog Person URL'] = new URL(`/person/${userId}`, this.posthogHost).toString();
2408
- const properties = {
2409
- // PostHog Exception Properties
2410
- $exception_message: event.exception.values[0]?.value,
2411
- $exception_type: event.exception.values[0]?.type,
2412
- $exception_personURL: event.tags['PostHog Person URL'],
2413
- // Sentry Exception Properties
2414
- $sentry_event_id: event.event_id,
2415
- $sentry_exception: event.exception,
2416
- $sentry_exception_message: event.exception.values[0]?.value,
2417
- $sentry_exception_type: event.exception.values[0]?.type,
2418
- $sentry_tags: event.tags
2419
- };
2420
- const projectId = sentry.getClient()?.getDsn()?.projectId;
2421
- if (this.organization !== undefined && projectId !== undefined && event.event_id !== undefined) {
2422
- properties.$sentry_url = `${this.prefix ?? 'https://sentry.io/organizations'}/${this.organization}/issues/?project=${projectId}&query=${event.event_id}`;
2423
- }
2424
- this.posthog.capture({
2425
- event: '$exception',
2426
- distinctId: userId,
2427
- properties
2428
- });
2387
+ const severityLevels = ['fatal', 'error', 'warning', 'log', 'info', 'debug'];
2388
+ const NAME = 'posthog-node';
2389
+ function createEventProcessor(_posthog, {
2390
+ organization,
2391
+ projectId,
2392
+ prefix,
2393
+ severityAllowList = ['error']
2394
+ } = {}) {
2395
+ return event => {
2396
+ const shouldProcessLevel = severityAllowList === '*' || severityAllowList.includes(event.level);
2397
+ if (!shouldProcessLevel) {
2398
+ return event;
2399
+ }
2400
+ if (!event.tags) {
2401
+ event.tags = {};
2402
+ }
2403
+ // Get the PostHog user ID from a specific tag, which users can set on their Sentry scope as they need.
2404
+ const userId = event.tags[PostHogSentryIntegration.POSTHOG_ID_TAG];
2405
+ if (userId === undefined) {
2406
+ // 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.
2429
2407
  return event;
2408
+ }
2409
+ const uiHost = _posthog.options.host ?? 'https://us.i.posthog.com';
2410
+ const personUrl = new URL(`/project/${_posthog.apiKey}/person/${userId}`, uiHost).toString();
2411
+ event.tags['PostHog Person URL'] = personUrl;
2412
+ const exceptions = event.exception?.values || [];
2413
+ exceptions.map(exception => {
2414
+ if (exception.stacktrace) {
2415
+ exception.stacktrace.type = 'raw';
2416
+ }
2417
+ });
2418
+ const properties = {
2419
+ // PostHog Exception Properties,
2420
+ $exception_message: exceptions[0]?.value || event.message,
2421
+ $exception_type: exceptions[0]?.type,
2422
+ $exception_personURL: personUrl,
2423
+ $exception_level: event.level,
2424
+ $exception_list: exceptions,
2425
+ // Sentry Exception Properties
2426
+ $sentry_event_id: event.event_id,
2427
+ $sentry_exception: event.exception,
2428
+ $sentry_exception_message: exceptions[0]?.value || event.message,
2429
+ $sentry_exception_type: exceptions[0]?.type,
2430
+ $sentry_tags: event.tags
2431
+ };
2432
+ if (organization && projectId) {
2433
+ properties['$sentry_url'] = (prefix || 'https://sentry.io/organizations/') + organization + '/issues/?project=' + projectId + '&query=' + event.event_id;
2434
+ }
2435
+ _posthog.capture({
2436
+ event: '$exception',
2437
+ distinctId: userId,
2438
+ properties
2430
2439
  });
2440
+ return event;
2441
+ };
2442
+ }
2443
+ // V8 integration - function based
2444
+ function sentryIntegration(_posthog, options) {
2445
+ const processor = createEventProcessor(_posthog, options);
2446
+ return {
2447
+ name: NAME,
2448
+ processEvent(event) {
2449
+ return processor(event);
2450
+ }
2451
+ };
2452
+ }
2453
+ // V7 integration - class based
2454
+ class PostHogSentryIntegration {
2455
+ constructor(_posthog, organization, prefix, severityAllowList) {
2456
+ this.name = NAME;
2457
+ // setupOnce gets called by Sentry when it intializes the plugin
2458
+ this.name = NAME;
2459
+ this.setupOnce = function (addGlobalEventProcessor, getCurrentHub) {
2460
+ const projectId = getCurrentHub()?.getClient()?.getDsn()?.projectId;
2461
+ addGlobalEventProcessor(createEventProcessor(_posthog, {
2462
+ organization,
2463
+ projectId,
2464
+ prefix,
2465
+ severityAllowList
2466
+ }));
2467
+ };
2431
2468
  }
2432
2469
  }
2433
2470
  PostHogSentryIntegration.POSTHOG_ID_TAG = 'posthog_distinct_id';
2434
2471
 
2435
- export { PostHog, PostHogSentryIntegration };
2472
+ export { PostHog, PostHogSentryIntegration, createEventProcessor, sentryIntegration, severityLevels };
2436
2473
  //# sourceMappingURL=index.esm.js.map