@posthog/core 1.58.1 → 1.59.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.
@@ -42,7 +42,7 @@ export declare const flagDetailsToResults: (flagDetails: Record<string, FeatureF
42
42
  * The list is the union across client and server SDKs; entries are inert where an
43
43
  * SDK never sets them.
44
44
  */
45
- export declare const MINIMAL_FLAG_CALLED_EVENT_CAMPAIGN_PROPERTIES: readonly ['utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'gad_source', 'mc_cid', 'gclid', 'gclsrc', 'dclid', 'gbraid', 'wbraid', 'fbclid', 'msclkid', 'twclid', 'li_fat_id', 'igshid', 'ttclid', 'rdt_cid', 'epik', 'qclid', 'sccid', 'oppref', 'irclid', '_kx'];
45
+ export declare const MINIMAL_FLAG_CALLED_EVENT_CAMPAIGN_PROPERTIES: readonly ['utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'gad_source', 'mc_cid', 'gclid', 'gclsrc', 'dclid', 'gbraid', 'wbraid', 'fbclid', 'msclkid', 'twclid', 'li_fat_id', 'igshid', 'ttclid', 'rdt_cid', 'epik', 'qclid', 'sccid', 'oppref', 'bfcid', 'irclid', '_kx'];
46
46
  export declare const MINIMAL_FLAG_CALLED_EVENT_PROPERTIES: readonly string[];
47
47
  /**
48
48
  * Builds the minimal `$feature_flag_called` property set from fully assembled event
@@ -1 +1 @@
1
- {"version":3,"file":"featureFlagUtils.d.ts","sourceRoot":"","sources":["../src/featureFlagUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,iBAAiB,EACjB,gBAAgB,EAEhB,oBAAoB,EACpB,sBAAsB,EACtB,sBAAsB,EAEtB,mBAAmB,EACnB,2BAA2B,EAC5B,MAAM,SAAS,CAAA;AAEhB,eAAO,MAAM,sBAAsB,kBAE7B,mBAAmB,CAAC,sBAAsB,EAAE,OAAO,CAAC,GACpD,mBAAmB,CAAC,sBAAsB,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AG,OAAO,CAEpE;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,gBAAgB,GAAG,MAAM,GAAG,SAAS,CAE/E;AAED,eAAO,MAAM,oBAAoB,gBAAiB,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAAC,KAAG,iBAAiB,EAWtG,CAAA;AAED;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,6CAA6C,YACxD,YAAY,EACZ,YAAY,EACZ,cAAc,EACd,aAAa,EACb,UAAU,EACV,YAAY,EACZ,QAAQ,EACR,OAAO,EACP,QAAQ,EACR,OAAO,EACP,QAAQ,EACR,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,QAAQ,EACR,WAAW,EACX,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,MAAM,EACN,OAAO,EACP,OAAO,EACP,QAAQ,EACR,OAAO,EACP,QAAQ,EACR,KAAK,CACG,CAAA;AAEV,eAAO,MAAM,oCAAoC,EAAE,SAAS,MAAM,EAmCjE,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,iCAAiC,eAChC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,kBAChB,SAAS,MAAM,EAAE,KAC/B,MAAM,CAAC,MAAM,EAAE,GAAG,CAUpB,CAAA"}
@@ -154,6 +154,7 @@ const MINIMAL_FLAG_CALLED_EVENT_CAMPAIGN_PROPERTIES = [
154
154
  'qclid',
155
155
  'sccid',
156
156
  'oppref',
157
+ 'bfcid',
157
158
  'irclid',
158
159
  '_kx'
159
160
  ];
@@ -125,6 +125,7 @@ const MINIMAL_FLAG_CALLED_EVENT_CAMPAIGN_PROPERTIES = [
125
125
  'qclid',
126
126
  'sccid',
127
127
  'oppref',
128
+ 'bfcid',
128
129
  'irclid',
129
130
  '_kx'
130
131
  ];
@@ -14,6 +14,10 @@ import type { MetricsHost, ResolvedPostHogMetricsConfig } from './types';
14
14
  * Deliberately unlike logs, no per-user context (distinct ID, session ID) is
15
15
  * attached: every attribute value creates a new series, and per-user series
16
16
  * are the canonical metrics-cardinality explosion.
17
+ *
18
+ * A sample recorded inside a sampled span stamps its series with an exemplar
19
+ * (trace ID, span ID, value). One per series per window, last sample wins, so a
20
+ * metric point links to one representative trace at no cardinality cost.
17
21
  */
18
22
  export declare class PostHogMetrics {
19
23
  private readonly _instance;
@@ -46,6 +50,11 @@ export declare class PostHogMetrics {
46
50
  /** Clears the flush timer, drops the current window, and invalidates in-flight flushes. */
47
51
  reset(): void;
48
52
  private _capture;
53
+ /**
54
+ * The sampled span a sample is recorded in. An unsampled span is never
55
+ * exported, so an exemplar pointing at it would link to a missing trace.
56
+ */
57
+ private _activeTraceContext;
49
58
  /**
50
59
  * Cardinality gate for adding a series to the live window — warns once per
51
60
  * window when the cap is hit. Applied on capture and on merge-back alike.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/metrics/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,oBAAoB,EAMpB,kBAAkB,EAEnB,MAAM,gBAAgB,CAAA;AACvB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AActC,OAAO,KAAK,EAAE,WAAW,EAAE,4BAA4B,EAAE,MAAM,SAAS,CAAA;AA2BxE;;;;;;;;;;;;;GAaG;AACH,qBAAa,cAAc;IA0BvB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,OAAO;IA3B1B,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;IAChD,OAAO,CAAC,WAAW,CAAyB;IAC5C,OAAO,CAAC,yBAAyB,CAAI;IACrC,OAAO,CAAC,YAAY,CAAY;IAIhC,OA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2F;IAC3F,KAAK,IAAI,IAAI,CAWZ;IAED,OAAO,CAAC,QAAQ;IA0EhB;;;OAGG;IACH,OAAO,CAAC,mBAAmB;IAY3B;;;OAGG;IACH,OAAO,CAAC,eAAe;IAcvB,OAAO,CAAC,KAAK;IA6Bb,OAAO,CAAC,cAAc;IAyBtB,OAAO,CAAC,cAAc;IAWtB,OAAO,CAAC,eAAe;YAYT,QAAQ;IAyDtB,OAAO,CAAC,aAAa;IASrB;;;OAGG;IACH,OAAO,CAAC,aAAa;IA4DrB,oFAAoF;IACpF,OAAO,CAAC,gBAAgB;CAuCzB;AAqBD,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"}
@@ -39,6 +39,7 @@ const flush_timer_js_namespaceObject = require("../utils/flush-timer.js");
39
39
  const retry_after_js_namespaceObject = require("../utils/retry-after.js");
40
40
  const backoff_js_namespaceObject = require("../utils/backoff.js");
41
41
  const otlp_any_value_js_namespaceObject = require("../utils/otlp-any-value.js");
42
+ const traceparent_js_namespaceObject = require("../traces/traceparent.js");
42
43
  const external_metrics_utils_js_namespaceObject = require("./metrics-utils.js");
43
44
  const external_config_js_namespaceObject = require("./config.js");
44
45
  const OTLP_TEMPORALITY_DELTA = 1;
@@ -157,8 +158,23 @@ class PostHogMetrics {
157
158
  this._logger.warn(`Metric name '${filtered.name}' is already used as a ${seenType}; recording it as a ${filtered.type} too will blend both series in charts. Use a distinct name.`);
158
159
  }
159
160
  this._fold(state, filtered.value);
161
+ const traceContext = this._activeTraceContext();
162
+ if (traceContext) state.exemplar = {
163
+ ...traceContext,
164
+ value: filtered.value,
165
+ timeMs: Date.now()
166
+ };
160
167
  this._armFlushTimer();
161
168
  }
169
+ _activeTraceContext() {
170
+ try {
171
+ const parsed = (0, traceparent_js_namespaceObject.parseTraceparent)(this._instance.getActiveSpan?.()?.traceparent());
172
+ if (parsed && parsed.flags === traceparent_js_namespaceObject.TRACE_FLAGS_SAMPLED) return {
173
+ traceId: parsed.traceId,
174
+ spanId: parsed.spanId
175
+ };
176
+ } catch {}
177
+ }
162
178
  _admitNewSeries() {
163
179
  if (this._series.size < this._config.maxSeriesPerFlush) return true;
164
180
  if (!this._seriesCapWarned) {
@@ -288,19 +304,22 @@ class PostHogMetrics {
288
304
  }
289
305
  const attributes = (0, otlp_any_value_js_namespaceObject.toOtlpKeyValueList)(state.attributes ?? {}, this._logger);
290
306
  const startNano = (0, external_metrics_utils_js_namespaceObject.msToUnixNano)(state.windowStartMs);
307
+ const exemplars = toOtlpExemplars(state.exemplar);
291
308
  if ('count' === state.type) {
292
309
  const dp = {
293
310
  attributes,
294
311
  startTimeUnixNano: startNano,
295
312
  timeUnixNano: nowNano,
296
- asDouble: state.total ?? 0
313
+ asDouble: state.total ?? 0,
314
+ ...exemplars
297
315
  };
298
316
  metric.sum.dataPoints.push(dp);
299
317
  } else if ('gauge' === state.type) {
300
318
  const dp = {
301
319
  attributes,
302
320
  timeUnixNano: nowNano,
303
- asDouble: state.last ?? 0
321
+ asDouble: state.last ?? 0,
322
+ ...exemplars
304
323
  };
305
324
  metric.gauge.dataPoints.push(dp);
306
325
  } else if (state.hist) {
@@ -313,7 +332,8 @@ class PostHogMetrics {
313
332
  min: state.hist.min,
314
333
  max: state.hist.max,
315
334
  bucketCounts: state.hist.bucketCounts,
316
- explicitBounds: external_metrics_utils_js_namespaceObject.DEFAULT_HISTOGRAM_BOUNDS
335
+ explicitBounds: external_metrics_utils_js_namespaceObject.DEFAULT_HISTOGRAM_BOUNDS,
336
+ ...exemplars
317
337
  };
318
338
  metric.histogram.dataPoints.push(dp);
319
339
  }
@@ -328,6 +348,7 @@ class PostHogMetrics {
328
348
  continue;
329
349
  }
330
350
  current.windowStartMs = Math.min(current.windowStartMs, old.windowStartMs);
351
+ current.exemplar = current.exemplar ?? old.exemplar;
331
352
  switch(current.type){
332
353
  case 'count':
333
354
  current.total = (current.total ?? 0) + (old.total ?? 0);
@@ -347,6 +368,20 @@ class PostHogMetrics {
347
368
  }
348
369
  }
349
370
  }
371
+ function toOtlpExemplars(exemplar) {
372
+ if (!exemplar) return {};
373
+ return {
374
+ exemplars: [
375
+ {
376
+ filteredAttributes: [],
377
+ timeUnixNano: (0, external_metrics_utils_js_namespaceObject.msToUnixNano)(exemplar.timeMs),
378
+ asDouble: exemplar.value,
379
+ traceId: exemplar.traceId,
380
+ spanId: exemplar.spanId
381
+ }
382
+ ]
383
+ };
384
+ }
350
385
  exports.DEFAULT_HISTOGRAM_BOUNDS = __webpack_exports__.DEFAULT_HISTOGRAM_BOUNDS;
351
386
  exports.PostHogMetrics = __webpack_exports__.PostHogMetrics;
352
387
  exports.buildMetricsResourceAttributes = __webpack_exports__.buildMetricsResourceAttributes;
@@ -3,6 +3,7 @@ import { FlushTimer } from "../utils/flush-timer.mjs";
3
3
  import { RetryAfterWindow } from "../utils/retry-after.mjs";
4
4
  import { MAX_FLUSH_BACKOFF_MS, NO_JITTER, backoffDelayMs, drawJitter } from "../utils/backoff.mjs";
5
5
  import { toOtlpKeyValueList } from "../utils/otlp-any-value.mjs";
6
+ import { TRACE_FLAGS_SAMPLED, parseTraceparent } from "../traces/traceparent.mjs";
6
7
  import { DEFAULT_HISTOGRAM_BOUNDS, bucketIndexFor, buildMetricsResourceAttributes, buildOtlpMetricsPayload, msToUnixNano, seriesKey } from "./metrics-utils.mjs";
7
8
  const OTLP_TEMPORALITY_DELTA = 1;
8
9
  class PostHogMetrics {
@@ -120,8 +121,23 @@ class PostHogMetrics {
120
121
  this._logger.warn(`Metric name '${filtered.name}' is already used as a ${seenType}; recording it as a ${filtered.type} too will blend both series in charts. Use a distinct name.`);
121
122
  }
122
123
  this._fold(state, filtered.value);
124
+ const traceContext = this._activeTraceContext();
125
+ if (traceContext) state.exemplar = {
126
+ ...traceContext,
127
+ value: filtered.value,
128
+ timeMs: Date.now()
129
+ };
123
130
  this._armFlushTimer();
124
131
  }
132
+ _activeTraceContext() {
133
+ try {
134
+ const parsed = parseTraceparent(this._instance.getActiveSpan?.()?.traceparent());
135
+ if (parsed && parsed.flags === TRACE_FLAGS_SAMPLED) return {
136
+ traceId: parsed.traceId,
137
+ spanId: parsed.spanId
138
+ };
139
+ } catch {}
140
+ }
125
141
  _admitNewSeries() {
126
142
  if (this._series.size < this._config.maxSeriesPerFlush) return true;
127
143
  if (!this._seriesCapWarned) {
@@ -251,19 +267,22 @@ class PostHogMetrics {
251
267
  }
252
268
  const attributes = toOtlpKeyValueList(state.attributes ?? {}, this._logger);
253
269
  const startNano = msToUnixNano(state.windowStartMs);
270
+ const exemplars = toOtlpExemplars(state.exemplar);
254
271
  if ('count' === state.type) {
255
272
  const dp = {
256
273
  attributes,
257
274
  startTimeUnixNano: startNano,
258
275
  timeUnixNano: nowNano,
259
- asDouble: state.total ?? 0
276
+ asDouble: state.total ?? 0,
277
+ ...exemplars
260
278
  };
261
279
  metric.sum.dataPoints.push(dp);
262
280
  } else if ('gauge' === state.type) {
263
281
  const dp = {
264
282
  attributes,
265
283
  timeUnixNano: nowNano,
266
- asDouble: state.last ?? 0
284
+ asDouble: state.last ?? 0,
285
+ ...exemplars
267
286
  };
268
287
  metric.gauge.dataPoints.push(dp);
269
288
  } else if (state.hist) {
@@ -276,7 +295,8 @@ class PostHogMetrics {
276
295
  min: state.hist.min,
277
296
  max: state.hist.max,
278
297
  bucketCounts: state.hist.bucketCounts,
279
- explicitBounds: DEFAULT_HISTOGRAM_BOUNDS
298
+ explicitBounds: DEFAULT_HISTOGRAM_BOUNDS,
299
+ ...exemplars
280
300
  };
281
301
  metric.histogram.dataPoints.push(dp);
282
302
  }
@@ -291,6 +311,7 @@ class PostHogMetrics {
291
311
  continue;
292
312
  }
293
313
  current.windowStartMs = Math.min(current.windowStartMs, old.windowStartMs);
314
+ current.exemplar = current.exemplar ?? old.exemplar;
294
315
  switch(current.type){
295
316
  case 'count':
296
317
  current.total = (current.total ?? 0) + (old.total ?? 0);
@@ -310,5 +331,19 @@ class PostHogMetrics {
310
331
  }
311
332
  }
312
333
  }
334
+ function toOtlpExemplars(exemplar) {
335
+ if (!exemplar) return {};
336
+ return {
337
+ exemplars: [
338
+ {
339
+ filteredAttributes: [],
340
+ timeUnixNano: msToUnixNano(exemplar.timeMs),
341
+ asDouble: exemplar.value,
342
+ traceId: exemplar.traceId,
343
+ spanId: exemplar.spanId
344
+ }
345
+ ]
346
+ };
347
+ }
313
348
  export { resolveMetricsConfig } from "./config.mjs";
314
349
  export { DEFAULT_HISTOGRAM_BOUNDS, PostHogMetrics, buildMetricsResourceAttributes, buildOtlpMetricsPayload };
@@ -1,5 +1,5 @@
1
- export type { MetricAttributeValue, MetricAttributes, MetricType, MetricsConfig, CaptureMetricOptions, MetricSample, BeforeSendMetricFn, Metrics, OtlpNumberDataPoint, OtlpHistogramDataPoint, OtlpMetric, OtlpMetricsPayload, } from '@posthog/types';
2
- import type { BeforeSendMetricFn, MetricAttributeValue, OtlpMetricsPayload } from '@posthog/types';
1
+ export type { MetricAttributeValue, MetricAttributes, MetricType, MetricsConfig, CaptureMetricOptions, MetricSample, BeforeSendMetricFn, Metrics, OtlpNumberDataPoint, OtlpHistogramDataPoint, OtlpMetric, OtlpMetricsPayload, OtlpExemplar, } from '@posthog/types';
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 = {
5
5
  kind: 'ok';
@@ -30,6 +30,11 @@ export interface MetricsHost {
30
30
  _sendMetricsBatch(payload: OtlpMetricsPayload): Promise<SendMetricsBatchOutcome>;
31
31
  getLibraryId(): string;
32
32
  getLibraryVersion(): string;
33
+ /**
34
+ * The span active where a sample is recorded. A sampled one stamps the
35
+ * sample's series with an exemplar. Hosts without tracing leave it out.
36
+ */
37
+ getActiveSpan?(): Span | null;
33
38
  }
34
39
  /**
35
40
  * Configuration for the metrics feature on `new PostHog(key, { metrics: ... })`.
@@ -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,GACnB,MAAM,gBAAgB,CAAA;AAEvB,OAAO,KAAK,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAA;AAElG,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,EAAE,OAAO,CAAA;IAC1B,iBAAiB,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAAA;IAChF,YAAY,IAAI,MAAM,CAAA;IACtB,iBAAiB,IAAI,MAAM,CAAA;CAC5B;AAED;;;GAGG;AACH,MAAM,WAAW,oBAAoB;IACnC;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAA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D;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAE7D"}
1
+ {"version":3,"file":"ids.d.ts","sourceRoot":"","sources":["../../src/traces/ids.ts"],"names":[],"mappings":"AAoBA;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,UAAU,EAAE,MAAM,GAAG,UAAU,CAe7D;AAiBD,wBAAgB,UAAU,IAAI,MAAM,CAEnC;AAED,wBAAgB,SAAS,IAAI,MAAM,CAElC;AAMD,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAE9D;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAE7D"}
@@ -31,8 +31,8 @@ const TRACE_ID_BYTES = 16;
31
31
  const SPAN_ID_BYTES = 8;
32
32
  const TRACE_ID_HEX = 2 * TRACE_ID_BYTES;
33
33
  const SPAN_ID_HEX = 2 * SPAN_ID_BYTES;
34
- const INVALID_TRACE_ID = '0'.repeat(TRACE_ID_HEX);
35
- const INVALID_SPAN_ID = '0'.repeat(SPAN_ID_HEX);
34
+ const INVALID_SPAN_ID = '0000000000000000';
35
+ const INVALID_TRACE_ID = INVALID_SPAN_ID + INVALID_SPAN_ID;
36
36
  const HEX_RE = /^[0-9a-f]+$/;
37
37
  function getRandomBytes(byteLength) {
38
38
  const bytes = new Uint8Array(byteLength);
@@ -2,8 +2,8 @@ const TRACE_ID_BYTES = 16;
2
2
  const SPAN_ID_BYTES = 8;
3
3
  const TRACE_ID_HEX = 2 * TRACE_ID_BYTES;
4
4
  const SPAN_ID_HEX = 2 * SPAN_ID_BYTES;
5
- const INVALID_TRACE_ID = '0'.repeat(TRACE_ID_HEX);
6
- const INVALID_SPAN_ID = '0'.repeat(SPAN_ID_HEX);
5
+ const INVALID_SPAN_ID = '0000000000000000';
6
+ const INVALID_TRACE_ID = INVALID_SPAN_ID + INVALID_SPAN_ID;
7
7
  const HEX_RE = /^[0-9a-f]+$/;
8
8
  function getRandomBytes(byteLength) {
9
9
  const bytes = new Uint8Array(byteLength);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@posthog/core",
3
- "version": "1.58.1",
3
+ "version": "1.59.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.415.3"
75
+ "@posthog/types": "^1.416.0"
76
76
  },
77
77
  "devDependencies": {
78
78
  "@rslib/core": "0.23.2",
@@ -213,6 +213,7 @@ export const MINIMAL_FLAG_CALLED_EVENT_CAMPAIGN_PROPERTIES = [
213
213
  'qclid',
214
214
  'sccid',
215
215
  'oppref',
216
+ 'bfcid',
216
217
  'irclid',
217
218
  '_kx',
218
219
  ] as const
@@ -203,6 +203,81 @@ describe('PostHogMetrics', () => {
203
203
  })
204
204
  })
205
205
 
206
+ describe('exemplars', () => {
207
+ const TRACE_ID = '4bf92f3577b34da6a3ce929d0e0e4736'
208
+ const SPAN_ID = '00f067aa0ba902b7'
209
+ const withActiveSpan = (traceparent: string | null): any =>
210
+ createMockInstance({ getActiveSpan: () => ({ traceparent: () => traceparent }) })
211
+
212
+ const dataPoints = (instance: any): any[] =>
213
+ sentMetrics(instance).map((m) => (m.sum ?? m.gauge ?? m.histogram)!.dataPoints[0])
214
+
215
+ it('stamps every metric type with the last sample recorded inside a sampled span', async () => {
216
+ const instance = withActiveSpan(`00-${TRACE_ID}-${SPAN_ID}-01`)
217
+ const metrics = createMetrics(undefined, instance)
218
+ metrics.count('c', 1)
219
+ metrics.count('c', 4)
220
+ metrics.gauge('g', 7)
221
+ metrics.histogram('h', 12)
222
+ await metrics.flush()
223
+
224
+ const points = dataPoints(instance)
225
+ expect(points.map((p) => p.exemplars)).toEqual(
226
+ [4, 7, 12].map((value) => [
227
+ {
228
+ filteredAttributes: [],
229
+ timeUnixNano: expect.stringMatching(/^\d+$/),
230
+ asDouble: value,
231
+ traceId: TRACE_ID,
232
+ spanId: SPAN_ID,
233
+ },
234
+ ])
235
+ )
236
+ })
237
+
238
+ it.each([
239
+ ['no getActiveSpan on the host', createMockInstance],
240
+ ['no active span', () => createMockInstance({ getActiveSpan: () => null })],
241
+ ['an unsampled span', () => withActiveSpan(`00-${TRACE_ID}-${SPAN_ID}-00`)],
242
+ ['a span with no traceparent', () => withActiveSpan(null)],
243
+ [
244
+ 'a throwing host',
245
+ () =>
246
+ createMockInstance({
247
+ getActiveSpan: () => {
248
+ throw new Error('boom')
249
+ },
250
+ }),
251
+ ],
252
+ ])('records the sample without an exemplar for %s', async (_, makeInstance) => {
253
+ const instance = makeInstance()
254
+ const metrics = createMetrics(undefined, instance)
255
+ metrics.count('c', 2)
256
+ await metrics.flush()
257
+
258
+ const [point] = dataPoints(instance)
259
+ expect(point.asDouble).toBe(2)
260
+ expect(point).not.toHaveProperty('exemplars')
261
+ })
262
+
263
+ it('keeps the exemplar of a window merged back after a failed flush', async () => {
264
+ const instance = withActiveSpan(`00-${TRACE_ID}-${SPAN_ID}-01`)
265
+ instance._sendMetricsBatch
266
+ .mockResolvedValueOnce({ kind: 'retry-later', error: new Error('503') })
267
+ .mockResolvedValue({ kind: 'ok' })
268
+ const metrics = createMetrics(undefined, instance)
269
+ metrics.count('c', 1)
270
+ await metrics.flush()
271
+ instance.getActiveSpan = () => null
272
+ metrics.count('c', 2)
273
+ await metrics.flush()
274
+
275
+ const point = dataPoints(instance)[1]
276
+ expect(point.asDouble).toBe(3)
277
+ expect(point.exemplars[0]).toMatchObject({ traceId: TRACE_ID, spanId: SPAN_ID, asDouble: 1 })
278
+ })
279
+ })
280
+
206
281
  describe('resource attributes', () => {
207
282
  it('attaches service.name, sdk identity, and user resource attributes', async () => {
208
283
  const metrics = createMetrics({
@@ -3,6 +3,7 @@ import type {
3
3
  MetricAttributes,
4
4
  MetricSample,
5
5
  MetricType,
6
+ OtlpExemplar,
6
7
  OtlpHistogramDataPoint,
7
8
  OtlpMetric,
8
9
  OtlpMetricsPayload,
@@ -14,6 +15,7 @@ import { FlushTimer } from '../utils/flush-timer'
14
15
  import { RetryAfterWindow } from '../utils/retry-after'
15
16
  import { MAX_FLUSH_BACKOFF_MS, NO_JITTER, backoffDelayMs, drawJitter } from '../utils/backoff'
16
17
  import { toOtlpKeyValueList } from '../utils/otlp-any-value'
18
+ import { TRACE_FLAGS_SAMPLED, parseTraceparent } from '../traces/traceparent'
17
19
  import {
18
20
  DEFAULT_HISTOGRAM_BOUNDS,
19
21
  bucketIndexFor,
@@ -26,6 +28,13 @@ import type { MetricsHost, ResolvedPostHogMetricsConfig } from './types'
26
28
 
27
29
  const OTLP_TEMPORALITY_DELTA = 1
28
30
 
31
+ interface ExemplarState {
32
+ traceId: string
33
+ spanId: string
34
+ value: number
35
+ timeMs: number
36
+ }
37
+
29
38
  interface HistogramState {
30
39
  count: number
31
40
  sum: number
@@ -47,6 +56,8 @@ interface SeriesState {
47
56
  total?: number
48
57
  last?: number
49
58
  hist?: HistogramState
59
+ /** The last sample recorded inside a sampled span this window. */
60
+ exemplar?: ExemplarState
50
61
  }
51
62
 
52
63
  /**
@@ -62,6 +73,10 @@ interface SeriesState {
62
73
  * Deliberately unlike logs, no per-user context (distinct ID, session ID) is
63
74
  * attached: every attribute value creates a new series, and per-user series
64
75
  * are the canonical metrics-cardinality explosion.
76
+ *
77
+ * A sample recorded inside a sampled span stamps its series with an exemplar
78
+ * (trace ID, span ID, value). One per series per window, last sample wins, so a
79
+ * metric point links to one representative trace at no cardinality cost.
65
80
  */
66
81
  export class PostHogMetrics {
67
82
  private _series = new Map<string, SeriesState>()
@@ -224,9 +239,29 @@ export class PostHogMetrics {
224
239
  }
225
240
 
226
241
  this._fold(state, filtered.value)
242
+ const traceContext = this._activeTraceContext()
243
+ if (traceContext) {
244
+ state.exemplar = { ...traceContext, value: filtered.value, timeMs: Date.now() }
245
+ }
227
246
  this._armFlushTimer()
228
247
  }
229
248
 
249
+ /**
250
+ * The sampled span a sample is recorded in. An unsampled span is never
251
+ * exported, so an exemplar pointing at it would link to a missing trace.
252
+ */
253
+ private _activeTraceContext(): { traceId: string; spanId: string } | undefined {
254
+ try {
255
+ const parsed = parseTraceparent(this._instance.getActiveSpan?.()?.traceparent())
256
+ if (parsed && parsed.flags === TRACE_FLAGS_SAMPLED) {
257
+ return { traceId: parsed.traceId, spanId: parsed.spanId }
258
+ }
259
+ } catch {
260
+ // A throwing host never costs the sample itself.
261
+ }
262
+ return undefined
263
+ }
264
+
230
265
  /**
231
266
  * Cardinality gate for adding a series to the live window — warns once per
232
267
  * window when the cap is hit. Applied on capture and on merge-back alike.
@@ -413,6 +448,7 @@ export class PostHogMetrics {
413
448
 
414
449
  const attributes = toOtlpKeyValueList(state.attributes ?? {}, this._logger)
415
450
  const startNano = msToUnixNano(state.windowStartMs)
451
+ const exemplars = toOtlpExemplars(state.exemplar)
416
452
 
417
453
  if (state.type === 'count') {
418
454
  const dp: OtlpNumberDataPoint = {
@@ -420,6 +456,7 @@ export class PostHogMetrics {
420
456
  startTimeUnixNano: startNano,
421
457
  timeUnixNano: nowNano,
422
458
  asDouble: state.total ?? 0,
459
+ ...exemplars,
423
460
  }
424
461
  metric.sum!.dataPoints.push(dp)
425
462
  } else if (state.type === 'gauge') {
@@ -427,6 +464,7 @@ export class PostHogMetrics {
427
464
  attributes,
428
465
  timeUnixNano: nowNano,
429
466
  asDouble: state.last ?? 0,
467
+ ...exemplars,
430
468
  }
431
469
  metric.gauge!.dataPoints.push(dp)
432
470
  } else if (state.hist) {
@@ -440,6 +478,7 @@ export class PostHogMetrics {
440
478
  max: state.hist.max,
441
479
  bucketCounts: state.hist.bucketCounts,
442
480
  explicitBounds: DEFAULT_HISTOGRAM_BOUNDS,
481
+ ...exemplars,
443
482
  }
444
483
  metric.histogram!.dataPoints.push(dp)
445
484
  }
@@ -461,6 +500,8 @@ export class PostHogMetrics {
461
500
  continue
462
501
  }
463
502
  current.windowStartMs = Math.min(current.windowStartMs, old.windowStartMs)
503
+ // The live window's exemplar is newer; the old one only fills a gap.
504
+ current.exemplar = current.exemplar ?? old.exemplar
464
505
  switch (current.type) {
465
506
  case 'count':
466
507
  current.total = (current.total ?? 0) + (old.total ?? 0)
@@ -488,6 +529,25 @@ export class PostHogMetrics {
488
529
  }
489
530
  }
490
531
 
532
+ // Spread into the data point, so a point without an exemplar keeps its exact
533
+ // pre-exemplar wire shape.
534
+ function toOtlpExemplars(exemplar: ExemplarState | undefined): { exemplars?: OtlpExemplar[] } {
535
+ if (!exemplar) {
536
+ return {}
537
+ }
538
+ return {
539
+ exemplars: [
540
+ {
541
+ filteredAttributes: [],
542
+ timeUnixNano: msToUnixNano(exemplar.timeMs),
543
+ asDouble: exemplar.value,
544
+ traceId: exemplar.traceId,
545
+ spanId: exemplar.spanId,
546
+ },
547
+ ],
548
+ }
549
+ }
550
+
491
551
  export { buildOtlpMetricsPayload, buildMetricsResourceAttributes, DEFAULT_HISTOGRAM_BOUNDS } from './metrics-utils'
492
552
  export { resolveMetricsConfig } from './config'
493
553
  export type { MetricsHost, PostHogMetricsConfig, ResolvedPostHogMetricsConfig, SendMetricsBatchOutcome } from './types'
@@ -13,9 +13,10 @@ export type {
13
13
  OtlpHistogramDataPoint,
14
14
  OtlpMetric,
15
15
  OtlpMetricsPayload,
16
+ OtlpExemplar,
16
17
  } from '@posthog/types'
17
18
 
18
- import type { BeforeSendMetricFn, MetricAttributeValue, OtlpMetricsPayload } from '@posthog/types'
19
+ import type { BeforeSendMetricFn, MetricAttributeValue, OtlpMetricsPayload, Span } from '@posthog/types'
19
20
 
20
21
  /** Same tagged outcome shape as `SendLogsBatchOutcome` — one policy for both signals. */
21
22
  export type SendMetricsBatchOutcome =
@@ -43,6 +44,11 @@ export interface MetricsHost {
43
44
  _sendMetricsBatch(payload: OtlpMetricsPayload): Promise<SendMetricsBatchOutcome>
44
45
  getLibraryId(): string
45
46
  getLibraryVersion(): string
47
+ /**
48
+ * The span active where a sample is recorded. A sampled one stamps the
49
+ * sample's series with an exemplar. Hosts without tracing leave it out.
50
+ */
51
+ getActiveSpan?(): Span | null
46
52
  }
47
53
 
48
54
  /**
package/src/traces/ids.ts CHANGED
@@ -9,8 +9,10 @@ const SPAN_ID_BYTES = 8
9
9
  const TRACE_ID_HEX = TRACE_ID_BYTES * 2
10
10
  const SPAN_ID_HEX = SPAN_ID_BYTES * 2
11
11
 
12
- const INVALID_TRACE_ID = '0'.repeat(TRACE_ID_HEX)
13
- const INVALID_SPAN_ID = '0'.repeat(SPAN_ID_HEX)
12
+ // Spelled out rather than `'0'.repeat(...)`: this runs at module load and the
13
+ // browser bundle, which reaches it through metrics, still loads in IE11.
14
+ const INVALID_SPAN_ID = '0000000000000000'
15
+ const INVALID_TRACE_ID = INVALID_SPAN_ID + INVALID_SPAN_ID
14
16
 
15
17
  const HEX_RE = /^[0-9a-f]+$/
16
18