@posthog/core 1.39.6 → 1.40.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ bucketIndexFor: ()=>bucketIndexFor,
28
+ buildMetricsResourceAttributes: ()=>buildMetricsResourceAttributes,
29
+ buildOtlpMetricsPayload: ()=>buildOtlpMetricsPayload,
30
+ msToUnixNano: ()=>msToUnixNano,
31
+ seriesKey: ()=>seriesKey,
32
+ DEFAULT_HISTOGRAM_BOUNDS: ()=>DEFAULT_HISTOGRAM_BOUNDS
33
+ });
34
+ const logs_utils_js_namespaceObject = require("../logs/logs-utils.js");
35
+ const DEFAULT_HISTOGRAM_BOUNDS = [
36
+ 0,
37
+ 5,
38
+ 10,
39
+ 25,
40
+ 50,
41
+ 75,
42
+ 100,
43
+ 250,
44
+ 500,
45
+ 750,
46
+ 1000,
47
+ 2500,
48
+ 5000,
49
+ 7500,
50
+ 10000
51
+ ];
52
+ function msToUnixNano(ms) {
53
+ return String(ms) + '000000';
54
+ }
55
+ function seriesKey(type, name, unit, attributes) {
56
+ let attrsKey = '';
57
+ if (attributes) {
58
+ const keys = Object.keys(attributes).sort();
59
+ attrsKey = keys.map((k)=>`${JSON.stringify(k)}:${JSON.stringify(attributes[k])}`).join(',');
60
+ }
61
+ return `${type}\u0000${name}\u0000${unit ?? ''}\u0000${attrsKey}`;
62
+ }
63
+ function bucketIndexFor(value, bounds) {
64
+ for(let i = 0; i < bounds.length; i++)if (value <= bounds[i]) return i;
65
+ return bounds.length;
66
+ }
67
+ function buildMetricsResourceAttributes(config, scopeName, scopeVersion) {
68
+ return {
69
+ ...config.resourceAttributes,
70
+ 'service.name': config.serviceName || 'unknown_service',
71
+ ...config.environment && {
72
+ 'deployment.environment': config.environment
73
+ },
74
+ ...config.serviceVersion && {
75
+ 'service.version': config.serviceVersion
76
+ },
77
+ 'telemetry.sdk.name': scopeName,
78
+ 'telemetry.sdk.version': scopeVersion
79
+ };
80
+ }
81
+ function buildOtlpMetricsPayload(metrics, resourceAttributes, scopeName, scopeVersion) {
82
+ return {
83
+ resourceMetrics: [
84
+ {
85
+ resource: {
86
+ attributes: (0, logs_utils_js_namespaceObject.toOtlpKeyValueList)(resourceAttributes)
87
+ },
88
+ scopeMetrics: [
89
+ {
90
+ scope: {
91
+ name: scopeName,
92
+ version: scopeVersion
93
+ },
94
+ metrics
95
+ }
96
+ ]
97
+ }
98
+ ]
99
+ };
100
+ }
101
+ exports.DEFAULT_HISTOGRAM_BOUNDS = __webpack_exports__.DEFAULT_HISTOGRAM_BOUNDS;
102
+ exports.bucketIndexFor = __webpack_exports__.bucketIndexFor;
103
+ exports.buildMetricsResourceAttributes = __webpack_exports__.buildMetricsResourceAttributes;
104
+ exports.buildOtlpMetricsPayload = __webpack_exports__.buildOtlpMetricsPayload;
105
+ exports.msToUnixNano = __webpack_exports__.msToUnixNano;
106
+ exports.seriesKey = __webpack_exports__.seriesKey;
107
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
108
+ "DEFAULT_HISTOGRAM_BOUNDS",
109
+ "bucketIndexFor",
110
+ "buildMetricsResourceAttributes",
111
+ "buildOtlpMetricsPayload",
112
+ "msToUnixNano",
113
+ "seriesKey"
114
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
115
+ Object.defineProperty(exports, '__esModule', {
116
+ value: true
117
+ });
@@ -0,0 +1,68 @@
1
+ import { toOtlpKeyValueList } from "../logs/logs-utils.mjs";
2
+ const DEFAULT_HISTOGRAM_BOUNDS = [
3
+ 0,
4
+ 5,
5
+ 10,
6
+ 25,
7
+ 50,
8
+ 75,
9
+ 100,
10
+ 250,
11
+ 500,
12
+ 750,
13
+ 1000,
14
+ 2500,
15
+ 5000,
16
+ 7500,
17
+ 10000
18
+ ];
19
+ function msToUnixNano(ms) {
20
+ return String(ms) + '000000';
21
+ }
22
+ function seriesKey(type, name, unit, attributes) {
23
+ let attrsKey = '';
24
+ if (attributes) {
25
+ const keys = Object.keys(attributes).sort();
26
+ attrsKey = keys.map((k)=>`${JSON.stringify(k)}:${JSON.stringify(attributes[k])}`).join(',');
27
+ }
28
+ return `${type}\u0000${name}\u0000${unit ?? ''}\u0000${attrsKey}`;
29
+ }
30
+ function bucketIndexFor(value, bounds) {
31
+ for(let i = 0; i < bounds.length; i++)if (value <= bounds[i]) return i;
32
+ return bounds.length;
33
+ }
34
+ function buildMetricsResourceAttributes(config, scopeName, scopeVersion) {
35
+ return {
36
+ ...config.resourceAttributes,
37
+ 'service.name': config.serviceName || 'unknown_service',
38
+ ...config.environment && {
39
+ 'deployment.environment': config.environment
40
+ },
41
+ ...config.serviceVersion && {
42
+ 'service.version': config.serviceVersion
43
+ },
44
+ 'telemetry.sdk.name': scopeName,
45
+ 'telemetry.sdk.version': scopeVersion
46
+ };
47
+ }
48
+ function buildOtlpMetricsPayload(metrics, resourceAttributes, scopeName, scopeVersion) {
49
+ return {
50
+ resourceMetrics: [
51
+ {
52
+ resource: {
53
+ attributes: toOtlpKeyValueList(resourceAttributes)
54
+ },
55
+ scopeMetrics: [
56
+ {
57
+ scope: {
58
+ name: scopeName,
59
+ version: scopeVersion
60
+ },
61
+ metrics
62
+ }
63
+ ]
64
+ }
65
+ ]
66
+ };
67
+ }
68
+ export { DEFAULT_HISTOGRAM_BOUNDS, bucketIndexFor, buildMetricsResourceAttributes, buildOtlpMetricsPayload, msToUnixNano, seriesKey };
@@ -0,0 +1,69 @@
1
+ export type { MetricAttributeValue, MetricAttributes, MetricType, CaptureMetricOptions, MetricSample, BeforeSendMetricFn, Metrics, OtlpNumberDataPoint, OtlpHistogramDataPoint, OtlpMetric, OtlpMetricsPayload, } from '@posthog/types';
2
+ import type { BeforeSendMetricFn, MetricAttributeValue, OtlpMetricsPayload } from '@posthog/types';
3
+ /** Same tagged outcome shape as `SendLogsBatchOutcome` — one policy for both signals. */
4
+ export type SendMetricsBatchOutcome = {
5
+ kind: 'ok';
6
+ } | {
7
+ kind: 'retry-later';
8
+ error: unknown;
9
+ } | {
10
+ kind: 'too-large';
11
+ } | {
12
+ kind: 'fatal';
13
+ error: unknown;
14
+ };
15
+ /**
16
+ * The minimal host surface `PostHogMetrics` depends on. `PostHogCoreStateless`
17
+ * satisfies it structurally; the browser supplies an adapter backed by its
18
+ * own request layer.
19
+ */
20
+ export interface MetricsHost {
21
+ readonly isDisabled: boolean;
22
+ readonly optedOut: boolean;
23
+ _sendMetricsBatch(payload: OtlpMetricsPayload): Promise<SendMetricsBatchOutcome>;
24
+ getLibraryId(): string;
25
+ getLibraryVersion(): string;
26
+ }
27
+ /**
28
+ * Configuration for the metrics feature on `new PostHog(key, { metrics: ... })`.
29
+ * All fields are optional; per-SDK defaults apply.
30
+ */
31
+ export interface PostHogMetricsConfig {
32
+ /**
33
+ * Service name attached as the OTLP `service.name` resource attribute.
34
+ * Part of every series' identity; used by the Metrics UI for filtering.
35
+ * Defaults to `'unknown_service'` when unset.
36
+ */
37
+ serviceName?: string;
38
+ /** Service version attached as OTLP `service.version`. */
39
+ serviceVersion?: string;
40
+ /** Deployment environment attached as OTLP `deployment.environment`. */
41
+ environment?: string;
42
+ /**
43
+ * Extra OTLP resource attributes attached to every batch. Spread first;
44
+ * SDK-controlled keys are layered on top so users can't clobber them.
45
+ */
46
+ resourceAttributes?: Record<string, MetricAttributeValue>;
47
+ /**
48
+ * How often the aggregated window is flushed (ms). Samples are aggregated
49
+ * in memory between flushes — one data point per series per window, no
50
+ * matter how many calls. Default: 10000.
51
+ */
52
+ flushIntervalMs?: number;
53
+ /**
54
+ * Cardinality guardrail: max distinct series (name + type + unit +
55
+ * attribute combination) held per flush window. Samples for series beyond
56
+ * the cap are dropped with one warning per window. Default: 1000.
57
+ */
58
+ maxSeriesPerFlush?: number;
59
+ /**
60
+ * Pre-aggregation filter. See {@link BeforeSendMetricFn}. Configure as a
61
+ * single function or a chain.
62
+ */
63
+ beforeSend?: BeforeSendMetricFn | BeforeSendMetricFn[];
64
+ }
65
+ export interface ResolvedPostHogMetricsConfig extends PostHogMetricsConfig {
66
+ flushIntervalMs: number;
67
+ maxSeriesPerFlush: number;
68
+ }
69
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +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,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,CAAA;CAAE,GACvC;IAAE,IAAI,EAAE,WAAW,CAAA;CAAE,GACrB;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,CAAA;IAEpB,0DAA0D;IAC1D,cAAc,CAAC,EAAE,MAAM,CAAA;IAEvB,wEAAwE;IACxE,WAAW,CAAC,EAAE,MAAM,CAAA;IAEpB;;;OAGG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,oBAAoB,CAAC,CAAA;IAEzD;;;;OAIG;IACH,eAAe,CAAC,EAAE,MAAM,CAAA;IAExB;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAE1B;;;OAGG;IACH,UAAU,CAAC,EAAE,kBAAkB,GAAG,kBAAkB,EAAE,CAAA;CACvD;AAID,MAAM,WAAW,4BAA6B,SAAQ,oBAAoB;IACxE,eAAe,EAAE,MAAM,CAAA;IACvB,iBAAiB,EAAE,MAAM,CAAA;CAC1B"}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.r = (exports1)=>{
5
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
6
+ value: 'Module'
7
+ });
8
+ Object.defineProperty(exports1, '__esModule', {
9
+ value: true
10
+ });
11
+ };
12
+ })();
13
+ var __webpack_exports__ = {};
14
+ __webpack_require__.r(__webpack_exports__);
15
+ for(var __webpack_i__ in __webpack_exports__)exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
16
+ Object.defineProperty(exports, '__esModule', {
17
+ value: true
18
+ });
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@posthog/core",
3
- "version": "1.39.6",
3
+ "version": "1.40.0",
4
4
  "bugs": {
5
5
  "url": "https://github.com/PostHog/posthog-js/issues"
6
6
  },
