@posthog/core 1.52.2 → 1.53.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.
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +82 -21
- package/dist/index.mjs +4 -0
- package/dist/logs/index.d.ts +3 -2
- package/dist/logs/index.d.ts.map +1 -1
- package/dist/logs/index.js +28 -22
- package/dist/logs/index.mjs +29 -23
- package/dist/logs/logs-utils.js +1 -1
- package/dist/logs/logs-utils.mjs +2 -2
- package/dist/logs/types.d.ts +2 -2
- package/dist/metrics/index.d.ts +5 -2
- package/dist/metrics/index.d.ts.map +1 -1
- package/dist/metrics/index.js +28 -14
- package/dist/metrics/index.mjs +29 -15
- package/dist/metrics/metrics-utils.d.ts.map +1 -1
- package/dist/metrics/metrics-utils.js +1 -2
- package/dist/metrics/metrics-utils.mjs +2 -3
- package/dist/metrics/types.d.ts +7 -0
- package/dist/metrics/types.d.ts.map +1 -1
- package/dist/posthog-core-stateless.d.ts +32 -4
- package/dist/posthog-core-stateless.d.ts.map +1 -1
- package/dist/posthog-core-stateless.js +68 -20
- package/dist/posthog-core-stateless.mjs +68 -20
- package/dist/traces/config.d.ts +13 -0
- package/dist/traces/config.d.ts.map +1 -0
- package/dist/traces/config.js +101 -0
- package/dist/traces/config.mjs +63 -0
- package/dist/traces/context.d.ts +18 -0
- package/dist/traces/context.d.ts.map +1 -0
- package/dist/traces/context.js +53 -0
- package/dist/traces/context.mjs +15 -0
- package/dist/traces/ids.d.ts +12 -0
- package/dist/traces/ids.d.ts.map +1 -0
- package/dist/traces/ids.js +92 -0
- package/dist/traces/ids.mjs +42 -0
- package/dist/traces/index.d.ts +150 -0
- package/dist/traces/index.d.ts.map +1 -0
- package/dist/traces/index.js +551 -0
- package/dist/traces/index.mjs +513 -0
- package/dist/traces/otlp.d.ts +24 -0
- package/dist/traces/otlp.d.ts.map +1 -0
- package/dist/traces/otlp.js +158 -0
- package/dist/traces/otlp.mjs +108 -0
- package/dist/traces/sanitize.d.ts +32 -0
- package/dist/traces/sanitize.d.ts.map +1 -0
- package/dist/traces/sanitize.js +93 -0
- package/dist/traces/sanitize.mjs +43 -0
- package/dist/traces/span.d.ts +189 -0
- package/dist/traces/span.d.ts.map +1 -0
- package/dist/traces/span.js +533 -0
- package/dist/traces/span.mjs +462 -0
- package/dist/traces/traceparent.d.ts +51 -0
- package/dist/traces/traceparent.d.ts.map +1 -0
- package/dist/traces/traceparent.js +116 -0
- package/dist/traces/traceparent.mjs +63 -0
- package/dist/traces/types.d.ts +114 -0
- package/dist/traces/types.d.ts.map +1 -0
- package/dist/traces/types.js +18 -0
- package/dist/traces/types.mjs +1 -0
- package/dist/utils/backoff.d.ts +23 -0
- package/dist/utils/backoff.d.ts.map +1 -0
- package/dist/utils/backoff.js +65 -0
- package/dist/utils/backoff.mjs +14 -0
- package/dist/utils/flush-timer.d.ts +26 -0
- package/dist/utils/flush-timer.d.ts.map +1 -0
- package/dist/utils/flush-timer.js +67 -0
- package/dist/utils/flush-timer.mjs +29 -0
- package/dist/utils/json-utils.d.ts +7 -0
- package/dist/utils/json-utils.d.ts.map +1 -1
- package/dist/utils/json-utils.js +27 -0
- package/dist/utils/json-utils.mjs +25 -1
- package/dist/utils/otlp-resource.d.ts +13 -2
- package/dist/utils/otlp-resource.d.ts.map +1 -1
- package/dist/utils/otlp-resource.js +34 -7
- package/dist/utils/otlp-resource.mjs +26 -2
- package/dist/utils/retry-after.d.ts +55 -0
- package/dist/utils/retry-after.d.ts.map +1 -0
- package/dist/utils/retry-after.js +95 -0
- package/dist/utils/retry-after.mjs +50 -0
- package/package.json +3 -3
- package/src/index.ts +18 -0
- package/src/logs/index.spec.ts +490 -2
- package/src/logs/index.ts +63 -31
- package/src/logs/logs-utils.ts +2 -2
- package/src/logs/types.ts +2 -2
- package/src/metrics/index.spec.ts +304 -0
- package/src/metrics/index.ts +53 -16
- package/src/metrics/metrics-utils.ts +2 -3
- package/src/metrics/types.ts +10 -2
- package/src/posthog-core-stateless.ts +157 -38
- package/src/traces/config.spec.ts +271 -0
- package/src/traces/config.ts +142 -0
- package/src/traces/context.ts +30 -0
- package/src/traces/ids.spec.ts +115 -0
- package/src/traces/ids.ts +75 -0
- package/src/traces/index.spec.ts +3187 -0
- package/src/traces/index.ts +1060 -0
- package/src/traces/live-spans.spec.ts +69 -0
- package/src/traces/otlp.spec.ts +368 -0
- package/src/traces/otlp.ts +191 -0
- package/src/traces/sanitize.ts +116 -0
- package/src/traces/span.spec.ts +1122 -0
- package/src/traces/span.ts +822 -0
- package/src/traces/traceparent.spec.ts +184 -0
- package/src/traces/traceparent.ts +172 -0
- package/src/traces/types.ts +140 -0
- package/src/utils/backoff.ts +44 -0
- package/src/utils/flush-timer.ts +50 -0
- package/src/utils/json-utils.ts +33 -0
- package/src/utils/otlp-resource.spec.ts +55 -9
- package/src/utils/otlp-resource.ts +45 -4
- package/src/utils/retry-after.ts +135 -0
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { buildResourceAttributes } from '../logs/logs-utils'
|
|
1
|
+
import { buildOtlpLogsPayload, buildResourceAttributes } from '../logs/logs-utils'
|
|
2
2
|
import type { ResolvedPostHogLogsConfig } from '../logs/types'
|
|
3
|
-
import { buildMetricsResourceAttributes } from '../metrics/metrics-utils'
|
|
3
|
+
import { buildMetricsResourceAttributes, buildOtlpMetricsPayload } from '../metrics/metrics-utils'
|
|
4
4
|
import type { ResolvedPostHogMetricsConfig } from '../metrics/types'
|
|
5
|
+
import { buildOtlpTracesPayload, buildTracesResourceAttributes } from '../traces/otlp'
|
|
6
|
+
import type { ResolvedTracesConfig } from '../traces/types'
|
|
5
7
|
import { normalizeOsName, osResourceAttributes } from './otlp-resource'
|
|
6
8
|
|
|
7
9
|
const shared = {
|
|
@@ -25,9 +27,10 @@ const conflicting = {
|
|
|
25
27
|
},
|
|
26
28
|
}
|
|
27
29
|
|
|
28
|
-
const
|
|
30
|
+
const allThree = (partial: object): Record<string, unknown>[] => [
|
|
29
31
|
buildResourceAttributes(partial as ResolvedPostHogLogsConfig, 'posthog-node', '1.0.0'),
|
|
30
32
|
buildMetricsResourceAttributes(partial as ResolvedPostHogMetricsConfig, 'posthog-node', '1.0.0'),
|
|
33
|
+
buildTracesResourceAttributes(partial as ResolvedTracesConfig, 'posthog-node', '1.0.0'),
|
|
31
34
|
]
|
|
32
35
|
|
|
33
36
|
describe('shared OTLP resource attributes', () => {
|
|
@@ -35,14 +38,16 @@ describe('shared OTLP resource attributes', () => {
|
|
|
35
38
|
['a fully populated config', shared],
|
|
36
39
|
['a config with conflicting user attributes', conflicting],
|
|
37
40
|
['an empty config', {}],
|
|
38
|
-
])('produces the same attributes for logs and
|
|
39
|
-
const [logs, metrics] =
|
|
41
|
+
])('produces the same attributes for logs, metrics and traces given %s', (_label, config) => {
|
|
42
|
+
const [logs, metrics, traces] = allThree(config)
|
|
40
43
|
expect(metrics).toEqual(logs)
|
|
44
|
+
expect(traces).toEqual(logs)
|
|
41
45
|
expect(Object.keys(metrics)).toEqual(Object.keys(logs))
|
|
46
|
+
expect(Object.keys(traces)).toEqual(Object.keys(logs))
|
|
42
47
|
})
|
|
43
48
|
|
|
44
49
|
it('layers the identity keys over user resource attributes', () => {
|
|
45
|
-
for (const attributes of
|
|
50
|
+
for (const attributes of allThree(conflicting)) {
|
|
46
51
|
expect(attributes).toEqual({
|
|
47
52
|
'service.name': 'checkout',
|
|
48
53
|
'service.version': '2.1.0',
|
|
@@ -55,7 +60,7 @@ describe('shared OTLP resource attributes', () => {
|
|
|
55
60
|
})
|
|
56
61
|
|
|
57
62
|
it('keeps user resource attributes that do not collide', () => {
|
|
58
|
-
for (const attributes of
|
|
63
|
+
for (const attributes of allThree(shared)) {
|
|
59
64
|
expect(attributes).toEqual({
|
|
60
65
|
'host.name': 'web-01',
|
|
61
66
|
'service.name': 'checkout',
|
|
@@ -68,7 +73,7 @@ describe('shared OTLP resource attributes', () => {
|
|
|
68
73
|
})
|
|
69
74
|
|
|
70
75
|
it('falls back to unknown_service and omits unset optional keys', () => {
|
|
71
|
-
for (const attributes of
|
|
76
|
+
for (const attributes of allThree({})) {
|
|
72
77
|
expect(attributes).toEqual({
|
|
73
78
|
'service.name': 'unknown_service',
|
|
74
79
|
'telemetry.sdk.name': 'posthog-node',
|
|
@@ -76,6 +81,41 @@ describe('shared OTLP resource attributes', () => {
|
|
|
76
81
|
})
|
|
77
82
|
}
|
|
78
83
|
})
|
|
84
|
+
|
|
85
|
+
it.each([
|
|
86
|
+
['a named key', 'payload'],
|
|
87
|
+
// Integer-like keys enumerate ahead of every string key, whatever the insertion order.
|
|
88
|
+
['an integer-like key', '0'],
|
|
89
|
+
])('keeps the identity keys when %s exhausts the encoder budget', (_label, key) => {
|
|
90
|
+
const identity = { serviceName: 'checkout', serviceVersion: '2.1.0', environment: 'production' }
|
|
91
|
+
const [logs, metrics, traces] = allThree({
|
|
92
|
+
...identity,
|
|
93
|
+
resourceAttributes: { [key]: Array.from({ length: 10 }, () => Array(1000).fill(1)) },
|
|
94
|
+
})
|
|
95
|
+
const encoded = [
|
|
96
|
+
buildOtlpLogsPayload([], logs as any, 's', '1').resourceLogs[0].resource.attributes,
|
|
97
|
+
buildOtlpMetricsPayload([], metrics as any, 's', '1').resourceMetrics[0].resource.attributes,
|
|
98
|
+
buildOtlpTracesPayload([], traces as any, 's', '1').resourceSpans[0].resource.attributes,
|
|
99
|
+
]
|
|
100
|
+
// Every key the builder sets on its own, so one added there without being
|
|
101
|
+
// reserved fails here.
|
|
102
|
+
const sdkSet = Object.entries(
|
|
103
|
+
buildTracesResourceAttributes(identity as ResolvedTracesConfig, 'posthog-node', '1.0.0')
|
|
104
|
+
)
|
|
105
|
+
expect(sdkSet.map(([sdkKey]) => sdkKey)).toEqual([
|
|
106
|
+
'service.name',
|
|
107
|
+
'deployment.environment',
|
|
108
|
+
'service.version',
|
|
109
|
+
'telemetry.sdk.name',
|
|
110
|
+
'telemetry.sdk.version',
|
|
111
|
+
])
|
|
112
|
+
for (const attributes of encoded) {
|
|
113
|
+
expect(attributes.map((attribute) => attribute.key)).toEqual([key, ...sdkSet.map(([sdkKey]) => sdkKey)])
|
|
114
|
+
for (const [sdkKey, sdkValue] of sdkSet) {
|
|
115
|
+
expect(attributes).toContainEqual({ key: sdkKey, value: { stringValue: sdkValue } })
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
})
|
|
79
119
|
})
|
|
80
120
|
|
|
81
121
|
describe('osResourceAttributes', () => {
|
|
@@ -86,6 +126,12 @@ describe('osResourceAttributes', () => {
|
|
|
86
126
|
['linux', 'Linux'],
|
|
87
127
|
['android', 'Android'],
|
|
88
128
|
['freebsd', 'FreeBSD'],
|
|
129
|
+
['openbsd', 'OpenBSD'],
|
|
130
|
+
['netbsd', 'NetBSD'],
|
|
131
|
+
['sunos', 'SunOS'],
|
|
132
|
+
['aix', 'AIX'],
|
|
133
|
+
['haiku', 'Haiku'],
|
|
134
|
+
['cygwin', 'Windows'],
|
|
89
135
|
// detectOS spellings
|
|
90
136
|
['Mac OS X', 'macOS'],
|
|
91
137
|
['iOS', 'iOS'],
|
|
@@ -97,7 +143,7 @@ describe('osResourceAttributes', () => {
|
|
|
97
143
|
})
|
|
98
144
|
|
|
99
145
|
it('passes an unmapped name through rather than dropping it', () => {
|
|
100
|
-
expect(normalizeOsName('
|
|
146
|
+
expect(normalizeOsName('Plan 9')).toBe('Plan 9')
|
|
101
147
|
expect(normalizeOsName('constructor')).toBe('constructor')
|
|
102
148
|
})
|
|
103
149
|
|
|
@@ -1,5 +1,10 @@
|
|
|
1
|
+
import type { OtlpKeyValue } from '@posthog/types'
|
|
2
|
+
import type { Logger } from '../types'
|
|
3
|
+
import { assignUserAttributes } from './json-utils'
|
|
4
|
+
import { toOtlpKeyValueList } from './otlp-any-value'
|
|
5
|
+
|
|
1
6
|
/**
|
|
2
|
-
* Shape the logs and
|
|
7
|
+
* Shape the logs, metrics and traces resolved configs share for resource
|
|
3
8
|
* attribution. Generic over the attribute value type so each signal keeps its
|
|
4
9
|
* own value union.
|
|
5
10
|
*/
|
|
@@ -11,7 +16,7 @@ export interface OtlpResourceConfig<TAttributeValue> {
|
|
|
11
16
|
}
|
|
12
17
|
|
|
13
18
|
/**
|
|
14
|
-
* OTLP resource attributes shared by the logs and
|
|
19
|
+
* OTLP resource attributes shared by the logs, metrics and traces envelopes.
|
|
15
20
|
*
|
|
16
21
|
* User `resourceAttributes` are spread first, then SDK-controlled keys on top so
|
|
17
22
|
* a stray user key can't clobber the ingestion-attribution ones; the dedicated
|
|
@@ -26,7 +31,10 @@ export function buildOtlpResourceAttributes<TAttributeValue>(
|
|
|
26
31
|
sdkVersion: string
|
|
27
32
|
): Record<string, TAttributeValue | string> {
|
|
28
33
|
return {
|
|
29
|
-
|
|
34
|
+
// Read key by key: a throwing accessor on a user-supplied attribute runs on
|
|
35
|
+
// every flush, before the pipeline's own error handling, and would otherwise
|
|
36
|
+
// stop the signal exporting entirely.
|
|
37
|
+
...assignUserAttributes<Record<string, TAttributeValue>>({}, config.resourceAttributes),
|
|
30
38
|
'service.name': config.serviceName || 'unknown_service',
|
|
31
39
|
...(config.environment && { 'deployment.environment': config.environment }),
|
|
32
40
|
...(config.serviceVersion && { 'service.version': config.serviceVersion }),
|
|
@@ -35,6 +43,35 @@ export function buildOtlpResourceAttributes<TAttributeValue>(
|
|
|
35
43
|
}
|
|
36
44
|
}
|
|
37
45
|
|
|
46
|
+
/** The keys `buildOtlpResourceAttributes` sets itself, in the order it sets them. */
|
|
47
|
+
const SDK_RESOURCE_KEYS = [
|
|
48
|
+
'service.name',
|
|
49
|
+
'deployment.environment',
|
|
50
|
+
'service.version',
|
|
51
|
+
'telemetry.sdk.name',
|
|
52
|
+
'telemetry.sdk.version',
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Encodes resource attributes for an OTLP envelope. The SDK-set keys are
|
|
57
|
+
* encoded on a traversal budget of their own, after the user's: a user
|
|
58
|
+
* attribute large enough to exhaust the shared budget would otherwise cost the
|
|
59
|
+
* resource its `service.name`, which ingestion attributes every record by.
|
|
60
|
+
*
|
|
61
|
+
* @internal Shared within this SDK; not part of the stable public API.
|
|
62
|
+
*/
|
|
63
|
+
export function toOtlpResourceKeyValueList(attributes: Record<string, unknown>, logger?: Logger): OtlpKeyValue[] {
|
|
64
|
+
const user = assignUserAttributes<Record<string, unknown>>({}, attributes)
|
|
65
|
+
const sdk: Record<string, unknown> = {}
|
|
66
|
+
for (const key of SDK_RESOURCE_KEYS) {
|
|
67
|
+
if (Object.prototype.hasOwnProperty.call(user, key)) {
|
|
68
|
+
sdk[key] = user[key]
|
|
69
|
+
delete user[key]
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return [...toOtlpKeyValueList(user, logger), ...toOtlpKeyValueList(sdk, logger)]
|
|
73
|
+
}
|
|
74
|
+
|
|
38
75
|
/**
|
|
39
76
|
* OTLP `os.name` values, keyed by the spellings the JS SDKs detect natively:
|
|
40
77
|
* `node:os` `platform()` identifiers and the names `detectOS` reads out of a
|
|
@@ -47,15 +84,19 @@ export function buildOtlpResourceAttributes<TAttributeValue>(
|
|
|
47
84
|
* platforms they cover.
|
|
48
85
|
*/
|
|
49
86
|
const OS_NAMES: Record<string, string> = {
|
|
50
|
-
// node:os platform()
|
|
87
|
+
// node:os platform(), all eleven of them
|
|
51
88
|
darwin: 'macOS',
|
|
52
89
|
win32: 'Windows',
|
|
90
|
+
// Cygwin is a POSIX layer over Windows, so it belongs under the same filter.
|
|
91
|
+
cygwin: 'Windows',
|
|
53
92
|
linux: 'Linux',
|
|
54
93
|
android: 'Android',
|
|
55
94
|
freebsd: 'FreeBSD',
|
|
56
95
|
openbsd: 'OpenBSD',
|
|
96
|
+
netbsd: 'NetBSD',
|
|
57
97
|
sunos: 'SunOS',
|
|
58
98
|
aix: 'AIX',
|
|
99
|
+
haiku: 'Haiku',
|
|
59
100
|
// detectOS
|
|
60
101
|
'Mac OS X': 'macOS',
|
|
61
102
|
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Longest `Retry-After` the SDK will wait. A rate-limit window can legitimately
|
|
3
|
+
* be long, but nothing upstream of the SDK bounds this header — it is as likely
|
|
4
|
+
* to come from a proxy or CDN as from PostHog — and an unbounded value would
|
|
5
|
+
* strand a queue for hours, so the wait is capped and the retry happens early.
|
|
6
|
+
*/
|
|
7
|
+
export const MAX_RETRY_AFTER_MS = 5 * 60_000
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* How far the wall clock may run behind the moment a window was installed before
|
|
11
|
+
* the window is discarded. NTP corrections on a healthy host are milliseconds,
|
|
12
|
+
* so a step past this is a clock change the deadline can no longer measure.
|
|
13
|
+
*/
|
|
14
|
+
const CLOCK_STEP_TOLERANCE_MS = 5_000
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* `Retry-After` as milliseconds from now. The header is either delta-seconds or
|
|
18
|
+
* an HTTP-date; anything else, a date already in the past, or a non-positive
|
|
19
|
+
* delta yields `undefined` so the caller keeps its own backoff. The result is
|
|
20
|
+
* capped at `MAX_RETRY_AFTER_MS`.
|
|
21
|
+
*/
|
|
22
|
+
export function parseRetryAfterMs(value: unknown, now: number = Date.now()): number | undefined {
|
|
23
|
+
if (typeof value !== 'string' || !value) {
|
|
24
|
+
return undefined
|
|
25
|
+
}
|
|
26
|
+
const raw = value.trim()
|
|
27
|
+
// `headers.get` joins repeated headers with ", ", so a CDN and a load
|
|
28
|
+
// balancer that each append one yield "60, 120". Read the first, which the
|
|
29
|
+
// outermost hop set — but only for delta-seconds, since an HTTP-date carries
|
|
30
|
+
// a comma of its own ("Wed, 21 Oct 2015 07:28:00 GMT").
|
|
31
|
+
const trimmed = /^\d+\s*,/.test(raw) ? raw.slice(0, raw.indexOf(',')).trim() : raw
|
|
32
|
+
// Integer seconds. Not parseFloat: "10 minutes" must not read as 10 seconds.
|
|
33
|
+
const seconds = /^\d+$/.test(trimmed) ? Number(trimmed) : Number.NaN
|
|
34
|
+
if (!Number.isFinite(seconds) && /^[+-]?[\d.]+$/.test(trimmed)) {
|
|
35
|
+
// Numeric but not delta-seconds, so it is malformed. `Date.parse` reads
|
|
36
|
+
// "-5", "+5" and "5.5" as dates in 2001 rather than rejecting them, which
|
|
37
|
+
// on a device whose clock predates that would surface as a real wait.
|
|
38
|
+
return undefined
|
|
39
|
+
}
|
|
40
|
+
const ms = Number.isFinite(seconds) ? seconds * 1000 : Date.parse(trimmed) - now
|
|
41
|
+
if (!Number.isFinite(ms) || ms <= 0) {
|
|
42
|
+
return undefined
|
|
43
|
+
}
|
|
44
|
+
return Math.min(ms, MAX_RETRY_AFTER_MS)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The part of an export outcome the window reads. */
|
|
48
|
+
export type RetryAfterOutcome = {
|
|
49
|
+
kind: 'ok' | 'retry-later' | 'too-large' | 'fatal'
|
|
50
|
+
retryAfterMs?: number
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The wait an ingestion endpoint asked for, shared by the logs, metrics and
|
|
55
|
+
* traces export queues.
|
|
56
|
+
*
|
|
57
|
+
* Held as an absolute deadline rather than a duration, so a timer armed while
|
|
58
|
+
* the wait is already part-served counts down the remainder instead of
|
|
59
|
+
* restarting it.
|
|
60
|
+
*/
|
|
61
|
+
export class RetryAfterWindow {
|
|
62
|
+
private _until = 0
|
|
63
|
+
private _installedAt = 0
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Folds one export outcome into the window.
|
|
67
|
+
*
|
|
68
|
+
* A refusal naming a longer wait than the one being served pushes the
|
|
69
|
+
* deadline out, but never past `MAX_RETRY_AFTER_MS` from where the window was
|
|
70
|
+
* first installed. That ceiling is the SDK's own policy rather than OTLP's,
|
|
71
|
+
* which asks for the header to be honoured and names no cap: a host flushing
|
|
72
|
+
* faster than the window is long would otherwise refresh the deadline forever
|
|
73
|
+
* and never recover, since logs gates its size trigger and `onReconnect` on
|
|
74
|
+
* the window and metrics re-arms its timer from it. The cost is that a wait
|
|
75
|
+
* longer than the ceiling is served short.
|
|
76
|
+
*
|
|
77
|
+
* The deadline is never pulled in, so a shorter header cannot cut a wait the
|
|
78
|
+
* endpoint already asked for.
|
|
79
|
+
*/
|
|
80
|
+
record(outcome: RetryAfterOutcome): void {
|
|
81
|
+
if (outcome.kind === 'too-large') {
|
|
82
|
+
// A verdict on the body's size, whether the SDK's own or the endpoint's
|
|
83
|
+
// 413, and either way silent on the endpoint's rate limit.
|
|
84
|
+
return
|
|
85
|
+
}
|
|
86
|
+
if (outcome.kind !== 'retry-later') {
|
|
87
|
+
// A stale deadline left set here would pin every later send at a window
|
|
88
|
+
// the endpoint has moved on from.
|
|
89
|
+
this.reset()
|
|
90
|
+
return
|
|
91
|
+
}
|
|
92
|
+
if (!outcome.retryAfterMs) {
|
|
93
|
+
// A refusal that names no wait — a network error, a timeout, a
|
|
94
|
+
// header-less 503 — does not revoke one the endpoint already named.
|
|
95
|
+
return
|
|
96
|
+
}
|
|
97
|
+
// Read before `_installedAt` is used: a spent window resets it, and a
|
|
98
|
+
// backward clock step is caught here rather than extending off a stale one.
|
|
99
|
+
const open = this.isOpen()
|
|
100
|
+
const now = Date.now()
|
|
101
|
+
const asked = Math.min(outcome.retryAfterMs, MAX_RETRY_AFTER_MS)
|
|
102
|
+
if (!open) {
|
|
103
|
+
this._installedAt = now
|
|
104
|
+
this._until = now + asked
|
|
105
|
+
return
|
|
106
|
+
}
|
|
107
|
+
this._until = Math.max(this._until, Math.min(now + asked, this._installedAt + MAX_RETRY_AFTER_MS))
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Milliseconds left of the wait, `0` when none is open. Closes a window it
|
|
112
|
+
* finds spent, so a later backward clock step cannot bring it back.
|
|
113
|
+
*/
|
|
114
|
+
remainingMs(): number {
|
|
115
|
+
const now = Date.now()
|
|
116
|
+
if (now < this._installedAt - CLOCK_STEP_TOLERANCE_MS) {
|
|
117
|
+
this.reset()
|
|
118
|
+
return 0
|
|
119
|
+
}
|
|
120
|
+
const remaining = Math.min(MAX_RETRY_AFTER_MS, Math.max(0, this._until - now))
|
|
121
|
+
if (remaining === 0) {
|
|
122
|
+
this.reset()
|
|
123
|
+
}
|
|
124
|
+
return remaining
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
isOpen(): boolean {
|
|
128
|
+
return this.remainingMs() > 0
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
reset(): void {
|
|
132
|
+
this._until = 0
|
|
133
|
+
this._installedAt = 0
|
|
134
|
+
}
|
|
135
|
+
}
|