autotel 7.1.0 → 7.2.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/README.md +25 -0
- package/dist/analysis.cjs +17 -0
- package/dist/analysis.d.cts +9 -1
- package/dist/analysis.d.ts +9 -1
- package/dist/analysis.js +17 -1
- package/dist/auto.cjs +2 -3
- package/dist/auto.js +1 -2
- package/dist/correlation-id.cjs +1 -1
- package/dist/correlation-id.js +1 -1
- package/dist/decorators.cjs +1 -1
- package/dist/decorators.d.cts +1 -1
- package/dist/decorators.d.ts +1 -1
- package/dist/decorators.js +1 -1
- package/dist/event.cjs +1 -1
- package/dist/event.js +1 -1
- package/dist/{functional-BhhP4Er_.cjs → functional-C925G95W.cjs} +44 -10
- package/dist/{functional-CJn2zY4o.d.cts → functional-CgWcRXGX.d.cts} +12 -5
- package/dist/{functional-Cub1ku-C.js → functional-Doi0hFm3.js} +40 -6
- package/dist/{functional-DXy1cHFF.d.ts → functional-RFFpQzAV.d.ts} +12 -5
- package/dist/functional.cjs +1 -1
- package/dist/functional.d.cts +1 -1
- package/dist/functional.d.ts +1 -1
- package/dist/functional.js +1 -1
- package/dist/http.cjs +1 -1
- package/dist/http.js +1 -1
- package/dist/index.cjs +53 -15
- package/dist/index.d.cts +24 -4
- package/dist/index.d.ts +24 -4
- package/dist/index.js +40 -5
- package/dist/{init-DrlL1eis.d.cts → init-8GSy2bza.d.cts} +1 -1
- package/dist/{init-DDyaLzeg.js → init-DzAdgcFM.js} +2181 -1303
- package/dist/{init-BgnYi2sm.d.ts → init-UnKrXhpq.d.ts} +1 -1
- package/dist/{init-C5aHRqi2.cjs → init-aTZKsAwy.cjs} +2324 -1294
- package/dist/instrumentation.cjs +2 -3
- package/dist/instrumentation.js +1 -2
- package/dist/messaging.cjs +5 -5
- package/dist/messaging.js +2 -2
- package/dist/{sampling-CM5S2Lp3.d.ts → sampling-DBng4Foa.d.ts} +20 -1
- package/dist/{sampling-CmRdnXUi.d.cts → sampling-Er217Mge.d.cts} +20 -1
- package/dist/sampling.cjs +22 -601
- package/dist/sampling.d.cts +2 -2
- package/dist/sampling.d.ts +2 -2
- package/dist/sampling.js +2 -585
- package/dist/semantic-helpers.cjs +1 -1
- package/dist/semantic-helpers.js +1 -1
- package/dist/tail-sampling-processor.cjs +2 -26
- package/dist/tail-sampling-processor.js +1 -25
- package/dist/webhook.cjs +1 -1
- package/dist/webhook.js +1 -1
- package/dist/workflow-distributed.cjs +1 -1
- package/dist/workflow-distributed.js +1 -1
- package/dist/workflow.cjs +1 -1
- package/dist/workflow.js +1 -1
- package/dist/yaml-config.cjs +4 -248
- package/dist/yaml-config.d.cts +2 -2
- package/dist/yaml-config.d.ts +2 -2
- package/dist/yaml-config.js +1 -242
- package/package.json +1 -1
package/dist/sampling.cjs
CHANGED
|
@@ -1,603 +1,24 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const
|
|
3
|
-
let _opentelemetry_api = require("@opentelemetry/api");
|
|
2
|
+
const require_init = require('./init-aTZKsAwy.cjs');
|
|
4
3
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return probability > 0 ? 1 / probability : 0;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Map a string to a stable, evenly spread position in the unit interval.
|
|
30
|
-
*
|
|
31
|
-
* Two processes that hash the same key reach the same number, which is what
|
|
32
|
-
* lets independent services agree on one trace's sampling decision.
|
|
33
|
-
*
|
|
34
|
-
* The spread matters as much as the stability. Real sampling keys share long
|
|
35
|
-
* prefixes: `user_1000`, `user_1001`, `checkout-trace-0001`. A plain
|
|
36
|
-
* multiply-and-add hash lets that shared prefix dominate the high bits, so a
|
|
37
|
-
* whole key family lands in one narrow band and a rate of 0.1 keeps all of
|
|
38
|
-
* them or none of them. FNV-1a followed by the murmur3 finalizer mixes the
|
|
39
|
-
* low bits back through the word, so keys that differ in one character land
|
|
40
|
-
* far apart.
|
|
41
|
-
*/
|
|
42
|
-
function hashUnitInterval(value) {
|
|
43
|
-
let hash = 2166136261;
|
|
44
|
-
for (let i = 0; i < value.length; i++) {
|
|
45
|
-
hash ^= value.codePointAt(i) ?? 0;
|
|
46
|
-
hash = Math.imul(hash, 16777619);
|
|
47
|
-
}
|
|
48
|
-
hash ^= hash >>> 16;
|
|
49
|
-
hash = Math.imul(hash, 2246822507);
|
|
50
|
-
hash ^= hash >>> 13;
|
|
51
|
-
hash = Math.imul(hash, 3266489909);
|
|
52
|
-
hash ^= hash >>> 16;
|
|
53
|
-
return (hash >>> 0) / 4294967296;
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Simple random sampler
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* ```typescript
|
|
60
|
-
* new RandomSampler(0.1) // Sample 10% of requests
|
|
61
|
-
* ```
|
|
62
|
-
*/
|
|
63
|
-
var RandomSampler = class {
|
|
64
|
-
rate;
|
|
65
|
-
constructor(rate) {
|
|
66
|
-
this.rate = rate;
|
|
67
|
-
if (rate < 0 || rate > 1) throw new Error("Sample rate must be between 0 and 1");
|
|
68
|
-
}
|
|
69
|
-
shouldSample(_context) {
|
|
70
|
-
return Math.random() < this.rate;
|
|
71
|
-
}
|
|
72
|
-
sampleRate() {
|
|
73
|
-
return toSampleRate(this.rate);
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
/**
|
|
77
|
-
* Always sample (100% tracing)
|
|
78
|
-
*/
|
|
79
|
-
var AlwaysSampler = class {
|
|
80
|
-
shouldSample(_context) {
|
|
81
|
-
return true;
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
/**
|
|
85
|
-
* Never sample (0% tracing)
|
|
86
|
-
*/
|
|
87
|
-
var NeverSampler = class {
|
|
88
|
-
shouldSample(_context) {
|
|
89
|
-
return false;
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
/**
|
|
93
|
-
* Adaptive sampler that always traces errors and slow requests
|
|
94
|
-
*
|
|
95
|
-
* This is the recommended sampler for production use.
|
|
96
|
-
* It ensures you never miss critical issues while keeping costs down.
|
|
97
|
-
*
|
|
98
|
-
* Strategy:
|
|
99
|
-
* - Always trace errors (critical for debugging)
|
|
100
|
-
* - Always trace slow requests (performance issues)
|
|
101
|
-
* - Use baseline sample rate for successful fast requests
|
|
102
|
-
*
|
|
103
|
-
* **IMPORTANT - Tail Sampling Requirement:**
|
|
104
|
-
* This sampler uses tail sampling (makes decisions AFTER execution).
|
|
105
|
-
* You MUST use TailSamplingSpanProcessor for it to work correctly:
|
|
106
|
-
*
|
|
107
|
-
* - If using initInstrumentation(): TailSamplingSpanProcessor is auto-configured
|
|
108
|
-
* - If using custom TracerProvider: You MUST manually register TailSamplingSpanProcessor
|
|
109
|
-
*
|
|
110
|
-
* Without TailSamplingSpanProcessor, ALL spans are exported (defeating the cost savings).
|
|
111
|
-
*
|
|
112
|
-
* @see TailSamplingSpanProcessor
|
|
113
|
-
* @see README.md "Tail Sampling with Custom Providers" section
|
|
114
|
-
*
|
|
115
|
-
* @example
|
|
116
|
-
* ```typescript
|
|
117
|
-
* new AdaptiveSampler({
|
|
118
|
-
* baselineSampleRate: 0.1, // 10% of normal requests
|
|
119
|
-
* slowThresholdMs: 1000, // Requests > 1s are "slow"
|
|
120
|
-
* alwaysSampleErrors: true, // Always trace errors
|
|
121
|
-
* alwaysSampleSlow: true // Always trace slow requests
|
|
122
|
-
* })
|
|
123
|
-
* ```
|
|
124
|
-
*/
|
|
125
|
-
var AdaptiveSampler = class {
|
|
126
|
-
baselineSampleRate;
|
|
127
|
-
slowThresholdMs;
|
|
128
|
-
alwaysSampleErrors;
|
|
129
|
-
alwaysSampleSlow;
|
|
130
|
-
linksBased;
|
|
131
|
-
linksRate;
|
|
132
|
-
logger;
|
|
133
|
-
samplingDecisions = /* @__PURE__ */ new WeakMap();
|
|
134
|
-
operationResults = /* @__PURE__ */ new WeakMap();
|
|
135
|
-
constructor(options = {}) {
|
|
136
|
-
this.baselineSampleRate = options.baselineSampleRate ?? .1;
|
|
137
|
-
this.slowThresholdMs = options.slowThresholdMs ?? 1e3;
|
|
138
|
-
this.alwaysSampleErrors = options.alwaysSampleErrors ?? true;
|
|
139
|
-
this.alwaysSampleSlow = options.alwaysSampleSlow ?? true;
|
|
140
|
-
this.linksBased = options.linksBased ?? false;
|
|
141
|
-
this.linksRate = options.linksRate ?? 1;
|
|
142
|
-
this.logger = options.logger;
|
|
143
|
-
if (this.baselineSampleRate < 0 || this.baselineSampleRate > 1) throw new Error("Baseline sample rate must be between 0 and 1");
|
|
144
|
-
if (this.linksRate < 0 || this.linksRate > 1) throw new Error("Links rate must be between 0 and 1");
|
|
145
|
-
}
|
|
146
|
-
needsTailSampling() {
|
|
147
|
-
return true;
|
|
148
|
-
}
|
|
149
|
-
shouldSample(context) {
|
|
150
|
-
const baselineDecision = Math.random() < this.baselineSampleRate;
|
|
151
|
-
this.samplingDecisions.set(context.args, baselineDecision);
|
|
152
|
-
return true;
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Check if any links point to sampled spans.
|
|
156
|
-
*
|
|
157
|
-
* A span is considered linked to a sampled span if any of its links
|
|
158
|
-
* have trace_flags with the sampled bit set (0x01).
|
|
159
|
-
*
|
|
160
|
-
* @param links - Array of span links to check
|
|
161
|
-
* @returns true if any linked span is sampled, false otherwise
|
|
162
|
-
*/
|
|
163
|
-
hasSampledLink(links) {
|
|
164
|
-
if (!links || links.length === 0) return false;
|
|
165
|
-
return links.some((link) => link.context && (link.context.traceFlags & _opentelemetry_api.TraceFlags.SAMPLED) !== 0);
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* Re-evaluate sampling decision after operation completes
|
|
169
|
-
*
|
|
170
|
-
* This allows us to always capture errors and slow requests,
|
|
171
|
-
* even if they weren't initially sampled.
|
|
172
|
-
*
|
|
173
|
-
* @param context - Sampling context
|
|
174
|
-
* @param result - Operation result
|
|
175
|
-
* @returns true if this operation should be kept (not discarded)
|
|
176
|
-
*/
|
|
177
|
-
shouldKeepTrace(context, result) {
|
|
178
|
-
const baselineDecision = this.samplingDecisions.get(context.args) ?? false;
|
|
179
|
-
if (this.alwaysSampleErrors && !result.success) {
|
|
180
|
-
if (!baselineDecision) this.logger?.debug({
|
|
181
|
-
operation: context.operationName,
|
|
182
|
-
error: result.error?.message
|
|
183
|
-
}, "Adaptive sampling: Keeping error trace");
|
|
184
|
-
return true;
|
|
185
|
-
}
|
|
186
|
-
if (this.alwaysSampleSlow && result.duration >= this.slowThresholdMs) {
|
|
187
|
-
if (!baselineDecision) this.logger?.debug({
|
|
188
|
-
operation: context.operationName,
|
|
189
|
-
duration: result.duration
|
|
190
|
-
}, "Adaptive sampling: Keeping slow trace");
|
|
191
|
-
return true;
|
|
192
|
-
}
|
|
193
|
-
if (this.linksBased && context.links && this.hasSampledLink(context.links)) {
|
|
194
|
-
const keepLinked = Math.random() < this.linksRate;
|
|
195
|
-
if (keepLinked && !baselineDecision) this.logger?.debug({
|
|
196
|
-
operation: context.operationName,
|
|
197
|
-
linkCount: context.links.length
|
|
198
|
-
}, "Adaptive sampling: Keeping trace due to sampled link");
|
|
199
|
-
return keepLinked;
|
|
200
|
-
}
|
|
201
|
-
return baselineDecision;
|
|
202
|
-
}
|
|
203
|
-
};
|
|
204
|
-
/**
|
|
205
|
-
* User-based sampler for consistent tracing
|
|
206
|
-
*
|
|
207
|
-
* Always samples requests from specific user IDs.
|
|
208
|
-
* Useful for debugging specific user issues or monitoring VIP users.
|
|
209
|
-
*
|
|
210
|
-
* @example
|
|
211
|
-
* ```typescript
|
|
212
|
-
* new UserIdSampler({
|
|
213
|
-
* baselineSampleRate: 0.01, // 1% of normal users
|
|
214
|
-
* alwaysSampleUsers: ['vip_123'], // Always trace VIP users
|
|
215
|
-
* extractUserId: (args) => args[0]?.userId // Extract user ID from first arg
|
|
216
|
-
* })
|
|
217
|
-
* ```
|
|
218
|
-
*/
|
|
219
|
-
var UserIdSampler = class {
|
|
220
|
-
baselineSampleRate;
|
|
221
|
-
alwaysSampleUsers;
|
|
222
|
-
extractUserId;
|
|
223
|
-
logger;
|
|
224
|
-
constructor(options) {
|
|
225
|
-
this.baselineSampleRate = options.baselineSampleRate ?? .1;
|
|
226
|
-
this.alwaysSampleUsers = new Set(options.alwaysSampleUsers || []);
|
|
227
|
-
this.extractUserId = options.extractUserId;
|
|
228
|
-
this.logger = options.logger;
|
|
229
|
-
}
|
|
230
|
-
shouldSample(context) {
|
|
231
|
-
const userId = this.extractUserId(context.args);
|
|
232
|
-
if (userId && this.alwaysSampleUsers.has(userId)) {
|
|
233
|
-
this.logger?.debug({
|
|
234
|
-
operation: context.operationName,
|
|
235
|
-
userId
|
|
236
|
-
}, "Sampling user request");
|
|
237
|
-
return true;
|
|
238
|
-
}
|
|
239
|
-
if (userId) return this.hashString(userId) < this.baselineSampleRate;
|
|
240
|
-
return Math.random() < this.baselineSampleRate;
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* Add user IDs to always-sample list
|
|
244
|
-
*/
|
|
245
|
-
addAlwaysSampleUsers(...userIds) {
|
|
246
|
-
for (const userId of userIds) this.alwaysSampleUsers.add(userId);
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* Remove user IDs from always-sample list
|
|
250
|
-
*/
|
|
251
|
-
removeAlwaysSampleUsers(...userIds) {
|
|
252
|
-
for (const userId of userIds) this.alwaysSampleUsers.delete(userId);
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Simple hash function for consistent user sampling
|
|
256
|
-
*/
|
|
257
|
-
hashString(str) {
|
|
258
|
-
return hashUnitInterval(str);
|
|
259
|
-
}
|
|
260
|
-
};
|
|
261
|
-
/**
|
|
262
|
-
* Consistent sampler: every service reaches the same verdict for one trace.
|
|
263
|
-
*
|
|
264
|
-
* `RandomSampler` rolls the dice per process, so an upstream service can keep
|
|
265
|
-
* a trace that its downstream drops, leaving a waterfall with holes in it.
|
|
266
|
-
* Hashing a key that travels with the request removes the disagreement. Pass
|
|
267
|
-
* the trace id, or any identifier every hop already shares.
|
|
268
|
-
*
|
|
269
|
-
* @example
|
|
270
|
-
* ```typescript
|
|
271
|
-
* new DeterministicSampler({
|
|
272
|
-
* sampleRate: 0.1,
|
|
273
|
-
* key: (context) => trace.getActiveSpan()?.spanContext().traceId,
|
|
274
|
-
* })
|
|
275
|
-
* ```
|
|
276
|
-
*/
|
|
277
|
-
var DeterministicSampler = class {
|
|
278
|
-
rate;
|
|
279
|
-
key;
|
|
280
|
-
constructor(options) {
|
|
281
|
-
if (options.sampleRate < 0 || options.sampleRate > 1) throw new Error("Sample rate must be between 0 and 1");
|
|
282
|
-
this.rate = options.sampleRate;
|
|
283
|
-
this.key = options.key;
|
|
284
|
-
}
|
|
285
|
-
shouldSample(context) {
|
|
286
|
-
const key = this.key(context);
|
|
287
|
-
if (key === void 0) return Math.random() < this.rate;
|
|
288
|
-
return hashUnitInterval(key) < this.rate;
|
|
289
|
-
}
|
|
290
|
-
sampleRate() {
|
|
291
|
-
return toSampleRate(this.rate);
|
|
292
|
-
}
|
|
293
|
-
};
|
|
294
|
-
/** Bucket for keys seen after the tracked map fills up. */
|
|
295
|
-
const OVERFLOW_KEY = "__overflow__";
|
|
296
|
-
/**
|
|
297
|
-
* Per-key target-rate sampler for workloads with uneven traffic.
|
|
298
|
-
*
|
|
299
|
-
* A single rate serves a skewed workload badly: 1% floods storage with the
|
|
300
|
-
* busiest endpoint and still loses the rare tenant whose failures you need.
|
|
301
|
-
* This sampler counts traffic per key over a rolling window, then sets each
|
|
302
|
-
* key its own rate so every key contributes roughly `targetPerKey` events.
|
|
303
|
-
* Quiet keys survive intact; loud keys get thinned.
|
|
304
|
-
*
|
|
305
|
-
* The first window keeps everything, because no traffic history exists yet.
|
|
306
|
-
* Rates take effect from the second window onward.
|
|
307
|
-
*
|
|
308
|
-
* @example
|
|
309
|
-
* ```typescript
|
|
310
|
-
* new KeyTargetRateSampler({
|
|
311
|
-
* key: (context) => context.operationName,
|
|
312
|
-
* targetPerKey: 10, // ~10 events per key per window
|
|
313
|
-
* windowMs: 30_000,
|
|
314
|
-
* })
|
|
315
|
-
* ```
|
|
316
|
-
*/
|
|
317
|
-
var KeyTargetRateSampler = class {
|
|
318
|
-
key;
|
|
319
|
-
targetPerKey;
|
|
320
|
-
windowMs;
|
|
321
|
-
maxKeys;
|
|
322
|
-
counts = /* @__PURE__ */ new Map();
|
|
323
|
-
rates = /* @__PURE__ */ new Map();
|
|
324
|
-
windowStart = Date.now();
|
|
325
|
-
constructor(options) {
|
|
326
|
-
this.key = options.key;
|
|
327
|
-
this.targetPerKey = options.targetPerKey ?? 10;
|
|
328
|
-
this.windowMs = options.windowMs ?? 3e4;
|
|
329
|
-
this.maxKeys = options.maxKeys ?? 1e3;
|
|
330
|
-
if (this.targetPerKey <= 0) throw new Error("Target per key must be greater than 0");
|
|
331
|
-
if (this.windowMs <= 0) throw new Error("Window must be greater than 0");
|
|
332
|
-
}
|
|
333
|
-
/** Turn the window's observed counts into the next window's rates. */
|
|
334
|
-
roll(now) {
|
|
335
|
-
if (now - this.windowStart < this.windowMs) return;
|
|
336
|
-
const rates = /* @__PURE__ */ new Map();
|
|
337
|
-
for (const [key, count] of this.counts) rates.set(key, Math.max(1, count / this.targetPerKey));
|
|
338
|
-
this.rates = rates;
|
|
339
|
-
this.counts = /* @__PURE__ */ new Map();
|
|
340
|
-
this.windowStart = now;
|
|
341
|
-
}
|
|
342
|
-
/**
|
|
343
|
-
* Resolve the key, collapsing into one bucket once the map is full.
|
|
344
|
-
*
|
|
345
|
-
* An unbounded key function would otherwise grow the map without limit,
|
|
346
|
-
* which turns a sampler meant to cut cost into a memory leak.
|
|
347
|
-
*/
|
|
348
|
-
resolveKey(context) {
|
|
349
|
-
const key = this.key(context) ?? OVERFLOW_KEY;
|
|
350
|
-
if (this.counts.has(key) || this.counts.size < this.maxKeys) return key;
|
|
351
|
-
return OVERFLOW_KEY;
|
|
352
|
-
}
|
|
353
|
-
shouldSample(context) {
|
|
354
|
-
this.roll(Date.now());
|
|
355
|
-
const key = this.resolveKey(context);
|
|
356
|
-
this.counts.set(key, (this.counts.get(key) ?? 0) + 1);
|
|
357
|
-
const rate = this.rates.get(key) ?? 1;
|
|
358
|
-
return rate <= 1 || Math.random() < 1 / rate;
|
|
359
|
-
}
|
|
360
|
-
sampleRate(context) {
|
|
361
|
-
return this.rates.get(this.resolveKey(context)) ?? 1;
|
|
362
|
-
}
|
|
363
|
-
};
|
|
364
|
-
/**
|
|
365
|
-
* Composite sampler that combines multiple samplers
|
|
366
|
-
*
|
|
367
|
-
* Samples if ANY of the child samplers returns true.
|
|
368
|
-
*
|
|
369
|
-
* @example
|
|
370
|
-
* ```typescript
|
|
371
|
-
* new CompositeSampler([
|
|
372
|
-
* new UserIdSampler({ extractUserId: (args) => args[0]?.userId }),
|
|
373
|
-
* new AdaptiveSampler({ baselineSampleRate: 0.1 })
|
|
374
|
-
* ])
|
|
375
|
-
* ```
|
|
376
|
-
*/
|
|
377
|
-
var CompositeSampler = class {
|
|
378
|
-
samplers;
|
|
379
|
-
constructor(samplers) {
|
|
380
|
-
this.samplers = samplers;
|
|
381
|
-
if (samplers.length === 0) throw new Error("CompositeSampler requires at least one child sampler");
|
|
382
|
-
}
|
|
383
|
-
shouldSample(context) {
|
|
384
|
-
return this.samplers.some((sampler) => sampler.shouldSample(context));
|
|
385
|
-
}
|
|
386
|
-
};
|
|
387
|
-
/**
|
|
388
|
-
* Feature flag sampler
|
|
389
|
-
*
|
|
390
|
-
* Always samples requests with specific feature flags enabled.
|
|
391
|
-
* Perfect for correlating A/B test experiments with metrics.
|
|
392
|
-
*
|
|
393
|
-
* @example
|
|
394
|
-
* ```typescript
|
|
395
|
-
* new FeatureFlagSampler({
|
|
396
|
-
* baselineSampleRate: 0.01,
|
|
397
|
-
* alwaysSampleFlags: ['new_checkout', 'experimental_ui'],
|
|
398
|
-
* extractFlags: (args, metadata) => metadata?.featureFlags
|
|
399
|
-
* })
|
|
400
|
-
* ```
|
|
401
|
-
*/
|
|
402
|
-
var FeatureFlagSampler = class {
|
|
403
|
-
baselineSampleRate;
|
|
404
|
-
alwaysSampleFlags;
|
|
405
|
-
extractFlags;
|
|
406
|
-
logger;
|
|
407
|
-
constructor(options) {
|
|
408
|
-
this.baselineSampleRate = options.baselineSampleRate ?? .1;
|
|
409
|
-
this.alwaysSampleFlags = new Set(options.alwaysSampleFlags || []);
|
|
410
|
-
this.extractFlags = options.extractFlags;
|
|
411
|
-
this.logger = options.logger;
|
|
412
|
-
}
|
|
413
|
-
shouldSample(context) {
|
|
414
|
-
const flags = this.extractFlags(context.args, context.metadata);
|
|
415
|
-
if (flags && flags.some((flag) => this.alwaysSampleFlags.has(flag))) {
|
|
416
|
-
this.logger?.debug({
|
|
417
|
-
operation: context.operationName,
|
|
418
|
-
flags
|
|
419
|
-
}, "Sampling feature flag request");
|
|
420
|
-
return true;
|
|
421
|
-
}
|
|
422
|
-
return Math.random() < this.baselineSampleRate;
|
|
423
|
-
}
|
|
424
|
-
/**
|
|
425
|
-
* Add feature flags to always-sample list
|
|
426
|
-
*/
|
|
427
|
-
addAlwaysSampleFlags(...flags) {
|
|
428
|
-
for (const flag of flags) this.alwaysSampleFlags.add(flag);
|
|
429
|
-
}
|
|
430
|
-
/**
|
|
431
|
-
* Remove feature flags from always-sample list
|
|
432
|
-
*/
|
|
433
|
-
removeAlwaysSampleFlags(...flags) {
|
|
434
|
-
for (const flag of flags) this.alwaysSampleFlags.delete(flag);
|
|
435
|
-
}
|
|
436
|
-
};
|
|
437
|
-
/**
|
|
438
|
-
* Sampling preset factories.
|
|
439
|
-
*
|
|
440
|
-
* For most users, the string shorthand on `init()` is simpler:
|
|
441
|
-
* ```typescript
|
|
442
|
-
* init({ service: 'my-app', sampling: 'production' })
|
|
443
|
-
* ```
|
|
444
|
-
*
|
|
445
|
-
* Use factories when you need to customize:
|
|
446
|
-
* ```typescript
|
|
447
|
-
* init({ service: 'my-app', sampler: samplingPresets.production({ baselineSampleRate: 0.05 }) })
|
|
448
|
-
* ```
|
|
449
|
-
*/
|
|
450
|
-
const samplingPresets = {
|
|
451
|
-
/** Capture everything — best for local development and debugging */
|
|
452
|
-
development: () => new AlwaysSampler(),
|
|
453
|
-
/** Only bad outcomes — zero baseline, errors always kept */
|
|
454
|
-
errorsOnly: () => new AdaptiveSampler({
|
|
455
|
-
baselineSampleRate: 0,
|
|
456
|
-
alwaysSampleErrors: true
|
|
457
|
-
}),
|
|
458
|
-
/**
|
|
459
|
-
* Balanced production defaults — 10% baseline + errors + slow traces.
|
|
460
|
-
* Pass overrides to tune (uses the same option names as AdaptiveSampler).
|
|
461
|
-
*/
|
|
462
|
-
production: (overrides) => new AdaptiveSampler({
|
|
463
|
-
baselineSampleRate: .1,
|
|
464
|
-
alwaysSampleErrors: true,
|
|
465
|
-
alwaysSampleSlow: true,
|
|
466
|
-
slowThresholdMs: 1e3,
|
|
467
|
-
...overrides
|
|
468
|
-
}),
|
|
469
|
-
/** Disable sampling entirely */
|
|
470
|
-
off: () => new NeverSampler()
|
|
471
|
-
};
|
|
472
|
-
/**
|
|
473
|
-
* Resolve a preset string to a Sampler instance.
|
|
474
|
-
* Used internally by `init()` when `sampling` string is provided.
|
|
475
|
-
*
|
|
476
|
-
* @throws Error if preset is not recognized
|
|
477
|
-
*/
|
|
478
|
-
function resolveSamplingPreset(preset) {
|
|
479
|
-
switch (preset) {
|
|
480
|
-
case "development": return samplingPresets.development();
|
|
481
|
-
case "errors-only": return samplingPresets.errorsOnly();
|
|
482
|
-
case "production": return samplingPresets.production();
|
|
483
|
-
case "off": return samplingPresets.off();
|
|
484
|
-
default: throw new Error(`Unknown sampling preset: "${preset}". Valid presets: development, errors-only, production, off`);
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
/**
|
|
488
|
-
* Create a Link from W3C trace context headers (e.g., from a message queue).
|
|
489
|
-
*
|
|
490
|
-
* This is useful for message consumers that need to link to the producer span.
|
|
491
|
-
* The headers should contain at least a `traceparent` header in W3C format.
|
|
492
|
-
*
|
|
493
|
-
* @param headers - Dictionary containing traceparent/tracestate headers
|
|
494
|
-
* @param attributes - Optional attributes for the link
|
|
495
|
-
* @returns Link object if context could be extracted, null otherwise
|
|
496
|
-
*
|
|
497
|
-
* @example
|
|
498
|
-
* ```typescript
|
|
499
|
-
* // In a Kafka consumer
|
|
500
|
-
* const headers = { traceparent: '00-abc123...-def456...-01' };
|
|
501
|
-
* const link = createLinkFromHeaders(headers);
|
|
502
|
-
* if (link) {
|
|
503
|
-
* // Use with tracer.startActiveSpan options or ctx.addLink()
|
|
504
|
-
* tracer.startActiveSpan('process.message', { links: [link] }, span => { ... });
|
|
505
|
-
* }
|
|
506
|
-
* ```
|
|
507
|
-
*/
|
|
508
|
-
function createLinkFromHeaders(headers, attributes) {
|
|
509
|
-
const traceparent = headers.traceparent || headers["traceparent"];
|
|
510
|
-
if (!traceparent) return null;
|
|
511
|
-
const spanContext = parseTraceparent(traceparent);
|
|
512
|
-
if (!spanContext || !isValidSpanContext(spanContext)) return null;
|
|
513
|
-
return {
|
|
514
|
-
context: spanContext,
|
|
515
|
-
attributes: attributes ?? {}
|
|
516
|
-
};
|
|
517
|
-
}
|
|
518
|
-
/**
|
|
519
|
-
* Extract Links from a batch of messages for fan-in scenarios.
|
|
520
|
-
*
|
|
521
|
-
* Useful for batch processing where multiple producer spans should be linked.
|
|
522
|
-
* This enables tracing causality in event-driven architectures where a single
|
|
523
|
-
* consumer processes messages from multiple producers.
|
|
524
|
-
*
|
|
525
|
-
* @param messages - List of message objects
|
|
526
|
-
* @param headersKey - Key in each message containing trace headers (default: 'headers')
|
|
527
|
-
* @returns List of Link objects for all valid trace contexts
|
|
528
|
-
*
|
|
529
|
-
* @example
|
|
530
|
-
* ```typescript
|
|
531
|
-
* // Processing a batch of SQS/Kafka messages
|
|
532
|
-
* const messages = [
|
|
533
|
-
* { body: '...', headers: { traceparent: '...' } },
|
|
534
|
-
* { body: '...', headers: { traceparent: '...' } },
|
|
535
|
-
* ];
|
|
536
|
-
* const links = extractLinksFromBatch(messages);
|
|
537
|
-
*
|
|
538
|
-
* tracer.startActiveSpan('process.batch', { links }, span => {
|
|
539
|
-
* for (const msg of messages) {
|
|
540
|
-
* processMessage(msg);
|
|
541
|
-
* }
|
|
542
|
-
* });
|
|
543
|
-
* ```
|
|
544
|
-
*/
|
|
545
|
-
function extractLinksFromBatch(messages, headersKey = "headers") {
|
|
546
|
-
const links = [];
|
|
547
|
-
for (const msg of messages) {
|
|
548
|
-
const msgHeaders = require_values.asStringRecord(msg[headersKey]);
|
|
549
|
-
if (msgHeaders) {
|
|
550
|
-
const link = createLinkFromHeaders(msgHeaders, { "messaging.batch.message_index": links.length });
|
|
551
|
-
if (link) links.push(link);
|
|
552
|
-
}
|
|
553
|
-
}
|
|
554
|
-
return links;
|
|
555
|
-
}
|
|
556
|
-
/**
|
|
557
|
-
* Parse W3C traceparent header into SpanContext
|
|
558
|
-
* Format: version-traceId-spanId-traceFlags (e.g., 00-abc123...-def456...-01)
|
|
559
|
-
*
|
|
560
|
-
* @see https://www.w3.org/TR/trace-context/#traceparent-header
|
|
561
|
-
*/
|
|
562
|
-
function parseTraceparent(traceparent) {
|
|
563
|
-
const match = traceparent.match(/^([0-9a-f]{2})-([0-9a-f]{32})-([0-9a-f]{16})-([0-9a-f]{2})$/i);
|
|
564
|
-
if (!match || match.length < 5) return null;
|
|
565
|
-
const version = match[1];
|
|
566
|
-
const traceId = match[2];
|
|
567
|
-
const spanId = match[3];
|
|
568
|
-
const flags = match[4];
|
|
569
|
-
if (!version || !traceId || !spanId || !flags) return null;
|
|
570
|
-
if (version === "ff") return null;
|
|
571
|
-
return {
|
|
572
|
-
traceId,
|
|
573
|
-
spanId,
|
|
574
|
-
traceFlags: Number.parseInt(flags, 16),
|
|
575
|
-
isRemote: true
|
|
576
|
-
};
|
|
577
|
-
}
|
|
578
|
-
/**
|
|
579
|
-
* Check if a SpanContext is valid (has non-zero trace and span IDs)
|
|
580
|
-
*/
|
|
581
|
-
function isValidSpanContext(spanContext) {
|
|
582
|
-
if (!spanContext) return false;
|
|
583
|
-
return spanContext.traceId !== "00000000000000000000000000000000" && spanContext.spanId !== "0000000000000000";
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
//#endregion
|
|
587
|
-
exports.AUTOTEL_SAMPLING_RATE = AUTOTEL_SAMPLING_RATE;
|
|
588
|
-
exports.AUTOTEL_SAMPLING_TAIL_EVALUATED = AUTOTEL_SAMPLING_TAIL_EVALUATED;
|
|
589
|
-
exports.AUTOTEL_SAMPLING_TAIL_KEEP = AUTOTEL_SAMPLING_TAIL_KEEP;
|
|
590
|
-
exports.AdaptiveSampler = AdaptiveSampler;
|
|
591
|
-
exports.AlwaysSampler = AlwaysSampler;
|
|
592
|
-
exports.CompositeSampler = CompositeSampler;
|
|
593
|
-
exports.DeterministicSampler = DeterministicSampler;
|
|
594
|
-
exports.FeatureFlagSampler = FeatureFlagSampler;
|
|
595
|
-
exports.KeyTargetRateSampler = KeyTargetRateSampler;
|
|
596
|
-
exports.NeverSampler = NeverSampler;
|
|
597
|
-
exports.RandomSampler = RandomSampler;
|
|
598
|
-
exports.UserIdSampler = UserIdSampler;
|
|
599
|
-
exports.createLinkFromHeaders = createLinkFromHeaders;
|
|
600
|
-
exports.extractLinksFromBatch = extractLinksFromBatch;
|
|
601
|
-
exports.hashUnitInterval = hashUnitInterval;
|
|
602
|
-
exports.resolveSamplingPreset = resolveSamplingPreset;
|
|
603
|
-
exports.samplingPresets = samplingPresets;
|
|
4
|
+
exports.AUTOTEL_DEBUG_BAGGAGE_KEY = require_init.AUTOTEL_DEBUG_BAGGAGE_KEY;
|
|
5
|
+
exports.AUTOTEL_SAMPLING_RATE = require_init.AUTOTEL_SAMPLING_RATE;
|
|
6
|
+
exports.AUTOTEL_SAMPLING_TAIL_EVALUATED = require_init.AUTOTEL_SAMPLING_TAIL_EVALUATED;
|
|
7
|
+
exports.AUTOTEL_SAMPLING_TAIL_KEEP = require_init.AUTOTEL_SAMPLING_TAIL_KEEP;
|
|
8
|
+
exports.AdaptiveSampler = require_init.AdaptiveSampler;
|
|
9
|
+
exports.AlwaysSampler = require_init.AlwaysSampler;
|
|
10
|
+
exports.CompositeSampler = require_init.CompositeSampler;
|
|
11
|
+
exports.DeterministicSampler = require_init.DeterministicSampler;
|
|
12
|
+
exports.FeatureFlagSampler = require_init.FeatureFlagSampler;
|
|
13
|
+
exports.KeyTargetRateSampler = require_init.KeyTargetRateSampler;
|
|
14
|
+
exports.NeverSampler = require_init.NeverSampler;
|
|
15
|
+
exports.RandomSampler = require_init.RandomSampler;
|
|
16
|
+
exports.UserIdSampler = require_init.UserIdSampler;
|
|
17
|
+
exports.createLinkFromHeaders = require_init.createLinkFromHeaders;
|
|
18
|
+
exports.debugCaptureRequested = require_init.debugCaptureRequested;
|
|
19
|
+
exports.extractLinksFromBatch = require_init.extractLinksFromBatch;
|
|
20
|
+
exports.hashUnitInterval = require_init.hashUnitInterval;
|
|
21
|
+
exports.isForceKept = require_init.isForceKept;
|
|
22
|
+
exports.markForceKept = require_init.markForceKept;
|
|
23
|
+
exports.resolveSamplingPreset = require_init.resolveSamplingPreset;
|
|
24
|
+
exports.samplingPresets = require_init.samplingPresets;
|
package/dist/sampling.d.cts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { _ as
|
|
2
|
-
export { AUTOTEL_SAMPLING_RATE, AUTOTEL_SAMPLING_TAIL_EVALUATED, AUTOTEL_SAMPLING_TAIL_KEEP, AdaptiveSampler, AlwaysSampler, CompositeSampler, DeterministicSampler, FeatureFlagSampler, KeyTargetRateSampler, NeverSampler, OperationResult, RandomSampler, Sampler, SamplingContext, SamplingPreset, UserIdSampler, createLinkFromHeaders, extractLinksFromBatch, hashUnitInterval, resolveSamplingPreset, samplingPresets };
|
|
1
|
+
import { C as markForceKept, S as isForceKept, T as samplingPresets, _ as UserIdSampler, a as AdaptiveSampler, b as extractLinksFromBatch, c as DeterministicSampler, d as NeverSampler, f as OperationResult, g as SamplingPreset, h as SamplingContext, i as AUTOTEL_SAMPLING_TAIL_KEEP, l as FeatureFlagSampler, m as Sampler, n as AUTOTEL_SAMPLING_RATE, o as AlwaysSampler, p as RandomSampler, r as AUTOTEL_SAMPLING_TAIL_EVALUATED, s as CompositeSampler, t as AUTOTEL_DEBUG_BAGGAGE_KEY, u as KeyTargetRateSampler, v as createLinkFromHeaders, w as resolveSamplingPreset, x as hashUnitInterval, y as debugCaptureRequested } from "./sampling-Er217Mge.cjs";
|
|
2
|
+
export { AUTOTEL_DEBUG_BAGGAGE_KEY, AUTOTEL_SAMPLING_RATE, AUTOTEL_SAMPLING_TAIL_EVALUATED, AUTOTEL_SAMPLING_TAIL_KEEP, AdaptiveSampler, AlwaysSampler, CompositeSampler, DeterministicSampler, FeatureFlagSampler, KeyTargetRateSampler, NeverSampler, OperationResult, RandomSampler, Sampler, SamplingContext, SamplingPreset, UserIdSampler, createLinkFromHeaders, debugCaptureRequested, extractLinksFromBatch, hashUnitInterval, isForceKept, markForceKept, resolveSamplingPreset, samplingPresets };
|
package/dist/sampling.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { _ as
|
|
2
|
-
export { AUTOTEL_SAMPLING_RATE, AUTOTEL_SAMPLING_TAIL_EVALUATED, AUTOTEL_SAMPLING_TAIL_KEEP, AdaptiveSampler, AlwaysSampler, CompositeSampler, DeterministicSampler, FeatureFlagSampler, KeyTargetRateSampler, NeverSampler, OperationResult, RandomSampler, Sampler, SamplingContext, SamplingPreset, UserIdSampler, createLinkFromHeaders, extractLinksFromBatch, hashUnitInterval, resolveSamplingPreset, samplingPresets };
|
|
1
|
+
import { C as markForceKept, S as isForceKept, T as samplingPresets, _ as UserIdSampler, a as AdaptiveSampler, b as extractLinksFromBatch, c as DeterministicSampler, d as NeverSampler, f as OperationResult, g as SamplingPreset, h as SamplingContext, i as AUTOTEL_SAMPLING_TAIL_KEEP, l as FeatureFlagSampler, m as Sampler, n as AUTOTEL_SAMPLING_RATE, o as AlwaysSampler, p as RandomSampler, r as AUTOTEL_SAMPLING_TAIL_EVALUATED, s as CompositeSampler, t as AUTOTEL_DEBUG_BAGGAGE_KEY, u as KeyTargetRateSampler, v as createLinkFromHeaders, w as resolveSamplingPreset, x as hashUnitInterval, y as debugCaptureRequested } from "./sampling-DBng4Foa.js";
|
|
2
|
+
export { AUTOTEL_DEBUG_BAGGAGE_KEY, AUTOTEL_SAMPLING_RATE, AUTOTEL_SAMPLING_TAIL_EVALUATED, AUTOTEL_SAMPLING_TAIL_KEEP, AdaptiveSampler, AlwaysSampler, CompositeSampler, DeterministicSampler, FeatureFlagSampler, KeyTargetRateSampler, NeverSampler, OperationResult, RandomSampler, Sampler, SamplingContext, SamplingPreset, UserIdSampler, createLinkFromHeaders, debugCaptureRequested, extractLinksFromBatch, hashUnitInterval, isForceKept, markForceKept, resolveSamplingPreset, samplingPresets };
|