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

How best to handle iterables / generators?

Open BridgeAR opened this issue 7 months ago • 0 comments

Discussed in https://github.com/DataDog/dd-trace-js/discussions/3820

Originally posted by vstreame November 23, 2023 Hi there,

We tend to use as a lot of async iterables in our codebase. So we often have functions that look like

async *paginateFollowers(channelID: string): AsyncIterator<Follower[]> {
  for await (const dbFollowers of await this.db.queryFollowers(channelID)) {
    yield dbFollowers.map(x => convertFromDBToDomainObject(x));
  }
}

But what we've noticed is that this makes it so when we wrap these functions in trace, they the span ends immediately as the async iterator is returned immediately before any work happens.

Screenshot 2023-11-23 at 3 27 33 PM

To be fair, I'm not sure how tracing would work with any function that yields to it's caller, but I'm wondering if there is any best practices so that the DB calls that happen within this generator are associated with this span rather than the span only lasting nano seconds. Or is that just not a good idea in the first place?

BridgeAR avatar May 05 '25 13:05 BridgeAR