Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix trace sampling rate limit applying to individual rules #4169

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions packages/dd-trace/src/priority_sampler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const RateLimiter = require('./rate_limiter')
const Sampler = require('./sampler')
const { setSamplingRules } = require('./startup-log')
const SamplingRule = require('./sampling_rule')
Expand Down Expand Up @@ -43,6 +44,7 @@ class PrioritySampler {
configure (env, { sampleRate, rateLimit = 100, rules = [] } = {}) {
this._env = env
this._rules = this._normalizeRules(rules, sampleRate, rateLimit)
this._limiter = new RateLimiter(rateLimit)

setSamplingRules(this._rules)
}
Expand Down Expand Up @@ -136,14 +138,17 @@ class PrioritySampler {
context._trace[SAMPLING_RULE_DECISION] = rule.sampleRate
context._sampling.mechanism = SAMPLING_MECHANISM_RULE

const sampled = rule.sample()
const priority = sampled ? USER_KEEP : USER_REJECT
return rule.sample() && this._isSampledByRateLimit(context)
? USER_KEEP
: USER_REJECT
}

if (sampled) {
context._trace[SAMPLING_LIMIT_DECISION] = rule.effectiveRate
}
_isSampledByRateLimit (context) {
const allowed = this._limiter.isAllowed()

context._trace[SAMPLING_LIMIT_DECISION] = this._limiter.effectiveRate()

return priority
return allowed
}

_getPriorityByAgent (context) {
Expand Down
23 changes: 23 additions & 0 deletions packages/dd-trace/test/priority_sampler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,29 @@ describe('PrioritySampler', () => {
expect(context._sampling.mechanism).to.equal(SAMPLING_MECHANISM_RULE)
})

it('should support a global rate limit', () => {
prioritySampler = new PrioritySampler('test', {
sampleRate: 1,
rateLimit: 1,
rules: [{
service: 'test',
sampleRate: 1,
rateLimit: 1000
}]
})
prioritySampler.sample(context)

expect(context._sampling).to.have.property('priority', USER_KEEP)
expect(context._sampling.mechanism).to.equal(SAMPLING_MECHANISM_RULE)

delete context._sampling.priority

prioritySampler.sample(context)

expect(context._sampling).to.have.property('priority', USER_REJECT)
expect(context._sampling.mechanism).to.equal(SAMPLING_MECHANISM_RULE)
})

it('should support disabling the rate limit', () => {
prioritySampler = new PrioritySampler('test', {
sampleRate: 1,
Expand Down
Loading