Commit 2e6aa40
feat(sampling): add custom sampling function support (#141)
## Description
Add `SamplingFunction` for dynamic, context-aware session sampling
decisions. Users can now implement custom sampling logic based on user
attributes, app environment, or other session metadata.
This extends the existing fixed-rate sampling (#139) with a
function-based approach, allowing use cases like:
- Sample all beta testers at 100% while sampling regular users at 10%
- Different sampling rates per environment
- A/B testing with different sampling for user segments
- Sampling based on custom session attributes
## Related Issue(s)
Fixes #89
## Type of Change
- [ ] 🛠️ Bug fix (non-breaking change which fixes an issue)
- [x] 🚀 New feature (non-breaking change which adds functionality)
- [ ] 💥 Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [x] 📝 Documentation
- [ ] 📈 Performance improvement
- [ ] 🏗️ Code refactoring
- [ ] 🧹 Chore / Housekeeping
## Checklist
- [x] I have made corresponding changes to the documentation
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] I have updated the CHANGELOG.md under the "Unreleased" section
## Changes
### SDK Core
- Add `Sampling` sealed class with `SamplingRate` and `SamplingFunction`
variants
- Add `SamplingContext` exposing session metadata (user, app, session
attributes) to sampling functions
- Update `FaroConfig` to accept optional `sampling` parameter
- Update `SessionSamplingProvider` to evaluate sampling functions with
context
### Documentation
- Add comprehensive "Session Sampling" section to `Configurations.md`
- Include examples for fixed rate, dynamic sampling, and available
context properties
### Example App
- Add sampling settings feature to test different sampling
configurations
- Follows established Riverpod ViewModel pattern from
`features/tracing/`
### Tests
- Unit tests for `Sampling`, `SamplingRate`, `SamplingFunction`
- Unit tests for `SamplingContext`
- Integration tests for end-to-end sampling behavior with functions
## Additional Notes
This aligns with [Faro Web SDK sampling
behavior](https://grafana.com/docs/grafana-cloud/monitor-applications/frontend-observability/instrument/sampling/)
where sampling decisions are head-based (made once at SDK init) and
apply to all telemetry types.
Made with [Cursor](https://cursor.com)
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> Introduces a new public configuration surface for sampling and changes
SDK initialization order (app meta set before sampling) which can affect
telemetry collection behavior across sessions; covered by expanded
unit/integration tests but impacts core data-drop logic.
>
> **Overview**
> Adds a new `sampling` API to replace `samplingRate`, introducing a
sealed `Sampling` abstraction with `SamplingRate` (fixed) and
`SamplingFunction` (context-aware) plus a `SamplingContext` exposing
session/user/app meta for dynamic decisions (values clamped to 0.0–1.0).
>
> Updates SDK init to compute sampling once per session using the new
config, exposes `Faro().isSampled`, and ensures app metadata is set
before sampling so functions can depend on it. Documentation, changelog,
and tests are updated accordingly, and the example app gains a new
sampling settings screen (persisted via SharedPreferences/Riverpod) to
demo switching sampling strategies.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
2bcc739. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
---------
Co-authored-by: Cursor <cursoragent@cursor.com>1 parent aadc221 commit 2e6aa40
File tree
24 files changed
+1684
-182
lines changed- doc
- example/lib
- features/sampling_settings
- domain
- models
- presentation
- widgets
- lib
- src
- configurations
- session
- test/src
- configurations
- integrations
- session
24 files changed
+1684
-182
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
13 | | - | |
14 | | - | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
15 | 16 | | |
16 | | - | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
46 | | - | |
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
53 | | - | |
54 | 53 | | |
55 | 54 | | |
56 | 55 | | |
57 | 56 | | |
58 | 57 | | |
59 | 58 | | |
60 | | - | |
61 | 59 | | |
62 | 60 | | |
63 | 61 | | |
| |||
129 | 127 | | |
130 | 128 | | |
131 | 129 | | |
132 | | - | |
133 | 130 | | |
134 | 131 | | |
135 | 132 | | |
| |||
146 | 143 | | |
147 | 144 | | |
148 | 145 | | |
149 | | - | |
150 | 146 | | |
151 | | - | |
152 | 147 | | |
153 | 148 | | |
154 | 149 | | |
| |||
164 | 159 | | |
165 | 160 | | |
166 | 161 | | |
167 | | - | |
168 | 162 | | |
169 | 163 | | |
170 | 164 | | |
171 | 165 | | |
172 | 166 | | |
173 | 167 | | |
174 | 168 | | |
175 | | - | |
176 | 169 | | |
177 | 170 | | |
178 | 171 | | |
| |||
184 | 177 | | |
185 | 178 | | |
186 | 179 | | |
187 | | - | |
188 | 180 | | |
189 | 181 | | |
190 | 182 | | |
191 | 183 | | |
192 | 184 | | |
193 | | - | |
194 | 185 | | |
195 | 186 | | |
196 | 187 | | |
| |||
202 | 193 | | |
203 | 194 | | |
204 | 195 | | |
205 | | - | |
206 | 196 | | |
207 | 197 | | |
208 | 198 | | |
209 | 199 | | |
210 | 200 | | |
211 | | - | |
212 | 201 | | |
213 | 202 | | |
214 | 203 | | |
215 | 204 | | |
216 | | - | |
217 | 205 | | |
218 | 206 | | |
219 | 207 | | |
| |||
239 | 227 | | |
240 | 228 | | |
241 | 229 | | |
242 | | - | |
243 | 230 | | |
244 | 231 | | |
245 | 232 | | |
246 | 233 | | |
247 | 234 | | |
248 | 235 | | |
249 | 236 | | |
250 | | - | |
251 | 237 | | |
252 | 238 | | |
253 | 239 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
503 | 503 | | |
504 | 504 | | |
505 | 505 | | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
506 | 510 | | |
507 | 511 | | |
508 | 512 | | |
509 | 513 | | |
510 | | - | |
| 514 | + | |
511 | 515 | | |
512 | 516 | | |
513 | 517 | | |
514 | 518 | | |
515 | 519 | | |
516 | 520 | | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
517 | 559 | | |
518 | 560 | | |
519 | | - | |
520 | 561 | | |
521 | 562 | | |
522 | 563 | | |
| 564 | + | |
523 | 565 | | |
524 | | - | |
| 566 | + | |
525 | 567 | | |
526 | | - | |
527 | | - | |
528 | | - | |
529 | | - | |
530 | | - | |
531 | | - | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
532 | 578 | | |
533 | 579 | | |
534 | 580 | | |
535 | 581 | | |
536 | 582 | | |
537 | 583 | | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
538 | 591 | | |
539 | 592 | | |
540 | 593 | | |
| |||
Lines changed: 142 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
0 commit comments