Advanced Sampling
This guide covers advanced sampling patterns for high-throughput production environments. Learn how to fine-tune log volume while preserving critical information.
Sampling Architecture
Vestig's sampling system consists of:
text
Probability Sampling
Random sampling based on configured probability.
Basic Usage
typescript
Shorthand Syntax
typescript
Probability Values
| Value | Meaning |
|---|---|
0 |
Drop all logs (0%) |
0.01 |
Keep 1% of logs |
0.1 |
Keep 10% of logs |
0.5 |
Keep 50% of logs |
1 |
Keep all logs (100%) |
Values are clamped between 0 and 1.
Rate Limit Sampling
Cap logs to a maximum rate per time window.
Basic Usage
typescript
Custom Time Windows
typescript
Use Cases
- Prevent log storms: Protect infrastructure during failures
- Cost control: Limit logs to paid services
- Backpressure: Prevent queue overflow
Behavior During Bursts
typescript
Namespace Sampling
Apply different sampling rates to different parts of your application.
Basic Usage
typescript
Wildcard Patterns
| Pattern | Matches |
|---|---|
api |
Exactly "api" |
api.* |
"api.users", "api.orders" |
*.error |
"payment.error", "auth.error" |
* |
Everything |
Pattern Priority
Exact matches take precedence over wildcards:
typescript
Nested Sampling
Combine sampling types per namespace:
typescript
Bypass Rules
Ensure critical logs are never dropped.
Error Bypass
typescript
Level Bypass
typescript
Bypass Levels
| bypassLevel | Bypassed Levels |
|---|---|
'trace' |
All levels (sampling disabled) |
'debug' |
debug, info, warn, error |
'info' |
info, warn, error |
'warn' |
warn, error |
'error' |
error only (default) |
Composite Sampling
Combine bypass rules with any inner sampler:
typescript
Custom Samplers
Implement the Sampler interface for custom logic:
typescript
Stateful Sampler
typescript
Hash-Based Consistent Sampling
Sample same requests consistently (useful for debugging):
typescript
Production Patterns
Environment-Based Sampling
typescript
Trace-Based Sampling
Sample entire request traces consistently:
typescript
Adaptive Sampling
Adjust sampling based on load:
typescript
API Reference
Types
typescript
Factory Functions
| Function | Description |
|---|---|
createProbabilitySampler(config) |
Random sampling |
createRateLimitSampler(config) |
Max logs per second |
createNamespaceSampler(config) |
Per-namespace rules |
createCompositeSampler(inner, opts) |
Add bypass rules |
createSampler(config) |
Create from SamplingConfig |
createSamplerFromConfig(config) |
Create from SamplerConfig |
Best Practices
- Always bypass errors: Keep
alwaysSampleErrors: true - Use namespace sampling: Different rates for different concerns
- Test sampling rates: Verify you're keeping enough logs for debugging
- Monitor dropped logs: Track how many logs are being sampled out
- Adjust based on load: Higher traffic → more aggressive sampling
- Keep traces together: Sample by traceId for consistent debugging