Skip to content

Commit 6f8dd96

Browse files
committed
test: add a few database tests to cover recent reports better
1 parent 875e585 commit 6f8dd96

File tree

7 files changed

+364
-14
lines changed

7 files changed

+364
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Temporary Items
3232
# Logs
3333
logs
3434
*.log
35+
node-*-junit.xml
3536
npm-debug.log*
3637
yarn-debug.log*
3738
yarn-error.log*

packages/datadog-plugin-mongodb-core/test/core.spec.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,58 @@ describe('Plugin', () => {
524524
})
525525
})
526526

527+
describe('with dbmPropagationMode full from tracer configuration', () => {
528+
before(() => {
529+
return agent.load('mongodb-core', {
530+
dbmPropagationMode: 'full',
531+
}, {
532+
sampler: { sampleRate: 1 },
533+
})
534+
})
535+
536+
after(() => {
537+
return agent.close({ ritmReset: false })
538+
})
539+
540+
beforeEach(done => {
541+
const Server = getServer()
542+
543+
server = new Server({
544+
host: '127.0.0.1',
545+
port: 27017,
546+
reconnect: false,
547+
})
548+
549+
server.on('connect', () => done())
550+
server.on('error', done)
551+
552+
server.connect()
553+
554+
startSpy = sinon.spy(MongodbCorePlugin.prototype, 'start')
555+
})
556+
557+
afterEach(() => {
558+
startSpy?.restore()
559+
})
560+
561+
it('DBM propagation should inject full mode comment with traceparent', done => {
562+
agent
563+
.assertFirstTraceSpan(span => {
564+
const traceId = span.meta['_dd.p.tid'] + span.trace_id.toString(16).padStart(16, '0')
565+
const spanId = span.span_id.toString(16).padStart(16, '0')
566+
567+
assert.strictEqual(startSpy.called, true)
568+
const { comment } = startSpy.getCall(0).args[0].ops
569+
assert.ok(comment.includes(`traceparent='00-${traceId}-${spanId}-01'`))
570+
assert.strictEqual(span.meta['_dd.dbm_trace_injected'], 'true')
571+
})
572+
.then(done)
573+
.catch(done)
574+
575+
server.insert(`test.${collection}`, [{ a: 1 }], () => {})
576+
})
577+
})
578+
527579
describe('with dbmPropagationMode service', () => {
528580
before(() => {
529581
return agent.load('mongodb-core', { dbmPropagationMode: 'service' })

packages/datadog-plugin-mysql/test/index.spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,42 @@ describe('Plugin', () => {
427427
})
428428
})
429429
})
430+
431+
describe('with DBM propagation enabled with service using tracer configurations', () => {
432+
let connection
433+
434+
before(async () => {
435+
await agent.load('mysql', { service: 'serviced', dbmPropagationMode: 'service' })
436+
mysql = proxyquire(`../../../versions/mysql@${version}`, {}).get()
437+
438+
connection = mysql.createConnection({
439+
host: '127.0.0.1',
440+
user: 'root',
441+
database: 'db',
442+
})
443+
connection.connect()
444+
})
445+
446+
after((done) => {
447+
connection.end(() => {
448+
agent.close({ ritmReset: false }).then(done)
449+
})
450+
})
451+
452+
it('should contain service mode comment in query text', done => {
453+
connection.query('SELECT 1 + 1 AS solution', () => {
454+
try {
455+
assert.strictEqual(connection._protocol._queue[0].sql,
456+
'/*dddb=\'db\',dddbs=\'serviced\',dde=\'tester\',ddh=\'127.0.0.1\',ddps=\'test\',' +
457+
`ddpv='${ddpv}'*/ SELECT 1 + 1 AS solution`)
458+
} catch (e) {
459+
done(e)
460+
}
461+
done()
462+
})
463+
})
464+
})
465+
430466
describe('DBM propagation should handle special characters', () => {
431467
let connection
432468

packages/datadog-plugin-pg/test/index.spec.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,48 @@ describe('Plugin', () => {
864864
await queryPromise
865865
})
866866
})
867+
868+
describe('with DBM propagation enabled with append comment using tracer configuration', () => {
869+
before(async () => {
870+
await agent.load('pg', {
871+
appendComment: true,
872+
service: () => 'serviced',
873+
dbmPropagationMode: 'service',
874+
})
875+
pg = require(`../../../versions/pg@${version}`).get()
876+
})
877+
878+
after(() => {
879+
return agent.close({ ritmReset: false })
880+
})
881+
882+
beforeEach((done) => {
883+
client = new pg.Client({
884+
host: '127.0.0.1',
885+
user: 'postgres',
886+
password: 'postgres',
887+
database: 'postgres',
888+
})
889+
client.connect(err => done(err))
890+
})
891+
892+
afterEach((done) => {
893+
client.end(done)
894+
})
895+
896+
it('should append service mode comment in query text', async () => {
897+
const queryQueueName = Object.hasOwn(client, '_queryQueue') ? '_queryQueue' : 'queryQueue'
898+
899+
const queryPromise = client.query('SELECT $1::text as message', ['Hello world!'])
900+
901+
assert.strictEqual(client[queryQueueName][0].text,
902+
'SELECT $1::text as message /*dddb=\'postgres\',dddbs=\'serviced\',dde=\'tester\',' +
903+
`ddh='127.0.0.1',ddps='test',ddpv='${ddpv}'*/`
904+
)
905+
906+
await queryPromise
907+
})
908+
})
867909
})
868910
})
869911
})

