posthog-node 5.57.2 → 5.59.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/README.md +39 -0
- package/dist/client.d.ts +13 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +17 -7
- package/dist/client.mjs +17 -7
- package/dist/entrypoints/otel.d.ts +18 -0
- package/dist/entrypoints/otel.d.ts.map +1 -0
- package/dist/entrypoints/otel.js +99 -0
- package/dist/entrypoints/otel.mjs +58 -0
- package/dist/exports.d.ts +1 -0
- package/dist/exports.d.ts.map +1 -1
- package/dist/exports.js +30 -20
- package/dist/exports.mjs +1 -0
- package/dist/extensions/context/context.d.ts.map +1 -1
- package/dist/extensions/context/context.js +12 -8
- package/dist/extensions/context/context.mjs +12 -8
- package/dist/extensions/context/event-context.d.ts +5 -0
- package/dist/extensions/context/event-context.d.ts.map +1 -0
- package/dist/extensions/context/event-context.js +57 -0
- package/dist/extensions/context/event-context.mjs +16 -0
- package/dist/extensions/context/lifetime.d.ts +8 -0
- package/dist/extensions/context/lifetime.d.ts.map +1 -0
- package/dist/extensions/context/lifetime.js +60 -0
- package/dist/extensions/context/lifetime.mjs +19 -0
- package/dist/extensions/context/listeners.d.ts +11 -0
- package/dist/extensions/context/listeners.d.ts.map +1 -0
- package/dist/extensions/context/listeners.js +65 -0
- package/dist/extensions/context/listeners.mjs +21 -0
- package/dist/extensions/context/types.d.ts +5 -0
- package/dist/extensions/context/types.d.ts.map +1 -1
- package/dist/extensions/error-tracking/index.d.ts +2 -1
- package/dist/extensions/error-tracking/index.d.ts.map +1 -1
- package/dist/extensions/error-tracking/index.js +6 -4
- package/dist/extensions/error-tracking/index.mjs +6 -4
- package/dist/extensions/express.d.ts.map +1 -1
- package/dist/extensions/express.js +10 -3
- package/dist/extensions/express.mjs +10 -3
- package/dist/extensions/nestjs.d.ts.map +1 -1
- package/dist/extensions/nestjs.js +2 -0
- package/dist/extensions/nestjs.mjs +2 -0
- package/dist/feature-flag-evaluations.d.ts +29 -4
- package/dist/feature-flag-evaluations.d.ts.map +1 -1
- package/dist/feature-flag-evaluations.js +5 -4
- package/dist/feature-flag-evaluations.mjs +5 -4
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/version.mjs +1 -1
- package/package.json +12 -5
- package/src/client.ts +36 -7
- package/src/entrypoints/otel.ts +75 -0
- package/src/exports.ts +1 -0
- package/src/extensions/context/context.ts +16 -10
- package/src/extensions/context/event-context.ts +14 -0
- package/src/extensions/context/lifetime.ts +28 -0
- package/src/extensions/context/listeners.ts +36 -0
- package/src/extensions/context/types.ts +5 -0
- package/src/extensions/error-tracking/index.ts +11 -4
- package/src/extensions/express.ts +11 -3
- package/src/extensions/nestjs.ts +2 -0
- package/src/feature-flag-evaluations.ts +36 -8
- package/src/version.ts +1 -1
|
@@ -38,6 +38,11 @@ export type FlagCalledEventParams = {
|
|
|
38
38
|
*/
|
|
39
39
|
export interface FeatureFlagEvaluationsHost {
|
|
40
40
|
captureFlagCalledEventIfNeeded(params: FlagCalledEventParams): void;
|
|
41
|
+
/**
|
|
42
|
+
* Whether a read sends `$feature_flag_called`: the per-read option, then the client's
|
|
43
|
+
* `sendFeatureFlagEvent` option, then `true`.
|
|
44
|
+
*/
|
|
45
|
+
resolveSendFeatureFlagEvents(perRead: boolean | undefined): boolean;
|
|
41
46
|
logWarning(message: string): void;
|
|
42
47
|
}
|
|
43
48
|
/**
|
|
@@ -96,27 +101,46 @@ export declare class FeatureFlagEvaluations {
|
|
|
96
101
|
/**
|
|
97
102
|
* Check whether a feature flag is enabled. Fires a `$feature_flag_called` event
|
|
98
103
|
* on the first access per (distinctId, flag, value) tuple, deduped via the SDK's
|
|
99
|
-
* existing cache.
|
|
104
|
+
* existing cache, unless events are off for this read (see `options.sendFeatureFlagEvents`).
|
|
100
105
|
*
|
|
101
106
|
* Flags that were not returned from the underlying evaluation resolve to
|
|
102
107
|
* `options.defaultValue` (`false` unless overridden). A flag that has a value —
|
|
103
108
|
* including a conclusive `false` result and variant strings — always wins over
|
|
104
109
|
* `options.defaultValue`. Use `getFlag()` to distinguish an absent result from `false`.
|
|
110
|
+
*
|
|
111
|
+
* @param key - The feature flag key
|
|
112
|
+
* @param options - `defaultValue`: the result when the flag is not in the snapshot.
|
|
113
|
+
* `sendFeatureFlagEvents`: whether this read sends a `$feature_flag_called` event.
|
|
114
|
+
* Defaults to the client's `sendFeatureFlagEvent` option, which defaults to `true`.
|
|
115
|
+
* When `false`, the read sends nothing and does not mark the flag as reported, so a
|
|
116
|
+
* later read that sends events still reports it. The flag still counts as accessed
|
|
117
|
+
* for `onlyAccessed()`.
|
|
105
118
|
*/
|
|
106
119
|
isEnabled(key: string, options?: {
|
|
107
120
|
defaultValue?: boolean;
|
|
121
|
+
sendFeatureFlagEvents?: boolean;
|
|
108
122
|
}): boolean;
|
|
109
123
|
/**
|
|
110
124
|
* Get the evaluated value of a feature flag. Fires a `$feature_flag_called` event
|
|
111
|
-
* on the first access per (distinctId, flag, value) tuple
|
|
125
|
+
* on the first access per (distinctId, flag, value) tuple, unless events are off for
|
|
126
|
+
* this read (see `options.sendFeatureFlagEvents`).
|
|
112
127
|
*
|
|
113
128
|
* Returns the variant string for multivariate flags, `true` for boolean flags that
|
|
114
129
|
* evaluate on, `false` for boolean flags that conclusively evaluate off, and `undefined`
|
|
115
130
|
* for flags that were not returned by the evaluation. Cached inactive definitions are
|
|
116
131
|
* conclusive `false` results during local evaluation, while remote evaluation omits
|
|
117
132
|
* globally inactive flags.
|
|
133
|
+
*
|
|
134
|
+
* @param key - The feature flag key
|
|
135
|
+
* @param options - `sendFeatureFlagEvents`: whether this read sends a `$feature_flag_called`
|
|
136
|
+
* event. Defaults to the client's `sendFeatureFlagEvent` option, which defaults to `true`.
|
|
137
|
+
* When `false`, the read sends nothing and does not mark the flag as reported, so a
|
|
138
|
+
* later read that sends events still reports it. The flag still counts as accessed
|
|
139
|
+
* for `onlyAccessed()`.
|
|
118
140
|
*/
|
|
119
|
-
getFlag(key: string
|
|
141
|
+
getFlag(key: string, options?: {
|
|
142
|
+
sendFeatureFlagEvents?: boolean;
|
|
143
|
+
}): FeatureFlagValue | undefined;
|
|
120
144
|
/**
|
|
121
145
|
* Get the payload associated with a feature flag. Does not count as an access
|
|
122
146
|
* for `onlyAccessed()` and does not fire any event.
|
|
@@ -124,7 +148,8 @@ export declare class FeatureFlagEvaluations {
|
|
|
124
148
|
getFlagPayload(key: string): JsonType | undefined;
|
|
125
149
|
/**
|
|
126
150
|
* Return a filtered copy containing only flags that have been accessed via
|
|
127
|
-
* `isEnabled()` or `getFlag()` before this call
|
|
151
|
+
* `isEnabled()` or `getFlag()` before this call, including reads that sent no
|
|
152
|
+
* `$feature_flag_called` event.
|
|
128
153
|
*
|
|
129
154
|
* Order-dependent: if nothing has been accessed yet, the returned snapshot is
|
|
130
155
|
* empty. The method honors its name — pre-access if you want a populated result.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"feature-flag-evaluations.d.ts","sourceRoot":"","sources":["../src/feature-flag-evaluations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAA;AAIhF;;;;;GAKG;AACH,MAAM,MAAM,mBAAmB,GAAG;IAChC,GAAG,EAAE,MAAM,CAAA;IACX,OAAO,EAAE,OAAO,CAAA;IAChB,OAAO,EAAE,MAAM,GAAG,SAAS,CAAA;IAC3B,OAAO,EAAE,QAAQ,GAAG,SAAS,CAAA;IAC7B,EAAE,EAAE,MAAM,GAAG,SAAS,CAAA;IACtB,OAAO,EAAE,MAAM,GAAG,SAAS,CAAA;IAC3B,MAAM,EAAE,MAAM,GAAG,SAAS,CAAA;IAC1B,gBAAgB,EAAE,OAAO,CAAA;IACzB,gGAAgG;IAChG,aAAa,EAAE,OAAO,GAAG,SAAS,CAAA;CACnC,CAAA;AAED;;;;GAIG;AACH,MAAM,MAAM,qBAAqB,GAAG;I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,CAEpE;IAED;;;;;;OAMG;IACH,mBAAmB,IAAI,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAezC;IAED,OAAO,CAAC,UAAU;IAqBlB,OAAO,CAAC,aAAa;CA+EtB"}
|
|
@@ -51,12 +51,12 @@ class FeatureFlagEvaluations {
|
|
|
51
51
|
}
|
|
52
52
|
isEnabled(key, options = {}) {
|
|
53
53
|
const flag = this._flags[key];
|
|
54
|
-
this._recordAccess(key);
|
|
54
|
+
this._recordAccess(key, options.sendFeatureFlagEvents);
|
|
55
55
|
return flag?.enabled ?? options.defaultValue ?? false;
|
|
56
56
|
}
|
|
57
|
-
getFlag(key) {
|
|
57
|
+
getFlag(key, options = {}) {
|
|
58
58
|
const flag = this._flags[key];
|
|
59
|
-
this._recordAccess(key);
|
|
59
|
+
this._recordAccess(key, options.sendFeatureFlagEvents);
|
|
60
60
|
if (!flag) return;
|
|
61
61
|
if (!flag.enabled) return false;
|
|
62
62
|
return flag.variant ?? true;
|
|
@@ -119,8 +119,9 @@ class FeatureFlagEvaluations {
|
|
|
119
119
|
isSlice: true
|
|
120
120
|
});
|
|
121
121
|
}
|
|
122
|
-
_recordAccess(key) {
|
|
122
|
+
_recordAccess(key, sendFeatureFlagEvents) {
|
|
123
123
|
this._accessed.add(key);
|
|
124
|
+
if (!this._host.resolveSendFeatureFlagEvents(sendFeatureFlagEvents)) return;
|
|
124
125
|
if ('' === this._distinctId) return;
|
|
125
126
|
if (this._isSlice && !(key in this._flags)) return;
|
|
126
127
|
const flag = this._flags[key];
|
|
@@ -19,12 +19,12 @@ class FeatureFlagEvaluations {
|
|
|
19
19
|
}
|
|
20
20
|
isEnabled(key, options = {}) {
|
|
21
21
|
const flag = this._flags[key];
|
|
22
|
-
this._recordAccess(key);
|
|
22
|
+
this._recordAccess(key, options.sendFeatureFlagEvents);
|
|
23
23
|
return flag?.enabled ?? options.defaultValue ?? false;
|
|
24
24
|
}
|
|
25
|
-
getFlag(key) {
|
|
25
|
+
getFlag(key, options = {}) {
|
|
26
26
|
const flag = this._flags[key];
|
|
27
|
-
this._recordAccess(key);
|
|
27
|
+
this._recordAccess(key, options.sendFeatureFlagEvents);
|
|
28
28
|
if (!flag) return;
|
|
29
29
|
if (!flag.enabled) return false;
|
|
30
30
|
return flag.variant ?? true;
|
|
@@ -87,8 +87,9 @@ class FeatureFlagEvaluations {
|
|
|
87
87
|
isSlice: true
|
|
88
88
|
});
|
|
89
89
|
}
|
|
90
|
-
_recordAccess(key) {
|
|
90
|
+
_recordAccess(key, sendFeatureFlagEvents) {
|
|
91
91
|
this._accessed.add(key);
|
|
92
|
+
if (!this._host.resolveSendFeatureFlagEvents(sendFeatureFlagEvents)) return;
|
|
92
93
|
if ('' === this._distinctId) return;
|
|
93
94
|
if (this._isSlice && !(key in this._flags)) return;
|
|
94
95
|
const flag = this._flags[key];
|
package/dist/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const version = "5.
|
|
1
|
+
export declare const version = "5.59.0";
|
|
2
2
|
//# sourceMappingURL=version.d.ts.map
|
package/dist/version.js
CHANGED
package/dist/version.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const version = "5.
|
|
1
|
+
const version = "5.59.0";
|
|
2
2
|
export { version };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "posthog-node",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.59.0",
|
|
4
4
|
"bugs": {
|
|
5
5
|
"url": "https://github.com/PostHog/posthog-js/issues"
|
|
6
6
|
},
|
|
@@ -29,21 +29,22 @@
|
|
|
29
29
|
"module": "dist/entrypoints/index.node.mjs",
|
|
30
30
|
"types": "dist/entrypoints/index.node.d.ts",
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@posthog/core": "^1.
|
|
32
|
+
"@posthog/core": "^1.61.2"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@edge-runtime/vm": "^5.0.0",
|
|
36
36
|
"@opentelemetry/api": "^1.9.0",
|
|
37
37
|
"@opentelemetry/context-async-hooks": "^2.2.0",
|
|
38
|
+
"@opentelemetry/sdk-trace-base": "^2.10.0",
|
|
38
39
|
"@rslib/core": "0.23.2",
|
|
39
40
|
"@types/express": "^5.0.6",
|
|
40
41
|
"@types/node": "^20.0.0",
|
|
41
42
|
"esbuild": "^0.25.0",
|
|
42
|
-
"rxjs": "^7.8.0",
|
|
43
43
|
"@vitest/coverage-v8": "^1.6.1",
|
|
44
|
-
"
|
|
44
|
+
"rxjs": "^7.8.0",
|
|
45
45
|
"typescript": "7.0.2",
|
|
46
|
-
"
|
|
46
|
+
"vitest": "^1.6.1",
|
|
47
|
+
"@posthog/types": "^1.418.0",
|
|
47
48
|
"@posthog-tooling/tsconfig-base": "1.1.1"
|
|
48
49
|
},
|
|
49
50
|
"peerDependencies": {
|
|
@@ -106,6 +107,12 @@
|
|
|
106
107
|
"import": "./dist/experimental.mjs",
|
|
107
108
|
"require": "./dist/experimental.js",
|
|
108
109
|
"default": "./dist/experimental.js"
|
|
110
|
+
},
|
|
111
|
+
"./otel": {
|
|
112
|
+
"types": "./dist/entrypoints/otel.d.ts",
|
|
113
|
+
"import": "./dist/entrypoints/otel.mjs",
|
|
114
|
+
"require": "./dist/entrypoints/otel.js",
|
|
115
|
+
"default": "./dist/entrypoints/otel.js"
|
|
109
116
|
}
|
|
110
117
|
},
|
|
111
118
|
"scripts": {
|
package/src/client.ts
CHANGED
|
@@ -72,6 +72,8 @@ import ErrorTracking from './extensions/error-tracking'
|
|
|
72
72
|
import { getActiveTraceProperties } from './extensions/error-tracking/trace-context'
|
|
73
73
|
import { PostHogMemoryStorage } from './storage-memory'
|
|
74
74
|
import { ContextData, ContextOptions, IPostHogContext } from './extensions/context/types'
|
|
75
|
+
import { notifyContextListeners } from './extensions/context/listeners'
|
|
76
|
+
import { getEventContext } from './extensions/context/event-context'
|
|
75
77
|
import { type CaptureMode, resolveCaptureMode } from './capture-v1/config'
|
|
76
78
|
import { AI_ROUTE, ANALYTICS_ROUTE, isLegacyOnlyEvent } from './capture-v1/routing'
|
|
77
79
|
import { V1CaptureSender } from './capture-v1/sender'
|
|
@@ -1769,7 +1771,7 @@ export abstract class PostHogBackendClient extends PostHogCoreStateless implemen
|
|
|
1769
1771
|
)
|
|
1770
1772
|
const result = await this._getFeatureFlagResult(key, distinctId, {
|
|
1771
1773
|
...options,
|
|
1772
|
-
sendFeatureFlagEvents: options?.sendFeatureFlagEvents
|
|
1774
|
+
sendFeatureFlagEvents: this._resolveSendFeatureFlagEvents(options?.sendFeatureFlagEvents),
|
|
1773
1775
|
})
|
|
1774
1776
|
if (result === undefined) {
|
|
1775
1777
|
return undefined
|
|
@@ -1921,7 +1923,7 @@ export abstract class PostHogBackendClient extends PostHogCoreStateless implemen
|
|
|
1921
1923
|
|
|
1922
1924
|
return this._getFeatureFlagResult(key, resolvedDistinctId, {
|
|
1923
1925
|
...resolvedOptions,
|
|
1924
|
-
sendFeatureFlagEvents: resolvedOptions?.sendFeatureFlagEvents
|
|
1926
|
+
sendFeatureFlagEvents: this._resolveSendFeatureFlagEvents(resolvedOptions?.sendFeatureFlagEvents),
|
|
1925
1927
|
})
|
|
1926
1928
|
}
|
|
1927
1929
|
|
|
@@ -2031,7 +2033,7 @@ export abstract class PostHogBackendClient extends PostHogCoreStateless implemen
|
|
|
2031
2033
|
// Bypass the public `getFeatureFlag` so the user only sees one deprecation warning per call.
|
|
2032
2034
|
const result = await this._getFeatureFlagResult(key, distinctId, {
|
|
2033
2035
|
...options,
|
|
2034
|
-
sendFeatureFlagEvents: options?.sendFeatureFlagEvents
|
|
2036
|
+
sendFeatureFlagEvents: this._resolveSendFeatureFlagEvents(options?.sendFeatureFlagEvents),
|
|
2035
2037
|
})
|
|
2036
2038
|
if (result === undefined) {
|
|
2037
2039
|
return undefined
|
|
@@ -2242,6 +2244,13 @@ export abstract class PostHogBackendClient extends PostHogCoreStateless implemen
|
|
|
2242
2244
|
* a cached inactive definition is a conclusive `false` result locally, while remote evaluation
|
|
2243
2245
|
* omits globally inactive flags and therefore leaves those keys absent.
|
|
2244
2246
|
*
|
|
2247
|
+
* **Exposure events.** `isEnabled()` and `getFlag()` send a `$feature_flag_called` event the
|
|
2248
|
+
* first time each flag value is read for a user. Experiments use these events as exposures.
|
|
2249
|
+
* The client's `sendFeatureFlagEvent` option sets the default for these reads. Pass
|
|
2250
|
+
* `{ sendFeatureFlagEvents: true }` or `{ sendFeatureFlagEvents: false }` to a read to override
|
|
2251
|
+
* it, so one snapshot can serve both experiment flags and flags that should send no events.
|
|
2252
|
+
* `getFlagPayload()` never sends events.
|
|
2253
|
+
*
|
|
2245
2254
|
* **Trim the request.** Pass `flagKeys` to scope local evaluation, the underlying
|
|
2246
2255
|
* `/flags` request, and the returned snapshot to a subset of flags. Remote evaluation
|
|
2247
2256
|
* responses are not cached, so a key missing both locally and remotely costs one
|
|
@@ -2563,12 +2572,22 @@ export abstract class PostHogBackendClient extends PostHogCoreStateless implemen
|
|
|
2563
2572
|
})
|
|
2564
2573
|
}
|
|
2565
2574
|
|
|
2575
|
+
/**
|
|
2576
|
+
* Whether a flag read sends `$feature_flag_called`: the per-call option wins, then the
|
|
2577
|
+
* client's `sendFeatureFlagEvent` option, then `true`. Shared by the single-flag getters
|
|
2578
|
+
* and `FeatureFlagEvaluations.isEnabled() / getFlag()`.
|
|
2579
|
+
*/
|
|
2580
|
+
private _resolveSendFeatureFlagEvents(perCall?: boolean): boolean {
|
|
2581
|
+
return perCall ?? this.options.sendFeatureFlagEvent ?? true
|
|
2582
|
+
}
|
|
2583
|
+
|
|
2566
2584
|
private _featureFlagEvaluationsHost?: FeatureFlagEvaluationsHost
|
|
2567
2585
|
|
|
2568
2586
|
private _getFeatureFlagEvaluationsHost(): FeatureFlagEvaluationsHost {
|
|
2569
2587
|
if (!this._featureFlagEvaluationsHost) {
|
|
2570
2588
|
this._featureFlagEvaluationsHost = {
|
|
2571
2589
|
captureFlagCalledEventIfNeeded: (params) => this._captureFlagCalledEventIfNeeded(params),
|
|
2590
|
+
resolveSendFeatureFlagEvents: (perRead) => this._resolveSendFeatureFlagEvents(perRead),
|
|
2572
2591
|
logWarning: (message) => {
|
|
2573
2592
|
if (this.options.featureFlagsLogWarnings !== false) {
|
|
2574
2593
|
// These warnings guide API usage (misuse of `onlyAccessed()` / `only()`) and
|
|
@@ -2843,7 +2862,14 @@ export abstract class PostHogBackendClient extends PostHogCoreStateless implemen
|
|
|
2843
2862
|
return fn()
|
|
2844
2863
|
}
|
|
2845
2864
|
|
|
2846
|
-
return this.context.run(
|
|
2865
|
+
return this.context.run(
|
|
2866
|
+
data,
|
|
2867
|
+
() => {
|
|
2868
|
+
notifyContextListeners(this)
|
|
2869
|
+
return fn()
|
|
2870
|
+
},
|
|
2871
|
+
options
|
|
2872
|
+
)
|
|
2847
2873
|
}
|
|
2848
2874
|
|
|
2849
2875
|
/**
|
|
@@ -2882,6 +2908,7 @@ export abstract class PostHogBackendClient extends PostHogCoreStateless implemen
|
|
|
2882
2908
|
*/
|
|
2883
2909
|
enterContext(data: Partial<ContextData>, options?: ContextOptions): void {
|
|
2884
2910
|
this.context?.enter(data as ContextData, options)
|
|
2911
|
+
notifyContextListeners(this)
|
|
2885
2912
|
}
|
|
2886
2913
|
|
|
2887
2914
|
/**
|
|
@@ -3176,7 +3203,8 @@ export abstract class PostHogBackendClient extends PostHogCoreStateless implemen
|
|
|
3176
3203
|
error,
|
|
3177
3204
|
{ syntheticException },
|
|
3178
3205
|
distinctId,
|
|
3179
|
-
additionalProperties
|
|
3206
|
+
additionalProperties,
|
|
3207
|
+
this.getContext()
|
|
3180
3208
|
).then((msg) => this._capturePreparedEvent({ ...msg, uuid, flags }, false))
|
|
3181
3209
|
)
|
|
3182
3210
|
}
|
|
@@ -3232,7 +3260,8 @@ export abstract class PostHogBackendClient extends PostHogCoreStateless implemen
|
|
|
3232
3260
|
error,
|
|
3233
3261
|
{ syntheticException },
|
|
3234
3262
|
distinctId,
|
|
3235
|
-
additionalProperties
|
|
3263
|
+
additionalProperties,
|
|
3264
|
+
this.getContext()
|
|
3236
3265
|
).then((msg) => this.captureImmediate({ ...msg, flags }))
|
|
3237
3266
|
)
|
|
3238
3267
|
}
|
|
@@ -3268,7 +3297,7 @@ export abstract class PostHogBackendClient extends PostHogCoreStateless implemen
|
|
|
3268
3297
|
uuid,
|
|
3269
3298
|
}: EventMessage = props
|
|
3270
3299
|
|
|
3271
|
-
const contextData = this.context?.get()
|
|
3300
|
+
const contextData = getEventContext(props, this.context?.get())
|
|
3272
3301
|
const includeContextProperties = options.includeContextProperties ?? true
|
|
3273
3302
|
|
|
3274
3303
|
let mergedDistinctId = distinctId || contextData?.distinctId
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { trace, SpanKind, type Attributes, type Context, type Span } from '@opentelemetry/api'
|
|
2
|
+
import type { PostHogBackendClient } from '../client'
|
|
3
|
+
import { addContextListener } from '../extensions/context/listeners'
|
|
4
|
+
|
|
5
|
+
export { enrichCurrentSpan } from '../extensions/context/listeners'
|
|
6
|
+
|
|
7
|
+
type ReadableSpan = Span & { readonly attributes: Attributes }
|
|
8
|
+
|
|
9
|
+
function hasReadableAttributes(span: Span | undefined): span is ReadableSpan {
|
|
10
|
+
return !!span && 'attributes' in span && typeof span.attributes === 'object' && span.attributes !== null
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class PostHogContextSpanProcessor {
|
|
14
|
+
private active = true
|
|
15
|
+
private readonly snapshots = new WeakSet<Span>()
|
|
16
|
+
private readonly parents = new WeakMap<Span, WeakRef<Span>>()
|
|
17
|
+
private readonly unsubscribe: () => void
|
|
18
|
+
|
|
19
|
+
constructor(private readonly posthog: PostHogBackendClient) {
|
|
20
|
+
this.unsubscribe = addContextListener(posthog, (requestEntry) => {
|
|
21
|
+
const span = trace.getActiveSpan()
|
|
22
|
+
this.enrich(span, requestEntry)
|
|
23
|
+
if (requestEntry) this.enrichNearestServerSpan(span)
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
private enrich(span: Span | undefined, requestEntry = false): void {
|
|
28
|
+
if (!this.active) return
|
|
29
|
+
try {
|
|
30
|
+
if (this.posthog.isDisabled || this.posthog.optedOut) return
|
|
31
|
+
if (!hasReadableAttributes(span) || !span.isRecording() || (this.snapshots.has(span) && !requestEntry)) return
|
|
32
|
+
const context = this.posthog.getContext()
|
|
33
|
+
if (!context) return
|
|
34
|
+
this.snapshots.add(span)
|
|
35
|
+
if (context.sessionId && span.attributes.sessionId === undefined) {
|
|
36
|
+
span.setAttribute('sessionId', context.sessionId)
|
|
37
|
+
}
|
|
38
|
+
if (context.distinctId && span.attributes.posthogDistinctId === undefined) {
|
|
39
|
+
span.setAttribute('posthogDistinctId', context.distinctId)
|
|
40
|
+
}
|
|
41
|
+
} catch {}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private enrichNearestServerSpan(current: Span | undefined): void {
|
|
45
|
+
let span = current
|
|
46
|
+
while (span) {
|
|
47
|
+
if ('kind' in span && span.kind === SpanKind.SERVER && span.isRecording()) {
|
|
48
|
+
if (span !== current) this.enrich(span, true)
|
|
49
|
+
return
|
|
50
|
+
}
|
|
51
|
+
span = this.parents.get(span)?.deref()
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
onStart(span: Span, parentContext?: Context): void {
|
|
56
|
+
if (!this.active) return
|
|
57
|
+
const parent = parentContext && trace.getSpan(parentContext)
|
|
58
|
+
if (parent && parent !== span) this.parents.set(span, new WeakRef(parent))
|
|
59
|
+
this.enrich(span)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
onEnd(): void {}
|
|
63
|
+
|
|
64
|
+
forceFlush(): Promise<void> {
|
|
65
|
+
return Promise.resolve()
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
shutdown(): Promise<void> {
|
|
69
|
+
if (this.active) {
|
|
70
|
+
this.active = false
|
|
71
|
+
this.unsubscribe()
|
|
72
|
+
}
|
|
73
|
+
return Promise.resolve()
|
|
74
|
+
}
|
|
75
|
+
}
|
package/src/exports.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from './extensions/sentry-integration'
|
|
2
2
|
export * from './extensions/express'
|
|
3
3
|
export * from './types'
|
|
4
|
+
export { enrichCurrentSpan } from './extensions/context/listeners'
|
|
4
5
|
|
|
5
6
|
export { FeatureFlagEvaluations } from './feature-flag-evaluations'
|
|
6
7
|
export type {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from 'node:async_hooks'
|
|
2
2
|
import { ContextData, ContextOptions, IPostHogContext } from './types'
|
|
3
|
+
import { inheritContextLifetime, isContextActive } from './lifetime'
|
|
3
4
|
|
|
4
5
|
export class PostHogContext implements IPostHogContext {
|
|
5
6
|
private storage: AsyncLocalStorage<ContextData>
|
|
@@ -9,7 +10,8 @@ export class PostHogContext implements IPostHogContext {
|
|
|
9
10
|
}
|
|
10
11
|
|
|
11
12
|
get(): ContextData | undefined {
|
|
12
|
-
|
|
13
|
+
const context = this.storage.getStore()
|
|
14
|
+
return context && isContextActive(context) ? context : undefined
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
run<T>(context: ContextData, fn: () => T, options?: ContextOptions): T {
|
|
@@ -21,18 +23,22 @@ export class PostHogContext implements IPostHogContext {
|
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
private resolve(context: ContextData, options?: ContextOptions): ContextData {
|
|
26
|
+
const current = this.get()
|
|
24
27
|
if (options?.fresh === true) {
|
|
25
|
-
return context
|
|
28
|
+
return inheritContextLifetime(undefined, { ...context }, options.signal)
|
|
26
29
|
}
|
|
27
30
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
return inheritContextLifetime(
|
|
32
|
+
current,
|
|
33
|
+
{
|
|
34
|
+
distinctId: context.distinctId ?? current?.distinctId,
|
|
35
|
+
sessionId: context.sessionId ?? current?.sessionId,
|
|
36
|
+
properties: {
|
|
37
|
+
...(current?.properties || {}),
|
|
38
|
+
...(context.properties || {}),
|
|
39
|
+
},
|
|
35
40
|
},
|
|
36
|
-
|
|
41
|
+
options?.signal
|
|
42
|
+
)
|
|
37
43
|
}
|
|
38
44
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { EventMessage } from '../../types'
|
|
2
|
+
import type { ContextData } from './types'
|
|
3
|
+
|
|
4
|
+
const capturedContext = Symbol('capturedContext')
|
|
5
|
+
type ContextEvent = EventMessage & { [capturedContext]?: ContextData | null }
|
|
6
|
+
|
|
7
|
+
export function snapshotEventContext(context: ContextData | undefined): (event: EventMessage) => EventMessage {
|
|
8
|
+
const snapshot = context ? { ...context, properties: { ...context.properties } } : null
|
|
9
|
+
return (event) => Object.assign(event, { [capturedContext]: snapshot })
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function getEventContext(event: EventMessage, fallback: ContextData | undefined): ContextData | undefined {
|
|
13
|
+
return capturedContext in event ? ((event as ContextEvent)[capturedContext] ?? undefined) : fallback
|
|
14
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ContextData } from './types'
|
|
2
|
+
|
|
3
|
+
export interface ContextLifetime {
|
|
4
|
+
signal: AbortSignal
|
|
5
|
+
parent?: ContextLifetime
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const lifetimes = new WeakMap<ContextData, ContextLifetime>()
|
|
9
|
+
|
|
10
|
+
export function inheritContextLifetime(
|
|
11
|
+
parent: ContextData | undefined,
|
|
12
|
+
child: ContextData,
|
|
13
|
+
signal?: AbortSignal
|
|
14
|
+
): ContextData {
|
|
15
|
+
const lifetime = parent && lifetimes.get(parent)
|
|
16
|
+
if (signal) lifetimes.set(child, { signal, parent: lifetime })
|
|
17
|
+
else if (lifetime) lifetimes.set(child, lifetime)
|
|
18
|
+
return child
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function isContextActive(context: ContextData): boolean {
|
|
22
|
+
let lifetime = lifetimes.get(context)
|
|
23
|
+
while (lifetime) {
|
|
24
|
+
if (lifetime.signal.aborted) return false
|
|
25
|
+
lifetime = lifetime.parent
|
|
26
|
+
}
|
|
27
|
+
return true
|
|
28
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { PostHogBackendClient } from '../../client'
|
|
2
|
+
|
|
3
|
+
type ContextListener = (requestEntry: boolean) => void
|
|
4
|
+
|
|
5
|
+
type ContextListenerRegistry = WeakMap<PostHogBackendClient, Set<ContextListener>>
|
|
6
|
+
|
|
7
|
+
// The CommonJS and ESM builds are separate module copies, so they share one registry through a global.
|
|
8
|
+
const REGISTRY_KEY = Symbol.for('posthog-node.context-listeners')
|
|
9
|
+
const registryHolder = globalThis as typeof globalThis & { [REGISTRY_KEY]?: ContextListenerRegistry }
|
|
10
|
+
const listeners = (registryHolder[REGISTRY_KEY] ??= new WeakMap())
|
|
11
|
+
|
|
12
|
+
export function addContextListener(client: PostHogBackendClient, listener: ContextListener): () => void {
|
|
13
|
+
const callbacks = listeners.get(client) ?? new Set<ContextListener>()
|
|
14
|
+
listeners.set(client, callbacks)
|
|
15
|
+
callbacks.add(listener)
|
|
16
|
+
return () => {
|
|
17
|
+
callbacks.delete(listener)
|
|
18
|
+
if (!callbacks.size) listeners.delete(client)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function notifyContextListeners(client: PostHogBackendClient, requestEntry = false): void {
|
|
23
|
+
for (const listener of listeners.get(client) ?? []) {
|
|
24
|
+
try {
|
|
25
|
+
listener(requestEntry)
|
|
26
|
+
} catch {}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
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.
|
|
33
|
+
*/
|
|
34
|
+
export function enrichCurrentSpan(client: PostHogBackendClient): void {
|
|
35
|
+
notifyContextListeners(client, true)
|
|
36
|
+
}
|
|
@@ -11,6 +11,11 @@ export interface ContextOptions {
|
|
|
11
11
|
* @default false
|
|
12
12
|
*/
|
|
13
13
|
fresh?: boolean
|
|
14
|
+
/**
|
|
15
|
+
* Ends this context and its inherited scopes when aborted, without cancelling the callback.
|
|
16
|
+
* An already aborted signal starts no active context. A fresh context has an independent lifetime.
|
|
17
|
+
*/
|
|
18
|
+
signal?: AbortSignal
|
|
14
19
|
}
|
|
15
20
|
|
|
16
21
|
export interface IPostHogContext {
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { addUncaughtExceptionListener, addUnhandledRejectionListener } from './autocapture'
|
|
2
2
|
import { PostHogBackendClient } from '@/client'
|
|
3
|
+
import type { ContextData } from '../context/types'
|
|
4
|
+
import { snapshotEventContext } from '../context/event-context'
|
|
3
5
|
import { isObject } from '@posthog/core'
|
|
4
6
|
import { EventMessage, PostHogOptions } from '@/types'
|
|
5
7
|
import type { Logger } from '@posthog/core'
|
|
@@ -47,8 +49,10 @@ export default class ErrorTracking {
|
|
|
47
49
|
error: unknown,
|
|
48
50
|
hint: CoreErrorTracking.EventHint,
|
|
49
51
|
distinctId?: string,
|
|
50
|
-
additionalProperties?: Record<string | number, any
|
|
52
|
+
additionalProperties?: Record<string | number, any>,
|
|
53
|
+
context?: ContextData
|
|
51
54
|
): Promise<EventMessage> {
|
|
55
|
+
const applyContextSnapshot = snapshotEventContext(context)
|
|
52
56
|
const properties: EventMessage['properties'] = { ...additionalProperties }
|
|
53
57
|
|
|
54
58
|
const exceptionProperties = builder.buildFromUnknown(error, hint)
|
|
@@ -59,7 +63,7 @@ export default class ErrorTracking {
|
|
|
59
63
|
properties.$release_id = injectedReleaseId
|
|
60
64
|
}
|
|
61
65
|
|
|
62
|
-
return {
|
|
66
|
+
return applyContextSnapshot({
|
|
63
67
|
event: '$exception',
|
|
64
68
|
// Leave distinctId resolution to prepareEventMessage which checks request context
|
|
65
69
|
// and falls back to a random UUID with $process_person_profile = false
|
|
@@ -69,7 +73,7 @@ export default class ErrorTracking {
|
|
|
69
73
|
...properties,
|
|
70
74
|
},
|
|
71
75
|
_originatedFromCaptureException: true,
|
|
72
|
-
}
|
|
76
|
+
})
|
|
73
77
|
}
|
|
74
78
|
|
|
75
79
|
private startAutocaptureIfEnabled(): void {
|
|
@@ -86,7 +90,10 @@ export default class ErrorTracking {
|
|
|
86
90
|
const eventMessage = await ErrorTracking.buildEventMessage(
|
|
87
91
|
this.client.getErrorPropertiesBuilder(),
|
|
88
92
|
exception,
|
|
89
|
-
hint
|
|
93
|
+
hint,
|
|
94
|
+
undefined,
|
|
95
|
+
undefined,
|
|
96
|
+
this.client.getContext()
|
|
90
97
|
)
|
|
91
98
|
const exceptionProperties = eventMessage.properties
|
|
92
99
|
const exceptionType = exceptionProperties?.$exception_list[0]?.type ?? 'Exception'
|
|
@@ -5,6 +5,7 @@ import { addProperty, getFirstHeaderValue, getPostHogTracingHeaderValues } from
|
|
|
5
5
|
import { normalizeRequestCurrentUrl, normalizeRequestPath } from './url-utils'
|
|
6
6
|
import type { Request, Response } from 'express'
|
|
7
7
|
import type { ContextData } from './context/types'
|
|
8
|
+
import { notifyContextListeners } from './context/listeners'
|
|
8
9
|
|
|
9
10
|
type ExpressMiddleware = (req: Request, res: Response, next: () => void) => void
|
|
10
11
|
|
|
@@ -34,7 +35,10 @@ function getClientIp(req: Request): string | undefined {
|
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
function buildRequestContextData(posthog: PostHogBackendClient, req: Request): Partial<ContextData> {
|
|
37
|
-
const
|
|
38
|
+
const headers = getPostHogTracingHeaderValues(req.headers)
|
|
39
|
+
const context = posthog.getContext()
|
|
40
|
+
const sessionId = context?.sessionId ?? headers.sessionId
|
|
41
|
+
const distinctId = context?.distinctId ?? headers.distinctId
|
|
38
42
|
const properties: Record<string, any> = {}
|
|
39
43
|
const disableCaptureUrlHashes = posthog.options.disable_capture_url_hashes === true
|
|
40
44
|
|
|
@@ -66,7 +70,10 @@ export function setupExpressRequestContext(
|
|
|
66
70
|
|
|
67
71
|
function posthogRequestContext(posthog: PostHogBackendClient): ExpressMiddleware {
|
|
68
72
|
return (req, _res, next): void => {
|
|
69
|
-
posthog.withContext(buildRequestContextData(posthog, req), () =>
|
|
73
|
+
posthog.withContext(buildRequestContextData(posthog, req), () => {
|
|
74
|
+
notifyContextListeners(posthog, true)
|
|
75
|
+
next()
|
|
76
|
+
})
|
|
70
77
|
}
|
|
71
78
|
}
|
|
72
79
|
|
|
@@ -122,7 +129,8 @@ function posthogErrorHandler(posthog: PostHogBackendClient): ExpressErrorMiddlew
|
|
|
122
129
|
error,
|
|
123
130
|
hint,
|
|
124
131
|
contextData.distinctId,
|
|
125
|
-
additionalProperties
|
|
132
|
+
additionalProperties,
|
|
133
|
+
posthog.getContext()
|
|
126
134
|
),
|
|
127
135
|
// Downstream error handlers can choose a different status, even asynchronously.
|
|
128
136
|
getResponseStatusCode(res),
|