@@ -70,7 +70,7 @@
70
70
  }
71
71
  },
72
72
  "dependencies": {
73
- "@posthog/types": "^1.392.0"
73
+ "@posthog/types": "^1.393.0"
74
74
  },
75
75
  "devDependencies": {
76
76
  "@rslib/core": "0.10.6",
package/src/index.ts CHANGED
@@ -37,6 +37,26 @@ export type {
37
37
  // via the `logs/types` barrel so consumers don't have to import from two
38
38
  // packages to type their `captureLog` calls.
39
39
  export type { CaptureLogOptions, LogAttributeValue, LogAttributes, LogSeverityLevel } from './logs/types'
40
+ export {
41
+ PostHogMetrics,
42
+ buildOtlpMetricsPayload,
43
+ buildMetricsResourceAttributes,
44
+ DEFAULT_HISTOGRAM_BOUNDS,
45
+ } from './metrics'
46
+ export type {
47
+ MetricsHost,
48
+ PostHogMetricsConfig,
49
+ ResolvedPostHogMetricsConfig,
50
+ SendMetricsBatchOutcome,
51
+ } from './metrics'
52
+ // Same barrel convention as logs for the user-facing metric types.
53
+ export type {
54
+ CaptureMetricOptions,
55
+ MetricAttributes,
56
+ MetricAttributeValue,
57
+ MetricSample,
58
+ MetricType,
59
+ } from './metrics/types'
40
60
  export { uuidv7 } from './vendor/uuidv7'
41
61
  export * from './cookie'
42
62
  export * from './posthog-core'