Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions packages/datadog-instrumentations/test/knex.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const { expect } = require('chai')
const { describe, it, beforeEach, afterEach } = require('mocha')

const agent = require('../../dd-trace/test/plugins/agent')
const { storage } = require('../../datadog-core')
const { withVersions } = require('../../dd-trace/test/setup/mocha')

Expand All @@ -16,27 +17,36 @@ describe('Instrumentation', () => {
describe('knex', () => {
withVersions('knex', 'knex', version => {
describe('without configuration', () => {
before(async () => {
await agent.load()
})

beforeEach(() => {
knex = require(`../../../versions/knex@${version}`).get()
client = knex({
client: 'pg',
client: 'sqlite3',
connection: {
filename: ':memory:'
}
})
})

afterEach(() => client.destroy())
after(async () => {
await agent.close()
})

it('should propagate context', () =>
storage('legacy').run(store, () =>
client.raw('PRAGMA user_version')
.finally(() => {
expect(storage('legacy').getStore()).to.equal(store)
})
.catch(() => {})
it('should propagate context', async () => {
let isInstrumented = false

await storage('legacy').run(store, () =>
client.raw('PRAGMA user_version').then(() => {
isInstrumented = storage('legacy').getStore() === store
}).catch(() => {})
)
)

expect(isInstrumented).to.equal(true)
})
})
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ withVersions('knex', 'knex', version => {
describe('ESM', () => {
let variants, proc, agent

useSandbox([`'knex@${version}'`, 'express'], false,
useSandbox([`'knex@${version}'`, 'express', 'sqlite3'], false,
['./packages/datadog-plugin-knex/test/integration-test/*'])

before(function () {
Expand Down
29 changes: 19 additions & 10 deletions packages/datadog-plugin-knex/test/integration-test/server.mjs
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import 'dd-trace/init.js'
import express from 'express'
import knex from 'knex'
import dc from 'dc-polyfill'
import { AsyncLocalStorage } from 'async_hooks'

const startRawQueryCh = dc.channel('datadog:knex:raw:start')
let counter = 0
startRawQueryCh.subscribe(() => {
counter += 1
})

const asyncStore = new AsyncLocalStorage()
const app = express()

const db = knex({ client: 'pg' })
const db = knex({
client: 'sqlite3',
connection: { filename: ':memory:' }
})

app.get('/', (req, res) => {
db.raw('select 1')
res.setHeader('X-Counter', counter)
res.end('ok')
const store = 'knex-test-store'

asyncStore.run(store, () => {
db.raw('PRAGMA user_version')
.then(() => {
if (asyncStore.getStore() === store) {
counter += 1
}
res.setHeader('X-Counter', counter)
res.end('ok')
})
.catch(() => {})
})
})

const server = app.listen(0, () => {
Expand Down
Loading