UNPKG

@graphql-hive/core

Version:
24 lines (23 loc) 776 B
export function randomSampling(sampleRate) { if (sampleRate > 1 || sampleRate < 0) { throw new Error(`Expected usage.sampleRate to be 0 <= x <= 1, received ${sampleRate}`); } return function shouldInclude() { return Math.random() <= sampleRate; }; } export function dynamicSampling(sampler) { return function shouldInclude(context) { let sampleRate = sampler(context); if (sampleRate === true) { sampleRate = 1; } else if (sampleRate === false) { sampleRate = 0; } if (sampleRate > 1 || sampleRate < 0) { throw new Error(`Expected usage.sampleRate to be 0 <= x <= 1, received ${sampleRate}`); } return Math.random() <= sampleRate; }; }