Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DBM: Add option to SQL comment propagation #5125

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions docs/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ tracer.use('paperplane', httpServerOptions);
tracer.use('playwright');
tracer.use('pg');
tracer.use('pg', { service: params => `${params.host}-${params.database}` });
tracer.use('pg', { appendComment: true });
tracer.use('pino');
tracer.use('protobufjs');
tracer.use('redis');
Expand Down
4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,10 @@ declare namespace tracer {
* The database monitoring propagation mode to be used for this plugin.
*/
dbmPropagationMode?: string;
/**
* Appends the SQL comment propagation to the query string. Prepends the comment if `false`. For long query strings, the appended propagation comment might be truncated, causing loss of correlation between the query and trace.
*/
appendComment?: boolean;
}

/**
Expand Down
50 changes: 50 additions & 0 deletions packages/datadog-plugin-pg/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,56 @@ describe('Plugin', () => {
queryText = client.queryQueue[0].text
})
})

describe.skip('with DBM propagation enabled with append comment configurations', () => {
before(() => {
return agent.load('pg', [{ dbmPropagationMode: 'service', service: () => 'serviced', appendComment: true }])
})

after(() => {
return agent.close({ ritmReset: false })
})

beforeEach(done => {
pg = require(`../../../versions/pg@${version}`).get()

client = new pg.Client({
host: '127.0.0.1',
user: 'postgres',
password: 'postgres',
database: 'postgres'
})
client.connect(err => done(err))
})

it('should append comment in query text', done => {
const client = new pg.Client({
host: '127.0.0.1',
user: 'postgres',
password: 'postgres',
database: 'postgres'
})

client.connect(err => done(err))

client.query('SELECT $1::text as message', ['Hello world!'], (err, result) => {
if (err) return done(err)

client.end((err) => {
if (err) return done(err)
})
})
if (client.queryQueue[0] !== undefined) {
try {
expect(client.queryQueue[0].text).to.equal(
'SELECT $1::text as message /*dddb=\'postgres\',dddbs=\'serviced\',dde=\'tester\',' +
`ddh='127.0.0.1',ddps='test',ddpv='${ddpv}'*/ `)
} catch (e) {
done(e)
}
}
})
})
})
})
})
10 changes: 8 additions & 2 deletions packages/dd-trace/src/plugins/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ class DatabasePlugin extends StoragePlugin {
return tracerService
}

addComment (query, comment) {
return this.config.appendComment
? `${query} /*${comment}*/`
: `/*${comment}*/ ${query}`
}

injectDbmQuery (span, query, serviceName, isPreparedStatement = false) {
const mode = this.config.dbmPropagationMode
const dbmService = this.getDbmServiceName(span, serviceName)
Expand All @@ -74,11 +80,11 @@ class DatabasePlugin extends StoragePlugin {
const servicePropagation = this.createDBMPropagationCommentService(dbmService, span)

if (isPreparedStatement || mode === 'service') {
return `/*${servicePropagation}*/ ${query}`
return this.addComment(query, servicePropagation)
} else if (mode === 'full') {
span.setTag('_dd.dbm_trace_injected', 'true')
const traceparent = span._spanContext.toTraceparent()
return `/*${servicePropagation},traceparent='${traceparent}'*/ ${query}`
return this.addComment(query, `${servicePropagation},traceparent='${traceparent}'`)
}
}

Expand Down
Loading