Commit 4141e53
feat(tracing): add ContextScope for span context lifetime control (#138)
## Description
Add `ContextScope` parameter to `startSpan()` that controls how long a
span remains active in zone context for automatic parent assignment.
- **`ContextScope.callback` (default)**: Span is deactivated when
callback completes. Timer/stream callbacks firing after the callback
ends will start fresh, independent traces.
- **`ContextScope.zone`**: Span stays active for the entire zone
lifetime, allowing timer callbacks to inherit the parent span.
This provides explicit control over timer callback behavior, fixing the
issue where spans that had ended remained active in zone context causing
unexpected parent-child relationships.
## Related Issue(s)
Fixes #105
## 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)
- [ ] 📝 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
## Additional Notes
### Implementation Details
- `SpanContextHolder` wrapper class tracks span + context scope + active
state
- `ContextScope` enum defined in `span.dart` with `callback` and `zone`
values
- `FaroZoneSpanManager.executeWithSpan()` conditionally deactivates the
holder based on scope
- Public API updated in `Faro.startSpan()` and `FaroTracer.startSpan()`
### Example App
Added "Context Scope" demo button in the tracing feature to test and
demonstrate both `ContextScope` values with timer callbacks.
### Documentation
Consolidated timer/async callback documentation in `doc/Features.md`
under "Timer & Async Callback Behavior" section.
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> Changes zone-based span context propagation semantics and adds a new
public parameter to `startSpan()`, which could alter parent/child
relationships for async callbacks (timers/streams) in existing
integrations.
>
> **Overview**
> Adds a new `contextScope` parameter to
`Faro().startSpan()`/`FaroTracer.startSpan()` to control how long a span
remains the active parent in zone context (`callback` default vs `zone`
for long-lived async work).
>
> Refactors zone propagation to store a `SpanContextHolder` in zone
values and conditionally deactivate it after the callback, so
timer/stream callbacks firing later don’t accidentally inherit an ended
span unless explicitly requested.
>
> Updates docs/CHANGELOG and the example tracing UI to demonstrate the
new behavior, and expands unit/integration tests around context scoping
and real timer behavior.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
4453420. 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 ea5e8b0 commit 4141e53
File tree
11 files changed
+860
-138
lines changed- doc
- example/lib/features/tracing
- domain
- presentation
- lib/src
- tracing
- test/src/tracing
11 files changed
+860
-138
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
| |||
34 | 36 | | |
35 | 37 | | |
36 | 38 | | |
| 39 | + | |
37 | 40 | | |
38 | 41 | | |
39 | 42 | | |
40 | 43 | | |
41 | 44 | | |
42 | 45 | | |
| 46 | + | |
43 | 47 | | |
44 | 48 | | |
45 | 49 | | |
46 | 50 | | |
47 | 51 | | |
48 | 52 | | |
| 53 | + | |
49 | 54 | | |
50 | 55 | | |
51 | 56 | | |
| |||
117 | 122 | | |
118 | 123 | | |
119 | 124 | | |
| 125 | + | |
120 | 126 | | |
121 | 127 | | |
122 | 128 | | |
| |||
133 | 139 | | |
134 | 140 | | |
135 | 141 | | |
| 142 | + | |
136 | 143 | | |
| 144 | + | |
137 | 145 | | |
138 | 146 | | |
139 | 147 | | |
| |||
149 | 157 | | |
150 | 158 | | |
151 | 159 | | |
| 160 | + | |
152 | 161 | | |
153 | 162 | | |
154 | 163 | | |
155 | 164 | | |
156 | 165 | | |
157 | 166 | | |
158 | 167 | | |
| 168 | + | |
159 | 169 | | |
160 | 170 | | |
161 | 171 | | |
| |||
167 | 177 | | |
168 | 178 | | |
169 | 179 | | |
| 180 | + | |
170 | 181 | | |
171 | 182 | | |
172 | 183 | | |
173 | 184 | | |
174 | 185 | | |
| 186 | + | |
175 | 187 | | |
176 | 188 | | |
177 | 189 | | |
| |||
183 | 195 | | |
184 | 196 | | |
185 | 197 | | |
| 198 | + | |
186 | 199 | | |
187 | 200 | | |
188 | 201 | | |
189 | 202 | | |
190 | 203 | | |
| 204 | + | |
191 | 205 | | |
192 | 206 | | |
193 | 207 | | |
194 | 208 | | |
| 209 | + | |
195 | 210 | | |
196 | 211 | | |
197 | 212 | | |
| |||
217 | 232 | | |
218 | 233 | | |
219 | 234 | | |
| 235 | + | |
220 | 236 | | |
221 | 237 | | |
222 | 238 | | |
223 | 239 | | |
224 | 240 | | |
225 | 241 | | |
226 | 242 | | |
| 243 | + | |
227 | 244 | | |
228 | 245 | | |
229 | 246 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
286 | 286 | | |
287 | 287 | | |
288 | 288 | | |
289 | | - | |
| 289 | + | |
290 | 290 | | |
291 | | - | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
292 | 296 | | |
293 | 297 | | |
294 | | - | |
295 | 298 | | |
296 | | - | |
| 299 | + | |
297 | 300 | | |
298 | | - | |
299 | | - | |
300 | | - | |
| 301 | + | |
| 302 | + | |
301 | 303 | | |
302 | 304 | | |
303 | 305 | | |
304 | 306 | | |
305 | | - | |
306 | | - | |
| 307 | + | |
| 308 | + | |
307 | 309 | | |
308 | 310 | | |
309 | | - | |
310 | | - | |
311 | | - | |
312 | | - | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
313 | 352 | | |
314 | 353 | | |
315 | 354 | | |
| |||
Lines changed: 101 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
272 | 273 | | |
273 | 274 | | |
274 | 275 | | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
275 | 376 | | |
276 | 377 | | |
277 | 378 | | |
| |||
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
120 | 126 | | |
121 | 127 | | |
122 | 128 | | |
| |||
Lines changed: 8 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
73 | 76 | | |
74 | 77 | | |
75 | 78 | | |
| |||
183 | 186 | | |
184 | 187 | | |
185 | 188 | | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
186 | 194 | | |
187 | 195 | | |
188 | 196 | | |
| |||
0 commit comments