Commit 4e59e90
Alex Wang
fix(otel): balance context attach and detach
Both plugins called opentelemetry.context.attach() without keeping
the returned token, and "restored" the enclosing span by attaching
another context rather than detaching. Every operation pushed two
context layers and popped none, leaving an ended span current after
its scope had finished.
The worst consequence: the invocation-start attach runs on the
Lambda handler thread, which is reused across warm invocations, so
the next execution's context extractor and GLOBAL-mode
ambient-parent lookup adopted the previous execution's ended
Workflow span. A valid parent overrides the deterministic ID
generator's trace ID, so two unrelated durable executions merged
into a single trace.
Each attach now keeps its token, keyed the same way as the span
registry, and the hook that pairs with it pops the scope:
- on_invocation_start attaches; on_invocation_end detaches.
- on_user_function_start attaches; on_user_function_end detaches,
replacing the re-attach that stood in for restoring the
enclosing context.
- Anything still held when the invocation ends is swept, since the
SDK re-raises SuspendExecution without calling
on_user_function_end.
A scope is only detached while it is still the current one. That
mirrors OpenTelemetry Java's ScopeImpl.close(), which ignores a
close that does not represent the current context, and it matters
more in Python: ContextVar.reset writes back its captured value
unconditionally, so an out-of-order or wrong-thread detach would
revive a stale context instead of failing safe. A skipped detach
keeps its entry so the owning thread can still undo it.
Tests no longer reset the OTel context to isolate themselves; an
autouse fixture asserts instead that every test leaves the context
exactly as it found it, and the tests that drove hooks without
completing the lifecycle now complete it. Adds coverage for warm
invocation reuse keeping two executions in separate traces, a
suspended operation's scope being swept, sequential steps not
accumulating layers, exact restore on success and failure, nested
child contexts, worker-thread confinement, and the identity guard
skipping both out-of-order and cross-thread detaches.1 parent c7b494e commit 4e59e90
8 files changed
Lines changed: 536 additions & 80 deletions
File tree
- packages/aws-durable-execution-sdk-python-otel
- src/aws_durable_execution_sdk_python_otel
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
235 | 235 | | |
236 | 236 | | |
237 | 237 | | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
238 | 244 | | |
239 | 245 | | |
240 | 246 | | |
| |||
Lines changed: 92 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
| |||
142 | 143 | | |
143 | 144 | | |
144 | 145 | | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
145 | 149 | | |
146 | 150 | | |
147 | 151 | | |
148 | 152 | | |
149 | 153 | | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
150 | 210 | | |
151 | 211 | | |
152 | 212 | | |
| |||
168 | 228 | | |
169 | 229 | | |
170 | 230 | | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
171 | 242 | | |
172 | 243 | | |
173 | 244 | | |
| |||
214 | 285 | | |
215 | 286 | | |
216 | 287 | | |
217 | | - | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
218 | 294 | | |
219 | | - | |
220 | | - | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
221 | 298 | | |
222 | 299 | | |
223 | 300 | | |
| |||
329 | 406 | | |
330 | 407 | | |
331 | 408 | | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
332 | 412 | | |
333 | 413 | | |
334 | 414 | | |
| |||
455 | 535 | | |
456 | 536 | | |
457 | 537 | | |
458 | | - | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
459 | 542 | | |
460 | 543 | | |
461 | 544 | | |
462 | 545 | | |
463 | 546 | | |
464 | 547 | | |
465 | 548 | | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
466 | 554 | | |
467 | 555 | | |
468 | 556 | | |
| |||
496 | 584 | | |
497 | 585 | | |
498 | 586 | | |
499 | | - | |
500 | | - | |
501 | | - | |
502 | | - | |
503 | | - | |
504 | | - | |
505 | | - | |
506 | | - | |
507 | | - | |
508 | | - | |
509 | | - | |
510 | 587 | | |
511 | 588 | | |
512 | 589 | | |
| |||
Lines changed: 83 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
167 | 168 | | |
168 | 169 | | |
169 | 170 | | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
170 | 174 | | |
171 | 175 | | |
172 | 176 | | |
| |||
176 | 180 | | |
177 | 181 | | |
178 | 182 | | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
179 | 236 | | |
180 | 237 | | |
181 | 238 | | |
| |||
196 | 253 | | |
197 | 254 | | |
198 | 255 | | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
199 | 267 | | |
200 | 268 | | |
201 | 269 | | |
| |||
453 | 521 | | |
454 | 522 | | |
455 | 523 | | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
456 | 528 | | |
457 | 529 | | |
458 | 530 | | |
| |||
562 | 634 | | |
563 | 635 | | |
564 | 636 | | |
565 | | - | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
566 | 641 | | |
567 | 642 | | |
568 | 643 | | |
| |||
578 | 653 | | |
579 | 654 | | |
580 | 655 | | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
581 | 663 | | |
582 | 664 | | |
583 | 665 | | |
| |||
610 | 692 | | |
611 | 693 | | |
612 | 694 | | |
613 | | - | |
614 | | - | |
615 | | - | |
616 | | - | |
617 | | - | |
618 | | - | |
619 | | - | |
620 | | - | |
621 | | - | |
622 | | - | |
623 | 695 | | |
624 | 696 | | |
625 | 697 | | |
| |||
0 commit comments