dd-trace-js
dd-trace-js copied to clipboard
How best to handle iterables / generators?
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.
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?