@posthog/core 1.66.0 → 1.67.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/dist/featureFlagLocalEvaluation.d.ts.map +1 -1
- package/dist/featureFlagLocalEvaluation.js +17 -2
- package/dist/featureFlagLocalEvaluation.mjs +17 -2
- 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/featureFlagLocalEvaluation.ts +23 -2
- package/src/metrics/index.spec.ts +70 -0
- package/src/metrics/index.ts +30 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"featureFlagLocalEvaluation.d.ts","sourceRoot":"","sources":["../src/featureFlagLocalEvaluation.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAEzD,MAAM,MAAM,wBAAwB,GAAG,QAAQ,CAAA;AAE/C,MAAM,MAAM,mBAAmB,GAAG;IAChC,GAAG,EAAE,MAAM,CAAA;IACX,KAAK,EAAE,wBAAwB,CAAA;IAC/B,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB,CAAA;AAED,MAAM,MAAM,8BAA8B,GAAG,QAAQ,GAAG,mBAAmB,CAAA;AAE3E,MAAM,MAAM,+BAA+B,GAAG;IAC5C,YAAY,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAA;IACxC,mBAAmB,CAAC,EAAE,8BAA8B,CAAA;IACpD,mGAAmG;IACnG,uBAAuB,CAAC,EAAE,MAAM,CAAA;CACjC,CAAA;AAED,MAAM,MAAM,kBAAkB,GAAG;IAC/B,GAAG,EAAE,MAAM,CAAA;IACX,kBAAkB,EAAE,MAAM,CAAA;CAC3B,CAAA;AAED,MAAM,MAAM,6BAA6B,GAAG;IAC1C,QAAQ,EAAE,MAAM,CAAA;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AAC,MAAM,CAAC,CAGjB;AAED;;;;;;GAMG;AACH,wBAAsB,iBAAiB,CACrC,OAAO,EAAE;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,oBAAoB,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,GAAG,SAAS,EACxE,cAAc,EAAE,MAAM,GACrB,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CA0B7B;AAED,wBAAgB,gCAAgC,CAC9C,QAAQ,EAAE,SAAS,kBAAkB,EAAE,GACtC,6BAA6B,EAAE,CASjC;AAED,wBAAsB,qBAAqB,CACzC,GAAG,EAAE,MAAM,EACX,cAAc,EAAE,MAAM,EACtB,QAAQ,EAAE,SAAS,kBAAkB,EAAE,GACtC,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAK7B;AAED,wBAAgB,yBAAyB,CACvC,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,GAAG,SAAS,CAAC,CAAC,GAAG,IAAI,GAAG,SAAS,EAC3E,SAAS,EAAE,gBAAgB,GAAG,IAAI,GAAG,SAAS,GAC7C,QAAQ,GAAG,IAAI,CAMjB"}
|
|
@@ -417,13 +417,28 @@ function relativeDateParseForFeatureFlagMatching(value) {
|
|
|
417
417
|
if ('h' === interval) parsedDt.setUTCHours(parsedDt.getUTCHours() - number);
|
|
418
418
|
else if ('d' === interval) parsedDt.setUTCDate(parsedDt.getUTCDate() - number);
|
|
419
419
|
else if ('w' === interval) parsedDt.setUTCDate(parsedDt.getUTCDate() - 7 * number);
|
|
420
|
-
else if ('m' === interval)
|
|
420
|
+
else if ('m' === interval) subtractUTCMonths(parsedDt, number);
|
|
421
421
|
else {
|
|
422
422
|
if ('y' !== interval) return null;
|
|
423
|
-
|
|
423
|
+
subtractUTCYears(parsedDt, number);
|
|
424
424
|
}
|
|
425
425
|
return parsedDt;
|
|
426
426
|
}
|
|
427
|
+
function subtractUTCMonths(date, months) {
|
|
428
|
+
const year = date.getUTCFullYear();
|
|
429
|
+
const month = date.getUTCMonth();
|
|
430
|
+
let day = date.getUTCDate();
|
|
431
|
+
for(let step = 1; step <= months && day > 28; step++){
|
|
432
|
+
const lastDayOfMonth = new Date(Date.UTC(year, month - step + 1, 0)).getUTCDate();
|
|
433
|
+
day = Math.min(day, lastDayOfMonth);
|
|
434
|
+
}
|
|
435
|
+
date.setUTCFullYear(year, month - months, day);
|
|
436
|
+
}
|
|
437
|
+
function subtractUTCYears(date, years) {
|
|
438
|
+
const month = date.getUTCMonth();
|
|
439
|
+
const day = years > 0 && 1 === month && 29 === date.getUTCDate() ? 28 : date.getUTCDate();
|
|
440
|
+
date.setUTCFullYear(date.getUTCFullYear() - years, month, day);
|
|
441
|
+
}
|
|
427
442
|
function parseDateFilter(value) {
|
|
428
443
|
if ('boolean' == typeof value) throw new InconclusiveMatchError('Date operations cannot be performed on boolean values');
|
|
429
444
|
return relativeDateParseForFeatureFlagMatching(String(value)) ?? convertToDateTime(value);
|
|
@@ -375,13 +375,28 @@ function relativeDateParseForFeatureFlagMatching(value) {
|
|
|
375
375
|
if ('h' === interval) parsedDt.setUTCHours(parsedDt.getUTCHours() - number);
|
|
376
376
|
else if ('d' === interval) parsedDt.setUTCDate(parsedDt.getUTCDate() - number);
|
|
377
377
|
else if ('w' === interval) parsedDt.setUTCDate(parsedDt.getUTCDate() - 7 * number);
|
|
378
|
-
else if ('m' === interval)
|
|
378
|
+
else if ('m' === interval) subtractUTCMonths(parsedDt, number);
|
|
379
379
|
else {
|
|
380
380
|
if ('y' !== interval) return null;
|
|
381
|
-
|
|
381
|
+
subtractUTCYears(parsedDt, number);
|
|
382
382
|
}
|
|
383
383
|
return parsedDt;
|
|
384
384
|
}
|
|
385
|
+
function subtractUTCMonths(date, months) {
|
|
386
|
+
const year = date.getUTCFullYear();
|
|
387
|
+
const month = date.getUTCMonth();
|
|
388
|
+
let day = date.getUTCDate();
|
|
389
|
+
for(let step = 1; step <= months && day > 28; step++){
|
|
390
|
+
const lastDayOfMonth = new Date(Date.UTC(year, month - step + 1, 0)).getUTCDate();
|
|
391
|
+
day = Math.min(day, lastDayOfMonth);
|
|
392
|
+
}
|
|
393
|
+
date.setUTCFullYear(year, month - months, day);
|
|
394
|
+
}
|
|
395
|
+
function subtractUTCYears(date, years) {
|
|
396
|
+
const month = date.getUTCMonth();
|
|
397
|
+
const day = years > 0 && 1 === month && 29 === date.getUTCDate() ? 28 : date.getUTCDate();
|
|
398
|
+
date.setUTCFullYear(date.getUTCFullYear() - years, month, day);
|
|
399
|
+
}
|
|
385
400
|
function parseDateFilter(value) {
|
|
386
401
|
if ('boolean' == typeof value) throw new InconclusiveMatchError('Date operations cannot be performed on boolean values');
|
|
387
402
|
return relativeDateParseForFeatureFlagMatching(String(value)) ?? convertToDateTime(value);
|
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,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AAetC,OAAO,KAAK,EAAE,WAAW,EAAE,4BAA4B,EAAE,MAAM,SAAS,CAAA;AAqCxE;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,cAAc;
|
|
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,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AAetC,OAAO,KAAK,EAAE,WAAW,EAAE,4BAA4B,EAAE,MAAM,SAAS,CAAA;AAqCxE;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,cAAc;IAoCvB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,OAAO;IArC1B,OAAO,CAAC,OAAO,CAAiC;IAChD,OAAO,CAAC,QAAQ,CAAC,WAAW,CAI3B;IAGD,OAAO,CAAC,aAAa,CAA6B;IAElD,OAAO,CAAC,YAAY,CAA6B;IAEjD,OAAO,CAAC,gBAAgB,CAAQ;IAIhC,OAAO,CAAC,WAAW,CAAgC;IACnD,OAAO,CAAC,oBAAoB,CAAoB;IAKhD,OAAO,CAAC,kBAAkB,CAA8B;IAExD,OAAO,CAAC,qBAAqB,CAAoB;IACjD,OAAO,CAAC,gBAAgB,CAAQ;IAChC,OAAO,CAAC,WAAW,CAAyB;IAC5C,OAAO,CAAC,yBAAy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,8BAA8B,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAA;AACnH,OAAO,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAA;AAC/C,YAAY,EAAE,WAAW,EAAE,oBAAoB,EAAE,4BAA4B,EAAE,uBAAuB,EAAE,MAAM,SAAS,CAAA"}
|
package/dist/metrics/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
|
@@ -478,13 +478,34 @@ export function relativeDateParseForFeatureFlagMatching(value: string): Date | n
|
|
|
478
478
|
if (interval === 'h') parsedDt.setUTCHours(parsedDt.getUTCHours() - number)
|
|
479
479
|
else if (interval === 'd') parsedDt.setUTCDate(parsedDt.getUTCDate() - number)
|
|
480
480
|
else if (interval === 'w') parsedDt.setUTCDate(parsedDt.getUTCDate() - number * 7)
|
|
481
|
-
else if (interval === 'm')
|
|
482
|
-
else if (interval === 'y')
|
|
481
|
+
else if (interval === 'm') subtractUTCMonths(parsedDt, number)
|
|
482
|
+
else if (interval === 'y') subtractUTCYears(parsedDt, number)
|
|
483
483
|
else return null
|
|
484
484
|
|
|
485
485
|
return parsedDt
|
|
486
486
|
}
|
|
487
487
|
|
|
488
|
+
// The SDK spec subtracts months one at a time and clamps the day after every step, so
|
|
489
|
+
// March 31 minus one month is February 28 and minus two months is January 28.
|
|
490
|
+
function subtractUTCMonths(date: Date, months: number): void {
|
|
491
|
+
const year = date.getUTCFullYear()
|
|
492
|
+
const month = date.getUTCMonth()
|
|
493
|
+
let day = date.getUTCDate()
|
|
494
|
+
// Every month has at least 28 days, so nothing can clamp further after that.
|
|
495
|
+
for (let step = 1; step <= months && day > 28; step++) {
|
|
496
|
+
const lastDayOfMonth = new Date(Date.UTC(year, month - step + 1, 0)).getUTCDate()
|
|
497
|
+
day = Math.min(day, lastDayOfMonth)
|
|
498
|
+
}
|
|
499
|
+
date.setUTCFullYear(year, month - months, day)
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
function subtractUTCYears(date: Date, years: number): void {
|
|
503
|
+
const month = date.getUTCMonth()
|
|
504
|
+
// The year before a leap year is never a leap year, so February 29 clamps on the first step.
|
|
505
|
+
const day = years > 0 && month === 1 && date.getUTCDate() === 29 ? 28 : date.getUTCDate()
|
|
506
|
+
date.setUTCFullYear(date.getUTCFullYear() - years, month, day)
|
|
507
|
+
}
|
|
508
|
+
|
|
488
509
|
function parseDateFilter(value: FeatureFlagPropertyValue): Date {
|
|
489
510
|
if (typeof value === 'boolean') {
|
|
490
511
|
throw new InconclusiveMatchError('Date operations cannot be performed on boolean values')
|
|
@@ -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
|