@posthog/core 1.63.2 → 1.64.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/error-tracking/parsers/node.d.ts.map +1 -1
- package/dist/error-tracking/parsers/node.js +1 -1
- package/dist/error-tracking/parsers/node.mjs +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/metrics/index.d.ts +15 -2
- package/dist/metrics/index.d.ts.map +1 -1
- package/dist/metrics/index.js +88 -35
- package/dist/metrics/index.mjs +88 -35
- package/dist/metrics/types.d.ts +1 -1
- package/dist/metrics/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/error-tracking/parsers/node.spec.ts +15 -0
- package/src/error-tracking/parsers/node.ts +2 -1
- package/src/index.ts +1 -0
- package/src/metrics/index.spec.ts +278 -0
- package/src/metrics/index.ts +160 -44
- package/src/metrics/types.ts +1 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../../../src/error-tracking/parsers/node.ts"],"names":[],"mappings":"AAIA,OAAO,EAAY,eAAe,EAAE,MAAM,UAAU,CAAA;
|
|
1
|
+
{"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../../../src/error-tracking/parsers/node.ts"],"names":[],"mappings":"AAIA,OAAO,EAAY,eAAe,EAAE,MAAM,UAAU,CAAA;AAWpD,eAAO,MAAM,mBAAmB,EAAE,eAiFjC,CAAA"}
|
|
@@ -32,7 +32,7 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
32
32
|
});
|
|
33
33
|
const external_base_js_namespaceObject = require("./base.js");
|
|
34
34
|
const FILENAME_MATCH = /^\s*[-]{4,}$/;
|
|
35
|
-
const FULL_MATCH = /at (?:async )?(?:(.+?)\s+\()?(?:(.+):(\d+):(\d+)?|([^)]+))\)?/;
|
|
35
|
+
const FULL_MATCH = /at (?:async (?!\((?!.*\)\s+\()))?(?:(.+?)\s+\()?(?:(.+):(\d+):(\d+)?|([^)]+))\)?/;
|
|
36
36
|
const PROMISE_COMBINATOR = /^Promise\.(?:all|any)$/;
|
|
37
37
|
const PROMISE_INDEX = /^index \d+$/;
|
|
38
38
|
const PROMISE_FRAME_FILENAME = 'node:internal/promise';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { UNKNOWN_FUNCTION } from "./base.mjs";
|
|
2
2
|
const FILENAME_MATCH = /^\s*[-]{4,}$/;
|
|
3
|
-
const FULL_MATCH = /at (?:async )?(?:(.+?)\s+\()?(?:(.+):(\d+):(\d+)?|([^)]+))\)?/;
|
|
3
|
+
const FULL_MATCH = /at (?:async (?!\((?!.*\)\s+\()))?(?:(.+?)\s+\()?(?:(.+):(\d+):(\d+)?|([^)]+))\)?/;
|
|
4
4
|
const PROMISE_COMBINATOR = /^Promise\.(?:all|any)$/;
|
|
5
5
|
const PROMISE_INDEX = /^index \d+$/;
|
|
6
6
|
const PROMISE_FRAME_FILENAME = 'node:internal/promise';
|
package/dist/index.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export type { ExceptionRateLimiterConfig } from '@posthog/types';
|
|
|
14
14
|
export type { Property, Properties } from '@posthog/types';
|
|
15
15
|
export { PostHogMetrics, buildOtlpMetricsPayload, buildMetricsResourceAttributes, DEFAULT_HISTOGRAM_BOUNDS, resolveMetricsConfig, } from './metrics';
|
|
16
16
|
export type { MetricsHost, PostHogMetricsConfig, ResolvedPostHogMetricsConfig, SendMetricsBatchOutcome, } from './metrics';
|
|
17
|
-
export type { CaptureMetricOptions, MetricAttributes, MetricAttributeValue, MetricSample, MetricType, Metrics, MetricsConfig, } from './metrics/types';
|
|
17
|
+
export type { CaptureMetricOptions, HistogramOptions, MetricAttributes, MetricAttributeValue, MetricSample, MetricType, Metrics, MetricsConfig, } from './metrics/types';
|
|
18
18
|
export { PostHogTraces } from './traces';
|
|
19
19
|
export { SyncSpanContextManager } from './traces/context';
|
|
20
20
|
export { inertSpan, runWithActiveSpan } from './traces/span';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,EACnB,YAAY,EACZ,oBAAoB,EACpB,6CAA6C,EAC7C,iCAAiC,GAClC,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EACL,gCAAgC,EAChC,kBAAkB,EAClB,qBAAqB,EACrB,gCAAgC,EAChC,iBAAiB,EACjB,QAAQ,EACR,sBAAsB,EACtB,wBAAwB,EACxB,sBAAsB,EACtB,uCAAuC,EACvC,yBAAyB,GAC1B,MAAM,8BAA8B,CAAA;AACrC,YAAY,EACV,mBAAmB,EACnB,wBAAwB,EACxB,8BAA8B,EAC9B,kBAAkB,EAClB,6BAA6B,EAC7B,+BAA+B,EAC/B,oBAAoB,GACrB,MAAM,8BAA8B,CAAA;AACrC,OAAO,EACL,YAAY,EACZ,UAAU,EACV,aAAa,EACb,eAAe,EACf,sBAAsB,EACtB,0BAA0B,GAC3B,MAAM,QAAQ,CAAA;AACf,cAAc,SAAS,CAAA;AACvB,OAAO,KAAK,aAAa,MAAM,kBAAkB,CAAA;AACjD,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EACpB,uBAAuB,EACvB,qBAAqB,EACrB,mBAAmB,GACpB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAA;AAC3E,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAA;AAC5D,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAA;AACpC,YAAY,EACV,eAAe,EACf,gBAAgB,EAChB,aAAa,EACb,aAAa,EACb,iBAAiB,EACjB,yBAAyB,GAC1B,MAAM,cAAc,CAAA;AAIrB,YAAY,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,aAAa,EAAE,gBA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AAA"}
|
package/dist/metrics/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CaptureMetricOptions, OtlpMetricsPayload } from '@posthog/types';
|
|
1
|
+
import type { CaptureMetricOptions, HistogramOptions, OtlpMetricsPayload } from '@posthog/types';
|
|
2
2
|
import type { Logger } from '../types';
|
|
3
3
|
import type { MetricsHost, ResolvedPostHogMetricsConfig } from './types';
|
|
4
4
|
/**
|
|
@@ -29,6 +29,9 @@ export declare class PostHogMetrics {
|
|
|
29
29
|
private _seriesCapWarned;
|
|
30
30
|
private _typeByName;
|
|
31
31
|
private _typeCollisionWarned;
|
|
32
|
+
private _boundsByHistogram;
|
|
33
|
+
private _boundsConflictWarned;
|
|
34
|
+
private _boundsCapWarned;
|
|
32
35
|
private _retryAfter;
|
|
33
36
|
private _consecutiveFlushFailures;
|
|
34
37
|
private _flushJitter;
|
|
@@ -36,7 +39,7 @@ export declare class PostHogMetrics {
|
|
|
36
39
|
constructor(_instance: MetricsHost, _config: ResolvedPostHogMetricsConfig, _logger: Logger);
|
|
37
40
|
count(name: string, value?: number, options?: CaptureMetricOptions): void;
|
|
38
41
|
gauge(name: string, value: number, options?: CaptureMetricOptions): void;
|
|
39
|
-
histogram(name: string, value: number, options?:
|
|
42
|
+
histogram(name: string, value: number, options?: HistogramOptions): void;
|
|
40
43
|
/** Sends everything aggregated so far without waiting for the flush interval. */
|
|
41
44
|
flush(): Promise<void>;
|
|
42
45
|
/**
|
|
@@ -49,6 +52,8 @@ export declare class PostHogMetrics {
|
|
|
49
52
|
drainWindow(): OtlpMetricsPayload | null;
|
|
50
53
|
/** Clears the flush timer, drops the current window, and invalidates in-flight flushes. */
|
|
51
54
|
reset(): void;
|
|
55
|
+
/** Clears the state that lives for one flush window. */
|
|
56
|
+
private _startWindow;
|
|
52
57
|
private _capture;
|
|
53
58
|
/**
|
|
54
59
|
* The sampled span a sample is recorded in. An unsampled span is never
|
|
@@ -60,6 +65,14 @@ export declare class PostHogMetrics {
|
|
|
60
65
|
* window when the cap is hit. Applied on capture and on merge-back alike.
|
|
61
66
|
*/
|
|
62
67
|
private _admitNewSeries;
|
|
68
|
+
/**
|
|
69
|
+
* The bounds a histogram sample folds into: the ones its histogram already
|
|
70
|
+
* has, or the requested bounds (default when absent) for a new histogram.
|
|
71
|
+
* A histogram past the bounds limit has no map entry and gets only the
|
|
72
|
+
* default bounds. Returns `undefined` when the sample must be dropped.
|
|
73
|
+
*/
|
|
74
|
+
private _histogramBounds;
|
|
75
|
+
private _warnOnBoundsConflict;
|
|
63
76
|
private _fold;
|
|
64
77
|
private _runBeforeSend;
|
|
65
78
|
private _armFlushTimer;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/metrics/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,oBAAoB,
|
|
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;IAkCvB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,OAAO;IAnC1B,OAAO,CAAC,OAAO,CAAiC;IAChD,OAAO,CAAC,QAAQ,CAAC,WAAW,CAI3B;IAGD,OAAO,CAAC,aAAa,CAA6B;IAElD,OAAO,CAAC,gBAAgB,CAAQ;IAIhC,OAAO,CAAC,WAAW,CAAgC;IACnD,OAAO,CAAC,oBAAoB,CAAoB;IAKhD,OAAO,CAAC,kBA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AAgB;CAoCzB;AAoDD,OAAO,EAAE,uBAAuB,EAAE,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
|
@@ -56,6 +56,9 @@ class PostHogMetrics {
|
|
|
56
56
|
this._seriesCapWarned = false;
|
|
57
57
|
this._typeByName = new Map();
|
|
58
58
|
this._typeCollisionWarned = new Set();
|
|
59
|
+
this._boundsByHistogram = new Map();
|
|
60
|
+
this._boundsConflictWarned = new Set();
|
|
61
|
+
this._boundsCapWarned = false;
|
|
59
62
|
this._retryAfter = new retry_after_js_namespaceObject.RetryAfterWindow();
|
|
60
63
|
this._consecutiveFlushFailures = 0;
|
|
61
64
|
this._flushJitter = backoff_js_namespaceObject.NO_JITTER;
|
|
@@ -85,7 +88,8 @@ class PostHogMetrics {
|
|
|
85
88
|
type: 'histogram',
|
|
86
89
|
value,
|
|
87
90
|
unit: options?.unit,
|
|
88
|
-
attributes: options?.attributes
|
|
91
|
+
attributes: options?.attributes,
|
|
92
|
+
bounds: (0, index_js_namespaceObject.isArray)(options?.bounds) ? options.bounds.slice() : options?.bounds
|
|
89
93
|
});
|
|
90
94
|
}
|
|
91
95
|
flush() {
|
|
@@ -103,10 +107,7 @@ class PostHogMetrics {
|
|
|
103
107
|
drainWindow() {
|
|
104
108
|
if (0 === this._series.size) return null;
|
|
105
109
|
const window = this._series;
|
|
106
|
-
this.
|
|
107
|
-
this._seriesCapWarned = false;
|
|
108
|
-
this._typeByName = new Map();
|
|
109
|
-
this._typeCollisionWarned = new Set();
|
|
110
|
+
this._startWindow();
|
|
110
111
|
return this._buildPayload(window);
|
|
111
112
|
}
|
|
112
113
|
reset() {
|
|
@@ -115,11 +116,17 @@ class PostHogMetrics {
|
|
|
115
116
|
this._consecutiveFlushFailures = 0;
|
|
116
117
|
this._flushJitter = backoff_js_namespaceObject.NO_JITTER;
|
|
117
118
|
this._flushTimer.clear();
|
|
118
|
-
this._series = new Map();
|
|
119
119
|
this._flushPromise = null;
|
|
120
|
+
this._startWindow();
|
|
121
|
+
this._boundsByHistogram = new Map();
|
|
122
|
+
this._boundsCapWarned = false;
|
|
123
|
+
}
|
|
124
|
+
_startWindow() {
|
|
125
|
+
this._series = new Map();
|
|
120
126
|
this._seriesCapWarned = false;
|
|
121
127
|
this._typeByName = new Map();
|
|
122
128
|
this._typeCollisionWarned = new Set();
|
|
129
|
+
this._boundsConflictWarned = new Set();
|
|
123
130
|
}
|
|
124
131
|
_capture(sample) {
|
|
125
132
|
if (this._instance.isDisabled || this._instance.optedOut) return;
|
|
@@ -127,6 +134,7 @@ class PostHogMetrics {
|
|
|
127
134
|
if (null === filtered) return;
|
|
128
135
|
if (!filtered.name || 'string' != typeof filtered.name) return void this._logger.warn('Dropping metric with empty name');
|
|
129
136
|
if ('number' != typeof filtered.value || !Number.isFinite(filtered.value)) return void this._logger.warn(`Dropping metric '${filtered.name}': value must be a finite number`);
|
|
137
|
+
if ('count' !== filtered.type && 'gauge' !== filtered.type && 'histogram' !== filtered.type) return void this._logger.warn(`Dropping metric '${filtered.name}': unknown type '${String(filtered.type)}'`);
|
|
130
138
|
if ('count' === filtered.type && filtered.value < 0) return void this._logger.warn(`Dropping count '${filtered.name}': counters are monotonic, value must be >= 0`);
|
|
131
139
|
let attributes;
|
|
132
140
|
let key;
|
|
@@ -140,14 +148,20 @@ class PostHogMetrics {
|
|
|
140
148
|
return;
|
|
141
149
|
}
|
|
142
150
|
let state = this._series.get(key);
|
|
151
|
+
if (!state && !this._admitNewSeries()) return;
|
|
152
|
+
let bounds;
|
|
153
|
+
if ('histogram' === filtered.type) {
|
|
154
|
+
bounds = this._histogramBounds(filtered.name, filtered.unit, filtered.bounds);
|
|
155
|
+
if (!bounds) return;
|
|
156
|
+
}
|
|
143
157
|
if (!state) {
|
|
144
|
-
if (!this._admitNewSeries()) return;
|
|
145
158
|
state = {
|
|
146
159
|
name: filtered.name,
|
|
147
160
|
type: filtered.type,
|
|
148
161
|
unit: filtered.unit,
|
|
149
162
|
attributes,
|
|
150
|
-
windowStartMs: Date.now()
|
|
163
|
+
windowStartMs: Date.now(),
|
|
164
|
+
hist: bounds && emptyHistogram(bounds)
|
|
151
165
|
};
|
|
152
166
|
this._series.set(key, state);
|
|
153
167
|
}
|
|
@@ -183,6 +197,31 @@ class PostHogMetrics {
|
|
|
183
197
|
}
|
|
184
198
|
return false;
|
|
185
199
|
}
|
|
200
|
+
_histogramBounds(name, unit, requested) {
|
|
201
|
+
if (void 0 !== requested && !isValidBounds(requested)) return void this._logger.warn(`Dropping histogram '${name}': bounds must be a non-empty array of finite, strictly increasing numbers`);
|
|
202
|
+
const histogramKey = (0, external_metrics_utils_js_namespaceObject.seriesKey)('histogram', name, unit, void 0);
|
|
203
|
+
const fixed = this._boundsByHistogram.get(histogramKey);
|
|
204
|
+
if (fixed) {
|
|
205
|
+
this._warnOnBoundsConflict(name, histogramKey, fixed, requested);
|
|
206
|
+
return fixed;
|
|
207
|
+
}
|
|
208
|
+
if (this._boundsByHistogram.size >= this._config.maxSeriesPerFlush) {
|
|
209
|
+
if (!this._boundsCapWarned) {
|
|
210
|
+
this._boundsCapWarned = true;
|
|
211
|
+
this._logger.warn(`Histogram bounds limit reached (${this._config.maxSeriesPerFlush} distinct histograms); new histograms that pass custom bounds are dropped. Reduce histogram name cardinality.`);
|
|
212
|
+
}
|
|
213
|
+
if (requested && !sameBounds(requested, external_metrics_utils_js_namespaceObject.DEFAULT_HISTOGRAM_BOUNDS)) return;
|
|
214
|
+
return external_metrics_utils_js_namespaceObject.DEFAULT_HISTOGRAM_BOUNDS;
|
|
215
|
+
}
|
|
216
|
+
const bounds = requested ? requested.slice() : external_metrics_utils_js_namespaceObject.DEFAULT_HISTOGRAM_BOUNDS;
|
|
217
|
+
this._boundsByHistogram.set(histogramKey, bounds);
|
|
218
|
+
return bounds;
|
|
219
|
+
}
|
|
220
|
+
_warnOnBoundsConflict(name, histogramKey, fixed, requested) {
|
|
221
|
+
if (sameBounds(fixed, requested ?? external_metrics_utils_js_namespaceObject.DEFAULT_HISTOGRAM_BOUNDS) || this._boundsConflictWarned.has(histogramKey)) return;
|
|
222
|
+
this._boundsConflictWarned.add(histogramKey);
|
|
223
|
+
this._logger.warn(`Histogram '${name}' already uses the bounds [${fixed.join(', ')}]; samples that pass different bounds are recorded in those buckets.`);
|
|
224
|
+
}
|
|
186
225
|
_fold(state, value) {
|
|
187
226
|
switch(state.type){
|
|
188
227
|
case 'count':
|
|
@@ -193,19 +232,12 @@ class PostHogMetrics {
|
|
|
193
232
|
break;
|
|
194
233
|
case 'histogram':
|
|
195
234
|
{
|
|
196
|
-
if (!state.hist) state.hist = {
|
|
197
|
-
count: 0,
|
|
198
|
-
sum: 0,
|
|
199
|
-
min: value,
|
|
200
|
-
max: value,
|
|
201
|
-
bucketCounts: new Array(external_metrics_utils_js_namespaceObject.DEFAULT_HISTOGRAM_BOUNDS.length + 1).fill(0)
|
|
202
|
-
};
|
|
203
235
|
const hist = state.hist;
|
|
204
236
|
hist.count += 1;
|
|
205
237
|
hist.sum += value;
|
|
206
238
|
hist.min = Math.min(hist.min, value);
|
|
207
239
|
hist.max = Math.max(hist.max, value);
|
|
208
|
-
hist.bucketCounts[(0, external_metrics_utils_js_namespaceObject.bucketIndexFor)(value,
|
|
240
|
+
hist.bucketCounts[(0, external_metrics_utils_js_namespaceObject.bucketIndexFor)(value, hist.bounds)] += 1;
|
|
209
241
|
break;
|
|
210
242
|
}
|
|
211
243
|
}
|
|
@@ -241,10 +273,7 @@ class PostHogMetrics {
|
|
|
241
273
|
this._flushTimer.clear();
|
|
242
274
|
if (0 === this._series.size) return;
|
|
243
275
|
const window = this._series;
|
|
244
|
-
this.
|
|
245
|
-
this._seriesCapWarned = false;
|
|
246
|
-
this._typeByName = new Map();
|
|
247
|
-
this._typeCollisionWarned = new Set();
|
|
276
|
+
this._startWindow();
|
|
248
277
|
const generation = this._generation;
|
|
249
278
|
const outcome = await this._instance._sendMetricsBatch(this._buildPayload(window));
|
|
250
279
|
if (generation !== this._generation) return;
|
|
@@ -322,17 +351,18 @@ class PostHogMetrics {
|
|
|
322
351
|
...exemplars
|
|
323
352
|
};
|
|
324
353
|
metric.gauge.dataPoints.push(dp);
|
|
325
|
-
} else
|
|
354
|
+
} else {
|
|
355
|
+
const hist = state.hist;
|
|
326
356
|
const dp = {
|
|
327
357
|
attributes,
|
|
328
358
|
startTimeUnixNano: startNano,
|
|
329
359
|
timeUnixNano: nowNano,
|
|
330
|
-
count:
|
|
331
|
-
sum:
|
|
332
|
-
min:
|
|
333
|
-
max:
|
|
334
|
-
bucketCounts:
|
|
335
|
-
explicitBounds:
|
|
360
|
+
count: hist.count,
|
|
361
|
+
sum: hist.sum,
|
|
362
|
+
min: hist.min,
|
|
363
|
+
max: hist.max,
|
|
364
|
+
bucketCounts: hist.bucketCounts,
|
|
365
|
+
explicitBounds: hist.bounds,
|
|
336
366
|
...exemplars
|
|
337
367
|
};
|
|
338
368
|
metric.histogram.dataPoints.push(dp);
|
|
@@ -356,18 +386,41 @@ class PostHogMetrics {
|
|
|
356
386
|
case 'gauge':
|
|
357
387
|
break;
|
|
358
388
|
case 'histogram':
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
389
|
+
{
|
|
390
|
+
const hist = current.hist;
|
|
391
|
+
const oldHist = old.hist;
|
|
392
|
+
hist.count += oldHist.count;
|
|
393
|
+
hist.sum += oldHist.sum;
|
|
394
|
+
hist.min = Math.min(hist.min, oldHist.min);
|
|
395
|
+
hist.max = Math.max(hist.max, oldHist.max);
|
|
396
|
+
for(let i = 0; i < hist.bucketCounts.length; i++)hist.bucketCounts[i] += oldHist.bucketCounts[i];
|
|
397
|
+
break;
|
|
398
|
+
}
|
|
367
399
|
}
|
|
368
400
|
}
|
|
369
401
|
}
|
|
370
402
|
}
|
|
403
|
+
function isValidBounds(bounds) {
|
|
404
|
+
if (!(0, index_js_namespaceObject.isArray)(bounds) || 0 === bounds.length) return false;
|
|
405
|
+
for(let i = 0; i < bounds.length; i++)if (!Number.isFinite(bounds[i]) || i > 0 && bounds[i] <= bounds[i - 1]) return false;
|
|
406
|
+
return true;
|
|
407
|
+
}
|
|
408
|
+
function sameBounds(a, b) {
|
|
409
|
+
if (a === b) return true;
|
|
410
|
+
if (a.length !== b.length) return false;
|
|
411
|
+
for(let i = 0; i < a.length; i++)if (a[i] !== b[i]) return false;
|
|
412
|
+
return true;
|
|
413
|
+
}
|
|
414
|
+
function emptyHistogram(bounds) {
|
|
415
|
+
return {
|
|
416
|
+
bounds,
|
|
417
|
+
count: 0,
|
|
418
|
+
sum: 0,
|
|
419
|
+
min: 1 / 0,
|
|
420
|
+
max: -1 / 0,
|
|
421
|
+
bucketCounts: new Array(bounds.length + 1).fill(0)
|
|
422
|
+
};
|
|
423
|
+
}
|
|
371
424
|
function toOtlpExemplars(exemplar) {
|
|
372
425
|
if (!exemplar) return {};
|
|
373
426
|
return {
|
package/dist/metrics/index.mjs
CHANGED
|
@@ -19,6 +19,9 @@ class PostHogMetrics {
|
|
|
19
19
|
this._seriesCapWarned = false;
|
|
20
20
|
this._typeByName = new Map();
|
|
21
21
|
this._typeCollisionWarned = new Set();
|
|
22
|
+
this._boundsByHistogram = new Map();
|
|
23
|
+
this._boundsConflictWarned = new Set();
|
|
24
|
+
this._boundsCapWarned = false;
|
|
22
25
|
this._retryAfter = new RetryAfterWindow();
|
|
23
26
|
this._consecutiveFlushFailures = 0;
|
|
24
27
|
this._flushJitter = NO_JITTER;
|
|
@@ -48,7 +51,8 @@ class PostHogMetrics {
|
|
|
48
51
|
type: 'histogram',
|
|
49
52
|
value,
|
|
50
53
|
unit: options?.unit,
|
|
51
|
-
attributes: options?.attributes
|
|
54
|
+
attributes: options?.attributes,
|
|
55
|
+
bounds: isArray(options?.bounds) ? options.bounds.slice() : options?.bounds
|
|
52
56
|
});
|
|
53
57
|
}
|
|
54
58
|
flush() {
|
|
@@ -66,10 +70,7 @@ class PostHogMetrics {
|
|
|
66
70
|
drainWindow() {
|
|
67
71
|
if (0 === this._series.size) return null;
|
|
68
72
|
const window = this._series;
|
|
69
|
-
this.
|
|
70
|
-
this._seriesCapWarned = false;
|
|
71
|
-
this._typeByName = new Map();
|
|
72
|
-
this._typeCollisionWarned = new Set();
|
|
73
|
+
this._startWindow();
|
|
73
74
|
return this._buildPayload(window);
|
|
74
75
|
}
|
|
75
76
|
reset() {
|
|
@@ -78,11 +79,17 @@ class PostHogMetrics {
|
|
|
78
79
|
this._consecutiveFlushFailures = 0;
|
|
79
80
|
this._flushJitter = NO_JITTER;
|
|
80
81
|
this._flushTimer.clear();
|
|
81
|
-
this._series = new Map();
|
|
82
82
|
this._flushPromise = null;
|
|
83
|
+
this._startWindow();
|
|
84
|
+
this._boundsByHistogram = new Map();
|
|
85
|
+
this._boundsCapWarned = false;
|
|
86
|
+
}
|
|
87
|
+
_startWindow() {
|
|
88
|
+
this._series = new Map();
|
|
83
89
|
this._seriesCapWarned = false;
|
|
84
90
|
this._typeByName = new Map();
|
|
85
91
|
this._typeCollisionWarned = new Set();
|
|
92
|
+
this._boundsConflictWarned = new Set();
|
|
86
93
|
}
|
|
87
94
|
_capture(sample) {
|
|
88
95
|
if (this._instance.isDisabled || this._instance.optedOut) return;
|
|
@@ -90,6 +97,7 @@ class PostHogMetrics {
|
|
|
90
97
|
if (null === filtered) return;
|
|
91
98
|
if (!filtered.name || 'string' != typeof filtered.name) return void this._logger.warn('Dropping metric with empty name');
|
|
92
99
|
if ('number' != typeof filtered.value || !Number.isFinite(filtered.value)) return void this._logger.warn(`Dropping metric '${filtered.name}': value must be a finite number`);
|
|
100
|
+
if ('count' !== filtered.type && 'gauge' !== filtered.type && 'histogram' !== filtered.type) return void this._logger.warn(`Dropping metric '${filtered.name}': unknown type '${String(filtered.type)}'`);
|
|
93
101
|
if ('count' === filtered.type && filtered.value < 0) return void this._logger.warn(`Dropping count '${filtered.name}': counters are monotonic, value must be >= 0`);
|
|
94
102
|
let attributes;
|
|
95
103
|
let key;
|
|
@@ -103,14 +111,20 @@ class PostHogMetrics {
|
|
|
103
111
|
return;
|
|
104
112
|
}
|
|
105
113
|
let state = this._series.get(key);
|
|
114
|
+
if (!state && !this._admitNewSeries()) return;
|
|
115
|
+
let bounds;
|
|
116
|
+
if ('histogram' === filtered.type) {
|
|
117
|
+
bounds = this._histogramBounds(filtered.name, filtered.unit, filtered.bounds);
|
|
118
|
+
if (!bounds) return;
|
|
119
|
+
}
|
|
106
120
|
if (!state) {
|
|
107
|
-
if (!this._admitNewSeries()) return;
|
|
108
121
|
state = {
|
|
109
122
|
name: filtered.name,
|
|
110
123
|
type: filtered.type,
|
|
111
124
|
unit: filtered.unit,
|
|
112
125
|
attributes,
|
|
113
|
-
windowStartMs: Date.now()
|
|
126
|
+
windowStartMs: Date.now(),
|
|
127
|
+
hist: bounds && emptyHistogram(bounds)
|
|
114
128
|
};
|
|
115
129
|
this._series.set(key, state);
|
|
116
130
|
}
|
|
@@ -146,6 +160,31 @@ class PostHogMetrics {
|
|
|
146
160
|
}
|
|
147
161
|
return false;
|
|
148
162
|
}
|
|
163
|
+
_histogramBounds(name, unit, requested) {
|
|
164
|
+
if (void 0 !== requested && !isValidBounds(requested)) return void this._logger.warn(`Dropping histogram '${name}': bounds must be a non-empty array of finite, strictly increasing numbers`);
|
|
165
|
+
const histogramKey = seriesKey('histogram', name, unit, void 0);
|
|
166
|
+
const fixed = this._boundsByHistogram.get(histogramKey);
|
|
167
|
+
if (fixed) {
|
|
168
|
+
this._warnOnBoundsConflict(name, histogramKey, fixed, requested);
|
|
169
|
+
return fixed;
|
|
170
|
+
}
|
|
171
|
+
if (this._boundsByHistogram.size >= this._config.maxSeriesPerFlush) {
|
|
172
|
+
if (!this._boundsCapWarned) {
|
|
173
|
+
this._boundsCapWarned = true;
|
|
174
|
+
this._logger.warn(`Histogram bounds limit reached (${this._config.maxSeriesPerFlush} distinct histograms); new histograms that pass custom bounds are dropped. Reduce histogram name cardinality.`);
|
|
175
|
+
}
|
|
176
|
+
if (requested && !sameBounds(requested, DEFAULT_HISTOGRAM_BOUNDS)) return;
|
|
177
|
+
return DEFAULT_HISTOGRAM_BOUNDS;
|
|
178
|
+
}
|
|
179
|
+
const bounds = requested ? requested.slice() : DEFAULT_HISTOGRAM_BOUNDS;
|
|
180
|
+
this._boundsByHistogram.set(histogramKey, bounds);
|
|
181
|
+
return bounds;
|
|
182
|
+
}
|
|
183
|
+
_warnOnBoundsConflict(name, histogramKey, fixed, requested) {
|
|
184
|
+
if (sameBounds(fixed, requested ?? DEFAULT_HISTOGRAM_BOUNDS) || this._boundsConflictWarned.has(histogramKey)) return;
|
|
185
|
+
this._boundsConflictWarned.add(histogramKey);
|
|
186
|
+
this._logger.warn(`Histogram '${name}' already uses the bounds [${fixed.join(', ')}]; samples that pass different bounds are recorded in those buckets.`);
|
|
187
|
+
}
|
|
149
188
|
_fold(state, value) {
|
|
150
189
|
switch(state.type){
|
|
151
190
|
case 'count':
|
|
@@ -156,19 +195,12 @@ class PostHogMetrics {
|
|
|
156
195
|
break;
|
|
157
196
|
case 'histogram':
|
|
158
197
|
{
|
|
159
|
-
if (!state.hist) state.hist = {
|
|
160
|
-
count: 0,
|
|
161
|
-
sum: 0,
|
|
162
|
-
min: value,
|
|
163
|
-
max: value,
|
|
164
|
-
bucketCounts: new Array(DEFAULT_HISTOGRAM_BOUNDS.length + 1).fill(0)
|
|
165
|
-
};
|
|
166
198
|
const hist = state.hist;
|
|
167
199
|
hist.count += 1;
|
|
168
200
|
hist.sum += value;
|
|
169
201
|
hist.min = Math.min(hist.min, value);
|
|
170
202
|
hist.max = Math.max(hist.max, value);
|
|
171
|
-
hist.bucketCounts[bucketIndexFor(value,
|
|
203
|
+
hist.bucketCounts[bucketIndexFor(value, hist.bounds)] += 1;
|
|
172
204
|
break;
|
|
173
205
|
}
|
|
174
206
|
}
|
|
@@ -204,10 +236,7 @@ class PostHogMetrics {
|
|
|
204
236
|
this._flushTimer.clear();
|
|
205
237
|
if (0 === this._series.size) return;
|
|
206
238
|
const window = this._series;
|
|
207
|
-
this.
|
|
208
|
-
this._seriesCapWarned = false;
|
|
209
|
-
this._typeByName = new Map();
|
|
210
|
-
this._typeCollisionWarned = new Set();
|
|
239
|
+
this._startWindow();
|
|
211
240
|
const generation = this._generation;
|
|
212
241
|
const outcome = await this._instance._sendMetricsBatch(this._buildPayload(window));
|
|
213
242
|
if (generation !== this._generation) return;
|
|
@@ -285,17 +314,18 @@ class PostHogMetrics {
|
|
|
285
314
|
...exemplars
|
|
286
315
|
};
|
|
287
316
|
metric.gauge.dataPoints.push(dp);
|
|
288
|
-
} else
|
|
317
|
+
} else {
|
|
318
|
+
const hist = state.hist;
|
|
289
319
|
const dp = {
|
|
290
320
|
attributes,
|
|
291
321
|
startTimeUnixNano: startNano,
|
|
292
322
|
timeUnixNano: nowNano,
|
|
293
|
-
count:
|
|
294
|
-
sum:
|
|
295
|
-
min:
|
|
296
|
-
max:
|
|
297
|
-
bucketCounts:
|
|
298
|
-
explicitBounds:
|
|
323
|
+
count: hist.count,
|
|
324
|
+
sum: hist.sum,
|
|
325
|
+
min: hist.min,
|
|
326
|
+
max: hist.max,
|
|
327
|
+
bucketCounts: hist.bucketCounts,
|
|
328
|
+
explicitBounds: hist.bounds,
|
|
299
329
|
...exemplars
|
|
300
330
|
};
|
|
301
331
|
metric.histogram.dataPoints.push(dp);
|
|
@@ -319,18 +349,41 @@ class PostHogMetrics {
|
|
|
319
349
|
case 'gauge':
|
|
320
350
|
break;
|
|
321
351
|
case 'histogram':
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
352
|
+
{
|
|
353
|
+
const hist = current.hist;
|
|
354
|
+
const oldHist = old.hist;
|
|
355
|
+
hist.count += oldHist.count;
|
|
356
|
+
hist.sum += oldHist.sum;
|
|
357
|
+
hist.min = Math.min(hist.min, oldHist.min);
|
|
358
|
+
hist.max = Math.max(hist.max, oldHist.max);
|
|
359
|
+
for(let i = 0; i < hist.bucketCounts.length; i++)hist.bucketCounts[i] += oldHist.bucketCounts[i];
|
|
360
|
+
break;
|
|
361
|
+
}
|
|
330
362
|
}
|
|
331
363
|
}
|
|
332
364
|
}
|
|
333
365
|
}
|
|
366
|
+
function isValidBounds(bounds) {
|
|
367
|
+
if (!isArray(bounds) || 0 === bounds.length) return false;
|
|
368
|
+
for(let i = 0; i < bounds.length; i++)if (!Number.isFinite(bounds[i]) || i > 0 && bounds[i] <= bounds[i - 1]) return false;
|
|
369
|
+
return true;
|
|
370
|
+
}
|
|
371
|
+
function sameBounds(a, b) {
|
|
372
|
+
if (a === b) return true;
|
|
373
|
+
if (a.length !== b.length) return false;
|
|
374
|
+
for(let i = 0; i < a.length; i++)if (a[i] !== b[i]) return false;
|
|
375
|
+
return true;
|
|
376
|
+
}
|
|
377
|
+
function emptyHistogram(bounds) {
|
|
378
|
+
return {
|
|
379
|
+
bounds,
|
|
380
|
+
count: 0,
|
|
381
|
+
sum: 0,
|
|
382
|
+
min: 1 / 0,
|
|
383
|
+
max: -1 / 0,
|
|
384
|
+
bucketCounts: new Array(bounds.length + 1).fill(0)
|
|
385
|
+
};
|
|
386
|
+
}
|
|
334
387
|
function toOtlpExemplars(exemplar) {
|
|
335
388
|
if (!exemplar) return {};
|
|
336
389
|
return {
|
package/dist/metrics/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type { MetricAttributeValue, MetricAttributes, MetricType, MetricsConfig, CaptureMetricOptions, MetricSample, BeforeSendMetricFn, Metrics, OtlpNumberDataPoint, OtlpHistogramDataPoint, OtlpMetric, OtlpMetricsPayload, OtlpExemplar, } from '@posthog/types';
|
|
1
|
+
export type { MetricAttributeValue, MetricAttributes, MetricType, MetricsConfig, CaptureMetricOptions, HistogramOptions, MetricSample, BeforeSendMetricFn, Metrics, OtlpNumberDataPoint, OtlpHistogramDataPoint, OtlpMetric, OtlpMetricsPayload, OtlpExemplar, } from '@posthog/types';
|
|
2
2
|
import type { BeforeSendMetricFn, MetricAttributeValue, OtlpMetricsPayload, Span } from '@posthog/types';
|
|
3
3
|
/** Same tagged outcome shape as `SendLogsBatchOutcome` — one policy for both signals. */
|
|
4
4
|
export type SendMetricsBatchOutcome = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/metrics/types.ts"],"names":[],"mappings":"AAEA,YAAY,EACV,oBAAoB,EACpB,gBAAgB,EAChB,UAAU,EACV,aAAa,EACb,oBAAoB,EACpB,YAAY,EACZ,kBAAkB,EAClB,OAAO,EACP,mBAAmB,EACnB,sBAAsB,EACtB,UAAU,EACV,kBAAkB,EAClB,YAAY,GACb,MAAM,gBAAgB,CAAA;AAEvB,OAAO,KAAK,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAA;AAExG,yFAAyF;AACzF,MAAM,MAAM,uBAAuB,GAC/B;IAAE,IAAI,EAAE,IAAI,CAAA;CAAE,GACd;IAAE,IAAI,EAAE,aAAa,CAAC;IAAC,KAAK,EAAE,OAAO,CAAC;IAAC,YAAY,CAAC,EAAE,MAAM,CAAA;CAAE,GAC9D;IACE,IAAI,EAAE,WAAW,CAAA;IACjB;;;;OAIG;IACH,eAAe,CAAC,EAAE,OAAO,CAAA;CAC1B,GACD;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,CAAA;AAErC;;;;GAIG;AACH,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAA;IAC5B,QAAQ,CAAC,QAAQ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E,MAAM,CAAA;CAC1B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@posthog/core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.64.1",
|
|
4
4
|
"bugs": {
|
|
5
5
|
"url": "https://github.com/PostHog/posthog-js/issues"
|
|
6
6
|
},
|
|
@@ -72,7 +72,7 @@
|
|
|
72
72
|
}
|
|
73
73
|
},
|
|
74
74
|
"dependencies": {
|
|
75
|
-
"@posthog/types": "^1.
|
|
75
|
+
"@posthog/types": "^1.421.0"
|
|
76
76
|
},
|
|
77
77
|
"devDependencies": {
|
|
78
78
|
"@rslib/core": "0.23.2",
|
|
@@ -16,4 +16,19 @@ describe('nodeStackLineParser', () => {
|
|
|
16
16
|
platform: 'node:javascript',
|
|
17
17
|
})
|
|
18
18
|
})
|
|
19
|
+
|
|
20
|
+
it.each([
|
|
21
|
+
['at async (/app/server/api/handler.ts:91:27)', 'async'],
|
|
22
|
+
['at async (work) (/app/server/api/handler.ts:91:27)', '(work)'],
|
|
23
|
+
])('parses the function name and filename in %s', (line, functionName) => {
|
|
24
|
+
expect(nodeStackLineParser(line, 'node:javascript')).toEqual({
|
|
25
|
+
filename: '/app/server/api/handler.ts',
|
|
26
|
+
module: undefined,
|
|
27
|
+
function: functionName,
|
|
28
|
+
lineno: 91,
|
|
29
|
+
colno: 27,
|
|
30
|
+
in_app: true,
|
|
31
|
+
platform: 'node:javascript',
|
|
32
|
+
})
|
|
33
|
+
})
|
|
19
34
|
})
|
|
@@ -7,7 +7,8 @@ import { UNKNOWN_FUNCTION } from './base'
|
|
|
7
7
|
|
|
8
8
|
/** Node Stack line parser */
|
|
9
9
|
const FILENAME_MATCH = /^\s*[-]{4,}$/
|
|
10
|
-
|
|
10
|
+
// `at async (file:1:2)` is a function named `async`; `at async (name) (file:1:2)` is an async frame.
|
|
11
|
+
const FULL_MATCH = /at (?:async (?!\((?!.*\)\s+\()))?(?:(.+?)\s+\()?(?:(.+):(\d+):(\d+)?|([^)]+))\)?/
|
|
11
12
|
const PROMISE_COMBINATOR = /^Promise\.(?:all|any)$/
|
|
12
13
|
const PROMISE_INDEX = /^index \d+$/
|
|
13
14
|
const PROMISE_FRAME_FILENAME = 'node:internal/promise'
|
package/src/index.ts
CHANGED
|
@@ -203,6 +203,272 @@ describe('PostHogMetrics', () => {
|
|
|
203
203
|
})
|
|
204
204
|
})
|
|
205
205
|
|
|
206
|
+
describe('histogram bounds', () => {
|
|
207
|
+
const DEFAULT_BOUNDS = [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000]
|
|
208
|
+
const histogramPoints = (name: string) =>
|
|
209
|
+
sentMetrics()
|
|
210
|
+
.filter((m) => m.name === name)
|
|
211
|
+
.flatMap((m) => m.histogram!.dataPoints)
|
|
212
|
+
const warnings = () => (logger.warn as vi.Mock).mock.calls.map((c) => String(c[0]))
|
|
213
|
+
|
|
214
|
+
it('uses the OpenTelemetry default bounds when none are passed', async () => {
|
|
215
|
+
const metrics = createMetrics()
|
|
216
|
+
metrics.histogram('h', 1)
|
|
217
|
+
await metrics.flush()
|
|
218
|
+
|
|
219
|
+
expect(histogramPoints('h')[0].explicitBounds).toEqual(DEFAULT_BOUNDS)
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
it('buckets values with the bounds the caller passes', async () => {
|
|
223
|
+
const metrics = createMetrics()
|
|
224
|
+
// (-inf, 100] ← 50 and 100, (100, 200] ← 150, overflow ← 201.
|
|
225
|
+
for (const v of [50, 100, 150, 201]) {
|
|
226
|
+
metrics.histogram('h', v, { bounds: [100, 200] })
|
|
227
|
+
}
|
|
228
|
+
await metrics.flush()
|
|
229
|
+
|
|
230
|
+
const dp = histogramPoints('h')[0]
|
|
231
|
+
expect(dp.explicitBounds).toEqual([100, 200])
|
|
232
|
+
expect(dp.bucketCounts).toEqual([2, 1, 1])
|
|
233
|
+
expect(dp.min).toBe(50)
|
|
234
|
+
expect(dp.max).toBe(201)
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
it('copies the bounds so a caller mutating its array cannot change the buckets', async () => {
|
|
238
|
+
const metrics = createMetrics()
|
|
239
|
+
const bounds = [100, 200]
|
|
240
|
+
metrics.histogram('h', 150, { bounds })
|
|
241
|
+
bounds[0] = 175
|
|
242
|
+
metrics.histogram('h', 150, { bounds: [100, 200] })
|
|
243
|
+
await metrics.flush()
|
|
244
|
+
|
|
245
|
+
const dp = histogramPoints('h')[0]
|
|
246
|
+
expect(dp.explicitBounds).toEqual([100, 200])
|
|
247
|
+
expect(dp.bucketCounts).toEqual([0, 2, 0])
|
|
248
|
+
// The second call passes equal bounds in a new array, which is not a conflict.
|
|
249
|
+
expect(logger.warn).not.toHaveBeenCalled()
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
it('records a sample with conflicting bounds in the first bounds and warns once', async () => {
|
|
253
|
+
const metrics = createMetrics()
|
|
254
|
+
metrics.histogram('h', 150, { bounds: [100, 200] })
|
|
255
|
+
metrics.histogram('h', 150, { bounds: [10, 20, 30] })
|
|
256
|
+
metrics.histogram('h', 150, { bounds: [10, 20, 30] })
|
|
257
|
+
metrics.histogram('h', 150)
|
|
258
|
+
await metrics.flush()
|
|
259
|
+
|
|
260
|
+
const dp = histogramPoints('h')[0]
|
|
261
|
+
expect(dp.explicitBounds).toEqual([100, 200])
|
|
262
|
+
expect(dp.bucketCounts).toEqual([0, 4, 0])
|
|
263
|
+
expect(warnings().filter((w) => w.includes("Histogram 'h' already uses the bounds"))).toHaveLength(1)
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
it('treats omitted bounds as the default when checking for a conflict', async () => {
|
|
267
|
+
const metrics = createMetrics()
|
|
268
|
+
metrics.histogram('h', 1)
|
|
269
|
+
metrics.histogram('h', 1, { bounds: [100, 200] })
|
|
270
|
+
await metrics.flush()
|
|
271
|
+
|
|
272
|
+
expect(histogramPoints('h')[0].explicitBounds).toHaveLength(15)
|
|
273
|
+
expect(warnings().some((w) => w.includes("Histogram 'h' already uses the bounds"))).toBe(true)
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
it('fixes the bounds per histogram across attribute series', async () => {
|
|
277
|
+
const metrics = createMetrics()
|
|
278
|
+
metrics.histogram('h', 150, { bounds: [100, 200], attributes: { route: 'a' } })
|
|
279
|
+
metrics.histogram('h', 150, { bounds: [10, 20], attributes: { route: 'b' } })
|
|
280
|
+
await metrics.flush()
|
|
281
|
+
|
|
282
|
+
const points = histogramPoints('h')
|
|
283
|
+
expect(points).toHaveLength(2)
|
|
284
|
+
expect(points.map((dp) => dp.explicitBounds)).toEqual([
|
|
285
|
+
[100, 200],
|
|
286
|
+
[100, 200],
|
|
287
|
+
])
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
it('keeps the bounds of a histogram separate per unit', async () => {
|
|
291
|
+
const metrics = createMetrics()
|
|
292
|
+
metrics.histogram('h', 1, { unit: 'ms', bounds: [100, 200] })
|
|
293
|
+
metrics.histogram('h', 1, { unit: 's', bounds: [1, 2, 3] })
|
|
294
|
+
await metrics.flush()
|
|
295
|
+
|
|
296
|
+
const byUnit = Object.fromEntries(sentMetrics().map((m) => [m.unit, m.histogram!.dataPoints[0].explicitBounds]))
|
|
297
|
+
expect(byUnit).toEqual({ ms: [100, 200], s: [1, 2, 3] })
|
|
298
|
+
expect(logger.warn).not.toHaveBeenCalled()
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
it('keeps the first bounds across flush windows and warns on a conflict once per window', async () => {
|
|
302
|
+
const metrics = createMetrics()
|
|
303
|
+
metrics.histogram('h', 150, { bounds: [100, 200] })
|
|
304
|
+
metrics.histogram('h', 150, { bounds: [10, 20] })
|
|
305
|
+
await metrics.flush()
|
|
306
|
+
metrics.histogram('h', 150, { bounds: [10, 20] })
|
|
307
|
+
await metrics.flush()
|
|
308
|
+
|
|
309
|
+
expect(histogramPoints('h').map((dp) => dp.explicitBounds)).toEqual([
|
|
310
|
+
[100, 200],
|
|
311
|
+
[100, 200],
|
|
312
|
+
])
|
|
313
|
+
expect(warnings().filter((w) => w.includes("Histogram 'h' already uses the bounds"))).toHaveLength(2)
|
|
314
|
+
})
|
|
315
|
+
|
|
316
|
+
it('merges a failed window back into a live window with the same bounds', async () => {
|
|
317
|
+
mockInstance._sendMetricsBatch
|
|
318
|
+
.mockResolvedValueOnce({ kind: 'retry-later', error: new Error('offline') })
|
|
319
|
+
.mockResolvedValue({ kind: 'ok' })
|
|
320
|
+
const metrics = createMetrics()
|
|
321
|
+
metrics.histogram('h', 50, { bounds: [100, 200] })
|
|
322
|
+
await metrics.flush()
|
|
323
|
+
metrics.histogram('h', 150, { bounds: [10, 20] })
|
|
324
|
+
await metrics.flush()
|
|
325
|
+
|
|
326
|
+
const dp = histogramPoints('h')[1]
|
|
327
|
+
expect(dp.explicitBounds).toEqual([100, 200])
|
|
328
|
+
expect(dp.bucketCounts).toEqual([1, 1, 0])
|
|
329
|
+
expect(dp.count).toBe(2)
|
|
330
|
+
expect(dp.sum).toBe(200)
|
|
331
|
+
expect(dp.min).toBe(50)
|
|
332
|
+
expect(dp.max).toBe(150)
|
|
333
|
+
})
|
|
334
|
+
|
|
335
|
+
it('lets reset() clear the bounds so the next sample sets them again', async () => {
|
|
336
|
+
const metrics = createMetrics()
|
|
337
|
+
metrics.histogram('h', 150, { bounds: [100, 200] })
|
|
338
|
+
metrics.reset()
|
|
339
|
+
metrics.histogram('h', 15, { bounds: [10, 20] })
|
|
340
|
+
await metrics.flush()
|
|
341
|
+
|
|
342
|
+
expect(histogramPoints('h').map((dp) => dp.explicitBounds)).toEqual([[10, 20]])
|
|
343
|
+
expect(logger.warn).not.toHaveBeenCalled()
|
|
344
|
+
})
|
|
345
|
+
|
|
346
|
+
it.each([
|
|
347
|
+
['empty', []],
|
|
348
|
+
['not an array', 'nope'],
|
|
349
|
+
['not increasing', [100, 100]],
|
|
350
|
+
['decreasing', [200, 100]],
|
|
351
|
+
['non-finite', [100, Infinity]],
|
|
352
|
+
['NaN', [NaN]],
|
|
353
|
+
['non-number', [100, '200']],
|
|
354
|
+
])('drops a sample whose bounds are %s', async (_name, bounds) => {
|
|
355
|
+
const metrics = createMetrics()
|
|
356
|
+
metrics.histogram('h', 1, { bounds: bounds as any })
|
|
357
|
+
await metrics.flush()
|
|
358
|
+
|
|
359
|
+
expect(sentMetrics()).toHaveLength(0)
|
|
360
|
+
expect(warnings().some((w) => w.includes("Dropping histogram 'h': bounds must be"))).toBe(true)
|
|
361
|
+
})
|
|
362
|
+
|
|
363
|
+
it('does not fix the bounds from a sample with invalid bounds', async () => {
|
|
364
|
+
const metrics = createMetrics()
|
|
365
|
+
metrics.histogram('h', 1, { bounds: [200, 100] })
|
|
366
|
+
metrics.histogram('h', 150, { bounds: [100, 200] })
|
|
367
|
+
await metrics.flush()
|
|
368
|
+
|
|
369
|
+
expect(histogramPoints('h')[0].explicitBounds).toEqual([100, 200])
|
|
370
|
+
})
|
|
371
|
+
|
|
372
|
+
it('drops new histograms that pass custom bounds past the bounds limit, and warns once', async () => {
|
|
373
|
+
const metrics = createMetrics({ maxSeriesPerFlush: 2 })
|
|
374
|
+
metrics.histogram('a', 1)
|
|
375
|
+
metrics.histogram('b', 1)
|
|
376
|
+
await metrics.flush()
|
|
377
|
+
metrics.histogram('c', 15, { bounds: [10, 20] })
|
|
378
|
+
metrics.histogram('d', 1)
|
|
379
|
+
await metrics.flush()
|
|
380
|
+
metrics.histogram('c', 150, { bounds: [100, 200] })
|
|
381
|
+
metrics.histogram('d', 1, { bounds: [...DEFAULT_BOUNDS] })
|
|
382
|
+
await metrics.flush()
|
|
383
|
+
|
|
384
|
+
expect(sentMetrics().map((m) => m.name)).toEqual(['a', 'b', 'd', 'd'])
|
|
385
|
+
expect(histogramPoints('d').map((dp) => dp.explicitBounds)).toEqual([DEFAULT_BOUNDS, DEFAULT_BOUNDS])
|
|
386
|
+
expect(warnings().filter((w) => w.includes('Histogram bounds limit reached'))).toHaveLength(1)
|
|
387
|
+
})
|
|
388
|
+
|
|
389
|
+
it('never sends one histogram with different bounds from two clients when only one is past the bounds limit', async () => {
|
|
390
|
+
const instanceA = createMockInstance()
|
|
391
|
+
const instanceB = createMockInstance()
|
|
392
|
+
const clientA = createMetrics({ maxSeriesPerFlush: 1 }, instanceA)
|
|
393
|
+
const clientB = createMetrics({ maxSeriesPerFlush: 1 }, instanceB)
|
|
394
|
+
|
|
395
|
+
clientA.histogram('h', 150, { bounds: [100, 200] })
|
|
396
|
+
await clientA.flush()
|
|
397
|
+
clientB.histogram('other', 1)
|
|
398
|
+
await clientB.flush()
|
|
399
|
+
clientB.histogram('h', 150, { bounds: [100, 200] })
|
|
400
|
+
await clientB.flush()
|
|
401
|
+
|
|
402
|
+
const bounds = [...sentMetrics(instanceA), ...sentMetrics(instanceB)]
|
|
403
|
+
.filter((m) => m.name === 'h')
|
|
404
|
+
.map((m) => m.histogram!.dataPoints[0].explicitBounds)
|
|
405
|
+
expect(bounds).toEqual([[100, 200]])
|
|
406
|
+
})
|
|
407
|
+
|
|
408
|
+
it('merges a failed window of a histogram past the bounds limit into the live window', async () => {
|
|
409
|
+
let traceparent: string | null = null
|
|
410
|
+
mockInstance = createMockInstance({ getActiveSpan: () => ({ traceparent: () => traceparent }) })
|
|
411
|
+
const metrics = createMetrics({ maxSeriesPerFlush: 1 })
|
|
412
|
+
metrics.histogram('a', 1)
|
|
413
|
+
await metrics.flush()
|
|
414
|
+
let resolveSend!: (outcome: SendMetricsBatchOutcome) => void
|
|
415
|
+
mockInstance._sendMetricsBatch.mockImplementationOnce(
|
|
416
|
+
() => new Promise<SendMetricsBatchOutcome>((resolve) => (resolveSend = resolve))
|
|
417
|
+
)
|
|
418
|
+
|
|
419
|
+
traceparent = '00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01'
|
|
420
|
+
metrics.histogram('c', 15)
|
|
421
|
+
const inflight = metrics.flush()
|
|
422
|
+
traceparent = null
|
|
423
|
+
metrics.histogram('c', 150)
|
|
424
|
+
resolveSend({ kind: 'retry-later', error: new Error('offline') })
|
|
425
|
+
await inflight
|
|
426
|
+
await metrics.flush()
|
|
427
|
+
|
|
428
|
+
const dp = histogramPoints('c')[1]
|
|
429
|
+
expect(dp.explicitBounds).toEqual(DEFAULT_BOUNDS)
|
|
430
|
+
expect(dp.count).toBe(2)
|
|
431
|
+
expect(dp.sum).toBe(165)
|
|
432
|
+
// 15 falls in (10, 25] and 150 in (100, 250].
|
|
433
|
+
expect(dp.bucketCounts).toEqual([0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0])
|
|
434
|
+
expect(dp.min).toBe(15)
|
|
435
|
+
expect(dp.max).toBe(150)
|
|
436
|
+
expect(dp.exemplars?.map((e) => e.asDouble)).toEqual([15])
|
|
437
|
+
})
|
|
438
|
+
|
|
439
|
+
it('keeps the caller bounds intact when beforeSend edits them in place', async () => {
|
|
440
|
+
const metrics = createMetrics({
|
|
441
|
+
beforeSend: (m) => {
|
|
442
|
+
m.bounds?.forEach((b, i) => (m.bounds![i] = b / 1000))
|
|
443
|
+
m.value /= 1000
|
|
444
|
+
m.unit = 's'
|
|
445
|
+
return m
|
|
446
|
+
},
|
|
447
|
+
})
|
|
448
|
+
const bounds = [100, 1000]
|
|
449
|
+
metrics.histogram('h', 500, { unit: 'ms', bounds })
|
|
450
|
+
metrics.histogram('h', 500, { unit: 'ms', bounds })
|
|
451
|
+
await metrics.flush()
|
|
452
|
+
|
|
453
|
+
expect(bounds).toEqual([100, 1000])
|
|
454
|
+
const dp = histogramPoints('h')[0]
|
|
455
|
+
expect(dp.explicitBounds).toEqual([0.1, 1])
|
|
456
|
+
expect(dp.bucketCounts).toEqual([0, 2, 0])
|
|
457
|
+
expect(logger.warn).not.toHaveBeenCalled()
|
|
458
|
+
})
|
|
459
|
+
|
|
460
|
+
it('does not fix the bounds for a sample the series cap drops', async () => {
|
|
461
|
+
const metrics = createMetrics({ maxSeriesPerFlush: 1 })
|
|
462
|
+
metrics.count('fills_the_window', 1)
|
|
463
|
+
metrics.histogram('h', 1, { bounds: [10, 20] })
|
|
464
|
+
await metrics.flush()
|
|
465
|
+
metrics.histogram('h', 150, { bounds: [100, 200] })
|
|
466
|
+
await metrics.flush()
|
|
467
|
+
|
|
468
|
+
expect(histogramPoints('h').map((dp) => dp.explicitBounds)).toEqual([[100, 200]])
|
|
469
|
+
})
|
|
470
|
+
})
|
|
471
|
+
|
|
206
472
|
describe('exemplars', () => {
|
|
207
473
|
const TRACE_ID = '4bf92f3577b34da6a3ce929d0e0e4736'
|
|
208
474
|
const SPAN_ID = '00f067aa0ba902b7'
|
|
@@ -828,6 +1094,18 @@ describe('PostHogMetrics', () => {
|
|
|
828
1094
|
expect(mockInstance._sendMetricsBatch).not.toHaveBeenCalled()
|
|
829
1095
|
})
|
|
830
1096
|
|
|
1097
|
+
it('drops a sample whose beforeSend result has an unknown type and still sends the rest', async () => {
|
|
1098
|
+
const metrics = createMetrics({
|
|
1099
|
+
beforeSend: (m) => (m.name === 'bad' ? ({ name: m.name, value: m.value } as any) : m),
|
|
1100
|
+
})
|
|
1101
|
+
metrics.count('good', 1)
|
|
1102
|
+
metrics.count('bad', 1)
|
|
1103
|
+
await metrics.flush()
|
|
1104
|
+
|
|
1105
|
+
expect(sentMetrics().map((m) => m.name)).toEqual(['good'])
|
|
1106
|
+
expect((logger.warn as vi.Mock).mock.calls.some((c) => String(c[0]).includes('unknown type'))).toBe(true)
|
|
1107
|
+
})
|
|
1108
|
+
|
|
831
1109
|
it('drops the sample when beforeSend throws', async () => {
|
|
832
1110
|
const metrics = createMetrics({
|
|
833
1111
|
beforeSend: () => {
|
package/src/metrics/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
CaptureMetricOptions,
|
|
3
|
+
HistogramOptions,
|
|
3
4
|
MetricAttributes,
|
|
4
5
|
MetricSample,
|
|
5
6
|
MetricType,
|
|
@@ -36,6 +37,7 @@ interface ExemplarState {
|
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
interface HistogramState {
|
|
40
|
+
bounds: number[]
|
|
39
41
|
count: number
|
|
40
42
|
sum: number
|
|
41
43
|
min: number
|
|
@@ -95,6 +97,14 @@ export class PostHogMetrics {
|
|
|
95
97
|
// types under one name produces charts that blend both series.
|
|
96
98
|
private _typeByName = new Map<string, MetricType>()
|
|
97
99
|
private _typeCollisionWarned = new Set<string>()
|
|
100
|
+
// Bounds per histogram (name and unit), fixed by its first sample. They
|
|
101
|
+
// outlive the flush window because buckets from two windows with different
|
|
102
|
+
// bounds cannot be merged. Only reset() clears them. The map holds at most
|
|
103
|
+
// maxSeriesPerFlush histograms.
|
|
104
|
+
private _boundsByHistogram = new Map<string, number[]>()
|
|
105
|
+
// Histograms warned about conflicting bounds this window.
|
|
106
|
+
private _boundsConflictWarned = new Set<string>()
|
|
107
|
+
private _boundsCapWarned = false
|
|
98
108
|
private _retryAfter = new RetryAfterWindow()
|
|
99
109
|
private _consecutiveFlushFailures = 0
|
|
100
110
|
private _flushJitter = NO_JITTER
|
|
@@ -117,8 +127,16 @@ export class PostHogMetrics {
|
|
|
117
127
|
this._capture({ name, type: 'gauge', value, unit: options?.unit, attributes: options?.attributes })
|
|
118
128
|
}
|
|
119
129
|
|
|
120
|
-
histogram(name: string, value: number, options?:
|
|
121
|
-
this._capture({
|
|
130
|
+
histogram(name: string, value: number, options?: HistogramOptions): void {
|
|
131
|
+
this._capture({
|
|
132
|
+
name,
|
|
133
|
+
type: 'histogram',
|
|
134
|
+
value,
|
|
135
|
+
unit: options?.unit,
|
|
136
|
+
attributes: options?.attributes,
|
|
137
|
+
// The copy keeps a beforeSend that edits the bounds in place from changing the caller's array.
|
|
138
|
+
bounds: isArray(options?.bounds) ? options.bounds.slice() : options?.bounds,
|
|
139
|
+
})
|
|
122
140
|
}
|
|
123
141
|
|
|
124
142
|
/** Sends everything aggregated so far without waiting for the flush interval. */
|
|
@@ -151,10 +169,7 @@ export class PostHogMetrics {
|
|
|
151
169
|
return null
|
|
152
170
|
}
|
|
153
171
|
const window = this._series
|
|
154
|
-
this.
|
|
155
|
-
this._seriesCapWarned = false
|
|
156
|
-
this._typeByName = new Map()
|
|
157
|
-
this._typeCollisionWarned = new Set()
|
|
172
|
+
this._startWindow()
|
|
158
173
|
return this._buildPayload(window)
|
|
159
174
|
}
|
|
160
175
|
|
|
@@ -165,11 +180,19 @@ export class PostHogMetrics {
|
|
|
165
180
|
this._consecutiveFlushFailures = 0
|
|
166
181
|
this._flushJitter = NO_JITTER
|
|
167
182
|
this._flushTimer.clear()
|
|
168
|
-
this._series = new Map()
|
|
169
183
|
this._flushPromise = null
|
|
184
|
+
this._startWindow()
|
|
185
|
+
this._boundsByHistogram = new Map()
|
|
186
|
+
this._boundsCapWarned = false
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Clears the state that lives for one flush window. */
|
|
190
|
+
private _startWindow(): void {
|
|
191
|
+
this._series = new Map()
|
|
170
192
|
this._seriesCapWarned = false
|
|
171
193
|
this._typeByName = new Map()
|
|
172
194
|
this._typeCollisionWarned = new Set()
|
|
195
|
+
this._boundsConflictWarned = new Set()
|
|
173
196
|
}
|
|
174
197
|
|
|
175
198
|
private _capture(sample: MetricSample): void {
|
|
@@ -190,6 +213,10 @@ export class PostHogMetrics {
|
|
|
190
213
|
this._logger.warn(`Dropping metric '${filtered.name}': value must be a finite number`)
|
|
191
214
|
return
|
|
192
215
|
}
|
|
216
|
+
if (filtered.type !== 'count' && filtered.type !== 'gauge' && filtered.type !== 'histogram') {
|
|
217
|
+
this._logger.warn(`Dropping metric '${filtered.name}': unknown type '${String(filtered.type)}'`)
|
|
218
|
+
return
|
|
219
|
+
}
|
|
193
220
|
// Checked after beforeSend so a hook can't turn a count negative either.
|
|
194
221
|
if (filtered.type === 'count' && filtered.value < 0) {
|
|
195
222
|
this._logger.warn(`Dropping count '${filtered.name}': counters are monotonic, value must be >= 0`)
|
|
@@ -211,16 +238,26 @@ export class PostHogMetrics {
|
|
|
211
238
|
}
|
|
212
239
|
|
|
213
240
|
let state = this._series.get(key)
|
|
214
|
-
if (!state) {
|
|
215
|
-
|
|
241
|
+
if (!state && !this._admitNewSeries()) {
|
|
242
|
+
return
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
let bounds: number[] | undefined
|
|
246
|
+
if (filtered.type === 'histogram') {
|
|
247
|
+
bounds = this._histogramBounds(filtered.name, filtered.unit, filtered.bounds)
|
|
248
|
+
if (!bounds) {
|
|
216
249
|
return
|
|
217
250
|
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (!state) {
|
|
218
254
|
state = {
|
|
219
255
|
name: filtered.name,
|
|
220
256
|
type: filtered.type,
|
|
221
257
|
unit: filtered.unit,
|
|
222
258
|
attributes,
|
|
223
259
|
windowStartMs: Date.now(),
|
|
260
|
+
hist: bounds && emptyHistogram(bounds),
|
|
224
261
|
}
|
|
225
262
|
this._series.set(key, state)
|
|
226
263
|
}
|
|
@@ -280,6 +317,68 @@ export class PostHogMetrics {
|
|
|
280
317
|
return false
|
|
281
318
|
}
|
|
282
319
|
|
|
320
|
+
/**
|
|
321
|
+
* The bounds a histogram sample folds into: the ones its histogram already
|
|
322
|
+
* has, or the requested bounds (default when absent) for a new histogram.
|
|
323
|
+
* A histogram past the bounds limit has no map entry and gets only the
|
|
324
|
+
* default bounds. Returns `undefined` when the sample must be dropped.
|
|
325
|
+
*/
|
|
326
|
+
private _histogramBounds(
|
|
327
|
+
name: string,
|
|
328
|
+
unit: string | undefined,
|
|
329
|
+
requested: number[] | undefined
|
|
330
|
+
): number[] | undefined {
|
|
331
|
+
if (requested !== undefined && !isValidBounds(requested)) {
|
|
332
|
+
this._logger.warn(
|
|
333
|
+
`Dropping histogram '${name}': bounds must be a non-empty array of finite, strictly increasing numbers`
|
|
334
|
+
)
|
|
335
|
+
return undefined
|
|
336
|
+
}
|
|
337
|
+
const histogramKey = seriesKey('histogram', name, unit, undefined)
|
|
338
|
+
const fixed = this._boundsByHistogram.get(histogramKey)
|
|
339
|
+
if (fixed) {
|
|
340
|
+
this._warnOnBoundsConflict(name, histogramKey, fixed, requested)
|
|
341
|
+
return fixed
|
|
342
|
+
}
|
|
343
|
+
// The limit keeps histogram names with interpolated IDs from growing the
|
|
344
|
+
// map without bound. The backend stores one set of bounds per series. A
|
|
345
|
+
// histogram that is not in the map therefore sends only the default bounds,
|
|
346
|
+
// and a sample that asks for other bounds is dropped. Otherwise this client
|
|
347
|
+
// could send other bounds than earlier windows or other clients sent.
|
|
348
|
+
if (this._boundsByHistogram.size >= this._config.maxSeriesPerFlush) {
|
|
349
|
+
if (!this._boundsCapWarned) {
|
|
350
|
+
this._boundsCapWarned = true
|
|
351
|
+
this._logger.warn(
|
|
352
|
+
`Histogram bounds limit reached (${this._config.maxSeriesPerFlush} distinct histograms); ` +
|
|
353
|
+
`new histograms that pass custom bounds are dropped. Reduce histogram name cardinality.`
|
|
354
|
+
)
|
|
355
|
+
}
|
|
356
|
+
if (requested && !sameBounds(requested, DEFAULT_HISTOGRAM_BOUNDS)) {
|
|
357
|
+
return undefined
|
|
358
|
+
}
|
|
359
|
+
return DEFAULT_HISTOGRAM_BOUNDS
|
|
360
|
+
}
|
|
361
|
+
const bounds = requested ? requested.slice() : DEFAULT_HISTOGRAM_BOUNDS
|
|
362
|
+
this._boundsByHistogram.set(histogramKey, bounds)
|
|
363
|
+
return bounds
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
private _warnOnBoundsConflict(
|
|
367
|
+
name: string,
|
|
368
|
+
histogramKey: string,
|
|
369
|
+
fixed: number[],
|
|
370
|
+
requested: number[] | undefined
|
|
371
|
+
): void {
|
|
372
|
+
if (sameBounds(fixed, requested ?? DEFAULT_HISTOGRAM_BOUNDS) || this._boundsConflictWarned.has(histogramKey)) {
|
|
373
|
+
return
|
|
374
|
+
}
|
|
375
|
+
this._boundsConflictWarned.add(histogramKey)
|
|
376
|
+
this._logger.warn(
|
|
377
|
+
`Histogram '${name}' already uses the bounds [${fixed.join(', ')}]; ` +
|
|
378
|
+
`samples that pass different bounds are recorded in those buckets.`
|
|
379
|
+
)
|
|
380
|
+
}
|
|
381
|
+
|
|
283
382
|
private _fold(state: SeriesState, value: number): void {
|
|
284
383
|
switch (state.type) {
|
|
285
384
|
case 'count':
|
|
@@ -289,21 +388,12 @@ export class PostHogMetrics {
|
|
|
289
388
|
state.last = value
|
|
290
389
|
break
|
|
291
390
|
case 'histogram': {
|
|
292
|
-
|
|
293
|
-
state.hist = {
|
|
294
|
-
count: 0,
|
|
295
|
-
sum: 0,
|
|
296
|
-
min: value,
|
|
297
|
-
max: value,
|
|
298
|
-
bucketCounts: new Array(DEFAULT_HISTOGRAM_BOUNDS.length + 1).fill(0),
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
const hist = state.hist
|
|
391
|
+
const hist = state.hist!
|
|
302
392
|
hist.count += 1
|
|
303
393
|
hist.sum += value
|
|
304
394
|
hist.min = Math.min(hist.min, value)
|
|
305
395
|
hist.max = Math.max(hist.max, value)
|
|
306
|
-
hist.bucketCounts[bucketIndexFor(value,
|
|
396
|
+
hist.bucketCounts[bucketIndexFor(value, hist.bounds)] += 1
|
|
307
397
|
break
|
|
308
398
|
}
|
|
309
399
|
}
|
|
@@ -368,10 +458,7 @@ export class PostHogMetrics {
|
|
|
368
458
|
// Snapshot and reset the window; samples captured while the send is in
|
|
369
459
|
// flight fold into a fresh window instead of the one being sent.
|
|
370
460
|
const window = this._series
|
|
371
|
-
this.
|
|
372
|
-
this._seriesCapWarned = false
|
|
373
|
-
this._typeByName = new Map()
|
|
374
|
-
this._typeCollisionWarned = new Set()
|
|
461
|
+
this._startWindow()
|
|
375
462
|
|
|
376
463
|
const generation = this._generation
|
|
377
464
|
const outcome = await this._instance._sendMetricsBatch(this._buildPayload(window))
|
|
@@ -467,17 +554,18 @@ export class PostHogMetrics {
|
|
|
467
554
|
...exemplars,
|
|
468
555
|
}
|
|
469
556
|
metric.gauge!.dataPoints.push(dp)
|
|
470
|
-
} else
|
|
557
|
+
} else {
|
|
558
|
+
const hist = state.hist!
|
|
471
559
|
const dp: OtlpHistogramDataPoint = {
|
|
472
560
|
attributes,
|
|
473
561
|
startTimeUnixNano: startNano,
|
|
474
562
|
timeUnixNano: nowNano,
|
|
475
|
-
count:
|
|
476
|
-
sum:
|
|
477
|
-
min:
|
|
478
|
-
max:
|
|
479
|
-
bucketCounts:
|
|
480
|
-
explicitBounds:
|
|
563
|
+
count: hist.count,
|
|
564
|
+
sum: hist.sum,
|
|
565
|
+
min: hist.min,
|
|
566
|
+
max: hist.max,
|
|
567
|
+
bucketCounts: hist.bucketCounts,
|
|
568
|
+
explicitBounds: hist.bounds,
|
|
481
569
|
...exemplars,
|
|
482
570
|
}
|
|
483
571
|
metric.histogram!.dataPoints.push(dp)
|
|
@@ -509,26 +597,54 @@ export class PostHogMetrics {
|
|
|
509
597
|
case 'gauge':
|
|
510
598
|
// The live window's value is newer — keep it.
|
|
511
599
|
break
|
|
512
|
-
case 'histogram':
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
for (let i = 0; i < current.hist.bucketCounts.length; i++) {
|
|
522
|
-
current.hist.bucketCounts[i] += old.hist.bucketCounts[i]
|
|
523
|
-
}
|
|
524
|
-
}
|
|
600
|
+
case 'histogram': {
|
|
601
|
+
const hist = current.hist!
|
|
602
|
+
const oldHist = old.hist!
|
|
603
|
+
hist.count += oldHist.count
|
|
604
|
+
hist.sum += oldHist.sum
|
|
605
|
+
hist.min = Math.min(hist.min, oldHist.min)
|
|
606
|
+
hist.max = Math.max(hist.max, oldHist.max)
|
|
607
|
+
for (let i = 0; i < hist.bucketCounts.length; i++) {
|
|
608
|
+
hist.bucketCounts[i] += oldHist.bucketCounts[i]
|
|
525
609
|
}
|
|
526
610
|
break
|
|
611
|
+
}
|
|
527
612
|
}
|
|
528
613
|
}
|
|
529
614
|
}
|
|
530
615
|
}
|
|
531
616
|
|
|
617
|
+
function isValidBounds(bounds: unknown): bounds is number[] {
|
|
618
|
+
if (!isArray(bounds) || bounds.length === 0) {
|
|
619
|
+
return false
|
|
620
|
+
}
|
|
621
|
+
for (let i = 0; i < bounds.length; i++) {
|
|
622
|
+
if (!Number.isFinite(bounds[i]) || (i > 0 && bounds[i] <= bounds[i - 1])) {
|
|
623
|
+
return false
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
return true
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
function sameBounds(a: number[], b: number[]): boolean {
|
|
630
|
+
if (a === b) {
|
|
631
|
+
return true
|
|
632
|
+
}
|
|
633
|
+
if (a.length !== b.length) {
|
|
634
|
+
return false
|
|
635
|
+
}
|
|
636
|
+
for (let i = 0; i < a.length; i++) {
|
|
637
|
+
if (a[i] !== b[i]) {
|
|
638
|
+
return false
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
return true
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
function emptyHistogram(bounds: number[]): HistogramState {
|
|
645
|
+
return { bounds, count: 0, sum: 0, min: Infinity, max: -Infinity, bucketCounts: new Array(bounds.length + 1).fill(0) }
|
|
646
|
+
}
|
|
647
|
+
|
|
532
648
|
// Spread into the data point, so a point without an exemplar keeps its exact
|
|
533
649
|
// pre-exemplar wire shape.
|
|
534
650
|
function toOtlpExemplars(exemplar: ExemplarState | undefined): { exemplars?: OtlpExemplar[] } {
|