Skip to content

DONOTMERGE feat(agent): Fiber Support#1198

Draft
zsistla wants to merge 15 commits into
devfrom
feat/fibers
Draft

DONOTMERGE feat(agent): Fiber Support#1198
zsistla wants to merge 15 commits into
devfrom
feat/fibers

Conversation

@zsistla

@zsistla zsistla commented Apr 10, 2026

Copy link
Copy Markdown
Contributor

No description provided.

* show_fiber functionality; indentations align with show_execs; the
special keyword is "show_fibers"
* basic user function fiber usage - does not include anything with
special instrumentation. Instrumentation of internal functions have not
been tested; although the show_fibers properly detects them.
* a few basic tests
* some get_current_segment, start_segment have been updated
* added values to track global asynchronous context (the actual context
we are in) and txn context (the context the txn is currently working
with which can be modified with set_current_segment)
* modified nr_txn_get_current_span_id for context to be passed
* added nr_txn_get_current_context
* added a few axiom unit tests

This is not the complete implementation, but will provide a base for
other PRs to iterate on.
@zsistla zsistla added this to the fiber milestone Apr 10, 2026
@zsistla zsistla added the on hold This issue or pull request is necessary, but better suited for the future label Apr 10, 2026
@newrelic-php-agent-bot

newrelic-php-agent-bot commented Apr 10, 2026

Copy link
Copy Markdown
Test Suite Status Result
Multiverse 18/18 passing
SOAK 140/146 passing

@codecov-commenter

codecov-commenter commented Apr 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.63158% with 47 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.46%. Comparing base (b235180) to head (41ea1dd).

Files with missing lines Patch % Lines
agent/php_observer.c 83.33% 11 Missing ⚠️
agent/php_internal_instrument.c 70.00% 9 Missing ⚠️
agent/php_execute.c 85.71% 7 Missing ⚠️
agent/lib_aws_sdk_php.c 0.00% 4 Missing ⚠️
agent/php_fibers.c 96.26% 4 Missing ⚠️
agent/lib_laminas_http.c 0.00% 2 Missing ⚠️
agent/lib_php_amqplib.c 0.00% 2 Missing ⚠️
axiom/nr_segment.c 81.81% 2 Missing ⚠️
axiom/nr_txn.c 90.47% 2 Missing ⚠️
agent/lib_mongodb.c 0.00% 1 Missing ⚠️
... and 3 more
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1198      +/-   ##
==========================================
+ Coverage   80.25%   80.46%   +0.20%     
==========================================
  Files         189      191       +2     
  Lines       28002    28308     +306     
==========================================
+ Hits        22474    22779     +305     
- Misses       5528     5529       +1     
Flag Coverage Δ
agent-for-php-7.2 80.26% <85.71%> (+0.10%) ⬆️
agent-for-php-7.3 80.28% <85.71%> (+0.10%) ⬆️
agent-for-php-7.4 80.15% <85.71%> (+0.10%) ⬆️
agent-for-php-8.0 79.72% <79.48%> (+0.06%) ⬆️
agent-for-php-8.1 80.04% <87.53%> (+0.16%) ⬆️
agent-for-php-8.2 79.65% <86.20%> (+0.15%) ⬆️
agent-for-php-8.3 79.72% <86.20%> (+0.14%) ⬆️
agent-for-php-8.4 79.74% <86.20%> (+0.14%) ⬆️
agent-for-php-8.5 79.05% <83.81%> (+0.14%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

zsistla added 8 commits April 28, 2026 10:55
… PHPs < 8.0 (#1211)

Most of references to force_current_segment or nr_txn_force_current
segment were placed in if/endif blocks with the OAPI effort. The change
was never explicitly mentioned and there's a chance it could be used in
a breaking manner. All remaining references in axiom specify explicitly
it is not to be used with oapi. Axiom doesn't understand ZEND_API
numbers so it can't be placed in if/endif blocks.
1) Updates `nr_txn_get_current_segment` usages to also incorporate new
get_current_segment calls.
2) New functions added are:
 `nr_txn_get_current_segment_txn_context` (in axiom)
`nr_php_txn_get_current_segment_php_context` (in agent since axiom is
php agnostic)
3) Added additional integration tests
@zsistla zsistla added the fibers label Jun 8, 2026
zsistla and others added 6 commits June 10, 2026 12:51
Analyze all segment starts to work with context information.
Will behave as usual for non-fibers.
Will use context information for starting fibers when in a fiber
environment.

1) Updated segment start logic across the agent to be context aware
2) Added fiber integration tests to exercise the code paths
3) Incorporated bug fixes that surfaced during increased test coverage
runs.


Of the tests:
* The _basic tests all follow the same pattern.
* For the pdo/*/base-class tests, once you've seen one of the variants,
the others are basically the same but for different databases.
* the span_events/fibers/*.php tests going through various scenarios
(starting/stopping/resume/noresume/errors/etc/nesting) etc.
* guzzle7 and curl_multi_exec are actually async so there's quite a few
more tests in that area.
## Summary

Each PHP Fiber now gets its own snapshot of the request-scoped globals
the
agent relies on (`ctx`), so instrumentation running inside
a fiber no longer trips over state owned by the main context (or another
fiber). The fiber observer hooks already in place from the parent branch
are wired up to populate, swap, and tear down these snapshots; the
`NRPRG_CTX` accessor transparently reads from the active
fiber's snapshot when one is set.

All new behavior is gated on PHP 8.1+. 

## What changed

**Per-fiber globals (`agent/php_fibers.{c,h}`, new)**
- New `fiber_globals_t` = `{ ctx_globals_t* }`.
- `nr_fiber_copy_ctx_globals` deep-copy the parts a fiber should own. 
- A per-request `fiber_globals_map` (keyed by `"%p"` of the
`zend_fiber_context*`) holds these snapshots; it has a destructor so
removed
entries are fully cleaned up. Init is lazy on first fiber; teardown
happens
in `nr_php_post_deactivate`. `php_rshutdown.c` clears
`NRPRG(fiber_globals)`
before normal global teardown so the existing free paths don't reach
into a
  snapshot.

**OAPI fiber observer wiring (`agent/php_observer.c`)**
- `nr_fiber_init_observe`: lazy-init `fiber_globals_map`, add a snapshot
for
  the new fiber under its `"%p"` key.
- `nr_fiber_destroy_observe`: remove the snapshot for that fiber.
- `nr_fiber_switch_observe`: at the end of the existing switch logic,
swap
  `NRPRG(fiber_globals)` to the destination context's snapshot via
  `nr_fiber_switch_global_context`.

**Globals accessors (`agent/php_newrelic.h`)**
- New `fiber_globals_t` struct; `NRPRG(fiber_globals_map)` and
  `NRPRG(fiber_globals)` added to module globals.
- On PHP 8.1+, `NRPRG_CTX(Y)` resolves through
`NRPRG(fiber_globals)` when it's non-NULL, otherwise read the
main-context
globals exactly as before. Callers don't have to know which context
they're
  in. PHP < 8.1 macros are unchanged.

---------

Co-authored-by: Amber Sistla <asistla@newrelic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fibers on hold This issue or pull request is necessary, but better suited for the future

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants