@@ -39,12 +39,28 @@ const DEFAULT_KEY = 'service:,env:'
39
39
40
40
const defaultSampler = new Sampler ( AUTO_KEEP )
41
41
42
+ /**
43
+ * from config.js
44
+ * @typedef { sampleRate: number, provenance: string, rateLimit: number, rules: SamplingRule[] } SamplingConfig
45
+ *
46
+ * empirically defined
47
+ * @typedef {2|-1|1|0 } SamplingPriority
48
+ */
42
49
class PrioritySampler {
50
+ /**
51
+ * @param env {string}
52
+ * @param config {SamplingConfig}
53
+ */
43
54
constructor ( env , config ) {
44
55
this . configure ( env , config )
45
56
this . update ( { } )
46
57
}
47
58
59
+ /**
60
+ *
61
+ * @param env {string}
62
+ * @param opts {SamplingConfig}
63
+ */
48
64
configure ( env , opts = { } ) {
49
65
const { sampleRate, provenance = undefined , rateLimit = 100 , rules = [ ] } = opts
50
66
this . _env = env
@@ -55,12 +71,22 @@ class PrioritySampler {
55
71
setSamplingRules ( this . _rules )
56
72
}
57
73
74
+ /**
75
+ * @param span {DatadogSpan}
76
+ * @returns {boolean }
77
+ */
58
78
isSampled ( span ) {
59
79
const priority = this . _getPriorityFromAuto ( span )
60
80
log . trace ( span )
61
81
return priority === USER_KEEP || priority === AUTO_KEEP
62
82
}
63
83
84
+ /**
85
+ *
86
+ * @param span {DatadogSpan}
87
+ * @param auto {boolean}
88
+ * @returns {void }
89
+ */
64
90
sample ( span , auto = true ) {
65
91
if ( ! span ) return
66
92
@@ -73,7 +99,7 @@ class PrioritySampler {
73
99
74
100
log . trace ( span , auto )
75
101
76
- const tag = this . _getPriorityFromTags ( context . _tags , context )
102
+ const tag = this . _getPriorityFromTags ( context . _tags )
77
103
78
104
if ( this . validate ( tag ) ) {
79
105
context . _sampling . priority = tag
@@ -87,14 +113,17 @@ class PrioritySampler {
87
113
this . _addDecisionMaker ( root )
88
114
}
89
115
116
+ /**
117
+ *
118
+ * @param rates {Object<string, number>}
119
+ * @returns {void }
120
+ */
90
121
update ( rates ) {
91
122
const samplers = { }
92
123
93
124
for ( const key in rates ) {
94
125
const rate = rates [ key ]
95
- const sampler = new Sampler ( rate )
96
-
97
- samplers [ key ] = sampler
126
+ samplers [ key ] = new Sampler ( rate )
98
127
}
99
128
100
129
samplers [ DEFAULT_KEY ] = samplers [ DEFAULT_KEY ] || defaultSampler
@@ -104,6 +133,11 @@ class PrioritySampler {
104
133
log . trace ( rates )
105
134
}
106
135
136
+ /**
137
+ *
138
+ * @param samplingPriority {SamplingPriority}
139
+ * @returns {boolean }
140
+ */
107
141
validate ( samplingPriority ) {
108
142
switch ( samplingPriority ) {
109
143
case USER_REJECT :
@@ -116,6 +150,12 @@ class PrioritySampler {
116
150
}
117
151
}
118
152
153
+ /**
154
+ *
155
+ * @param span {DatadogSpan}
156
+ * @param samplingPriority {SamplingPriority}
157
+ * @param product {import('./standalone/product').PRODUCTS}
158
+ */
119
159
setPriority ( span , samplingPriority , product ) {
120
160
if ( ! span || ! this . validate ( samplingPriority ) ) return
121
161
@@ -137,10 +177,22 @@ class PrioritySampler {
137
177
this . _addDecisionMaker ( root )
138
178
}
139
179
180
+ /**
181
+ *
182
+ * @param span {DatadogSpan}
183
+ * @returns {DatadogSpanContext }
184
+ * @private
185
+ */
140
186
_getContext ( span ) {
141
187
return typeof span . context === 'function' ? span . context ( ) : span
142
188
}
143
189
190
+ /**
191
+ *
192
+ * @param span {DatadogSpan}
193
+ * @returns {SamplingPriority }
194
+ * @private
195
+ */
144
196
_getPriorityFromAuto ( span ) {
145
197
const context = this . _getContext ( span )
146
198
const rule = this . _findRule ( span )
@@ -150,6 +202,12 @@ class PrioritySampler {
150
202
: this . _getPriorityByAgent ( context )
151
203
}
152
204
205
+ /**
206
+ *
207
+ * @param tags {Map<string, Object>}
208
+ * @returns {SamplingPriority }
209
+ * @private
210
+ */
153
211
_getPriorityFromTags ( tags ) {
154
212
if ( hasOwn ( tags , MANUAL_KEEP ) && tags [ MANUAL_KEEP ] !== false ) {
155
213
return USER_KEEP
@@ -166,6 +224,13 @@ class PrioritySampler {
166
224
}
167
225
}
168
226
227
+ /**
228
+ *
229
+ * @param context {DatadogSpanContext}
230
+ * @param rule {SamplingRule}
231
+ * @returns {SamplingPriority }
232
+ * @private
233
+ */
169
234
_getPriorityByRule ( context , rule ) {
170
235
context . _trace [ SAMPLING_RULE_DECISION ] = rule . sampleRate
171
236
context . _sampling . mechanism = SAMPLING_MECHANISM_RULE
@@ -177,6 +242,12 @@ class PrioritySampler {
177
242
: USER_REJECT
178
243
}
179
244
245
+ /**
246
+ *
247
+ * @param context {DatadogSpanContext}
248
+ * @returns {boolean }
249
+ * @private
250
+ */
180
251
_isSampledByRateLimit ( context ) {
181
252
const allowed = this . _limiter . isAllowed ( )
182
253
@@ -185,6 +256,12 @@ class PrioritySampler {
185
256
return allowed
186
257
}
187
258
259
+ /**
260
+ *
261
+ * @param context {DatadogSpanContext}
262
+ * @returns {SamplingPriority }
263
+ * @private
264
+ */
188
265
_getPriorityByAgent ( context ) {
189
266
const key = `service:${ context . _tags [ SERVICE_NAME ] } ,env:${ this . _env } `
190
267
const sampler = this . _samplers [ key ] || this . _samplers [ DEFAULT_KEY ]
@@ -200,6 +277,12 @@ class PrioritySampler {
200
277
return sampler . isSampled ( context ) ? AUTO_KEEP : AUTO_REJECT
201
278
}
202
279
280
+ /**
281
+ *
282
+ * @param span {DatadogSpan}
283
+ * @private
284
+ * @returns {void }
285
+ */
203
286
_addDecisionMaker ( span ) {
204
287
const context = span . context ( )
205
288
const trace = context . _trace
@@ -215,6 +298,15 @@ class PrioritySampler {
215
298
}
216
299
}
217
300
301
+ /**
302
+ *
303
+ * @param rules {SamplingRule[]}
304
+ * @param sampleRate {number}
305
+ * @param rateLimit {number}
306
+ * @param provenance {string}
307
+ * @returns {SamplingRule[] }
308
+ * @private
309
+ */
218
310
_normalizeRules ( rules , sampleRate , rateLimit , provenance ) {
219
311
rules = [ ] . concat ( rules || [ ] )
220
312
@@ -225,12 +317,23 @@ class PrioritySampler {
225
317
. map ( SamplingRule . from )
226
318
}
227
319
320
+ /**
321
+ *
322
+ * @param span {DatadogSpan}
323
+ * @returns {SamplingRule }
324
+ * @private
325
+ */
228
326
_findRule ( span ) {
229
327
for ( const rule of this . _rules ) {
230
328
if ( rule . match ( span ) ) return rule
231
329
}
232
330
}
233
331
332
+ /**
333
+ *
334
+ * @param span {DatadogSpan}
335
+ * @param product {import('./standalone/product').PRODUCTS}
336
+ */
234
337
static keepTrace ( span , product ) {
235
338
span ?. _prioritySampler ?. setPriority ( span , USER_KEEP , product )
236
339
}
0 commit comments