@zuplo/cli 7.14.3 → 7.14.5
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 +1 -0
- package/dist/cmds/variable/delete.d.ts +9 -0
- package/dist/cmds/variable/delete.d.ts.map +1 -0
- package/dist/cmds/variable/delete.js +72 -0
- package/dist/cmds/variable/delete.js.map +1 -0
- package/dist/cmds/variable/index.d.ts.map +1 -1
- package/dist/cmds/variable/index.js +2 -1
- package/dist/cmds/variable/index.js.map +1 -1
- package/dist/common/middleware/get-environment-param.d.ts.map +1 -1
- package/dist/common/middleware/get-environment-param.js +10 -12
- package/dist/common/middleware/get-environment-param.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/variable/delete/handler.d.ts +9 -0
- package/dist/variable/delete/handler.d.ts.map +1 -0
- package/dist/variable/delete/handler.js +13 -0
- package/dist/variable/delete/handler.js.map +1 -0
- package/node_modules/@posthog/core/dist/error-tracking/parsers/node.d.ts.map +1 -1
- package/node_modules/@posthog/core/dist/error-tracking/parsers/node.js +1 -1
- package/node_modules/@posthog/core/dist/error-tracking/parsers/node.mjs +1 -1
- package/node_modules/@posthog/core/dist/index.d.ts +1 -1
- package/node_modules/@posthog/core/dist/index.d.ts.map +1 -1
- package/node_modules/@posthog/core/dist/logs/logs-utils.js +5 -5
- package/node_modules/@posthog/core/dist/logs/logs-utils.mjs +5 -5
- package/node_modules/@posthog/core/dist/metrics/index.d.ts +15 -2
- package/node_modules/@posthog/core/dist/metrics/index.d.ts.map +1 -1
- package/node_modules/@posthog/core/dist/metrics/index.js +88 -35
- package/node_modules/@posthog/core/dist/metrics/index.mjs +88 -35
- package/node_modules/@posthog/core/dist/metrics/types.d.ts +1 -1
- package/node_modules/@posthog/core/dist/metrics/types.d.ts.map +1 -1
- package/node_modules/@posthog/core/dist/privacy.js +3 -3
- package/node_modules/@posthog/core/dist/privacy.mjs +3 -3
- package/node_modules/@posthog/core/dist/utils/user-agent-utils.d.ts.map +1 -1
- package/node_modules/@posthog/core/dist/utils/user-agent-utils.js +36 -36
- package/node_modules/@posthog/core/dist/utils/user-agent-utils.mjs +36 -36
- package/node_modules/@posthog/core/package.json +2 -2
- package/node_modules/@posthog/core/src/error-tracking/parsers/node.spec.ts +15 -0
- package/node_modules/@posthog/core/src/error-tracking/parsers/node.ts +2 -1
- package/node_modules/@posthog/core/src/index.ts +1 -0
- package/node_modules/@posthog/core/src/logs/logs-utils.ts +3 -3
- package/node_modules/@posthog/core/src/metrics/index.spec.ts +278 -0
- package/node_modules/@posthog/core/src/metrics/index.ts +160 -44
- package/node_modules/@posthog/core/src/metrics/types.ts +1 -0
- package/node_modules/@posthog/core/src/privacy.ts +3 -3
- package/node_modules/@posthog/core/src/utils/user-agent-utils.ts +39 -36
- package/node_modules/@posthog/types/dist/capture-metric.d.ts +24 -2
- package/node_modules/@posthog/types/dist/capture-metric.d.ts.map +1 -1
- package/node_modules/@posthog/types/dist/index.d.ts +1 -1
- package/node_modules/@posthog/types/dist/index.d.ts.map +1 -1
- package/node_modules/@posthog/types/dist/posthog-config.d.ts +2 -1
- package/node_modules/@posthog/types/dist/posthog-config.d.ts.map +1 -1
- package/node_modules/@posthog/types/package.json +1 -1
- package/node_modules/@posthog/types/src/capture-metric.ts +25 -2
- package/node_modules/@posthog/types/src/index.ts +1 -0
- package/node_modules/@posthog/types/src/posthog-config.ts +2 -1
- package/node_modules/@zuplo/core/package.json +1 -1
- package/node_modules/@zuplo/graphql/package.json +1 -1
- package/node_modules/@zuplo/openapi-tools/package.json +1 -1
- package/node_modules/@zuplo/otel/package.json +1 -1
- package/node_modules/@zuplo/runtime/out/esm/{chunk-ADPB2ZMZ.js → chunk-UGZSO3CK.js} +94 -94
- package/node_modules/@zuplo/runtime/out/esm/chunk-UGZSO3CK.js.map +1 -0
- package/node_modules/@zuplo/runtime/out/esm/index.js +1 -1
- package/node_modules/@zuplo/runtime/out/esm/mcp-gateway/index.js +12 -12
- package/node_modules/@zuplo/runtime/out/esm/mcp-gateway/index.js.map +1 -1
- package/node_modules/@zuplo/runtime/out/esm/mocks/index.js +1 -1
- package/node_modules/@zuplo/runtime/package.json +1 -1
- package/package.json +11 -11
- package/node_modules/@zuplo/runtime/out/esm/chunk-ADPB2ZMZ.js.map +0 -1
- /package/node_modules/@zuplo/runtime/out/esm/{chunk-ADPB2ZMZ.js.LEGAL.txt → chunk-UGZSO3CK.js.LEGAL.txt} +0 -0
|
@@ -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: () => {
|
|
@@ -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[] } {
|
|
@@ -40,13 +40,13 @@ const URL_PATTERN = /[a-z][a-z0-9+.-]{0,63}:[^\s<>"]+/gi
|
|
|
40
40
|
// A character no URL match can contain, so a cut just after one splits no URL.
|
|
41
41
|
const URL_END_PATTERN = /[\s<>"]/
|
|
42
42
|
/** The same pattern without `g`, for asking whether a value holds a URL at all. */
|
|
43
|
-
const URL_PATTERN_ONCE = new RegExp(URL_PATTERN.source, 'i')
|
|
43
|
+
const URL_PATTERN_ONCE = /* @__PURE__ */ (() => new RegExp(URL_PATTERN.source, 'i'))()
|
|
44
44
|
/** Finds the first scheme that brings a real authority, i.e. what the pattern required before it went authority-less. */
|
|
45
45
|
const URL_AUTHORITY_SEARCH = /[a-z][a-z0-9+.-]{0,63}:\/\//i
|
|
46
46
|
/** The same, anchored: does this match *open* with an authority? */
|
|
47
|
-
const URL_AUTHORITY_PATTERN = new RegExp(`^${URL_AUTHORITY_SEARCH.source}`, 'i')
|
|
47
|
+
const URL_AUTHORITY_PATTERN = /* @__PURE__ */ (() => new RegExp(`^${URL_AUTHORITY_SEARCH.source}`, 'i'))()
|
|
48
48
|
/** Every authority start in a value, in order. */
|
|
49
|
-
const URL_AUTHORITY_SEARCH_ALL = new RegExp(URL_AUTHORITY_SEARCH.source, 'gi')
|
|
49
|
+
const URL_AUTHORITY_SEARCH_ALL = /* @__PURE__ */ (() => new RegExp(URL_AUTHORITY_SEARCH.source, 'gi'))()
|
|
50
50
|
/** The field separators. `?` and `#` are structural only as delimiters — see {@link isStructural}. */
|
|
51
51
|
const URL_FIELD_CHARACTERS = '=&'
|
|
52
52
|
/** The legacy field separator. Never split on, only checked; see {@link sanitizeUrlFieldValue}. */
|