Skip to content

Commit

Permalink
feat: add alwaysoffsampler
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieDanielson committed Feb 14, 2024
1 parent 5318210 commit 36e851a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/deterministic-sampler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Attributes, Context, Link, SpanKind } from '@opentelemetry/api';
import {
AlwaysOffSampler,
AlwaysOnSampler,
Sampler,
SamplingResult,
Expand Down Expand Up @@ -29,6 +30,10 @@ export class DeterministicSampler implements Sampler {
constructor(sampleRate: number) {
this._sampleRate = sampleRate;
switch (sampleRate) {
// sample rate of 0 means send nothing
case 0:
this._sampler = new AlwaysOffSampler();
break;
// sample rate of 1 is default, send everything
case 1:
this._sampler = new AlwaysOnSampler();
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ export interface HoneycombOptions extends Partial<WebSDKConfiguration> {
serviceName?: string;

/** The sample rate used to determine whether a trace is exported.
* This must be a whole number greater than 1. Only 1 out of every `sampleRate` traces will be randomly selected to be sent.
* This must be a whole positive number. Only 1 out of every `sampleRate` traces will be randomly selected to be sent.
* Set to 0 to drop everything.
* Defaults to 1 (send everything).
*/
sampleRate?: number;
Expand Down
9 changes: 5 additions & 4 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ export const getTracesApiKey = (options?: HoneycombOptions) => {

export const getSampleRate = (options?: HoneycombOptions) => {
if (
// sample rate must be a whole integer greater than 0
options?.sampleRate &&
options?.sampleRate > 0 &&
Number.isSafeInteger(options?.sampleRate)
// must be a whole positive integer
options?.sampleRate !== null &&
options?.sampleRate !== undefined &&
options?.sampleRate >= 0 &&
Number.isSafeInteger(options?.sampleRate) === true
) {
return options?.sampleRate;
}
Expand Down
10 changes: 5 additions & 5 deletions test/deterministic-sampler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,22 @@ describe('configureDeterministicSampler', () => {
expect(result.attributes).toEqual({ SampleRate: 1 });
});

test('sample rate of 0 configures inner AlwaysOnSampler', () => {
test('sample rate of 0 configures inner AlwaysOffSampler', () => {
const options = {
sampleRate: 0,
};
const sampler = configureDeterministicSampler(options);
expect(sampler).toBeInstanceOf(DeterministicSampler);
expect(sampler.toString()).toBe('DeterministicSampler(AlwaysOnSampler)');
expect(sampler.toString()).toBe('DeterministicSampler(AlwaysOffSampler)');

const result = getSamplingResult(sampler);
expect(result.decision).toBe(SamplingDecision.RECORD_AND_SAMPLED);
expect(result.attributes).toEqual({ SampleRate: 1 });
expect(result.decision).toBe(SamplingDecision.NOT_RECORD);
expect(result.attributes).toEqual({ SampleRate: 0 });
});

test('sample rate of -42 configures inner AlwaysOn Sampler', () => {
const options = {
sampleRate: 0,
sampleRate: -42,
};
const sampler = configureDeterministicSampler(options);
expect(sampler).toBeInstanceOf(DeterministicSampler);
Expand Down
4 changes: 2 additions & 2 deletions test/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ describe('sample rate', () => {
expect(getSampleRate(options)).toBe(2);
});

it('should use default sample rate if provided with 0', () => {
it('should use 0 sample rate if provided with 0', () => {
const options = {
sampleRate: 0,
};
expect(getSampleRate(options)).toBe(1);
expect(getSampleRate(options)).toBe(0);
});

it('should use default sample rate if provided with negative', () => {
Expand Down

0 comments on commit 36e851a

Please sign in to comment.