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