packages/datadog-plugin-prisma/test/index.spec.js

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
'use strict'
22

33
const assert = require('node:assert/strict')
4-
const { execSync } = require('node:child_process')
4+
const { execFileSync, execSync } = require('node:child_process')
55
const fs = require('node:fs/promises')
66
const path = require('node:path')
7+
78
const { after, before, beforeEach, describe, it } = require('mocha')
89
const proxyquire = require('proxyquire')
910
const semifies = require('semifies')
11+
1012
const { assertObjectContains } = require('../../../integration-tests/helpers')
1113
const { storage } = require('../../datadog-core')
1214
const { ERROR_MESSAGE, ERROR_TYPE, ERROR_STACK } = require('../../dd-trace/src/constants')
@@ -82,7 +84,7 @@ async function copySchemaToVersionDir (schemaPath, range) {
8284
}
8385

8486
function createPrismaClient (prisma, config) {
85-
// With the introduction of v7 prisma now enforces the use of adpaters
87+
// With the introduction of v7 prisma now enforces the use of adapters
8688
if (config.v7) {
8789
const { PrismaPg } = require('@prisma/adapter-pg')
8890
const adapter = new PrismaPg({ connectionString: process.env[TEST_DATABASE_ENV_NAME] })
@@ -94,6 +96,21 @@ function createPrismaClient (prisma, config) {
9496
return new prisma.PrismaClient()
9597
}
9698

99+
function createEngineDbQuerySpan (queryText) {
100+
return [{
101+
id: '1',
102+
parentId: null,
103+
name: 'prisma:engine:db_query',
104+
startTime: [1745340876, 436861000],
105+
endTime: [1745340876, 438601541],
106+
kind: 'client',
107+
attributes: {
108+
'db.system': 'postgresql',
109+
'db.query.text': queryText,
110+
},
111+
}]
112+
}
113+
97114
describe('Plugin', () => {
98115
let prisma
99116
let prismaClient
@@ -488,24 +505,63 @@ describe('Plugin', () => {
488505
})
489506

490507
const engineSpans = [
491-
{
492-
id: '1',
493-
parentId: null,
494-
name: 'prisma:engine:db_query',
495-
startTime: [1745340876, 436861000],
496-
endTime: [1745340876, 438601541],
497-
kind: 'client',
498-
attributes: {
499-
'db.system': 'postgresql',
500-
'db.query.text': 'SELECT 1',
501-
},
502-
},
508+
...createEngineDbQuerySpan('SELECT 1'),
503509
]
504510
tracingHelper.dispatchEngineSpans(engineSpans)
505511
await Promise.all([
506512
tracingPromise,
507513
])
508514
})
515+
516+
if (config.v7) {
517+
it('should tag db_query spans with the active client adapter metadata in read-replica setups', async () => {
518+
const initialDbUrl = process.env[TEST_DATABASE_ENV_NAME]
519+
process.env[TEST_DATABASE_ENV_NAME] =
520+
'postgres://postgres:postgres@primary.db.internal:5432/postgres'
521+
const primaryClient = createPrismaClient(prisma, config)
522+
523+
process.env[TEST_DATABASE_ENV_NAME] =
524+
'postgres://postgres:postgres@replica.db.internal:5433/postgres'
525+
const replicaClient = createPrismaClient(prisma, config)
526+
527+
if (initialDbUrl === undefined) {
528+
delete process.env[TEST_DATABASE_ENV_NAME]
529+
} else {
530+
process.env[TEST_DATABASE_ENV_NAME] = initialDbUrl
531+
}
532+
533+
assert.ok(primaryClient._tracingHelper)
534+
assert.ok(replicaClient._tracingHelper)
535+
536+
const replicaReadTrace = agent.assertSomeTraces(traces => {
537+
const dbQuerySpan = traces[0].find(span => span.meta['prisma.name'] === 'db_query')
538+
assertObjectContains(dbQuerySpan, {
539+
resource: 'SELECT 1',
540+
meta: {
541+
'out.host': 'replica.db.internal',
542+
'network.destination.port': '5433',
543+
},
544+
})
545+
})
546+
replicaClient._tracingHelper.dispatchEngineSpans(createEngineDbQuerySpan('SELECT 1'))
547+
await replicaReadTrace
548+
549+
const primaryWriteTrace = agent.assertSomeTraces(traces => {
550+
const dbQuerySpan = traces[0].find(span => span.meta['prisma.name'] === 'db_query')
551+
assertObjectContains(dbQuerySpan, {
552+
resource: 'INSERT INTO "User" ("name") VALUES ($1)',
553+
meta: {
554+
'out.host': 'primary.db.internal',
555+
'network.destination.port': '5432',
556+
},
557+
})
558+
})
559+
primaryClient._tracingHelper.dispatchEngineSpans(createEngineDbQuerySpan(
560+
'INSERT INTO "User" ("name") VALUES ($1)'
561+
))
562+
await primaryWriteTrace
563+
})
564+
}
509565
})
510566

