Created
March 24, 2020 16:42
-
-
Save ericmustin/49d4cd9ac3e29981bdbc57ac4bc22ccd to your computer and use it in GitHub Desktop.
example script tracing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const configObj = require('../config/config'); | |
// "dd-trace": "^0.19.0" | |
const tracer = require('dd-trace').init({ | |
logInjection: true, | |
analytics: true, | |
env: process.env.NODE_ENV, | |
service: 'Imports', | |
debug: true | |
}); | |
const knex = require('knex'); | |
const { Model } = require('objection'); | |
const logger = require('../api/helpers/logger'); | |
// NOTE: Please include a require for http as it needs to get patched | |
const http = require('http') | |
// "knex": "^0.19.0" | |
// "pg": "^7.11.0" | |
const objection = knex({ | |
client: 'pg', | |
connection: configObj.DATABASE_CONN_DETAILS // http://knexjs.org/#Installation-client | |
}); | |
// "objection": "1.4.0" | |
class ImportModel extends Model { | |
static get tableName() { | |
return 'imports'; | |
} | |
} | |
ImportModel.knex(objection); | |
const scope = tracer.scope(); | |
console.log('is the scope active?:1', !!scope.active()); | |
const dbPromise = new Promise(function(resolve, reject) { | |
// NOTE: using tracer.trace to abstract away startSpan / activate / finish | |
tracer.trace('pg.request', () => { | |
console.log('is the scope active?:2', !!scope.active()); | |
ImportModel.query() | |
.first() | |
.where({ id: 'xxxxxxxx' }) | |
.throwIfNotFound() | |
.then((importRecord) => { | |
console.log('is the scope active?:3', !!scope.active()); | |
logger.info('Got the stuff', importRecord); | |
}) | |
.catch((error) => { | |
logger.error('Did not get the stuff', error); | |
}) | |
.finally(() => { | |
resolve('completed db query') | |
}); | |
}); | |
// NOTE: This could be done within the `finally` chained calback for knex but this represents | |
// How this could be done via promises | |
// using process.exit() instead of destroying the connection causes the outer tracer to get enqueued but not flushed | |
// before the process exits. | |
dbPromise.finally( () => { | |
knex.destroy() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment