@zuplo/cli 7.7.7 → 7.7.9
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/node_modules/@posthog/core/dist/error-tracking/index.d.ts +1 -1
- package/node_modules/@posthog/core/dist/error-tracking/index.d.ts.map +1 -1
- package/node_modules/@posthog/core/dist/error-tracking/parsers/base.d.ts.map +1 -1
- package/node_modules/@posthog/core/dist/error-tracking/parsers/base.js +19 -2
- package/node_modules/@posthog/core/dist/error-tracking/parsers/base.mjs +19 -2
- package/node_modules/@posthog/core/dist/index.d.ts +7 -0
- package/node_modules/@posthog/core/dist/index.d.ts.map +1 -1
- package/node_modules/@posthog/core/dist/index.js +82 -21
- package/node_modules/@posthog/core/dist/index.mjs +4 -0
- package/node_modules/@posthog/core/dist/logs/index.d.ts +3 -2
- package/node_modules/@posthog/core/dist/logs/index.d.ts.map +1 -1
- package/node_modules/@posthog/core/dist/logs/index.js +28 -22
- package/node_modules/@posthog/core/dist/logs/index.mjs +29 -23
- package/node_modules/@posthog/core/dist/logs/logs-utils.js +1 -1
- package/node_modules/@posthog/core/dist/logs/logs-utils.mjs +2 -2
- package/node_modules/@posthog/core/dist/logs/types.d.ts +2 -2
- package/node_modules/@posthog/core/dist/metrics/index.d.ts +5 -2
- package/node_modules/@posthog/core/dist/metrics/index.d.ts.map +1 -1
- package/node_modules/@posthog/core/dist/metrics/index.js +28 -14
- package/node_modules/@posthog/core/dist/metrics/index.mjs +29 -15
- package/node_modules/@posthog/core/dist/metrics/metrics-utils.d.ts.map +1 -1
- package/node_modules/@posthog/core/dist/metrics/metrics-utils.js +1 -2
- package/node_modules/@posthog/core/dist/metrics/metrics-utils.mjs +2 -3
- package/node_modules/@posthog/core/dist/metrics/types.d.ts +7 -0
- package/node_modules/@posthog/core/dist/metrics/types.d.ts.map +1 -1
- package/node_modules/@posthog/core/dist/posthog-core-stateless.d.ts +32 -4
- package/node_modules/@posthog/core/dist/posthog-core-stateless.d.ts.map +1 -1
- package/node_modules/@posthog/core/dist/posthog-core-stateless.js +68 -20
- package/node_modules/@posthog/core/dist/posthog-core-stateless.mjs +68 -20
- package/node_modules/@posthog/core/dist/traces/config.d.ts +13 -0
- package/node_modules/@posthog/core/dist/traces/config.d.ts.map +1 -0
- package/node_modules/@posthog/core/dist/traces/config.js +101 -0
- package/node_modules/@posthog/core/dist/traces/config.mjs +63 -0
- package/node_modules/@posthog/core/dist/traces/context.d.ts +18 -0
- package/node_modules/@posthog/core/dist/traces/context.d.ts.map +1 -0
- package/node_modules/@posthog/core/dist/traces/context.js +53 -0
- package/node_modules/@posthog/core/dist/traces/context.mjs +15 -0
- package/node_modules/@posthog/core/dist/traces/ids.d.ts +12 -0
- package/node_modules/@posthog/core/dist/traces/ids.d.ts.map +1 -0
- package/node_modules/@posthog/core/dist/traces/ids.js +92 -0
- package/node_modules/@posthog/core/dist/traces/ids.mjs +42 -0
- package/node_modules/@posthog/core/dist/traces/index.d.ts +150 -0
- package/node_modules/@posthog/core/dist/traces/index.d.ts.map +1 -0
- package/node_modules/@posthog/core/dist/traces/index.js +551 -0
- package/node_modules/@posthog/core/dist/traces/index.mjs +513 -0
- package/node_modules/@posthog/core/dist/traces/otlp.d.ts +24 -0
- package/node_modules/@posthog/core/dist/traces/otlp.d.ts.map +1 -0
- package/node_modules/@posthog/core/dist/traces/otlp.js +158 -0
- package/node_modules/@posthog/core/dist/traces/otlp.mjs +108 -0
- package/node_modules/@posthog/core/dist/traces/sanitize.d.ts +32 -0
- package/node_modules/@posthog/core/dist/traces/sanitize.d.ts.map +1 -0
- package/node_modules/@posthog/core/dist/traces/sanitize.js +93 -0
- package/node_modules/@posthog/core/dist/traces/sanitize.mjs +43 -0
- package/node_modules/@posthog/core/dist/traces/span.d.ts +189 -0
- package/node_modules/@posthog/core/dist/traces/span.d.ts.map +1 -0
- package/node_modules/@posthog/core/dist/traces/span.js +533 -0
- package/node_modules/@posthog/core/dist/traces/span.mjs +462 -0
- package/node_modules/@posthog/core/dist/traces/traceparent.d.ts +51 -0
- package/node_modules/@posthog/core/dist/traces/traceparent.d.ts.map +1 -0
- package/node_modules/@posthog/core/dist/traces/traceparent.js +116 -0
- package/node_modules/@posthog/core/dist/traces/traceparent.mjs +63 -0
- package/node_modules/@posthog/core/dist/traces/types.d.ts +114 -0
- package/node_modules/@posthog/core/dist/traces/types.d.ts.map +1 -0
- package/node_modules/@posthog/core/dist/traces/types.js +18 -0
- package/node_modules/@posthog/core/dist/traces/types.mjs +1 -0
- package/node_modules/@posthog/core/dist/utils/backoff.d.ts +23 -0
- package/node_modules/@posthog/core/dist/utils/backoff.d.ts.map +1 -0
- package/node_modules/@posthog/core/dist/utils/backoff.js +65 -0
- package/node_modules/@posthog/core/dist/utils/backoff.mjs +14 -0
- package/node_modules/@posthog/core/dist/utils/flush-timer.d.ts +26 -0
- package/node_modules/@posthog/core/dist/utils/flush-timer.d.ts.map +1 -0
- package/node_modules/@posthog/core/dist/utils/flush-timer.js +67 -0
- package/node_modules/@posthog/core/dist/utils/flush-timer.mjs +29 -0
- package/node_modules/@posthog/core/dist/utils/json-utils.d.ts +7 -0
- package/node_modules/@posthog/core/dist/utils/json-utils.d.ts.map +1 -1
- package/node_modules/@posthog/core/dist/utils/json-utils.js +27 -0
- package/node_modules/@posthog/core/dist/utils/json-utils.mjs +25 -1
- package/node_modules/@posthog/core/dist/utils/otlp-resource.d.ts +13 -2
- package/node_modules/@posthog/core/dist/utils/otlp-resource.d.ts.map +1 -1
- package/node_modules/@posthog/core/dist/utils/otlp-resource.js +34 -7
- package/node_modules/@posthog/core/dist/utils/otlp-resource.mjs +26 -2
- package/node_modules/@posthog/core/dist/utils/retry-after.d.ts +55 -0
- package/node_modules/@posthog/core/dist/utils/retry-after.d.ts.map +1 -0
- package/node_modules/@posthog/core/dist/utils/retry-after.js +95 -0
- package/node_modules/@posthog/core/dist/utils/retry-after.mjs +50 -0
- package/node_modules/@posthog/core/package.json +3 -3
- package/node_modules/@posthog/core/src/error-tracking/index.ts +24 -1
- package/node_modules/@posthog/core/src/error-tracking/parsers/base.spec.ts +77 -7
- package/node_modules/@posthog/core/src/error-tracking/parsers/base.ts +42 -19
- package/node_modules/@posthog/core/src/index.ts +18 -0
- package/node_modules/@posthog/core/src/logs/index.spec.ts +490 -2
- package/node_modules/@posthog/core/src/logs/index.ts +63 -31
- package/node_modules/@posthog/core/src/logs/logs-utils.ts +2 -2
- package/node_modules/@posthog/core/src/logs/types.ts +2 -2
- package/node_modules/@posthog/core/src/metrics/index.spec.ts +304 -0
- package/node_modules/@posthog/core/src/metrics/index.ts +53 -16
- package/node_modules/@posthog/core/src/metrics/metrics-utils.ts +2 -3
- package/node_modules/@posthog/core/src/metrics/types.ts +10 -2
- package/node_modules/@posthog/core/src/posthog-core-stateless.ts +157 -38
- package/node_modules/@posthog/core/src/traces/config.spec.ts +271 -0
- package/node_modules/@posthog/core/src/traces/config.ts +142 -0
- package/node_modules/@posthog/core/src/traces/context.ts +30 -0
- package/node_modules/@posthog/core/src/traces/ids.spec.ts +115 -0
- package/node_modules/@posthog/core/src/traces/ids.ts +75 -0
- package/node_modules/@posthog/core/src/traces/index.spec.ts +3187 -0
- package/node_modules/@posthog/core/src/traces/index.ts +1060 -0
- package/node_modules/@posthog/core/src/traces/live-spans.spec.ts +69 -0
- package/node_modules/@posthog/core/src/traces/otlp.spec.ts +368 -0
- package/node_modules/@posthog/core/src/traces/otlp.ts +191 -0
- package/node_modules/@posthog/core/src/traces/sanitize.ts +116 -0
- package/node_modules/@posthog/core/src/traces/span.spec.ts +1122 -0
- package/node_modules/@posthog/core/src/traces/span.ts +822 -0
- package/node_modules/@posthog/core/src/traces/traceparent.spec.ts +184 -0
- package/node_modules/@posthog/core/src/traces/traceparent.ts +172 -0
- package/node_modules/@posthog/core/src/traces/types.ts +140 -0
- package/node_modules/@posthog/core/src/utils/backoff.ts +44 -0
- package/node_modules/@posthog/core/src/utils/flush-timer.ts +50 -0
- package/node_modules/@posthog/core/src/utils/json-utils.ts +33 -0
- package/node_modules/@posthog/core/src/utils/otlp-resource.spec.ts +55 -9
- package/node_modules/@posthog/core/src/utils/otlp-resource.ts +45 -4
- package/node_modules/@posthog/core/src/utils/retry-after.ts +135 -0
- package/node_modules/@posthog/types/dist/index.d.ts +1 -0
- package/node_modules/@posthog/types/dist/index.d.ts.map +1 -1
- package/node_modules/@posthog/types/dist/posthog-config.d.ts +10 -5
- package/node_modules/@posthog/types/dist/posthog-config.d.ts.map +1 -1
- package/node_modules/@posthog/types/dist/traces.d.ts +387 -0
- package/node_modules/@posthog/types/dist/traces.d.ts.map +1 -0
- package/node_modules/@posthog/types/dist/traces.js +18 -0
- package/node_modules/@posthog/types/dist/traces.mjs +1 -0
- package/node_modules/@posthog/types/package.json +1 -1
- package/node_modules/@posthog/types/src/index.ts +19 -0
- package/node_modules/@posthog/types/src/posthog-config.ts +10 -5
- package/node_modules/@posthog/types/src/traces.ts +429 -0
- 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-FEB2VFS7.js → chunk-KNAYKBEE.js} +96 -96
- package/node_modules/@zuplo/runtime/out/esm/chunk-KNAYKBEE.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 +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-FEB2VFS7.js.map +0 -1
- /package/node_modules/@zuplo/runtime/out/esm/{chunk-FEB2VFS7.js.LEGAL.txt → chunk-KNAYKBEE.js.LEGAL.txt} +0 -0
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
import { TRACE_FLAGS_SAMPLED, formatTraceparent, normalizeTraceparent, sanitizeTracestate, traceparentHeader } from "./traceparent.mjs";
|
|
2
|
+
import { clampEndTime, resolveSuppliedTime, sanitizeName } from "./sanitize.mjs";
|
|
3
|
+
import { isArray, isError, isNullish } from "../utils/index.mjs";
|
|
4
|
+
import { CIRCULAR_VALUE, MAX_JSON_SAFE_VALUE_DEPTH, MAX_JSON_SAFE_VALUE_ITEMS, MAX_JSON_SAFE_VALUE_NODES, UNSERIALIZABLE_VALUE, assignUserAttributes } from "../utils/json-utils.mjs";
|
|
5
|
+
function monotonicNow() {
|
|
6
|
+
const perf = globalThis.performance;
|
|
7
|
+
return 'function' == typeof perf?.now ? perf.now() : void 0;
|
|
8
|
+
}
|
|
9
|
+
class PostHogSpan {
|
|
10
|
+
constructor(init, _onEnd, _logger){
|
|
11
|
+
this._onEnd = _onEnd;
|
|
12
|
+
this._logger = _logger;
|
|
13
|
+
this._events = [];
|
|
14
|
+
this._ended = false;
|
|
15
|
+
this._userAttributeCount = 0;
|
|
16
|
+
this._userEventCount = 0;
|
|
17
|
+
this._droppedAttributes = 0;
|
|
18
|
+
this._droppedEvents = 0;
|
|
19
|
+
this._traceId = init.traceId;
|
|
20
|
+
this._spanId = init.spanId;
|
|
21
|
+
this._parentSpanId = init.parentSpanId;
|
|
22
|
+
this._traceState = init.traceState;
|
|
23
|
+
this._traceFlags = init.traceFlags ?? TRACE_FLAGS_SAMPLED;
|
|
24
|
+
this._parentIsRemote = init.parentIsRemote ?? false;
|
|
25
|
+
this._name = init.name;
|
|
26
|
+
this._kind = init.kind;
|
|
27
|
+
this._autoKeys = new Set(init.autoAttributeKeys);
|
|
28
|
+
this._maxAttributes = init.maxAttributes;
|
|
29
|
+
this._maxEvents = init.maxEvents;
|
|
30
|
+
this._maxAttributesPerEvent = init.maxAttributesPerEvent;
|
|
31
|
+
this._maxAttributeValueLength = init.maxAttributeValueLength;
|
|
32
|
+
this._attributes = Object.create(null);
|
|
33
|
+
for (const key of Object.keys(init.attributes))this._writeAttribute(key, init.attributes[key]);
|
|
34
|
+
this._startTime = init.startTime;
|
|
35
|
+
this._startMono = init.backdated ? void 0 : monotonicNow();
|
|
36
|
+
}
|
|
37
|
+
_now() {
|
|
38
|
+
if (void 0 !== this._startMono) {
|
|
39
|
+
const mono = monotonicNow();
|
|
40
|
+
if (void 0 !== mono) return this._startTime + Math.max(0, mono - this._startMono);
|
|
41
|
+
}
|
|
42
|
+
return Date.now();
|
|
43
|
+
}
|
|
44
|
+
_mutable(operation) {
|
|
45
|
+
if (this._ended) {
|
|
46
|
+
this._logger?.debug(`Ignoring ${operation} on a span that has already ended`);
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
_writeAttribute(key, value) {
|
|
52
|
+
if (isNullish(value)) {
|
|
53
|
+
if (key in this._attributes && !this._autoKeys.has(key)) this._userAttributeCount--;
|
|
54
|
+
delete this._attributes[key];
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (!this._autoKeys.has(key) && !(key in this._attributes)) {
|
|
58
|
+
if (this._userAttributeCount >= this._maxAttributes) return void this._droppedAttributes++;
|
|
59
|
+
this._userAttributeCount++;
|
|
60
|
+
}
|
|
61
|
+
this._attributes[key] = truncateAttributeValue(value, this._maxAttributeValueLength);
|
|
62
|
+
}
|
|
63
|
+
setAttribute(key, value) {
|
|
64
|
+
if (this._mutable('setAttribute')) this._writeAttribute(key, value);
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
setAttributes(attributes) {
|
|
68
|
+
if (this._mutable('setAttributes')) {
|
|
69
|
+
const safe = assignUserAttributes({}, attributes);
|
|
70
|
+
for (const key of Object.keys(safe))this._writeAttribute(key, safe[key]);
|
|
71
|
+
}
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
addEvent(name, attributes, timestamp) {
|
|
75
|
+
if (this._mutable('addEvent')) {
|
|
76
|
+
if (this._userEventCount >= this._maxEvents) {
|
|
77
|
+
this._droppedEvents++;
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
this._userEventCount++;
|
|
81
|
+
const bounded = attributes && boundAttributes(attributes, this._maxAttributesPerEvent, this._maxAttributeValueLength);
|
|
82
|
+
this._events.push({
|
|
83
|
+
name: sanitizeName(name, 'Span event name', this._maxAttributeValueLength, this._logger),
|
|
84
|
+
timestamp: resolveSuppliedTime(timestamp, this._now(), 'event timestamp', this._logger),
|
|
85
|
+
...bounded && {
|
|
86
|
+
attributes: bounded.attributes,
|
|
87
|
+
...bounded.dropped && {
|
|
88
|
+
droppedAttributesCount: bounded.dropped
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
setStatus(status, message) {
|
|
96
|
+
if (this._mutable('setStatus')) {
|
|
97
|
+
if ('ok' !== status && 'error' !== status) {
|
|
98
|
+
this._logger?.debug(`Ignoring unknown span status "${String(status)}"; expected "ok" or "error"`);
|
|
99
|
+
return this;
|
|
100
|
+
}
|
|
101
|
+
this._status = {
|
|
102
|
+
code: status,
|
|
103
|
+
...message && {
|
|
104
|
+
message: truncateString(message, this._maxAttributeValueLength)
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
get statusIsExplicitlyOk() {
|
|
111
|
+
return this._status?.code === 'ok';
|
|
112
|
+
}
|
|
113
|
+
recordException(error) {
|
|
114
|
+
if (!this._mutable('recordException')) return this;
|
|
115
|
+
const { type, message, stack } = describeError(error);
|
|
116
|
+
this.addEvent(EXCEPTION_EVENT_NAME, {
|
|
117
|
+
'exception.type': type,
|
|
118
|
+
'exception.message': message,
|
|
119
|
+
...stack && {
|
|
120
|
+
'exception.stacktrace': stack
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
return this.setStatus('error', message);
|
|
124
|
+
}
|
|
125
|
+
updateName(name) {
|
|
126
|
+
if (this._mutable('updateName')) this._name = sanitizeName(name, 'Span name', this._maxAttributeValueLength, this._logger);
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
traceparent() {
|
|
130
|
+
return formatTraceparent(this._traceId, this._spanId, this._traceFlags);
|
|
131
|
+
}
|
|
132
|
+
tracestate() {
|
|
133
|
+
return this._traceState ?? null;
|
|
134
|
+
}
|
|
135
|
+
childContext() {
|
|
136
|
+
return {
|
|
137
|
+
traceId: this._traceId,
|
|
138
|
+
parentSpanId: this._spanId,
|
|
139
|
+
traceState: this._traceState,
|
|
140
|
+
traceFlags: this._traceFlags
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
end(endTime) {
|
|
144
|
+
if (this._ended) return void this._logger?.debug('Ignoring end() on a span that has already ended');
|
|
145
|
+
this._ended = true;
|
|
146
|
+
const derived = this._now();
|
|
147
|
+
const resolved = resolveSuppliedTime(endTime, derived, 'end time', this._logger);
|
|
148
|
+
this._onEnd({
|
|
149
|
+
traceId: this._traceId,
|
|
150
|
+
spanId: this._spanId,
|
|
151
|
+
...this._parentSpanId && {
|
|
152
|
+
parentSpanId: this._parentSpanId
|
|
153
|
+
},
|
|
154
|
+
...this._traceState && {
|
|
155
|
+
traceState: this._traceState
|
|
156
|
+
},
|
|
157
|
+
traceFlags: this._traceFlags,
|
|
158
|
+
parentIsRemote: this._parentIsRemote,
|
|
159
|
+
name: this._name,
|
|
160
|
+
kind: this._kind,
|
|
161
|
+
...this._status && {
|
|
162
|
+
status: this._status
|
|
163
|
+
},
|
|
164
|
+
attributes: {
|
|
165
|
+
...this._attributes
|
|
166
|
+
},
|
|
167
|
+
events: this._events,
|
|
168
|
+
startTime: this._startTime,
|
|
169
|
+
endTime: clampEndTime(resolved, this._startTime),
|
|
170
|
+
...this._droppedAttributes && {
|
|
171
|
+
droppedAttributesCount: this._droppedAttributes
|
|
172
|
+
},
|
|
173
|
+
...this._droppedEvents && {
|
|
174
|
+
droppedEventsCount: this._droppedEvents
|
|
175
|
+
}
|
|
176
|
+
}, this._autoKeys);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
const EXCEPTION_EVENT_NAME = 'exception';
|
|
180
|
+
const MAX_UINT32 = 0xffffffff;
|
|
181
|
+
function safeString(value) {
|
|
182
|
+
try {
|
|
183
|
+
return 'string' == typeof value ? value : String(value);
|
|
184
|
+
} catch {
|
|
185
|
+
return UNSERIALIZABLE_VALUE;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
function nonNegativeCount(value) {
|
|
189
|
+
if ('number' != typeof value || !Number.isFinite(value) || value <= 0) return 0;
|
|
190
|
+
return Math.min(Math.floor(value), MAX_UINT32);
|
|
191
|
+
}
|
|
192
|
+
function orderedKeys(attributes, keysBeforeHook) {
|
|
193
|
+
if (!keysBeforeHook.length) return Object.keys(attributes);
|
|
194
|
+
const beforeHook = keysBeforeHook.filter((key)=>Object.prototype.propertyIsEnumerable.call(attributes, key));
|
|
195
|
+
const seen = new Set(beforeHook);
|
|
196
|
+
return [
|
|
197
|
+
...beforeHook,
|
|
198
|
+
...Object.keys(attributes).filter((key)=>!seen.has(key))
|
|
199
|
+
];
|
|
200
|
+
}
|
|
201
|
+
function applySpanLimits(record, autoKeys, maxAttributes, maxEvents, maxAttributesPerEvent, maxAttributeValueLength, keysBeforeHook = []) {
|
|
202
|
+
let kept = 0;
|
|
203
|
+
let droppedAttributes = 0;
|
|
204
|
+
const attributes = {};
|
|
205
|
+
for (const key of orderedKeys(record.attributes, keysBeforeHook)){
|
|
206
|
+
const value = record.attributes[key];
|
|
207
|
+
if (!isNullish(value)) {
|
|
208
|
+
if (!autoKeys.has(key)) {
|
|
209
|
+
if (kept >= maxAttributes) {
|
|
210
|
+
droppedAttributes++;
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
kept++;
|
|
214
|
+
}
|
|
215
|
+
Object.defineProperty(attributes, key, {
|
|
216
|
+
value: truncateAttributeValue(value, maxAttributeValueLength),
|
|
217
|
+
enumerable: true,
|
|
218
|
+
writable: true,
|
|
219
|
+
configurable: true
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
record.attributes = attributes;
|
|
224
|
+
if (droppedAttributes) record.droppedAttributesCount = nonNegativeCount(record.droppedAttributesCount) + droppedAttributes;
|
|
225
|
+
let keptEvents = 0;
|
|
226
|
+
let droppedEvents = 0;
|
|
227
|
+
const events = [];
|
|
228
|
+
for (const event of record.events){
|
|
229
|
+
if (keptEvents >= maxEvents) {
|
|
230
|
+
droppedEvents++;
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
keptEvents++;
|
|
234
|
+
if (event.attributes) {
|
|
235
|
+
const bounded = boundAttributes(event.attributes, maxAttributesPerEvent, maxAttributeValueLength);
|
|
236
|
+
event.attributes = bounded.attributes;
|
|
237
|
+
if (bounded.dropped) event.droppedAttributesCount = nonNegativeCount(event.droppedAttributesCount) + bounded.dropped;
|
|
238
|
+
}
|
|
239
|
+
events.push(event);
|
|
240
|
+
}
|
|
241
|
+
record.events = events;
|
|
242
|
+
if (droppedEvents) record.droppedEventsCount = nonNegativeCount(record.droppedEventsCount) + droppedEvents;
|
|
243
|
+
if (record.status?.message) record.status = {
|
|
244
|
+
...record.status,
|
|
245
|
+
message: truncateString(safeString(record.status.message), maxAttributeValueLength)
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
class NoopSpan {
|
|
249
|
+
setAttribute() {
|
|
250
|
+
return this;
|
|
251
|
+
}
|
|
252
|
+
setAttributes() {
|
|
253
|
+
return this;
|
|
254
|
+
}
|
|
255
|
+
addEvent() {
|
|
256
|
+
return this;
|
|
257
|
+
}
|
|
258
|
+
setStatus() {
|
|
259
|
+
return this;
|
|
260
|
+
}
|
|
261
|
+
recordException() {
|
|
262
|
+
return this;
|
|
263
|
+
}
|
|
264
|
+
updateName() {
|
|
265
|
+
return this;
|
|
266
|
+
}
|
|
267
|
+
traceparent() {
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
tracestate() {
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
end() {}
|
|
274
|
+
}
|
|
275
|
+
const NOOP_SPAN = /* @__PURE__ */ new NoopSpan();
|
|
276
|
+
class PassThroughSpan extends NoopSpan {
|
|
277
|
+
constructor(_traceparent, _tracestate){
|
|
278
|
+
super(), this._traceparent = _traceparent, this._tracestate = _tracestate;
|
|
279
|
+
}
|
|
280
|
+
traceparent() {
|
|
281
|
+
return this._traceparent;
|
|
282
|
+
}
|
|
283
|
+
tracestate() {
|
|
284
|
+
return this._tracestate ?? null;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
function readStack(error) {
|
|
288
|
+
try {
|
|
289
|
+
const stack = error.stack;
|
|
290
|
+
return 'string' == typeof stack && stack ? {
|
|
291
|
+
stack
|
|
292
|
+
} : {};
|
|
293
|
+
} catch {
|
|
294
|
+
return {};
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
function inertSpan(options, active) {
|
|
298
|
+
const parent = traceparentHeader(options?.parent) ?? active;
|
|
299
|
+
const inbound = 'string' == typeof parent || null == parent ? parent : readHandle(parent, 'traceparent');
|
|
300
|
+
const traceparent = normalizeTraceparent(inbound);
|
|
301
|
+
if (!traceparent) return NOOP_SPAN;
|
|
302
|
+
const tracestate = 'string' == typeof parent || null == parent ? options?.tracestate : readHandle(parent, 'tracestate');
|
|
303
|
+
return new PassThroughSpan(traceparent, sanitizeTracestate(tracestate));
|
|
304
|
+
}
|
|
305
|
+
function readHandle(parent, method) {
|
|
306
|
+
try {
|
|
307
|
+
const fn = parent[method];
|
|
308
|
+
return 'function' == typeof fn ? fn.call(parent) : void 0;
|
|
309
|
+
} catch {
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
function truncateString(value, maxLength) {
|
|
314
|
+
return value.length > maxLength ? value.slice(0, maxLength) : value;
|
|
315
|
+
}
|
|
316
|
+
function truncateAttributeValue(value, maxLength) {
|
|
317
|
+
return truncateValue(value, maxLength, {
|
|
318
|
+
ancestors: new WeakSet(),
|
|
319
|
+
remainingNodes: MAX_JSON_SAFE_VALUE_NODES
|
|
320
|
+
}, 0);
|
|
321
|
+
}
|
|
322
|
+
function truncateValue(value, maxLength, state, depth) {
|
|
323
|
+
if (null === value || 'object' != typeof value) {
|
|
324
|
+
if (isNullish(value)) return value;
|
|
325
|
+
if (state.remainingNodes <= 0) return value;
|
|
326
|
+
state.remainingNodes--;
|
|
327
|
+
return 'string' == typeof value ? truncateString(value, maxLength) : value;
|
|
328
|
+
}
|
|
329
|
+
if (state.ancestors.has(value)) return CIRCULAR_VALUE;
|
|
330
|
+
if (state.remainingNodes <= 0 || depth >= MAX_JSON_SAFE_VALUE_DEPTH) return value;
|
|
331
|
+
state.remainingNodes--;
|
|
332
|
+
state.ancestors.add(value);
|
|
333
|
+
try {
|
|
334
|
+
if (value instanceof Date) return value;
|
|
335
|
+
const resolved = resolveToJson(value);
|
|
336
|
+
if (resolved.selfDescribed) return isNullish(resolved.value) ? String(resolved.value) : truncateValue(resolved.value, maxLength, state, depth + 1);
|
|
337
|
+
if (isArray(value)) {
|
|
338
|
+
const walked = Math.min(value.length, MAX_JSON_SAFE_VALUE_ITEMS);
|
|
339
|
+
const boundedItems = [];
|
|
340
|
+
for(let index = 0; index < walked; index++)try {
|
|
341
|
+
boundedItems.push(truncateValue(value[index], maxLength, state, depth + 1));
|
|
342
|
+
} catch {
|
|
343
|
+
boundedItems.push(UNSERIALIZABLE_VALUE);
|
|
344
|
+
}
|
|
345
|
+
if (value.length > walked) boundedItems.length = value.length;
|
|
346
|
+
return boundedItems;
|
|
347
|
+
}
|
|
348
|
+
const bounded = {};
|
|
349
|
+
let emittable = 0;
|
|
350
|
+
for (const key of Object.keys(value)){
|
|
351
|
+
if (emittable >= MAX_JSON_SAFE_VALUE_ITEMS) break;
|
|
352
|
+
let boundedItem;
|
|
353
|
+
try {
|
|
354
|
+
boundedItem = truncateValue(value[key], maxLength, state, depth + 1);
|
|
355
|
+
} catch {
|
|
356
|
+
boundedItem = UNSERIALIZABLE_VALUE;
|
|
357
|
+
}
|
|
358
|
+
if (key && !isNullish(boundedItem)) emittable++;
|
|
359
|
+
Object.defineProperty(bounded, key, {
|
|
360
|
+
value: boundedItem,
|
|
361
|
+
enumerable: true,
|
|
362
|
+
writable: true,
|
|
363
|
+
configurable: true
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
return bounded;
|
|
367
|
+
} catch {
|
|
368
|
+
return value;
|
|
369
|
+
} finally{
|
|
370
|
+
state.ancestors.delete(value);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
function resolveToJson(value) {
|
|
374
|
+
try {
|
|
375
|
+
const toJSON = value.toJSON;
|
|
376
|
+
if ('function' == typeof toJSON) return {
|
|
377
|
+
selfDescribed: true,
|
|
378
|
+
value: toJSON.call(value)
|
|
379
|
+
};
|
|
380
|
+
} catch {}
|
|
381
|
+
return {
|
|
382
|
+
selfDescribed: false
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
function boundAttributes(source, max, maxLength) {
|
|
386
|
+
let keys;
|
|
387
|
+
try {
|
|
388
|
+
keys = Object.keys(source);
|
|
389
|
+
} catch {
|
|
390
|
+
return {
|
|
391
|
+
attributes: {},
|
|
392
|
+
dropped: 0
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
const attributes = {};
|
|
396
|
+
let kept = 0;
|
|
397
|
+
let dropped = 0;
|
|
398
|
+
for (const key of keys){
|
|
399
|
+
if (kept >= max) {
|
|
400
|
+
dropped++;
|
|
401
|
+
continue;
|
|
402
|
+
}
|
|
403
|
+
let value;
|
|
404
|
+
try {
|
|
405
|
+
value = truncateAttributeValue(source[key], maxLength);
|
|
406
|
+
} catch {
|
|
407
|
+
value = UNSERIALIZABLE_VALUE;
|
|
408
|
+
}
|
|
409
|
+
if (!isNullish(value)) {
|
|
410
|
+
kept++;
|
|
411
|
+
Object.defineProperty(attributes, key, {
|
|
412
|
+
value,
|
|
413
|
+
enumerable: true,
|
|
414
|
+
writable: true,
|
|
415
|
+
configurable: true
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return {
|
|
420
|
+
attributes,
|
|
421
|
+
dropped
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
function truncateAttributes(attributes, maxLength) {
|
|
425
|
+
for (const key of Object.keys(attributes))attributes[key] = truncateAttributeValue(attributes[key], maxLength);
|
|
426
|
+
return attributes;
|
|
427
|
+
}
|
|
428
|
+
function runWithActiveSpan(contextManager, span, fn) {
|
|
429
|
+
return span === NOOP_SPAN ? fn(span) : contextManager.with(span, ()=>fn(span));
|
|
430
|
+
}
|
|
431
|
+
function describeError(error) {
|
|
432
|
+
try {
|
|
433
|
+
const stack = readStack(error);
|
|
434
|
+
if (isError(error)) return {
|
|
435
|
+
type: error.name || 'Error',
|
|
436
|
+
message: error.message || '',
|
|
437
|
+
...stack
|
|
438
|
+
};
|
|
439
|
+
if ('string' == typeof error) return {
|
|
440
|
+
type: 'string',
|
|
441
|
+
message: error
|
|
442
|
+
};
|
|
443
|
+
if (error && 'object' == typeof error) {
|
|
444
|
+
const maybe = error;
|
|
445
|
+
if ('string' == typeof maybe.message) return {
|
|
446
|
+
type: 'string' == typeof maybe.name ? maybe.name : 'Object',
|
|
447
|
+
message: maybe.message,
|
|
448
|
+
...stack
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
return {
|
|
452
|
+
type: typeof error,
|
|
453
|
+
message: String(error)
|
|
454
|
+
};
|
|
455
|
+
} catch {
|
|
456
|
+
return {
|
|
457
|
+
type: typeof error,
|
|
458
|
+
message: ''
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
export { NOOP_SPAN, NoopSpan, PassThroughSpan, PostHogSpan, applySpanLimits, describeError, inertSpan, monotonicNow, nonNegativeCount, runWithActiveSpan, truncateAttributeValue, truncateAttributes };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export interface RemoteSpanContext {
|
|
2
|
+
traceId: string;
|
|
3
|
+
spanId: string;
|
|
4
|
+
/** The inbound trace-flags byte, e.g. `01` sampled, `00` sampled out. */
|
|
5
|
+
flags: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Parses an incoming `traceparent` header value, returning `undefined` for
|
|
9
|
+
* anything malformed so a bad header starts a fresh root rather than throwing.
|
|
10
|
+
*
|
|
11
|
+
* A trace the caller sampled out (`00`) is still continued — PostHog records
|
|
12
|
+
* every captured span — but the inbound flag rides along, so what this SDK
|
|
13
|
+
* propagates onward says what the caller decided rather than overriding it.
|
|
14
|
+
*/
|
|
15
|
+
export declare function parseTraceparent(value: unknown): RemoteSpanContext | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* The inbound `traceparent` as received, or `undefined` when it is malformed.
|
|
18
|
+
*
|
|
19
|
+
* Returned whole rather than rebuilt: a version above `00` may append fields
|
|
20
|
+
* this SDK does not read, and rebuilding would forward a header still labelled
|
|
21
|
+
* with that version but missing what the version defines.
|
|
22
|
+
*/
|
|
23
|
+
export declare function normalizeTraceparent(value: unknown): string | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* A `traceparent` as the string it is, unwrapping the one-element array Node's
|
|
26
|
+
* `headersDistinct` hands over. A longer array is two different inbound values,
|
|
27
|
+
* and picking either would be a guess.
|
|
28
|
+
*/
|
|
29
|
+
export declare function traceparentHeader(value: unknown): unknown;
|
|
30
|
+
/** The W3C sampled bit, set on a trace this SDK started. */
|
|
31
|
+
export declare const TRACE_FLAGS_SAMPLED = "01";
|
|
32
|
+
/**
|
|
33
|
+
* Builds the `traceparent` header value for a span. A span continuing a remote
|
|
34
|
+
* trace propagates the flags byte it was handed: a downstream parent-based
|
|
35
|
+
* sampler must see the decision the head sampler actually made, not one this
|
|
36
|
+
* SDK invented. A trace started here is sampled, because it is recorded.
|
|
37
|
+
*/
|
|
38
|
+
export declare function formatTraceparent(traceId: string, spanId: string, flags?: string): string;
|
|
39
|
+
/**
|
|
40
|
+
* Validates an incoming `tracestate` far enough to know it is safe to echo back.
|
|
41
|
+
* A malformed one is discarded without invalidating its traceparent, so a bad
|
|
42
|
+
* vendor entry never costs us the trace continuation. More than 32 members is
|
|
43
|
+
* malformed: W3C's list grammar admits no more.
|
|
44
|
+
*
|
|
45
|
+
* A valid header over the length W3C asks us to propagate is trimmed instead,
|
|
46
|
+
* by whole members. Members over 128 characters go first — W3C names those as
|
|
47
|
+
* the ones to drop — and the rest from the right, so the entries nearest the
|
|
48
|
+
* caller survive.
|
|
49
|
+
*/
|
|
50
|
+
export declare function sanitizeTracestate(value: unknown): string | undefined;
|
|
51
|
+
//# sourceMappingURL=traceparent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"traceparent.d.ts","sourceRoot":"","sources":["../../src/traces/traceparent.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,MAAM,CAAA;IACf,MAAM,EAAE,MAAM,CAAA;IACd,yEAAyE;IACzE,KAAK,EAAE,MAAM,CAAA;CACd;AAOD;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,iBAAiB,GAAG,SAAS,CAG9E;AA8CD;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAEvE;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAEzD;AAED,4DAA4D;AAC5D,eAAO,MAAM,mBAAmB,OAAO,CAAA;AAKvC;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,GAAE,MAA4B,GAAG,MAAM,CAE9G;AAOD;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CA6BrE"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, getters, values)=>{
|
|
5
|
+
var define = (defs, kind)=>{
|
|
6
|
+
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
[kind]: defs[key]
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
define(getters, "get");
|
|
12
|
+
define(values, "value");
|
|
13
|
+
};
|
|
14
|
+
})();
|
|
15
|
+
(()=>{
|
|
16
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
17
|
+
})();
|
|
18
|
+
(()=>{
|
|
19
|
+
__webpack_require__.r = (exports1)=>{
|
|
20
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
21
|
+
value: 'Module'
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
24
|
+
value: true
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
})();
|
|
28
|
+
var __webpack_exports__ = {};
|
|
29
|
+
__webpack_require__.r(__webpack_exports__);
|
|
30
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
31
|
+
TRACE_FLAGS_SAMPLED: ()=>TRACE_FLAGS_SAMPLED,
|
|
32
|
+
formatTraceparent: ()=>formatTraceparent,
|
|
33
|
+
normalizeTraceparent: ()=>normalizeTraceparent,
|
|
34
|
+
parseTraceparent: ()=>parseTraceparent,
|
|
35
|
+
sanitizeTracestate: ()=>sanitizeTracestate,
|
|
36
|
+
traceparentHeader: ()=>traceparentHeader
|
|
37
|
+
});
|
|
38
|
+
const external_ids_js_namespaceObject = require("./ids.js");
|
|
39
|
+
const TRACEPARENT_RE = /^([0-9a-f]{2})-([0-9a-f]{32})-([0-9a-f]{16})-([0-9a-f]{2})(-.*)?$/;
|
|
40
|
+
function parseTraceparent(value) {
|
|
41
|
+
const fields = matchTraceparent(value);
|
|
42
|
+
return fields && {
|
|
43
|
+
traceId: fields.traceId,
|
|
44
|
+
spanId: fields.spanId,
|
|
45
|
+
flags: definedFlags(fields.flags)
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function definedFlags(flags) {
|
|
49
|
+
return 0x01 & parseInt(flags, 16) ? TRACE_FLAGS_SAMPLED : TRACE_FLAGS_UNSAMPLED;
|
|
50
|
+
}
|
|
51
|
+
function matchTraceparent(value) {
|
|
52
|
+
if ('string' != typeof value) return;
|
|
53
|
+
const match = TRACEPARENT_RE.exec(value.trim());
|
|
54
|
+
if (!match) return;
|
|
55
|
+
const [, version, traceId, spanId, flags, trailing] = match;
|
|
56
|
+
if ('ff' === version) return;
|
|
57
|
+
if ('00' === version && trailing) return;
|
|
58
|
+
if (!(0, external_ids_js_namespaceObject.isValidTraceId)(traceId) || !(0, external_ids_js_namespaceObject.isValidSpanId)(spanId)) return;
|
|
59
|
+
return {
|
|
60
|
+
version,
|
|
61
|
+
traceId,
|
|
62
|
+
spanId,
|
|
63
|
+
flags
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function normalizeTraceparent(value) {
|
|
67
|
+
return matchTraceparent(value) && value.trim();
|
|
68
|
+
}
|
|
69
|
+
function traceparentHeader(value) {
|
|
70
|
+
return Array.isArray(value) && 1 === value.length ? value[0] : value;
|
|
71
|
+
}
|
|
72
|
+
const TRACE_FLAGS_SAMPLED = '01';
|
|
73
|
+
const TRACE_FLAGS_UNSAMPLED = '00';
|
|
74
|
+
function formatTraceparent(traceId, spanId, flags = TRACE_FLAGS_SAMPLED) {
|
|
75
|
+
return `00-${traceId}-${spanId}-${flags}`;
|
|
76
|
+
}
|
|
77
|
+
const TRACESTATE_MAX_MEMBERS = 32;
|
|
78
|
+
const TRACESTATE_MAX_LENGTH = 512;
|
|
79
|
+
function sanitizeTracestate(value) {
|
|
80
|
+
if ('string' != typeof value) return;
|
|
81
|
+
const trimmed = value.trim();
|
|
82
|
+
if (!trimmed) return;
|
|
83
|
+
if (/[^\x20-\x7e\t]/.test(trimmed)) return;
|
|
84
|
+
const members = trimmed.split(',');
|
|
85
|
+
if (members.length > TRACESTATE_MAX_MEMBERS) return;
|
|
86
|
+
for (const member of members)if (member.trim() && !member.includes('=')) return;
|
|
87
|
+
if (trimmed.length <= TRACESTATE_MAX_LENGTH) return trimmed;
|
|
88
|
+
return trimToLength(members);
|
|
89
|
+
}
|
|
90
|
+
const TRACESTATE_LARGE_MEMBER_LENGTH = 128;
|
|
91
|
+
function trimToLength(members) {
|
|
92
|
+
const kept = [
|
|
93
|
+
...members
|
|
94
|
+
];
|
|
95
|
+
const joinedLength = ()=>kept.reduce((total, member)=>total + member.length, 0) + kept.length - 1;
|
|
96
|
+
for(let index = kept.length - 1; index >= 0 && joinedLength() > TRACESTATE_MAX_LENGTH; index--)if (kept[index].length > TRACESTATE_LARGE_MEMBER_LENGTH) kept.splice(index, 1);
|
|
97
|
+
while(kept.length && joinedLength() > TRACESTATE_MAX_LENGTH)kept.pop();
|
|
98
|
+
return kept.length ? kept.join(',') : void 0;
|
|
99
|
+
}
|
|
100
|
+
exports.TRACE_FLAGS_SAMPLED = __webpack_exports__.TRACE_FLAGS_SAMPLED;
|
|
101
|
+
exports.formatTraceparent = __webpack_exports__.formatTraceparent;
|
|
102
|
+
exports.normalizeTraceparent = __webpack_exports__.normalizeTraceparent;
|
|
103
|
+
exports.parseTraceparent = __webpack_exports__.parseTraceparent;
|
|
104
|
+
exports.sanitizeTracestate = __webpack_exports__.sanitizeTracestate;
|
|
105
|
+
exports.traceparentHeader = __webpack_exports__.traceparentHeader;
|
|
106
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
107
|
+
"TRACE_FLAGS_SAMPLED",
|
|
108
|
+
"formatTraceparent",
|
|
109
|
+
"normalizeTraceparent",
|
|
110
|
+
"parseTraceparent",
|
|
111
|
+
"sanitizeTracestate",
|
|
112
|
+
"traceparentHeader"
|
|
113
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
114
|
+
Object.defineProperty(exports, '__esModule', {
|
|
115
|
+
value: true
|
|
116
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { isValidSpanId, isValidTraceId } from "./ids.mjs";
|
|
2
|
+
const TRACEPARENT_RE = /^([0-9a-f]{2})-([0-9a-f]{32})-([0-9a-f]{16})-([0-9a-f]{2})(-.*)?$/;
|
|
3
|
+
function parseTraceparent(value) {
|
|
4
|
+
const fields = matchTraceparent(value);
|
|
5
|
+
return fields && {
|
|
6
|
+
traceId: fields.traceId,
|
|
7
|
+
spanId: fields.spanId,
|
|
8
|
+
flags: definedFlags(fields.flags)
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
function definedFlags(flags) {
|
|
12
|
+
return 0x01 & parseInt(flags, 16) ? TRACE_FLAGS_SAMPLED : TRACE_FLAGS_UNSAMPLED;
|
|
13
|
+
}
|
|
14
|
+
function matchTraceparent(value) {
|
|
15
|
+
if ('string' != typeof value) return;
|
|
16
|
+
const match = TRACEPARENT_RE.exec(value.trim());
|
|
17
|
+
if (!match) return;
|
|
18
|
+
const [, version, traceId, spanId, flags, trailing] = match;
|
|
19
|
+
if ('ff' === version) return;
|
|
20
|
+
if ('00' === version && trailing) return;
|
|
21
|
+
if (!isValidTraceId(traceId) || !isValidSpanId(spanId)) return;
|
|
22
|
+
return {
|
|
23
|
+
version,
|
|
24
|
+
traceId,
|
|
25
|
+
spanId,
|
|
26
|
+
flags
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function normalizeTraceparent(value) {
|
|
30
|
+
return matchTraceparent(value) && value.trim();
|
|
31
|
+
}
|
|
32
|
+
function traceparentHeader(value) {
|
|
33
|
+
return Array.isArray(value) && 1 === value.length ? value[0] : value;
|
|
34
|
+
}
|
|
35
|
+
const TRACE_FLAGS_SAMPLED = '01';
|
|
36
|
+
const TRACE_FLAGS_UNSAMPLED = '00';
|
|
37
|
+
function formatTraceparent(traceId, spanId, flags = TRACE_FLAGS_SAMPLED) {
|
|
38
|
+
return `00-${traceId}-${spanId}-${flags}`;
|
|
39
|
+
}
|
|
40
|
+
const TRACESTATE_MAX_MEMBERS = 32;
|
|
41
|
+
const TRACESTATE_MAX_LENGTH = 512;
|
|
42
|
+
function sanitizeTracestate(value) {
|
|
43
|
+
if ('string' != typeof value) return;
|
|
44
|
+
const trimmed = value.trim();
|
|
45
|
+
if (!trimmed) return;
|
|
46
|
+
if (/[^\x20-\x7e\t]/.test(trimmed)) return;
|
|
47
|
+
const members = trimmed.split(',');
|
|
48
|
+
if (members.length > TRACESTATE_MAX_MEMBERS) return;
|
|
49
|
+
for (const member of members)if (member.trim() && !member.includes('=')) return;
|
|
50
|
+
if (trimmed.length <= TRACESTATE_MAX_LENGTH) return trimmed;
|
|
51
|
+
return trimToLength(members);
|
|
52
|
+
}
|
|
53
|
+
const TRACESTATE_LARGE_MEMBER_LENGTH = 128;
|
|
54
|
+
function trimToLength(members) {
|
|
55
|
+
const kept = [
|
|
56
|
+
...members
|
|
57
|
+
];
|
|
58
|
+
const joinedLength = ()=>kept.reduce((total, member)=>total + member.length, 0) + kept.length - 1;
|
|
59
|
+
for(let index = kept.length - 1; index >= 0 && joinedLength() > TRACESTATE_MAX_LENGTH; index--)if (kept[index].length > TRACESTATE_LARGE_MEMBER_LENGTH) kept.splice(index, 1);
|
|
60
|
+
while(kept.length && joinedLength() > TRACESTATE_MAX_LENGTH)kept.pop();
|
|
61
|
+
return kept.length ? kept.join(',') : void 0;
|
|
62
|
+
}
|
|
63
|
+
export { TRACE_FLAGS_SAMPLED, formatTraceparent, normalizeTraceparent, parseTraceparent, sanitizeTracestate, traceparentHeader };
|