Skip to content

Commit

Permalink
add support for changing the pg service name based on connection para…
Browse files Browse the repository at this point in the history
…ms (#1110)
  • Loading branch information
rochdev authored Oct 16, 2020
1 parent aa9a458 commit 5a8b92b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ tracer.use('net');
tracer.use('paperplane');
tracer.use('paperplane', httpServerOptions);
tracer.use('pg');
tracer.use('pg', { service: params => `${params.host}-${params.database}` });
tracer.use('pino');
tracer.use('promise-js');
tracer.use('promise');
Expand Down
9 changes: 7 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ declare namespace plugins {
/**
* The service name to be used for this plugin.
*/
service?: string;
service?: string | any;

/** Whether to enable the plugin.
* @default true
Expand Down Expand Up @@ -1027,7 +1027,12 @@ declare namespace plugins {
* This plugin automatically instruments the
* [pg](https://node-postgres.com/) module.
*/
interface pg extends Instrumentation {}
interface pg extends Instrumentation {
/**
* The service name to be used for this plugin. If a function is used, it will be passed the connection parameters and its return value will be used as the service name.
*/
service?: string | ((params: any) => string);
}

/**
* This plugin patches the [pino](http://getpino.io)
Expand Down
15 changes: 13 additions & 2 deletions packages/datadog-plugin-pg/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ function createWrapQuery (tracer, config) {
return function queryWithTrace () {
const scope = tracer.scope()
const childOf = scope.active()
const params = this.connectionParameters
const service = getServiceName(tracer, config, params)
const span = tracer.startSpan(OPERATION_NAME, {
childOf,
tags: {
[Tags.SPAN_KIND]: Tags.SPAN_KIND_RPC_CLIENT,
'service.name': config.service || `${tracer._service}-postgres`,
'service.name': service,
'span.type': 'sql',
'db.type': 'postgres'
}
Expand All @@ -33,7 +35,6 @@ function createWrapQuery (tracer, config) {

const originalCallback = pgQuery.callback
const statement = pgQuery.text
const params = this.connectionParameters

span.setTag('resource.name', statement)

Expand Down Expand Up @@ -69,6 +70,16 @@ function createWrapQuery (tracer, config) {
}
}

function getServiceName (tracer, config, params) {
if (typeof config.service === 'function') {
return config.service(params)
} else if (config.service) {
return config.service
} else {
return `${tracer._service}-postgres`
}
}

module.exports = [
{
name: 'pg',
Expand Down
42 changes: 42 additions & 0 deletions packages/datadog-plugin-pg/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,48 @@ describe('Plugin', () => {
})
})
})

describe('with a service name callback', () => {
before(() => {
return agent.load('pg', { service: params => `${params.host}-${params.database}` })
})

after(() => {
return agent.close()
})

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

client = new pg.Client({
user: 'postgres',
password: 'postgres',
database: 'postgres'
})

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

it('should be configured with the correct service', done => {
agent.use(traces => {
try {
expect(traces[0][0]).to.have.property('service', 'localhost-postgres')

done()
} catch (e) {
done(e)
}
})

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

client.end((err) => {
if (err) throw err
})
})
})
})
})
})
})

0 comments on commit 5a8b92b

Please sign in to comment.