(eventName: string)
| 55 | * @returns The sample_rate if event should be logged, null if it should be dropped |
| 56 | */ |
| 57 | export function shouldSampleEvent(eventName: string): number | null { |
| 58 | const config = getEventSamplingConfig() |
| 59 | const eventConfig = config[eventName] |
| 60 | |
| 61 | // If no config for this event, log at 100% rate (no sampling) |
| 62 | if (!eventConfig) { |
| 63 | return null |
| 64 | } |
| 65 | |
| 66 | const sampleRate = eventConfig.sample_rate |
| 67 | |
| 68 | // Validate sample rate is in valid range |
| 69 | if (typeof sampleRate !== 'number' || sampleRate < 0 || sampleRate > 1) { |
| 70 | return null |
| 71 | } |
| 72 | |
| 73 | // Sample rate of 1 means log everything (no need to add metadata) |
| 74 | if (sampleRate >= 1) { |
| 75 | return null |
| 76 | } |
| 77 | |
| 78 | // Sample rate of 0 means drop everything |
| 79 | if (sampleRate <= 0) { |
| 80 | return 0 |
| 81 | } |
| 82 | |
| 83 | // Randomly decide whether to sample this event |
| 84 | return Math.random() < sampleRate ? sampleRate : 0 |
| 85 | } |
| 86 | |
| 87 | const BATCH_CONFIG_NAME = 'tengu_1p_event_batch_config' |
| 88 | type BatchConfig = { |
no test coverage detected