Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: undici and fetch() instrumentation #2858

Merged
merged 19 commits into from Aug 11, 2022
Merged

feat: undici and fetch() instrumentation #2858

merged 19 commits into from Aug 11, 2022

Conversation

trentm
Copy link
Member

@trentm trentm commented Aug 4, 2022

Closes: #2383

@trentm trentm self-assigned this Aug 4, 2022
@github-actions github-actions bot added the agent-nodejs Make available for APM Agents project planning. label Aug 4, 2022
@elastic-apm-tech elastic-apm-tech added this to In Progress in APM-Agents (OLD) Aug 4, 2022
@apmmachine
Copy link
Collaborator

apmmachine commented Aug 4, 2022

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview preview

Expand to view the summary

Build stats

  • Start Time: 2022-08-08T16:34:38.670+0000

  • Duration: 34 min 7 sec

Test stats 🧪

Test Results
Failed 0
Passed 258146
Skipped 0
Total 258146

🤖 GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

  • run module tests for <modules> : Run TAV tests for one or more modules, where <modules> can be either a comma separated list of modules (e.g. memcached,redis) or the string literal ALL to test all modules

  • run benchmark tests : Run the benchmark test only.

  • run elasticsearch-ci/docs : Re-trigger the docs validation. (use unformatted text in the comment!)

@trentm
Copy link
Member Author

trentm commented Aug 5, 2022

run module tests for undici,@elastic/elasticsearch

@trentm
Copy link
Member Author

trentm commented Aug 8, 2022

run module tests for undici,@elastic/elasticsearch

@trentm trentm marked this pull request as ready for review August 8, 2022 18:24
@trentm trentm requested a review from astorm August 8, 2022 18:24
@@ -191,7 +191,7 @@ tape.test('contextPropagationOnly', function (suite) {
// stacktrace collection when contextPropagationOnly=true. It isn't a
// perfect way to test that.
const durationMs = duration[0] / 1e3 + duration[1] / 1e6
const THRESHOLD_MS = 3 // Is this long enough for slow CI?
const THRESHOLD_MS = 5 // Is this long enough for slow CI?
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

REVIEW NOTE: This is an unrelated change. I hit a spurious test failure with this tight threshold, so I bumped the threshold to make it less likely to hit it in CI.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example this just failed on "main": https://github.com/elastic/apm-agent-nodejs/runs/7771135311?check_suite_focus=true

not ok 16 captureError is fast (<3ms): 3.015229ms

Copy link
Contributor

@astorm astorm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran this through some rudimentary tests -- spans are made, tracecontext is propagated/distributed tracing functions -- things seem to work and having this in place puts us ahead of most folks w/r/t undici and any bleeding edge folks bold enough to incorporate the new HTTP client into their projects (:wave: @ elastic client team)

Fine with this going in as is but I do have a question re: what looks like double instrumentation below, and I'm not sure if the lack of a CHANGELOG entry is deliberate or just an oversight.


if (nodeHasInstrumentableFetch && this._isModuleEnabled('undici')) {
this._log.debug('instrumenting fetch')
undiciInstr.instrumentUndici(this._agent)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I'm afraid to ask but) -- what's driving the need for this manual call to instrumentUndici. Naively -- it seems like adding undichi to MODULES will always load lib/instrumentation/modules/undici.js which also calls instrumentUndici. What's the weird case that leads us to need this second, earlier, call to instrumentUndici?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding undichi to MODULES will always load lib/instrumentation/modules/undici.js

It will load lib/instrumentation/modules/undici.js, yes, but the shimming (shimUndici) isn't called unless the require-in-the-middle hook sees a require('undici').

What's the weird case that leads ...

To instrument node's core fetch() implementation we need to enable the undici instrumentation even if there is no require('undici') anywhere. This is because node's core fetch() uses undici as an esbuild bundle: https://github.com/nodejs/node/blob/main/deps/undici/undici.js
That we can even instrument that is down to the hooks being via diagnostics_channel rather than needing to monkey-patch.

And to avoid double-instrumenting, there is a guard at the start of instrumentUndici:

function instrumentUndici (agent) {
  if (isInstrumented) {
    return
  }
  isInstrumented = true
...

I'm not sure if the lack of a CHANGELOG entry is deliberate or just an oversight.

Oh, that is totally an oversight. Thanks for pointing that out!

@trentm trentm merged commit 6939c03 into main Aug 11, 2022
APM-Agents (OLD) automation moved this from In Progress to Done Aug 11, 2022
@trentm trentm deleted the trentm/undici-instr branch August 11, 2022 00:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agent-nodejs Make available for APM Agents project planning.
Projects
Development

Successfully merging this pull request may close these issues.

add undici HTTP client instrumentation
3 participants