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

DD trace with Objection js support #657

Closed
liubinyi opened this issue Aug 22, 2019 · 10 comments
Closed

DD trace with Objection js support #657

liubinyi opened this issue Aug 22, 2019 · 10 comments
Labels
community question Further information is requested

Comments

@liubinyi
Copy link

liubinyi commented Aug 22, 2019

We have applications that uses objection.js
https://vincit.github.io/objection.js/
https://github.com/Vincit/objection.js/
When i use

tracker.use('pg') 
tracker.use('knex')

it doesn't display any span about pg_query or knex in datadog APM.

Is there any workaround for it?
if not could we add a objection js plugin in dd-trace-js?
or do we have anything that can monkey patch the 3rd party libraries like
https://github.com/DataDog/dd-trace-py/blob/master/ddtrace/monkey.py

also i can try to create a pr if i can get any help on where to start.

Thanks

@rochdev
Copy link
Member

rochdev commented Aug 22, 2019

This should work regardless of the ORM used since we patch the driver directly.

Do you not see Postgres spans specifically, or are you not seeing traces at all? If you see traces appear but not the Postgres spans, then do you see a service called <your_service_name>-postgres, and does it have any traces?

Also, do you call tracer.init() and tracer.use() before any other module has been imported? More information about this point can be found here.

@rochdev rochdev added community question Further information is requested and removed feature-request labels Aug 22, 2019
@liubinyi
Copy link
Author

liubinyi commented Aug 22, 2019

hi @rochdev
we only not see postgres spans specifically and we do have a service called <my_service_name>-postgres it has traces about the sql statement but for this one i think that is the result of we auto pulling from glcoud's could sql logs. and <my_service_name>-postgres trace is not correlated with the trace in <my_service_name>-postgres

Also, do you call tracer.init() and tracer.use() before any other module has been imported?

yes

we init our tracer as follows

const tracer = ddTrace.init({
  service: SERVICE_NAME,
  logInjection: true,
  runtimeMetrics: true,
});

tracer.use("pg", { service: SERVICE_NAME });
tracer.use("knex", { service: SERVICE_NAME });

export { tracer };

and export it in the first line of our server.ts file like below

import "./lib/tracer";
import { Model } from "objection";

@rochdev
Copy link
Member

rochdev commented Aug 22, 2019

Ok, it sounds like the scope is lost in the ORM which causes the trace to be disconnected. I'll have to look into it. If you can set up a small reproduction snippet that makes a simple call to the database and results in a disconnected trace, it would definitely help me expedite the fix since I don't know this ORM.

@liubinyi
Copy link
Author

sure, i will post it here once i have it ready. Thanks for the help!

@liubinyi
Copy link
Author

liubinyi commented Aug 22, 2019

a small modification of their example. tested on my local.
will it helpful?

// npm install objection knex pg, ddtrace postgresql

// import ddTrace from "dd-trace";
//
// const SERVICE_NAME="test";

// const tracer = ddTrace.init({
//   service: SERVICE_NAME,
//   logInjection: true,
//   runtimeMetrics: true,
// });
//
// tracer.use("pg", { service: SERVICE_NAME });
// tracer.use("knex", { service: SERVICE_NAME });

const { Model } = require('objection');
const Knex = require('knex');

// Initialize knex.
const knex = Knex({
  client: 'postgresql',
  useNullAsDefault: true,
  connection: {
    filename: 'example.db'
  }
});

// Give the knex instance to objection.
Model.knex(knex);

// Person model.
class Person extends Model {
  static get tableName() {
    return 'persons';
  }

  static get relationMappings() {
    return {
      children: {
        relation: Model.HasManyRelation,
        modelClass: Person,
        join: {
          from: 'persons.id',
          to: 'persons.parentId'
        }
      }
    };
  }
}

async function createSchema() {
  if (await knex.schema.hasTable('persons')) {
    return;
  }

  // Create database schema. You should use knex migration files
  // to do this. We create it here for simplicity.
  await knex.schema.createTable('persons', table => {
    table.increments('id').primary();
    table.integer('parentId').references('persons.id');
    table.string('firstName');
  });
}

async function main() {
  // Create some people.
  const sylvester = await Person.query().insertGraph({
    firstName: 'Sylvester',

    children: [
      {
        firstName: 'Sage'
      },
      {
        firstName: 'Sophia'
      }
    ]
  });

  console.log('created:', sylvester);

  // Fetch all people named Sylvester and sort them by id.
  // Load `children` relation eagerly.
  const sylvesters = await Person.query()
    .where('firstName', 'Sylvester')
    .eager('children')
    .orderBy('id');

  console.log('sylvesters:', sylvesters);
}

createSchema()
  .then(() => main())
  .then(() => knex.destroy())
  .catch(err => {
    console.error(err);
    return knex.destroy();
  });```

@rochdev
Copy link
Member

rochdev commented Aug 22, 2019

Sorry I think I misunderstood the original issue, since in this example you are using Knex directly. In order to use Knex with async/await we need to work around an issue in Node described in #654.

Can you try using dd-trace 0.15.0-beta.1 with the following configuration:

tracer.init({
  experimental: {
    thenables: true
  }
})

@liubinyi
Copy link
Author

sure, i will give a try and let you know how it goes.

@liubinyi
Copy link
Author

@rochdev I think dd-trace 0.15.0-beta worked for me. we can close this issue. btw by any chance do you know when will 0.15.0 be released?

@rochdev
Copy link
Member

rochdev commented Aug 26, 2019

I don't have an exact timeline, but it should be in the next few days.

@liubinyi
Copy link
Author

cool, thanks for your help!
closing out the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants