Skip to content

Commit

Permalink
fix: allow sampling rate to be less than 1 (#896)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjin authored Oct 29, 2018
1 parent b56926a commit 5220f9b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/tracing-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class TracePolicy {
* @param config Configuration for the TracePolicy instance.
*/
constructor(config: TracePolicyConfig) {
if (config.samplingRate >= 0 && config.samplingRate < 1) {
if (config.samplingRate === 0) {
this.sampler = {shouldTrace: () => true};
} else if (config.samplingRate < 0) {
this.sampler = {shouldTrace: () => false};
Expand Down
37 changes: 32 additions & 5 deletions test/test-trace-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ describe('TracePolicy', () => {
});

describe('Sampling', () => {
const tracesPerSecond = [10, 50, 150, 200, 500, 1000];
for (const expected of tracesPerSecond) {
it(`should throttle traces when samplingRate = ` + expected, () => {
const NUM_SECONDS = 10;
const testCases = [0.1, 0.5, 1, 10, 50, 150, 200, 500, 1000];
for (const testCase of testCases) {
it(`should throttle traces when samplingRate = ` + testCase, () => {
const policy =
new TracePolicy({samplingRate: expected, ignoreUrls: []});
new TracePolicy({samplingRate: testCase, ignoreUrls: []});
const expected = NUM_SECONDS * testCase;
let actual = 0;
const start = Date.now();
for (let timestamp = start; timestamp < start + 1000; timestamp++) {
for (let timestamp = start; timestamp < start + 1000 * NUM_SECONDS;
timestamp++) {
if (policy.shouldTrace({timestamp, url: ''})) {
actual++;
}
Expand All @@ -55,5 +58,29 @@ describe('TracePolicy', () => {
`Expected close to (>=0.8*) ${expected} traced but got ${actual}`);
});
}

it('should always sample when samplingRate = 0', () => {
const policy = new TracePolicy({samplingRate: 0, ignoreUrls: []});
let numSamples = 0;
const start = Date.now();
for (let timestamp = start; timestamp < start + 1000; timestamp++) {
if (policy.shouldTrace({timestamp, url: ''})) {
numSamples++;
}
}
assert.strictEqual(numSamples, 1000);
});

it('should never sample when samplingRate < 0', () => {
const policy = new TracePolicy({samplingRate: -1, ignoreUrls: []});
let numSamples = 0;
const start = Date.now();
for (let timestamp = start; timestamp < start + 1000; timestamp++) {
if (policy.shouldTrace({timestamp, url: ''})) {
numSamples++;
}
}
assert.strictEqual(numSamples, 0);
});
});
});

0 comments on commit 5220f9b

Please sign in to comment.