-
Notifications
You must be signed in to change notification settings - Fork 292
/
priority_sampler.js
166 lines (128 loc) · 4.48 KB
/
priority_sampler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use strict'
const RateLimiter = require('./rate_limiter')
const Sampler = require('./sampler')
const ext = require('../../../ext')
const { setSamplingRules } = require('./startup-log')
const {
SAMPLING_RULE_DECISION,
SAMPLING_LIMIT_DECISION,
SAMPLING_AGENT_DECISION
} = require('./constants')
const SERVICE_NAME = ext.tags.SERVICE_NAME
const SAMPLING_PRIORITY = ext.tags.SAMPLING_PRIORITY
const MANUAL_KEEP = ext.tags.MANUAL_KEEP
const MANUAL_DROP = ext.tags.MANUAL_DROP
const USER_REJECT = ext.priority.USER_REJECT
const AUTO_REJECT = ext.priority.AUTO_REJECT
const AUTO_KEEP = ext.priority.AUTO_KEEP
const USER_KEEP = ext.priority.USER_KEEP
const DEFAULT_KEY = 'service:,env:'
const defaultSampler = new Sampler(AUTO_KEEP)
class PrioritySampler {
constructor (env, { sampleRate, rateLimit = 100, rules = [] } = {}) {
this._env = env
this._rules = this._normalizeRules(rules, sampleRate)
this._limiter = new RateLimiter(rateLimit)
setSamplingRules(this._rules)
this.update({})
}
isSampled (span) {
const priority = this._getPriorityFromAuto(span)
return priority === USER_KEEP || priority === AUTO_KEEP
}
sample (span, auto = true) {
if (!span) return
const context = this._getContext(span)
const root = context._trace.started[0]
if (context._sampling.priority !== undefined) return
if (!root) return // noop span
const tag = this._getPriorityFromTags(context._tags)
if (this.validate(tag)) {
context._sampling.priority = tag
} else if (auto) {
context._sampling.priority = this._getPriorityFromAuto(root)
}
}
update (rates) {
const samplers = {}
for (const key in rates) {
const rate = rates[key]
const sampler = new Sampler(rate)
samplers[key] = sampler
}
samplers[DEFAULT_KEY] = samplers[DEFAULT_KEY] || defaultSampler
this._samplers = samplers
}
validate (samplingPriority) {
switch (samplingPriority) {
case USER_REJECT:
case USER_KEEP:
case AUTO_REJECT:
case AUTO_KEEP:
return true
default:
return false
}
}
_getContext (span) {
return typeof span.context === 'function' ? span.context() : span
}
_getPriorityFromAuto (span) {
const context = this._getContext(span)
const rule = this._findRule(context)
return rule
? this._getPriorityByRule(context, rule)
: this._getPriorityByAgent(context)
}
_getPriorityFromTags (tags) {
if (tags.hasOwnProperty(MANUAL_KEEP) && tags[MANUAL_KEEP] !== false) {
return USER_KEEP
} else if (tags.hasOwnProperty(MANUAL_DROP) && tags[MANUAL_DROP] !== false) {
return USER_REJECT
} else {
const priority = parseInt(tags[SAMPLING_PRIORITY], 10)
if (priority === 1 || priority === 2) {
return USER_KEEP
} else if (priority === 0 || priority === -1) {
return USER_REJECT
}
}
}
_getPriorityByRule (context, rule) {
context._trace[SAMPLING_RULE_DECISION] = rule.sampleRate
return rule.sampler.isSampled(context) && this._isSampledByRateLimit(context) ? USER_KEEP : USER_REJECT
}
_isSampledByRateLimit (context) {
const allowed = this._limiter.isAllowed()
context._trace[SAMPLING_LIMIT_DECISION] = this._limiter.effectiveRate()
return allowed
}
_getPriorityByAgent (context) {
const key = `service:${context._tags[SERVICE_NAME]},env:${this._env}`
const sampler = this._samplers[key] || this._samplers[DEFAULT_KEY]
context._trace[SAMPLING_AGENT_DECISION] = sampler.rate()
return sampler.isSampled(context) ? AUTO_KEEP : AUTO_REJECT
}
_normalizeRules (rules, sampleRate) {
return rules
.concat({ sampleRate })
.map(rule => ({ ...rule, sampleRate: parseFloat(rule.sampleRate) }))
.filter(rule => !isNaN(rule.sampleRate))
.map(rule => ({ ...rule, sampler: new Sampler(rule.sampleRate) }))
}
_findRule (context) {
for (let i = 0, l = this._rules.length; i < l; i++) {
if (this._matchRule(context, this._rules[i])) return this._rules[i]
}
}
_matchRule (context, rule) {
const name = context._name
const service = context._tags['service.name']
if (rule.name instanceof RegExp && !rule.name.test(name)) return false
if (typeof rule.name === 'string' && rule.name !== name) return false
if (rule.service instanceof RegExp && !rule.service.test(service)) return false
if (typeof rule.service === 'string' && rule.service !== service) return false
return true
}
}
module.exports = PrioritySampler