511567
describe('with configuration', () => {

packages/datadog-plugin-prisma/test/integration-test/client.spec.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,4 +341,90 @@ describe('esm', () => {
341341
})
342342
})
343343
})
344+
345+
describe('prisma-generator v7 pg adapter with OTel TracerProvider registration', () => {
346+
const isNodeSupported = semifies(semver.clean(process.version), '>=20.19.0')
347+
if (!isNodeSupported) {
348+
return
349+
}
350+
351+
withVersions('prisma', '@prisma/client', '7.0.0', version => {
352+
const config = {
353+
serverFile: 'server-ts-v7-otel.mjs',
354+
schema: `./packages/datadog-plugin-prisma/test/${SCHEMA_FIXTURES.tsEsmV7}`,
355+
configFile: `./packages/datadog-plugin-prisma/test/${SCHEMA_FIXTURES.tsEsmV7Config}`,
356+
env: {
357+
PRISMA_CLIENT_OUTPUT: './generated/prisma',
358+
DATABASE_URL: TEST_DATABASE_URL,
359+
},
360+
}
361+
const paths = [
362+
'./packages/datadog-plugin-prisma/test/integration-test/*',
363+
config.schema,
364+
config.configFile,
365+
]
366+
const deps = [`@prisma/client@${version}`]
367+
for (const ext of externals['@prisma/client']) {
368+
if (ext.node && !semifies(semver.clean(process.version), ext.node)) continue
369+
if (ext.dep === '@prisma/client') {
370+
deps.push(`${ext.name}@${version}`)
371+
} else if (ext.dep || ext.node) {
372+
deps.push(ext.name)
373+
}
374+
}
375+
376+
useSandbox(deps, false, paths)
377+
378+
beforeEach(async function () {
379+
this.timeout(60000)
380+
agent = await new FakeAgent().start()
381+
const cwd = sandboxCwd()
382+
const commands = [
383+
'./node_modules/.bin/prisma db push --accept-data-loss',
384+
'./node_modules/.bin/prisma generate',
385+
'./node_modules/.bin/tsc ./generated/**/*.ts' +
386+
' --outDir ./dist' +
387+
' --target ES2023' +
388+
' --module ESNext' +
389+
' --strict true' +
390+
' --moduleResolution node' +
391+
' --esModuleInterop true',
392+
]
393+
394+
execSync(commands.join(' && '), {
395+
cwd,
396+
stdio: 'inherit',
397+
env: {
398+
...process.env,
399+
...config.env,
400+
},
401+
})
402+
})
403+
404+
afterEach(async () => {
405+
proc?.kill()
406+
await agent?.stop()
407+
})
408+
409+
it('uses active OTel span IDs in Prisma DBM traceparent comments', async function () {
410+
this.timeout(60000)
411+
let stdout = ''
412+
await spawnPluginIntegrationTestProcAndExpectExit(
413+
sandboxCwd(),
414+
config.serverFile,
415+
agent.port,
416+
{ DD_TRACE_FLUSH_INTERVAL: '2000', ...config.env },
417+
undefined,
418+
data => {
419+
stdout += data.toString()
420+
}
421+
)
422+
423+
const marker = stdout.match(/TRACEPARENT_OK:(00-[a-f0-9]{32}-[a-f0-9]{16}-01)/)
424+
assert.ok(marker, `Expected TRACEPARENT_OK output marker, got: ${stdout}`)
425+
assert.notStrictEqual(marker[1], '00-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-bbbbbbbbbbbbbbbb-01')
426+
assert.notStrictEqual(marker[1], '00-00000000000000000000000000000000-0000000000000000-01')
427+
})
428+
})
429+
})
344430
})

0 commit comments

Comments
 (0)