posthog-node 6.2.0 → 6.2.2

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.
Files changed (49) hide show
  1. package/dist/client.d.ts +252 -125
  2. package/dist/client.d.ts.map +1 -1
  3. package/dist/client.js +35 -6
  4. package/dist/client.mjs +35 -6
  5. package/dist/entrypoints/otel.d.ts +20 -0
  6. package/dist/entrypoints/otel.d.ts.map +1 -1
  7. package/dist/exports.d.ts +2 -1
  8. package/dist/exports.d.ts.map +1 -1
  9. package/dist/extensions/context/listeners.d.ts +3 -2
  10. package/dist/extensions/context/listeners.d.ts.map +1 -1
  11. package/dist/extensions/context/types.d.ts +2 -0
  12. package/dist/extensions/context/types.d.ts.map +1 -1
  13. package/dist/extensions/express.d.ts +30 -4
  14. package/dist/extensions/express.d.ts.map +1 -1
  15. package/dist/extensions/feature-flags/feature-flags-v2.d.ts +254 -0
  16. package/dist/extensions/feature-flags/feature-flags-v2.d.ts.map +1 -0
  17. package/dist/extensions/feature-flags/feature-flags-v2.js +1408 -0
  18. package/dist/extensions/feature-flags/feature-flags-v2.mjs +1331 -0
  19. package/dist/extensions/feature-flags/feature-flags.d.ts +15 -4
  20. package/dist/extensions/feature-flags/feature-flags.d.ts.map +1 -1
  21. package/dist/extensions/feature-flags/feature-flags.js +55 -7
  22. package/dist/extensions/feature-flags/feature-flags.mjs +55 -7
  23. package/dist/extensions/nestjs.d.ts +23 -0
  24. package/dist/extensions/nestjs.d.ts.map +1 -1
  25. package/dist/feature-flag-evaluations.d.ts +8 -6
  26. package/dist/feature-flag-evaluations.d.ts.map +1 -1
  27. package/dist/flag-events.d.ts +2 -2
  28. package/dist/flag-events.d.ts.map +1 -1
  29. package/dist/flag-events.js +2 -1
  30. package/dist/flag-events.mjs +2 -1
  31. package/dist/types.d.ts +120 -95
  32. package/dist/types.d.ts.map +1 -1
  33. package/dist/version.d.ts +1 -1
  34. package/dist/version.js +1 -1
  35. package/dist/version.mjs +1 -1
  36. package/package.json +4 -4
  37. package/src/client.ts +293 -136
  38. package/src/entrypoints/otel.ts +20 -0
  39. package/src/exports.ts +2 -1
  40. package/src/extensions/context/listeners.ts +3 -2
  41. package/src/extensions/context/types.ts +2 -0
  42. package/src/extensions/express.ts +30 -4
  43. package/src/extensions/feature-flags/feature-flags-v2.ts +2086 -0
  44. package/src/extensions/feature-flags/feature-flags.ts +110 -20
  45. package/src/extensions/nestjs.ts +23 -0
  46. package/src/feature-flag-evaluations.ts +8 -6
  47. package/src/flag-events.ts +3 -2
  48. package/src/types.ts +125 -104
  49. package/src/version.ts +1 -1
@@ -10,12 +10,32 @@ function hasReadableAttributes(span: Span | undefined): span is ReadableSpan {
10
10
  return !!span && 'attributes' in span && typeof span.attributes === 'object' && span.attributes !== null
11
11
  }
12
12
 
