@posthog/core 1.66.0 → 1.67.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/dist/metrics/index.d.ts +9 -0
- package/dist/metrics/index.d.ts.map +1 -1
- package/dist/metrics/index.js +13 -0
- package/dist/metrics/index.mjs +13 -0
- package/package.json +1 -1
- package/src/metrics/index.spec.ts +70 -0
- package/src/metrics/index.ts +30 -0
package/dist/metrics/index.d.ts
CHANGED
|
@@ -26,6 +26,7 @@ export declare class PostHogMetrics {
|
|
|
26
26
|
private _series;
|
|
27
27
|
private readonly _flushTimer;
|
|
28
28
|
private _flushPromise;
|
|
29
|
+
private _queuedFlush;
|
|
29
30
|
private _seriesCapWarned;
|
|
30
31
|
private _typeByName;
|
|
31
32
|
private _typeCollisionWarned;
|
|
@@ -42,6 +43,14 @@ export declare class PostHogMetrics {
|
|
|
42
43
|
histogram(name: string, value: number, options?: HistogramOptions): void;
|
|
43
44
|
/** Sends everything aggregated so far without waiting for the flush interval. */
|
|
44
45
|
flush(): Promise<void>;
|
|
46
|
+
/**
|
|
47
|
+
* A flush for callers that ask once per unit of work (a served request, say) rather than once per window.
|
|
48
|
+
* Callers share one queued flush, so at most one send is in flight and one waits behind it however often
|
|
49
|
+
* this is called, and every sample captured before the call is in one of them. While a failed send is
|
|
50
|
+
* backing off it starts nothing and the retry timer owns the next send.
|
|
51
|
+
*/
|
|
52
|
+
flushCoalesced(): Promise<void>;
|
|
53
|
+
private _isBackingOff;
|
|
45
54
|
/**
|
|
46
55
|
* Synchronously snapshots the current window into an OTLP payload and
|
|
47
56
|
* resets it, bypassing the flush serializer entirely — for unload-time
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/metrics/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,oBAAoB,EACpB,gBAAgB,EAOhB,kBAAkB,EAEnB,MAAM,gBAAgB,CAAA;AACvB,OAA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ics/index.js
CHANGED
|
@@ -53,6 +53,7 @@ class PostHogMetrics {
|
|
|
53
53
|
this._logger.error('Metrics flush failed:', e);
|
|
54
54
|
}));
|
|
55
55
|
this._flushPromise = null;
|
|
56
|
+
this._queuedFlush = null;
|
|
56
57
|
this._seriesCapWarned = false;
|
|
57
58
|
this._typeByName = new Map();
|
|
58
59
|
this._typeCollisionWarned = new Set();
|
|
@@ -104,6 +105,17 @@ class PostHogMetrics {
|
|
|
104
105
|
this._flushPromise = p;
|
|
105
106
|
return p;
|
|
106
107
|
}
|
|
108
|
+
flushCoalesced() {
|
|
109
|
+
if (this._isBackingOff()) return this._flushPromise ?? Promise.resolve();
|
|
110
|
+
if (!this._queuedFlush) this._queuedFlush = (this._flushPromise ?? Promise.resolve()).catch(()=>{}).then(()=>{
|
|
111
|
+
this._queuedFlush = null;
|
|
112
|
+
return this._isBackingOff() ? void 0 : this.flush();
|
|
113
|
+
});
|
|
114
|
+
return this._queuedFlush;
|
|
115
|
+
}
|
|
116
|
+
_isBackingOff() {
|
|
117
|
+
return this._consecutiveFlushFailures > 0 || this._retryAfter.remainingMs() > 0;
|
|
118
|
+
}
|
|
107
119
|
drainWindow() {
|
|
108
120
|
if (0 === this._series.size) return null;
|
|
109
121
|
const window = this._series;
|
|
@@ -117,6 +129,7 @@ class PostHogMetrics {
|
|
|
117
129
|
this._flushJitter = backoff_js_namespaceObject.NO_JITTER;
|
|
118
130
|
this._flushTimer.clear();
|
|
119
131
|
this._flushPromise = null;
|
|
132
|
+
this._queuedFlush = null;
|
|
120
133
|
this._startWindow();
|
|
121
134
|
this._boundsByHistogram = new Map();
|
|
122
135
|
this._boundsCapWarned = false;
|
package/dist/metrics/index.mjs
CHANGED
|
@@ -16,6 +16,7 @@ class PostHogMetrics {
|
|
|
16
16
|
this._logger.error('Metrics flush failed:', e);
|
|
17
17
|
}));
|
|
18
18
|
this._flushPromise = null;
|
|
19
|
+
this._queuedFlush = null;
|
|
19
20
|
this._seriesCapWarned = false;
|
|
20
21
|
this._typeByName = new Map();
|
|
21
22
|
this._typeCollisionWarned = new Set();
|
|
@@ -67,6 +68,17 @@ class PostHogMetrics {
|
|
|
67
68
|
this._flushPromise = p;
|
|
68
69
|
return p;
|
|
69
70
|
}
|
|
71
|
+
flushCoalesced() {
|
|
72
|
+
if (this._isBackingOff()) return this._flushPromise ?? Promise.resolve();
|
|
73
|
+
if (!this._queuedFlush) this._queuedFlush = (this._flushPromise ?? Promise.resolve()).catch(()=>{}).then(()=>{
|
|
74
|
+
this._queuedFlush = null;
|
|
75
|
+
return this._isBackingOff() ? void 0 : this.flush();
|
|
76
|
+
});
|
|
77
|
+
return this._queuedFlush;
|
|
78
|
+
}
|
|
79
|
+
_isBackingOff() {
|
|
80
|
+
return this._consecutiveFlushFailures > 0 || this._retryAfter.remainingMs() > 0;
|
|
81
|
+
}
|
|
70
82
|
drainWindow() {
|
|
71
83
|
if (0 === this._series.size) return null;
|
|
72
84
|
const window = this._series;
|
|
@@ -80,6 +92,7 @@ class PostHogMetrics {
|
|
|
80
92
|
this._flushJitter = NO_JITTER;
|
|
81
93
|
this._flushTimer.clear();
|
|
82
94
|
this._flushPromise = null;
|
|
95
|
+
this._queuedFlush = null;
|
|
83
96
|
this._startWindow();
|
|
84
97
|
this._boundsByHistogram = new Map();
|
|
85
98
|
this._boundsCapWarned = false;
|
package/package.json
CHANGED
|
@@ -59,6 +59,76 @@ describe('PostHogMetrics', () => {
|
|
|
59
59
|
vi.useRealTimers()
|
|
60
60
|
})
|
|
61
61
|
|
|
62
|
+
describe('flushCoalesced', () => {
|
|
63
|
+
it('keeps at most one send in flight and one queued behind it, and sends every sample', async () => {
|
|
64
|
+
let resolveSend!: (o: SendMetricsBatchOutcome) => void
|
|
65
|
+
mockInstance._sendMetricsBatch
|
|
66
|
+
.mockImplementationOnce(() => new Promise<SendMetricsBatchOutcome>((r) => (resolveSend = r)))
|
|
67
|
+
.mockResolvedValue({ kind: 'ok' })
|
|
68
|
+
const metrics = createMetrics()
|
|
69
|
+
|
|
70
|
+
metrics.count('requests', 1)
|
|
71
|
+
const first = metrics.flushCoalesced()
|
|
72
|
+
await Promise.resolve()
|
|
73
|
+
await Promise.resolve()
|
|
74
|
+
expect(mockInstance._sendMetricsBatch).toHaveBeenCalledTimes(1)
|
|
75
|
+
|
|
76
|
+
// Many requests finish while the first send is in flight: they share one queued flush.
|
|
77
|
+
const queued = [1, 2, 3, 4, 5].map(() => {
|
|
78
|
+
metrics.count('requests', 1)
|
|
79
|
+
return metrics.flushCoalesced()
|
|
80
|
+
})
|
|
81
|
+
expect(new Set(queued).size).toBe(1)
|
|
82
|
+
|
|
83
|
+
resolveSend({ kind: 'ok' })
|
|
84
|
+
await Promise.all([first, ...queued])
|
|
85
|
+
expect(mockInstance._sendMetricsBatch).toHaveBeenCalledTimes(2)
|
|
86
|
+
expect(sentMetrics().reduce((sum, m) => sum + m.sum!.dataPoints[0].asDouble!, 0)).toBe(6)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('does not start a queued flush when the send it waited behind starts a backoff', async () => {
|
|
90
|
+
let resolveSend!: (o: SendMetricsBatchOutcome) => void
|
|
91
|
+
mockInstance._sendMetricsBatch
|
|
92
|
+
.mockImplementationOnce(() => new Promise<SendMetricsBatchOutcome>((r) => (resolveSend = r)))
|
|
93
|
+
.mockResolvedValue({ kind: 'ok' })
|
|
94
|
+
const metrics = createMetrics()
|
|
95
|
+
|
|
96
|
+
metrics.count('requests', 1)
|
|
97
|
+
const first = metrics.flushCoalesced()
|
|
98
|
+
await Promise.resolve()
|
|
99
|
+
await Promise.resolve()
|
|
100
|
+
metrics.count('requests', 1)
|
|
101
|
+
const queued = metrics.flushCoalesced()
|
|
102
|
+
|
|
103
|
+
resolveSend({ kind: 'retry-later', error: new Error('429'), retryAfterMs: 300_000 })
|
|
104
|
+
await Promise.all([first, queued])
|
|
105
|
+
expect(mockInstance._sendMetricsBatch).toHaveBeenCalledTimes(1)
|
|
106
|
+
|
|
107
|
+
// The retry timer delivers the retained samples once the backoff ends.
|
|
108
|
+
await vi.advanceTimersByTimeAsync(300_000)
|
|
109
|
+
expect(mockInstance._sendMetricsBatch).toHaveBeenCalledTimes(2)
|
|
110
|
+
const retried = sentPayloads()[1].resourceMetrics[0].scopeMetrics[0].metrics
|
|
111
|
+
expect(retried[0].sum!.dataPoints[0].asDouble).toBe(2)
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
it('does not send while a failed send is backing off', async () => {
|
|
115
|
+
mockInstance._sendMetricsBatch.mockResolvedValueOnce({
|
|
116
|
+
kind: 'retry-later',
|
|
117
|
+
error: new Error('429'),
|
|
118
|
+
retryAfterMs: 300_000,
|
|
119
|
+
})
|
|
120
|
+
const metrics = createMetrics()
|
|
121
|
+
metrics.count('requests', 1)
|
|
122
|
+
await metrics.flushCoalesced()
|
|
123
|
+
expect(mockInstance._sendMetricsBatch).toHaveBeenCalledTimes(1)
|
|
124
|
+
|
|
125
|
+
metrics.count('requests', 1)
|
|
126
|
+
await metrics.flushCoalesced()
|
|
127
|
+
await metrics.flushCoalesced()
|
|
128
|
+
expect(mockInstance._sendMetricsBatch).toHaveBeenCalledTimes(1)
|
|
129
|
+
})
|
|
130
|
+
})
|
|
131
|
+
|
|
62
132
|
describe('aggregation semantics', () => {
|
|
63
133
|
it('sums count samples for the same series into one delta data point', async () => {
|
|
64
134
|
const metrics = createMetrics()
|
package/src/metrics/index.ts
CHANGED
|
@@ -90,6 +90,8 @@ export class PostHogMetrics {
|
|
|
90
90
|
// Serializes flushes — a manual flush() during an in-flight timer flush
|
|
91
91
|
// queues behind it instead of racing it for the same window.
|
|
92
92
|
private _flushPromise: Promise<void> | null = null
|
|
93
|
+
// The one flush `flushCoalesced()` has queued but not yet started.
|
|
94
|
+
private _queuedFlush: Promise<void> | null = null
|
|
93
95
|
// One cardinality warning per window, however many series get dropped.
|
|
94
96
|
private _seriesCapWarned = false
|
|
95
97
|
// Type seen per metric name this window; reusing a name with a different
|
|
@@ -157,6 +159,33 @@ export class PostHogMetrics {
|
|
|
157
159
|
return p
|
|
158
160
|
}
|
|
159
161
|
|
|
162
|
+
/**
|
|
163
|
+
* A flush for callers that ask once per unit of work (a served request, say) rather than once per window.
|
|
164
|
+
* Callers share one queued flush, so at most one send is in flight and one waits behind it however often
|
|
165
|
+
* this is called, and every sample captured before the call is in one of them. While a failed send is
|
|
166
|
+
* backing off it starts nothing and the retry timer owns the next send.
|
|
167
|
+
*/
|
|
168
|
+
flushCoalesced(): Promise<void> {
|
|
169
|
+
if (this._isBackingOff()) {
|
|
170
|
+
return this._flushPromise ?? Promise.resolve()
|
|
171
|
+
}
|
|
172
|
+
if (!this._queuedFlush) {
|
|
173
|
+
// Starts after any send in flight, so samples captured while it runs are in this one.
|
|
174
|
+
this._queuedFlush = (this._flushPromise ?? Promise.resolve())
|
|
175
|
+
.catch(() => {})
|
|
176
|
+
.then(() => {
|
|
177
|
+
this._queuedFlush = null
|
|
178
|
+
// The send it waited behind may have failed and started a backoff.
|
|
179
|
+
return this._isBackingOff() ? undefined : this.flush()
|
|
180
|
+
})
|
|
181
|
+
}
|
|
182
|
+
return this._queuedFlush
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private _isBackingOff(): boolean {
|
|
186
|
+
return this._consecutiveFlushFailures > 0 || this._retryAfter.remainingMs() > 0
|
|
187
|
+
}
|
|
188
|
+
|
|
160
189
|
/**
|
|
161
190
|
* Synchronously snapshots the current window into an OTLP payload and
|
|
162
191
|
* resets it, bypassing the flush serializer entirely — for unload-time
|
|
@@ -181,6 +210,7 @@ export class PostHogMetrics {
|
|
|
181
210
|
this._flushJitter = NO_JITTER
|
|
182
211
|
this._flushTimer.clear()
|
|
183
212
|
this._flushPromise = null
|
|
213
|
+
this._queuedFlush = null
|
|
184
214
|
this._startWindow()
|
|
185
215
|
this._boundsByHistogram = new Map()
|
|
186
216
|
this._boundsCapWarned = false
|