dd-trace-js icon indicating copy to clipboard operation
dd-trace-js copied to clipboard

Feature Request: Undici / Global Fetch integration

Open flovilmart opened this issue 3 years ago • 26 comments

Hi there!

We're starting to shift away from axios / request and wish to roll out undici in our new deployments. Unfortunately, there is no 'official' tracing package for it.

Is it something that is being worked on? Would a PR be accepted to add an undici plugin? Should I be aware of any 🐉 ?

Thanks!

flovilmart avatar Sep 15 '21 20:09 flovilmart

Is it something that is being worked on?

We've been looking into using Undici for sending traces, but we haven't looked into writing a plugin for it yet.

Would a PR be accepted to add an undici plugin?

Definitely accepted and welcomed!

Should I be aware of any 🐉 ?

Not that I know of, it should be pretty similar to other HTTP servers/clients that are already supported.

rochdev avatar Sep 15 '21 20:09 rochdev

Thanks @rochdev ; I'll draw some inspiration from the http tracing plugin.

flovilmart avatar Sep 15 '21 20:09 flovilmart

@flovilmart Note also that undici has also recently added support for diagnostics channel, so we'll want to use that in versions of undici where that's available.

https://github.com/nodejs/undici/blob/main/docs/api/DiagnosticsChannel.md

bengl avatar Sep 15 '21 21:09 bengl

That's a super neat feature those diagnostics channels! I assume we want both implementations and patch based on the node.js and undici versions?

flovilmart avatar Sep 29 '21 20:09 flovilmart

@flovilmart It should be based on the undici version, but the Node version shouldn't matter since in older versions the module will not exist so the plugin will simply never run. Since we're in the process of rewriting a lot of how plugins work and there are no examples currently of using diagnostics channel in a plugin, I would recommend writing the plugin like any other plugin (for example HTTP) and not worry about diagnostics channel for now.

rochdev avatar Sep 30 '21 15:09 rochdev

OK that works for me @rochdev , we're not even on node 16 yet! I planned to get on that next week.

flovilmart avatar Oct 02 '21 02:10 flovilmart

Got this https://github.com/nodejs/undici/pull/1053 patch done for undici for better compatibility.

Outside the request call, patching all other API's (pipeline, stream etc...) is proven to be cumbersome.

The implementation through the diag channels is very straightforward with some book-keeping. I'll visit the implementation through patching the Client and Request objects after.

flovilmart avatar Oct 05 '21 14:10 flovilmart

@rochdev , what do you think about this:https://github.com/nodejs/undici/pull/1053#issuecomment-934560506?

flovilmart avatar Oct 05 '21 16:10 flovilmart

@flovilmart Actually it's fine if the implementation in Undici is backed 100% by diagnostics channel. My thought was that we need to do the monkey-patching anyway to support older versions of Undici without the channel available. In that sense, the idea was to first implement the monkey-patching and then add support to use the channel once it lands in Undici. This doesn't mean adding any monkey-patching ability to Undici since that would be redundant when the channels are available.

So basically:

  • dd-trace would support older versions of Undici with monkey-patching.
  • dd-trace would support newer versions of Undici with diagnostics channel subscribers when the publishers are implemented in Undici.

In both cases, no change is needed in Undici other than adding diagnostics channel support.

rochdev avatar Oct 05 '21 20:10 rochdev

@rochdev @bengl I've open the PR above to gather initial feedback around the implementation. Notably, the next tests seem to fails because of the inclusion of the diagnostic_channels module. Any guidance on the workaround?

For now, I'm waiting for a new release of undici, which explains why the tests are currently failing.

flovilmart avatar Oct 05 '21 21:10 flovilmart

Notably, the next tests seem to fails because of the inclusion of the diagnostic_channels module. Any guidance on the workaround?

@flovilmart Not sure why these would interact. Can you try rebasing the PR? There was a change to how Next is tests a few weeks ago, maybe that change wasn't in your branch and it would fix the issue.

rochdev avatar Oct 19 '21 15:10 rochdev

Interesting! I've rebased, let's see how it goes!

flovilmart avatar Oct 19 '21 16:10 flovilmart

Any resolution on this since? I see in your PR that there has been some back and forth on whether to solve monkey-patching or diagnostic channels approach first.

Are we pinning this to Undici DC PR as a dependency?

JustinTRoss avatar Jan 27 '22 02:01 JustinTRoss

@JustinTRoss I haven't had a chance to actually move forward with a new implementation; the monkey patching look very complex given undici's architecture; there are multiple APIs that leverage dispatchers differently, I haven't found any "clean" monkey patching strategy for all APIs (request, client, pipeline etc...)

flovilmart avatar Jan 27 '22 15:01 flovilmart

Any news on this subject? Since the last versions of NodeJS use Undici to implement the built-in Fetch API, it could be great to have this support =)

Grmiade avatar Aug 29 '22 15:08 Grmiade

@Grmiade yep agreed - I haven't had the chance to improve on the current design and address the comments.

flovilmart avatar Aug 29 '22 16:08 flovilmart

We're actually in the process of finalizing the approach of instrumenting with async_hooks and diagnostics_channel now that we have rewritten 50+ plugins and put it to the test, so might be worth it waiting a few weeks so that this can be added in Undici directly. We're planning to implement the new API directly in Node core and ship a polyfill.

rochdev avatar Aug 30 '22 01:08 rochdev

@flovilmart Maybe it might make sense to skip the complex monkey-patching altogether and only support newer versions of Undici?

kibertoad avatar Oct 13 '22 21:10 kibertoad

Quick update related to the above: we're currently finishing up the new approach which we'll be trying internally in the next few weeks, and we plan to add instrumentation to Undici directly in Node core soon after. That work can be tracked in https://github.com/nodejs/node/pull/44943

rochdev avatar Oct 13 '22 22:10 rochdev

Amazing work @rochdev glad your team was able to pull it through! That was not a simple endeavour!

flovilmart avatar Oct 13 '22 22:10 flovilmart

hello there, are there any updates on Undici support?

mrkosima avatar Apr 24 '23 11:04 mrkosima

Looks like this https://github.com/nodejs/node/pull/44943 was merged so I guess this is closer now?

saimon-moore avatar Jun 01 '23 11:06 saimon-moore

I see that support for fetch has been added here (#3258). Does this mean this issue can be closed now?

SimpleCreations avatar Aug 17 '23 11:08 SimpleCreations

We tested out undici for our gateway application and datadog didn't report any traces for backend services when we used undici. I don't think this issue can be closed until dd-trace supports undici.

We are using dd-trace-js v 4.14.0

cassidycodes avatar Oct 27 '23 13:10 cassidycodes

I meant to say that most people want undici support because the Fetch API uses undici. But I guess there are cases when one might use undici without fetch.

SimpleCreations avatar Oct 27 '23 17:10 SimpleCreations

https://datadoghq.dev/dd-trace-js/interfaces/export_.plugins.undici.html

Whatever this is, it doesn't work.

It's very hard to find docs on the over-the-wire format of datadog APM trace metadata (datadog or otel tracecontext) - everything points to libs.

Well AFAICT there's no datadog lib that can instrument undici yet, so I'm trying to do it manully.

Thus far, not having much luck with either otel's undici instrumentation or datadog's tease of one in the docs linked above.

edit: udpate - turns out our code was not using global fetch but one of the other libs. My mistake!

dpnova avatar Jun 29 '24 07:06 dpnova