13
+ /**
14
+ * OpenTelemetry `SpanProcessor` that stamps `posthogDistinctId` and `sessionId` from the active PostHog context onto recording spans and the nearest enclosing `SERVER` span.
15
+ *
16
+ * Requires `@opentelemetry/api` (`^1.2.0`) as an optional peer dependency. Skips spans when the client is disabled or opted out.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * import { BasicTracerProvider } from '@opentelemetry/sdk-trace-base'
21
+ * import { PostHog } from 'posthog-node'
22
+ * import { PostHogContextSpanProcessor } from 'posthog-node/otel'
23
+ *
24
+ * const posthog = new PostHog('<ph_project_api_key>', { host: 'https://us.i.posthog.com' })
25
+ * const provider = new BasicTracerProvider({ spanProcessors: [new PostHogContextSpanProcessor(posthog)] })
26
+ * ```
27
+ */
13
28
  export class PostHogContextSpanProcessor {
14
29
  private active = true
15
30
  private readonly snapshots = new WeakSet<Span>()
16
31
  private readonly parents = new WeakMap<Span, WeakRef<Span>>()
17
32
  private readonly unsubscribe: () => void
18
33
 
34
+ /**
35
+ * Create a span processor bound to one PostHog client instance.
36
+ *
37
+ * @param posthog - Client whose active request context populates span attributes
38
+ */
19
39
  constructor(private readonly posthog: PostHogBackendClient) {
20
40
  this.unsubscribe = addContextListener(posthog, (requestEntry) => {
21
41
  const span = trace.getActiveSpan()
package/src/exports.ts CHANGED
@@ -44,7 +44,8 @@ export type {
44
44
  // Re-export FeatureFlagError from core for backwards compatibility.
45
45
  // These were originally defined in posthog-node and moved to core for reuse across SDKs.
46
46
  export { FeatureFlagError } from '@posthog/core'
47
- export type { FeatureFlagErrorType, UnresolvedFlagReason } from '@posthog/core'
47
+ export type { FeatureFlagErrorType } from '@posthog/core'
48
+ export type { UnresolvedFlagReason } from './types'
48
49
 
49
50
  // Metrics types re-exported so consumers can name the `metrics` client option
50
51
  // and API surface without a direct @posthog/core dependency.
@@ -28,8 +28,9 @@ export function notifyContextListeners(client: PostHogBackendClient, requestEntr
28
28
  }
29
29
 
30
30
  /**
31
- * Notify installed span processors after request IDs become available.
32
- * This entry point does not load OpenTelemetry; without a processor it does nothing.
31
+ * Notify installed `PostHogContextSpanProcessor` instances after request IDs become available.
32
+ *
33
+ * This helper doesn't load OpenTelemetry, so it's a no-op unless you register `PostHogContextSpanProcessor` from `posthog-node/otel`.
33
34
  */
34
35
  export function enrichCurrentSpan(client: PostHogBackendClient): void {
35
36
  notifyContextListeners(client, true)
@@ -1,7 +1,9 @@
1
1
  import type { EventOptions } from '../../types'
2
2
 
3
3
  export interface ContextData {
4
+ /** Distinct ID applied to events, flag evaluations, logs, and spans inside the context when not passed directly. */
4
5
  distinctId?: string
6
+ /** Session ID (UUIDv7) attached as `$session_id` on events and `sessionId` on logs and spans inside the context. */
5
7
  sessionId?: string
6
8
  /** Properties that fill the keys an event left unset. The event's own values win. */
7
9
  properties?: Record<string, any>
@@ -65,9 +65,30 @@ function buildRequestContextData(posthog: PostHogBackendClient, req: Request): P
65
65
  }
66
66
 
67
67
  /**
68
- * Adds request context to events captured during an Express request. Call it before your routes.
69
- * Events then include `$current_url`, `$request_method`, `$request_path`, `$user_agent`, and `$ip`.
70
- * `$ip` is the first `x-forwarded-for` value when the request has that header, otherwise the socket's remote address.
68
+ * Register Express middleware that binds per-request PostHog context before your routes run.
69
+ *
70
+ * Reads `x-posthog-session-id` and `x-posthog-distinct-id` headers (or the `posthog-js` cookie when `readPostHogCookie` is enabled) and attaches `$current_url`, `$request_method`, `$request_path`, `$user_agent`, and `$ip` (first `x-forwarded-for` entry or `req.socket.remoteAddress`) to events in that request.
71
+ *
72
+ * Tracing headers are untrusted client data, never authentication, so pass an authenticated `distinctId` explicitly for authorization or sensitive decisions. Client IP geolocation from `$ip` also requires `disableGeoip: false`.
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * import express from 'express'
77
+ * import { PostHog, setupExpressErrorHandler, setupExpressRequestContext } from 'posthog-node'
78
+ *
79
+ * const app = express()
80
+ * const client = new PostHog('<ph_project_api_key>', { host: 'https://us.i.posthog.com' })
81
+ *
82
+ * setupExpressRequestContext(client, app)
83
+ * app.get('/', (_req, res) => {
84
+ * client.capture({ event: 'page_loaded' })
85
+ * res.send('ok')
86
+ * })
87
+ * setupExpressErrorHandler(client, app)
88
+ * ```
89
+ *
90
+ * @param _posthog - PostHog client instance
91
+ * @param app - Express app or router with `.use()`
71
92
  */
72
93
  export function setupExpressRequestContext(
73
94
  _posthog: PostHogBackendClient,
@@ -88,7 +109,12 @@ function posthogRequestContext(posthog: PostHogBackendClient): ExpressMiddleware
88
109
  }
89
110
 
90
111
  /**
91
- * Captures errors that Express passes to its error middleware. Call it after your routes.
112
+ * Register Express error-handling middleware after your routes to capture unhandled exceptions.
113
+ *
114
+ * Attaches request context, `$response_status_code`, and `$session_id`, skips errors already captured by process autocapture, and forwards each error to `next(error)`.
115
+ *
116
+ * @param _posthog - PostHog client instance
117
+ * @param app - Express app with `.use()`
92
118
  */
93
119
  export function setupExpressErrorHandler(
94
120
  _posthog: PostHogBackendClient,