Skip to content

Commit 145b25d

Browse files
khanayan123tlhunter
authored andcommitted
fix(prisma): correct database connection attributes in spans (#6305)
* fix bug * make clear distinction between user and database values in prisma integration
1 parent 80b23f3 commit 145b25d

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

packages/datadog-plugin-prisma/src/engine.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ class PrismaEngine extends DatabasePlugin {
5353
options.resource = originalStatement
5454
options.type = type || engineSpan.attributes['db.system']
5555
options.meta['db.type'] = dbType || engineSpan.attributes['db.system']
56-
options.meta['db.instance'] = dbConfig?.database
57-
options.meta['db.name'] = dbConfig?.user
56+
options.meta['db.name'] = dbConfig?.database
57+
options.meta['db.user'] = dbConfig?.user
5858
options.meta['out.host'] = dbConfig?.host
5959
options.meta[CLIENT_PORT_KEY] = dbConfig?.port
6060
}

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,49 @@ describe('Plugin', () => {
152152
tracingPromise
153153
])
154154
})
155+
156+
it('should include database connection attributes in db_query spans', async () => {
157+
// Set up database config that should be parsed from connection URL
158+
const dbConfig = {
159+
user: 'foo',
160+
host: 'localhost',
161+
port: '5432',
162+
database: 'postgres'
163+
}
164+
tracingHelper.setDbString(dbConfig)
165+
166+
const tracingPromise = agent.assertSomeTraces(traces => {
167+
// Find the db_query span
168+
const dbQuerySpan = traces[0].find(span => span.meta['prisma.name'] === 'db_query')
169+
expect(dbQuerySpan).to.exist
170+
171+
// Verify database connection attributes are present
172+
expect(dbQuerySpan.meta).to.have.property('db.name', 'postgres')
173+
expect(dbQuerySpan.meta).to.have.property('db.user', 'foo')
174+
expect(dbQuerySpan.meta).to.have.property('out.host', 'localhost')
175+
expect(dbQuerySpan.meta).to.have.property('network.destination.port', '5432')
176+
expect(dbQuerySpan.meta).to.have.property('db.type', 'postgres')
177+
})
178+
179+
const engineSpans = [
180+
{
181+
id: '1',
182+
parentId: null,
183+
name: 'prisma:engine:db_query',
184+
startTime: [1745340876, 436861000],
185+
endTime: [1745340876, 438601541],
186+
kind: 'client',
187+
attributes: {
188+
'db.system': 'postgresql',
189+
'db.query.text': 'SELECT 1'
190+
}
191+
}
192+
]
193+
tracingHelper.dispatchEngineSpans(engineSpans)
194+
await Promise.all([
195+
tracingPromise
196+
])
197+
})
155198
})
156199

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

0 commit comments

Comments
 (0)