posthog-node 5.74.0 → 6.0.1
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/MIGRATION.md +556 -0
- package/README.md +3 -1
- package/dist/ai-capture/routing.d.ts +0 -3
- package/dist/ai-capture/routing.d.ts.map +1 -1
- package/dist/ai-capture/routing.js +2 -14
- package/dist/ai-capture/routing.mjs +1 -4
- package/dist/capture-lane/lane.d.ts +109 -0
- package/dist/capture-lane/lane.d.ts.map +1 -0
- package/dist/capture-lane/lane.js +308 -0
- package/dist/capture-lane/lane.mjs +270 -0
- package/dist/capture-lane/limits.d.ts +7 -0
- package/dist/capture-lane/limits.d.ts.map +1 -0
- package/dist/{capture-v1/config.js → capture-lane/limits.js} +13 -11
- package/dist/capture-lane/limits.mjs +4 -0
- package/dist/capture-lane/settings.d.ts +30 -0
- package/dist/capture-lane/settings.d.ts.map +1 -0
- package/dist/capture-lane/settings.js +100 -0
- package/dist/capture-lane/settings.mjs +47 -0
- package/dist/capture-v1/compression.d.ts +18 -0
- package/dist/capture-v1/compression.d.ts.map +1 -0
- package/dist/capture-v1/compression.js +70 -0
- package/dist/capture-v1/compression.mjs +26 -0
- package/dist/capture-v1/errors.d.ts +23 -7
- package/dist/capture-v1/errors.d.ts.map +1 -1
- package/dist/capture-v1/errors.js +37 -8
- package/dist/capture-v1/errors.mjs +29 -6
- package/dist/capture-v1/sender.d.ts +63 -23
- package/dist/capture-v1/sender.d.ts.map +1 -1
- package/dist/capture-v1/sender.js +281 -74
- package/dist/capture-v1/sender.mjs +275 -77
- package/dist/capture-v1/serialize.d.ts +28 -0
- package/dist/capture-v1/serialize.d.ts.map +1 -0
- package/dist/capture-v1/serialize.js +82 -0
- package/dist/capture-v1/serialize.mjs +38 -0
- package/dist/capture-v1/transform.d.ts +10 -25
- package/dist/capture-v1/transform.d.ts.map +1 -1
- package/dist/capture-v1/transform.js +33 -64
- package/dist/capture-v1/transform.mjs +28 -53
- package/dist/capture-v1/types.d.ts +16 -14
- package/dist/capture-v1/types.d.ts.map +1 -1
- package/dist/capture-v1/types.js +27 -1
- package/dist/capture-v1/types.mjs +3 -1
- package/dist/capture-v1/uuid.d.ts +9 -0
- package/dist/capture-v1/uuid.d.ts.map +1 -0
- package/dist/capture-v1/uuid.js +75 -0
- package/dist/capture-v1/uuid.mjs +34 -0
- package/dist/client.d.ts +203 -96
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +456 -228
- package/dist/client.mjs +455 -227
- package/dist/compression.node.d.ts +13 -0
- package/dist/compression.node.d.ts.map +1 -0
- package/dist/{capture-v1/routing.js → compression.node.js} +23 -16
- package/dist/compression.node.mjs +18 -0
- package/dist/entrypoints/index.node.d.ts +2 -0
- package/dist/entrypoints/index.node.d.ts.map +1 -1
- package/dist/entrypoints/index.node.js +11 -4
- package/dist/entrypoints/index.node.mjs +4 -0
- package/dist/event-defaults.d.ts +46 -0
- package/dist/event-defaults.d.ts.map +1 -0
- package/dist/event-defaults.js +141 -0
- package/dist/event-defaults.mjs +79 -0
- package/dist/exports.d.ts +3 -0
- package/dist/exports.d.ts.map +1 -1
- package/dist/exports.js +22 -11
- package/dist/exports.mjs +1 -0
- package/dist/extensions/context/context.d.ts +0 -1
- package/dist/extensions/context/context.d.ts.map +1 -1
- package/dist/extensions/context/context.js +4 -3
- package/dist/extensions/context/context.mjs +4 -3
- package/dist/extensions/context/event-context.d.ts.map +1 -1
- package/dist/extensions/context/event-context.js +3 -0
- package/dist/extensions/context/event-context.mjs +3 -0
- package/dist/extensions/context/types.d.ts +9 -1
- package/dist/extensions/context/types.d.ts.map +1 -1
- package/dist/extensions/error-tracking/index.d.ts +1 -1
- package/dist/extensions/error-tracking/index.d.ts.map +1 -1
- package/dist/extensions/error-tracking/index.js +71 -7
- package/dist/extensions/error-tracking/index.mjs +75 -11
- package/dist/extensions/express.d.ts +8 -0
- package/dist/extensions/express.d.ts.map +1 -1
- package/dist/extensions/express.js +3 -3
- package/dist/extensions/express.mjs +4 -4
- package/dist/extensions/fetch-request.d.ts.map +1 -1
- package/dist/extensions/fetch-request.js +2 -2
- package/dist/extensions/fetch-request.mjs +3 -3
- package/dist/extensions/nestjs.d.ts.map +1 -1
- package/dist/extensions/nestjs.js +7 -4
- package/dist/extensions/nestjs.mjs +8 -5
- package/dist/extensions/tracing-headers.d.ts +7 -0
- package/dist/extensions/tracing-headers.d.ts.map +1 -1
- package/dist/extensions/tracing-headers.js +11 -1
- package/dist/extensions/tracing-headers.mjs +9 -2
- package/dist/flag-events.d.ts +1 -1
- package/dist/flag-events.d.ts.map +1 -1
- package/dist/flag-events.js +2 -2
- package/dist/flag-events.mjs +2 -2
- package/dist/types.d.ts +191 -27
- package/dist/types.d.ts.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +1 -1
- package/dist/version.mjs +1 -1
- package/package.json +3 -2
- package/src/ai-capture/routing.ts +0 -6
- package/src/capture-lane/lane.ts +414 -0
- package/src/capture-lane/limits.ts +8 -0
- package/src/capture-lane/settings.ts +119 -0
- package/src/capture-v1/compression.ts +50 -0
- package/src/capture-v1/errors.ts +50 -11
- package/src/capture-v1/sender.ts +405 -111
- package/src/capture-v1/serialize.ts +77 -0
- package/src/capture-v1/transform.ts +59 -92
- package/src/capture-v1/types.ts +19 -15
- package/src/capture-v1/uuid.ts +53 -0
- package/src/client.ts +847 -424
- package/src/compression.node.ts +30 -0
- package/src/entrypoints/index.node.ts +6 -0
- package/src/event-defaults.ts +177 -0
- package/src/exports.ts +3 -0
- package/src/extensions/context/context.ts +4 -4
- package/src/extensions/context/event-context.ts +3 -1
- package/src/extensions/context/types.ts +10 -1
- package/src/extensions/error-tracking/index.ts +51 -7
- package/src/extensions/express.ts +12 -3
- package/src/extensions/fetch-request.ts +7 -3
- package/src/extensions/nestjs.ts +9 -5
- package/src/extensions/tracing-headers.ts +26 -7
- package/src/flag-events.ts +5 -2
- package/src/types.ts +211 -44
- package/src/version.ts +1 -1
- package/dist/ai-capture/batching.d.ts +0 -11
- package/dist/ai-capture/batching.d.ts.map +0 -1
- package/dist/ai-capture/batching.js +0 -77
- package/dist/ai-capture/batching.mjs +0 -36
- package/dist/capture-v1/config.d.ts +0 -11
- package/dist/capture-v1/config.d.ts.map +0 -1
- package/dist/capture-v1/config.mjs +0 -8
- package/dist/capture-v1/routing.d.ts +0 -21
- package/dist/capture-v1/routing.d.ts.map +0 -1
- package/dist/capture-v1/routing.mjs +0 -7
- package/src/ai-capture/batching.ts +0 -47
- package/src/capture-v1/config.ts +0 -18
- package/src/capture-v1/routing.ts +0 -28
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import * as zlib from 'node:zlib'
|
|
2
|
+
import { promisify } from 'node:util'
|
|
3
|
+
|
|
4
|
+
import type { CaptureCodec, CaptureCompressor } from './capture-v1/compression'
|
|
5
|
+
|
|
6
|
+
type ZlibCompress = typeof zlib.gzip
|
|
7
|
+
|
|
8
|
+
/** The zlib functions the compressor uses; `zstdCompress` exists from Node 22.15. */
|
|
9
|
+
export interface ZlibCodecs {
|
|
10
|
+
gzip: ZlibCompress
|
|
11
|
+
deflate: ZlibCompress
|
|
12
|
+
zstdCompress?: ZlibCompress
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function createZlibCompressor(codecs: ZlibCodecs = zlib as ZlibCodecs): CaptureCompressor {
|
|
16
|
+
const compressors: Record<CaptureCodec, ((input: string) => Promise<Buffer>) | undefined> = {
|
|
17
|
+
gzip: promisify(codecs.gzip),
|
|
18
|
+
deflate: promisify(codecs.deflate),
|
|
19
|
+
zstd: typeof codecs.zstdCompress === 'function' ? promisify(codecs.zstdCompress) : undefined,
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
supports: (codec) => compressors[codec] !== undefined,
|
|
23
|
+
async compress(payload, codec) {
|
|
24
|
+
const compress = compressors[codec]
|
|
25
|
+
return compress ? new Uint8Array(await compress(payload)) : null
|
|
26
|
+
},
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const zlibCompressor = createZlibCompressor()
|
|
@@ -10,6 +10,8 @@ import { ErrorTracking as CoreErrorTracking } from '@posthog/core'
|
|
|
10
10
|
import type { SpanContextManager } from '@posthog/core'
|
|
11
11
|
import { PostHogContext } from '../extensions/context/context'
|
|
12
12
|
import { AsyncLocalStorageSpanContextManager } from '../extensions/context/span-context.node'
|
|
13
|
+
import type { CaptureCompressor } from '../capture-v1/compression'
|
|
14
|
+
import { zlibCompressor } from '../compression.node'
|
|
13
15
|
import { gzipCompress } from '../gzip.node'
|
|
14
16
|
import { hostOsResourceAttributes } from '../host-os.node'
|
|
15
17
|
|
|
@@ -22,6 +24,10 @@ export class PostHog extends PostHogBackendClient {
|
|
|
22
24
|
return gzipCompress(payload, this.isDebug)
|
|
23
25
|
}
|
|
24
26
|
|
|
27
|
+
protected override captureCompressor(): CaptureCompressor {
|
|
28
|
+
return zlibCompressor
|
|
29
|
+
}
|
|
30
|
+
|
|
25
31
|
protected initializeContext(): PostHogContext {
|
|
26
32
|
return new PostHogContext()
|
|
27
33
|
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EVENT_OPTION_LEGACY_PROPERTIES,
|
|
3
|
+
isPlainObject,
|
|
4
|
+
type JsonType,
|
|
5
|
+
type PostHogEventProperties,
|
|
6
|
+
} from '@posthog/core'
|
|
7
|
+
|
|
8
|
+
/** Properties that a default fills one level deep when the event's value and the default are both objects. */
|
|
9
|
+
const NESTED_FILL_PROPERTIES: ReadonlySet<string> = new Set(['$set', '$set_once', '$groups', '$group_set'])
|
|
10
|
+
|
|
11
|
+
/** Properties that are set only when they hold a non-empty string. */
|
|
12
|
+
const LIBRARY_PROPERTIES: ReadonlySet<string> = new Set(['$lib', '$lib_version'])
|
|
13
|
+
|
|
14
|
+
/** One layer of default values, or nothing. */
|
|
15
|
+
export type DefaultsLayer = Readonly<Record<string, unknown>> | undefined
|
|
16
|
+
|
|
17
|
+
function isPresentString(value: unknown): value is string {
|
|
18
|
+
return typeof value === 'string' && value.length > 0
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function isUnsetValue(key: string, value: unknown): boolean {
|
|
22
|
+
return LIBRARY_PROPERTIES.has(key) ? !isPresentString(value) : value === undefined
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A property is unset when its key is missing or its value is `undefined`; `null` is a value.
|
|
27
|
+
* `$lib` and `$lib_version` are unset unless they hold a non-empty string.
|
|
28
|
+
*/
|
|
29
|
+
export function isUnsetProperty(properties: PostHogEventProperties, key: string): boolean {
|
|
30
|
+
return isUnsetValue(key, properties[key])
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Fill the SDK's own `$lib` and `$lib_version`, after every other layer. When no layer set
|
|
35
|
+
* `$lib`, both get the SDK's values, replacing a lone `$lib_version`. When `$lib` is the SDK's
|
|
36
|
+
* own name, only a missing `$lib_version` is filled. Any other `$lib` keeps the pair as set.
|
|
37
|
+
*/
|
|
38
|
+
export function fillLibraryProperties(
|
|
39
|
+
properties: PostHogEventProperties,
|
|
40
|
+
libraryId: string,
|
|
41
|
+
libraryVersion: string
|
|
42
|
+
): void {
|
|
43
|
+
if (isUnsetProperty(properties, '$lib')) {
|
|
44
|
+
properties.$lib = libraryId
|
|
45
|
+
properties.$lib_version = libraryVersion
|
|
46
|
+
} else if (properties.$lib === libraryId && isUnsetProperty(properties, '$lib_version')) {
|
|
47
|
+
properties.$lib_version = libraryVersion
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** An option is unset when its key is missing or its value is `undefined` or `null`. */
|
|
52
|
+
export function isUnsetOption(options: Record<string, unknown>, key: string): boolean {
|
|
53
|
+
return options[key] === undefined || options[key] === null
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Fill the properties the event left unset from each layer in turn, highest layer first, so a
|
|
58
|
+
* value from an earlier layer is never replaced. A layer's `$lib` or `$lib_version` counts only
|
|
59
|
+
* when it is a non-empty string. `$set`, `$set_once`, `$groups` and `$group_set`
|
|
60
|
+
* fill one level deep when both values are objects without their own `toJSON`. Keys in `skipKeys` are never filled.
|
|
61
|
+
*/
|
|
62
|
+
export function fillProperties(
|
|
63
|
+
properties: PostHogEventProperties,
|
|
64
|
+
layers: readonly DefaultsLayer[],
|
|
65
|
+
skipKeys: readonly string[] = []
|
|
66
|
+
): void {
|
|
67
|
+
for (const layer of layers) {
|
|
68
|
+
if (!layer) {
|
|
69
|
+
continue
|
|
70
|
+
}
|
|
71
|
+
for (const [key, value] of Object.entries(layer)) {
|
|
72
|
+
if (isUnsetValue(key, value) || skipKeys.includes(key)) {
|
|
73
|
+
continue
|
|
74
|
+
}
|
|
75
|
+
const existing = properties[key]
|
|
76
|
+
if (isUnsetValue(key, existing)) {
|
|
77
|
+
properties[key] = value as JsonType
|
|
78
|
+
} else if (NESTED_FILL_PROPERTIES.has(key) && isMergeableObject(existing) && isMergeableObject(value)) {
|
|
79
|
+
properties[key] = fillNested(existing, value)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Fill the options the event left unset from each layer in turn, highest layer first. */
|
|
86
|
+
export function fillOptions(options: Record<string, JsonType>, layers: readonly DefaultsLayer[]): void {
|
|
87
|
+
for (const layer of layers) {
|
|
88
|
+
if (!layer) {
|
|
89
|
+
continue
|
|
90
|
+
}
|
|
91
|
+
for (const [key, value] of Object.entries(layer)) {
|
|
92
|
+
if (value !== undefined && isUnsetOption(options, key)) {
|
|
93
|
+
options[key] = value as JsonType
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Events that create or change a person or group, so a personless default would lose them. */
|
|
100
|
+
const IDENTIFY_STYLE_EVENTS: ReadonlySet<string> = new Set([
|
|
101
|
+
'$identify',
|
|
102
|
+
'$create_alias',
|
|
103
|
+
'$merge_dangerously',
|
|
104
|
+
'$groupidentify',
|
|
105
|
+
'$set',
|
|
106
|
+
'$set_once',
|
|
107
|
+
])
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Set `process_person_profile` on an identify-style event from the call alone, so no layered
|
|
111
|
+
* `false` makes it personless: the call's option, else its legacy `$process_person_profile`, else
|
|
112
|
+
* `true` unless the distinct id is SDK-generated. Read `callerLegacyValue` before any layer fills
|
|
113
|
+
* properties, since a filled value looks like the caller's.
|
|
114
|
+
*/
|
|
115
|
+
export function fillIdentifyStylePersonProfile(
|
|
116
|
+
event: string,
|
|
117
|
+
options: Record<string, JsonType>,
|
|
118
|
+
callerLegacyValue: JsonType | undefined,
|
|
119
|
+
hasDistinctId: boolean
|
|
120
|
+
): void {
|
|
121
|
+
if (!IDENTIFY_STYLE_EVENTS.has(event) || !isUnsetOption(options, 'process_person_profile')) {
|
|
122
|
+
return
|
|
123
|
+
}
|
|
124
|
+
if (callerLegacyValue !== undefined && callerLegacyValue !== null) {
|
|
125
|
+
options.process_person_profile = callerLegacyValue
|
|
126
|
+
} else if (hasDistinctId) {
|
|
127
|
+
options.process_person_profile = true
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Remove `$process_person_profile` from an identify-style event whose option is set, after every
|
|
133
|
+
* fill. The option already wins when the event is sent, and a `beforeSend` hook that drops
|
|
134
|
+
* `options` must not bring back a layered `false`.
|
|
135
|
+
*/
|
|
136
|
+
export function dropIdentifyStyleLegacyPersonProfile(
|
|
137
|
+
event: string,
|
|
138
|
+
options: Record<string, JsonType>,
|
|
139
|
+
properties: PostHogEventProperties
|
|
140
|
+
): void {
|
|
141
|
+
if (IDENTIFY_STYLE_EVENTS.has(event) && !isUnsetOption(options, 'process_person_profile')) {
|
|
142
|
+
delete properties[EVENT_OPTION_LEGACY_PROPERTIES.process_person_profile]
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* An object whose inner keys can be filled. A value with its own `toJSON` is sent as it encodes
|
|
148
|
+
* itself, which can hide some of its fields, so copying its entries would bypass that encoding.
|
|
149
|
+
*/
|
|
150
|
+
export function isMergeableObject(value: unknown): value is Record<string, unknown> {
|
|
151
|
+
if (!isPlainObject(value)) {
|
|
152
|
+
return false
|
|
153
|
+
}
|
|
154
|
+
try {
|
|
155
|
+
return typeof (value as { toJSON?: unknown }).toJSON !== 'function'
|
|
156
|
+
} catch {
|
|
157
|
+
// A `toJSON` getter that throws is left to serialization, which drops the event without
|
|
158
|
+
// logging the error, since its message can hold event content.
|
|
159
|
+
return false
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** A new object: the event's inner values, plus the default's inner keys the event left unset. */
|
|
164
|
+
function fillNested(existing: Record<string, unknown>, defaults: Record<string, unknown>): PostHogEventProperties {
|
|
165
|
+
const filled: Record<string, unknown> = { ...existing }
|
|
166
|
+
for (const [key, value] of Object.entries(defaults)) {
|
|
167
|
+
if (filled[key] === undefined && value !== undefined) {
|
|
168
|
+
filled[key] = value
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return filled as PostHogEventProperties
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** A shallow copy of a layer read at capture time, or undefined when it is not an object. */
|
|
175
|
+
export function snapshotLayer(layer: unknown): Record<string, unknown> | undefined {
|
|
176
|
+
return isPlainObject(layer) ? { ...layer } : undefined
|
|
177
|
+
}
|
package/src/exports.ts
CHANGED
|
@@ -5,6 +5,9 @@ export * from './types'
|
|
|
5
5
|
export { enrichCurrentSpan } from './extensions/context/listeners'
|
|
6
6
|
|
|
7
7
|
export { FeatureFlagEvaluations } from './feature-flag-evaluations'
|
|
8
|
+
export { CaptureError } from './capture-v1/errors'
|
|
9
|
+
export type { CaptureDroppedEvent, CaptureErrorParams } from './capture-v1/errors'
|
|
10
|
+
export type { CaptureEndpoint } from './capture-v1/types'
|
|
8
11
|
export { EmailError } from './emails/errors'
|
|
9
12
|
export type { EmailErrorName } from './emails/errors'
|
|
10
13
|
export type {
|
|
@@ -18,10 +18,6 @@ export class PostHogContext implements IPostHogContext {
|
|
|
18
18
|
return this.storage.run(this.resolve(context, options), fn)
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
enter(context: ContextData, options?: ContextOptions): void {
|
|
22
|
-
this.storage.enterWith(this.resolve(context, options))
|
|
23
|
-
}
|
|
24
|
-
|
|
25
21
|
private resolve(context: ContextData, options?: ContextOptions): ContextData {
|
|
26
22
|
const current = this.get()
|
|
27
23
|
if (options?.fresh === true) {
|
|
@@ -37,6 +33,10 @@ export class PostHogContext implements IPostHogContext {
|
|
|
37
33
|
...(current?.properties || {}),
|
|
38
34
|
...(context.properties || {}),
|
|
39
35
|
},
|
|
36
|
+
options: {
|
|
37
|
+
...(current?.options || {}),
|
|
38
|
+
...(context.options || {}),
|
|
39
|
+
},
|
|
40
40
|
},
|
|
41
41
|
options?.signal
|
|
42
42
|
)
|
|
@@ -5,7 +5,9 @@ const capturedContext = Symbol('capturedContext')
|
|
|
5
5
|
type ContextEvent = EventMessage & { [capturedContext]?: ContextData | null }
|
|
6
6
|
|
|
7
7
|
export function snapshotEventContext(context: ContextData | undefined): (event: EventMessage) => EventMessage {
|
|
8
|
-
const snapshot = context
|
|
8
|
+
const snapshot = context
|
|
9
|
+
? { ...context, properties: { ...context.properties }, options: { ...context.options } }
|
|
10
|
+
: null
|
|
9
11
|
return (event) => Object.assign(event, { [capturedContext]: snapshot })
|
|
10
12
|
}
|
|
11
13
|
|
|
@@ -1,7 +1,17 @@
|
|
|
1
|
+
import type { EventOptions } from '../../types'
|
|
2
|
+
|
|
1
3
|
export interface ContextData {
|
|
2
4
|
distinctId?: string
|
|
3
5
|
sessionId?: string
|
|
6
|
+
/** Properties that fill the keys an event left unset. The event's own values win. */
|
|
4
7
|
properties?: Record<string, any>
|
|
8
|
+
/**
|
|
9
|
+
* Options that fill the options an event left unset (missing or `null`). The event's own options
|
|
10
|
+
* win; these beat options from `registerOptions()`. A `process_person_profile` here does not
|
|
11
|
+
* reach `$identify`, `$create_alias`, `$merge_dangerously`, `$groupidentify`, `$set` or
|
|
12
|
+
* `$set_once` events unless their distinct id is generated.
|
|
13
|
+
*/
|
|
14
|
+
options?: EventOptions
|
|
5
15
|
}
|
|
6
16
|
|
|
7
17
|
export interface ContextOptions {
|
|
@@ -21,5 +31,4 @@ export interface ContextOptions {
|
|
|
21
31
|
export interface IPostHogContext {
|
|
22
32
|
get(): ContextData | undefined
|
|
23
33
|
run<T>(context: ContextData, fn: () => T, options?: ContextOptions): T
|
|
24
|
-
enter(context: ContextData, options?: ContextOptions): void
|
|
25
34
|
}
|
|
@@ -11,6 +11,45 @@ import { ErrorTracking as CoreErrorTracking } from '@posthog/core'
|
|
|
11
11
|
|
|
12
12
|
const SHUTDOWN_TIMEOUT = 2000
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Exception properties that the SDK or PostHog owns (sdk-specs `exception-event-metadata`), so
|
|
16
|
+
* a caller's properties never set them.
|
|
17
|
+
*/
|
|
18
|
+
const RESERVED_EXCEPTION_PROPERTIES: ReadonlySet<string> = new Set([
|
|
19
|
+
'$exception_list',
|
|
20
|
+
'$exception_level',
|
|
21
|
+
'$exception_source',
|
|
22
|
+
'$debug_images',
|
|
23
|
+
'$exception_types',
|
|
24
|
+
'$exception_values',
|
|
25
|
+
'$exception_sources',
|
|
26
|
+
'$exception_functions',
|
|
27
|
+
'$exception_fingerprint_version',
|
|
28
|
+
'$exception_fingerprint_record',
|
|
29
|
+
'$exception_issue_id',
|
|
30
|
+
'$exception_release',
|
|
31
|
+
'$cymbal_errors',
|
|
32
|
+
])
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The `$exception_level` values a caller may set (sdk-specs `exception-event-metadata`), read
|
|
36
|
+
* case-insensitively with the same aliases as posthog-python. Any other value keeps the default.
|
|
37
|
+
*/
|
|
38
|
+
const EXCEPTION_LEVELS: ReadonlyMap<string, CoreErrorTracking.SeverityLevel> = new Map([
|
|
39
|
+
['fatal', 'fatal'],
|
|
40
|
+
['critical', 'fatal'],
|
|
41
|
+
['alert', 'fatal'],
|
|
42
|
+
['emergency', 'fatal'],
|
|
43
|
+
['error', 'error'],
|
|
44
|
+
['warning', 'warning'],
|
|
45
|
+
['warn', 'warning'],
|
|
46
|
+
['log', 'log'],
|
|
47
|
+
['notice', 'info'],
|
|
48
|
+
['info', 'info'],
|
|
49
|
+
['trace', 'debug'],
|
|
50
|
+
['debug', 'debug'],
|
|
51
|
+
])
|
|
52
|
+
|
|
14
53
|
export default class ErrorTracking {
|
|
15
54
|
private client: PostHogBackendClient
|
|
16
55
|
private _exceptionAutocaptureEnabled: boolean
|
|
@@ -53,23 +92,27 @@ export default class ErrorTracking {
|
|
|
53
92
|
hint: CoreErrorTracking.EventHint,
|
|
54
93
|
distinctId?: string,
|
|
55
94
|
additionalProperties?: Record<string | number, any>,
|
|
95
|
+
level?: CoreErrorTracking.SeverityLevel,
|
|
56
96
|
context?: ContextData
|
|
57
97
|
): Promise<EventMessage> {
|
|
58
98
|
const applyContextSnapshot = snapshotEventContext(context)
|
|
59
|
-
const properties: EventMessage['properties'] = {
|
|
99
|
+
const properties: EventMessage['properties'] = {}
|
|
100
|
+
for (const [key, value] of Object.entries(additionalProperties ?? {})) {
|
|
101
|
+
if (!RESERVED_EXCEPTION_PROPERTIES.has(key)) {
|
|
102
|
+
properties[key] = value
|
|
103
|
+
}
|
|
104
|
+
}
|
|
60
105
|
|
|
61
106
|
const exceptionProperties = builder.buildFromUnknown(error, hint)
|
|
62
107
|
exceptionProperties.$exception_list = await builder.modifyFrames(exceptionProperties.$exception_list)
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
properties.$release_id = injectedReleaseId
|
|
108
|
+
const exceptionLevel = typeof level === 'string' ? EXCEPTION_LEVELS.get(level.toLowerCase()) : undefined
|
|
109
|
+
if (exceptionLevel !== undefined) {
|
|
110
|
+
exceptionProperties.$exception_level = exceptionLevel
|
|
67
111
|
}
|
|
68
112
|
|
|
69
113
|
return applyContextSnapshot({
|
|
70
114
|
event: '$exception',
|
|
71
|
-
//
|
|
72
|
-
// and falls back to a random UUID with $process_person_profile = false
|
|
115
|
+
// The client resolves a missing id: the context id, else a personless id.
|
|
73
116
|
distinctId: distinctId,
|
|
74
117
|
properties: {
|
|
75
118
|
...exceptionProperties,
|
|
@@ -130,6 +173,7 @@ export default class ErrorTracking {
|
|
|
130
173
|
hint,
|
|
131
174
|
undefined,
|
|
132
175
|
undefined,
|
|
176
|
+
undefined,
|
|
133
177
|
this.client.getContext()
|
|
134
178
|
)
|
|
135
179
|
const exceptionProperties = eventMessage.properties
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
addProperty,
|
|
6
6
|
getFirstHeaderValue,
|
|
7
7
|
getPostHogCookieReadOptions,
|
|
8
|
-
|
|
8
|
+
readPostHogTracingHeaders,
|
|
9
9
|
} from './tracing-headers'
|
|
10
10
|
import { normalizeRequestCurrentUrl, normalizeRequestPath } from './url-utils'
|
|
11
11
|
import type { Request, Response } from 'express'
|
|
@@ -40,12 +40,12 @@ function getClientIp(req: Request): string | undefined {
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
function buildRequestContextData(posthog: PostHogBackendClient, req: Request): Partial<ContextData> {
|
|
43
|
-
const headers =
|
|
43
|
+
const headers = readPostHogTracingHeaders(posthog.options, req.headers, getPostHogCookieReadOptions(posthog))
|
|
44
44
|
const context = posthog.getContext()
|
|
45
45
|
const sessionId = context?.sessionId ?? headers.sessionId
|
|
46
46
|
const distinctId = context?.distinctId ?? headers.distinctId
|
|
47
47
|
const properties: Record<string, any> = {}
|
|
48
|
-
const disableCaptureUrlHashes = posthog.options.
|
|
48
|
+
const disableCaptureUrlHashes = posthog.options.disableCaptureUrlHashes === true
|
|
49
49
|
|
|
50
50
|
addProperty(
|
|
51
51
|
properties,
|
|
@@ -64,6 +64,11 @@ function buildRequestContextData(posthog: PostHogBackendClient, req: Request): P
|
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
|
|
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.
|
|
71
|
+
*/
|
|
67
72
|
export function setupExpressRequestContext(
|
|
68
73
|
_posthog: PostHogBackendClient,
|
|
69
74
|
app: {
|
|
@@ -82,6 +87,9 @@ function posthogRequestContext(posthog: PostHogBackendClient): ExpressMiddleware
|
|
|
82
87
|
}
|
|
83
88
|
}
|
|
84
89
|
|
|
90
|
+
/**
|
|
91
|
+
* Captures errors that Express passes to its error middleware. Call it after your routes.
|
|
92
|
+
*/
|
|
85
93
|
export function setupExpressErrorHandler(
|
|
86
94
|
_posthog: PostHogBackendClient,
|
|
87
95
|
app: {
|
|
@@ -135,6 +143,7 @@ function posthogErrorHandler(posthog: PostHogBackendClient): ExpressErrorMiddlew
|
|
|
135
143
|
hint,
|
|
136
144
|
contextData.distinctId,
|
|
137
145
|
additionalProperties,
|
|
146
|
+
undefined,
|
|
138
147
|
posthog.getContext()
|
|
139
148
|
),
|
|
140
149
|
// Downstream error handlers can choose a different status, even asynchronously.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { PostHogBackendClient } from '../client'
|
|
2
2
|
import type { ContextData } from './context/types'
|
|
3
3
|
import { notifyContextListeners } from './context/listeners'
|
|
4
|
-
import { addProperty, getPostHogCookieReadOptions,
|
|
4
|
+
import { addProperty, getPostHogCookieReadOptions, readPostHogTracingHeaders } from './tracing-headers'
|
|
5
5
|
import { normalizeRequestPath } from './url-utils'
|
|
6
6
|
|
|
7
7
|
/** Duration histogram, in seconds, using the OpenTelemetry HTTP semantic convention name. */
|
|
@@ -104,9 +104,13 @@ async function instrument(
|
|
|
104
104
|
return handler()
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
const headers =
|
|
107
|
+
const headers = readPostHogTracingHeaders(
|
|
108
|
+
posthog.options,
|
|
109
|
+
headersToRecord(request.headers),
|
|
110
|
+
getPostHogCookieReadOptions(posthog)
|
|
111
|
+
)
|
|
108
112
|
const existing = posthog.getContext()
|
|
109
|
-
const disableCaptureUrlHashes = posthog.options.
|
|
113
|
+
const disableCaptureUrlHashes = posthog.options.disableCaptureUrlHashes === true
|
|
110
114
|
const pathname = safePathname(request)
|
|
111
115
|
const properties: Record<string, any> = {}
|
|
112
116
|
addProperty(properties, '$request_method', request.method)
|
package/src/extensions/nestjs.ts
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
addProperty,
|
|
8
8
|
getFirstHeaderValue,
|
|
9
9
|
getPostHogCookieReadOptions,
|
|
10
|
-
|
|
10
|
+
readPostHogTracingHeaders,
|
|
11
11
|
} from './tracing-headers'
|
|
12
12
|
import { normalizeRequestCurrentUrl, normalizeRequestPath } from './url-utils'
|
|
13
13
|
import { notifyContextListeners } from './context/listeners'
|
|
@@ -104,10 +104,14 @@ export class PostHogInterceptor implements NestInterceptor {
|
|
|
104
104
|
const response = httpHost.getResponse()
|
|
105
105
|
|
|
106
106
|
const headers = (request?.headers ?? {}) as IncomingHttpHeaders
|
|
107
|
-
const { sessionId, distinctId } =
|
|
107
|
+
const { sessionId, distinctId } = readPostHogTracingHeaders(
|
|
108
|
+
this.posthog.options,
|
|
109
|
+
headers,
|
|
110
|
+
getPostHogCookieReadOptions(this.posthog)
|
|
111
|
+
)
|
|
108
112
|
|
|
109
113
|
const properties: Record<string, any> = {}
|
|
110
|
-
const disableCaptureUrlHashes = this.posthog.options.
|
|
114
|
+
const disableCaptureUrlHashes = this.posthog.options.disableCaptureUrlHashes === true
|
|
111
115
|
addProperty(properties, '$current_url', normalizeRequestCurrentUrl(request?.url, disableCaptureUrlHashes))
|
|
112
116
|
addProperty(properties, '$request_method', request?.method)
|
|
113
117
|
addProperty(
|
|
@@ -162,11 +166,11 @@ export class PostHogInterceptor implements NestInterceptor {
|
|
|
162
166
|
return throwError(() => exception)
|
|
163
167
|
}
|
|
164
168
|
const responseStatus = status ?? response?.statusCode
|
|
165
|
-
const
|
|
169
|
+
const properties: Record<string, any> | undefined =
|
|
166
170
|
responseStatus !== undefined ? { $response_status_code: responseStatus } : undefined
|
|
167
171
|
// A copied distinct ID that the app already ended must not label this exception.
|
|
168
172
|
const exceptionDistinctId = copiesDistinctId && signal?.aborted ? undefined : requestDistinctId
|
|
169
|
-
this.posthog.captureException(exception, exceptionDistinctId,
|
|
173
|
+
this.posthog.captureException(exception, { distinctId: exceptionDistinctId, properties })
|
|
170
174
|
return throwError(() => exception)
|
|
171
175
|
})
|
|
172
176
|
)
|
|
@@ -151,6 +151,31 @@ function getSessionIdleTimeoutMs(cookie: PostHogCookieReadOptions): number {
|
|
|
151
151
|
return Math.min(Math.max(seconds, MIN_SESSION_IDLE_TIMEOUT_SECONDS), MAX_SESSION_IDLE_TIMEOUT_SECONDS) * 1000
|
|
152
152
|
}
|
|
153
153
|
|
|
154
|
+
function readCookieValues(headers: IncomingHttpHeaders, cookie: PostHogCookieReadOptions): PostHogTracingHeaderValues {
|
|
155
|
+
return getPostHogCookieValues(
|
|
156
|
+
headers.cookie,
|
|
157
|
+
cookie.apiKey,
|
|
158
|
+
Date.now(),
|
|
159
|
+
getSessionIdleTimeoutMs(cookie),
|
|
160
|
+
cookie.optOutCapturingByDefault === true
|
|
161
|
+
)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* The request's tracing ids, with the posthog-js cookie fallback when `cookie` is set. When the client's
|
|
166
|
+
* `captureTracingHeaders` is `false`, the tracing headers count as absent, so only the cookie can supply ids.
|
|
167
|
+
*/
|
|
168
|
+
export function readPostHogTracingHeaders(
|
|
169
|
+
options: { captureTracingHeaders?: boolean },
|
|
170
|
+
headers?: IncomingHttpHeaders,
|
|
171
|
+
cookie?: PostHogCookieReadOptions | null
|
|
172
|
+
): PostHogTracingHeaderValues {
|
|
173
|
+
if (options.captureTracingHeaders !== false) {
|
|
174
|
+
return getPostHogTracingHeaderValues(headers, cookie)
|
|
175
|
+
}
|
|
176
|
+
return headers && cookie ? readCookieValues(headers, cookie) : {}
|
|
177
|
+
}
|
|
178
|
+
|
|
154
179
|
/**
|
|
155
180
|
* Reads the PostHog tracing headers. With cookie read options, a request with neither header falls back to the posthog-js
|
|
156
181
|
* cookie. A request with either header uses headers only, so one request never mixes two identities.
|
|
@@ -167,13 +192,7 @@ export function getPostHogTracingHeaderValues(
|
|
|
167
192
|
const headerDistinctId = sanitizeTracingHeaderValue(headers[POSTHOG_TRACING_HEADERS.distinctId])
|
|
168
193
|
const { sessionId, distinctId } =
|
|
169
194
|
headerSessionId === undefined && headerDistinctId === undefined && cookie
|
|
170
|
-
?
|
|
171
|
-
headers.cookie,
|
|
172
|
-
cookie.apiKey,
|
|
173
|
-
Date.now(),
|
|
174
|
-
getSessionIdleTimeoutMs(cookie),
|
|
175
|
-
cookie.optOutCapturingByDefault === true
|
|
176
|
-
)
|
|
195
|
+
? readCookieValues(headers, cookie)
|
|
177
196
|
: { sessionId: headerSessionId, distinctId: headerDistinctId }
|
|
178
197
|
|
|
179
198
|
return {
|
package/src/flag-events.ts
CHANGED
|
@@ -154,8 +154,11 @@ export function applyFlagEventCapture(properties: Record<string, any>, capture:
|
|
|
154
154
|
*
|
|
155
155
|
* @internal
|
|
156
156
|
*/
|
|
157
|
-
export function minimizeConfigVersion2FlagCalledProperties(
|
|
158
|
-
|
|
157
|
+
export function minimizeConfigVersion2FlagCalledProperties(
|
|
158
|
+
properties: Record<string, any>,
|
|
159
|
+
transportKeys: readonly string[] = []
|
|
160
|
+
): Record<string, any> {
|
|
161
|
+
const minimal = minimizeFlagCalledEventProperties(properties, transportKeys)
|
|
159
162
|
for (const name of CONFIG_VERSION_2_ATTRIBUTION_PROPERTIES) {
|
|
160
163
|
if (properties[name] !== undefined) {
|
|
161
164
|
minimal[name] = properties[name]
|