googleapis / cloud-trace-nodejs

Node.js agent for Cloud Trace: automatically gather latency data about your application

Home Page:https://cloud.google.com/trace/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mysql2 requests aren't being attached to root scope

watchinharrison opened this issue · comments

The log is outputting INFO TraceApi#createChildSpan: [mysql2@1.6.4:lib/connection.js] Creating phantom child span [mysql-query] because there is no root span..

My setup is using knex with the client mysql2. The query is running in the knex raw method.

This is an example app: https://github.com/davidharrisonlb/mysql-tracer-test

Thanks for reporting this -- I'm looking into it now.

No problem. I spent the evening trying to work it out too. Think it could be knex’s use of bluebird for promises. Will update with anything further I find today.

I think I've found the issue -- it seems that the Trace Agent (or anything reliant on async_hooks, for that matter) currently doesn't work for code that awaits a custom thenable, like a Bluebird Promise (see this minimum repro). (Edit: The repro is inaccurate.)

This is a problem that is only addressable in Node core, I'll scan the issue list shortly and add it if it's not there. In the meantime, it seems like this functions as a workaround:

  // explicitly call the first `.then` rather than let it be implicitly be called via the `await` keyword
  const result = await database.raw('SELECT 1').then(_ => _);

It seems like it is indeed a variant of an existing issue: nodejs/node#22360.

On that note, however, it seems like a more immediate solution is to move forward with the bluebird plugin PR that I had open a while ago. I'll try to amend it to work for this use case as well and see if I can land it this week.

I'm glad to see this issue - I've been struggling with connecting distributed traces from a js grpc client, but I too am promisifying those methods with bluebird, so hopefully that PR will help.

Sorry that this has been stalled. I believe this is a fix that must be done in Node itself and not in userspace, so unfortunately I can't amend the bluebird PR to address this issue. You can follow the Node bug here: nodejs/node#26064

It looks like Bluebird has been removed in the newest release of knex. Does anyone know if this issue is fixed now?

@giuliano-barberi-tf I have tested this today, and it isn't fixed.

Experiencing a similar issue with Sequelize v5