Skip to content
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
12 changes: 12 additions & 0 deletions lib/instrumentation/pg.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ function getQuery(shim, original, name, args) {
return statement
}

function getQueryName(shim, original, name, args) {
var config = args[0]

if (config && config.name) {
return config.name
}

return null
}

module.exports = function initialize(agent, pgsql, moduleName, shim) {
shim.setDatastore(shim.POSTGRES)
// allows for native wrapping to not happen if not necessary
Expand Down Expand Up @@ -44,6 +54,7 @@ module.exports = function initialize(agent, pgsql, moduleName, shim) {
shim.recordQuery(this, 'query', {
callback: shim.LAST,
query: getQuery,
queryName: getQueryName,
stream: 'row',
parameters: getInstanceParameters(shim, this),
internal: false
Expand Down Expand Up @@ -82,6 +93,7 @@ module.exports = function initialize(agent, pgsql, moduleName, shim) {
return {
callback: shim.LAST,
query: getQuery,
queryName: getQueryName,
stream: 'row',
parameters: getInstanceParameters(shim, this),
internal: false
Expand Down
20 changes: 19 additions & 1 deletion lib/shim/datastore-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -721,9 +721,15 @@ function _recordQuery(suffix, nodule, properties, querySpec) {
}
shim.logger.trace('Found query %j', queryStr)

var queryName = _extractQueryName.call(shim, fn, fnName, queryDesc, this, args)

// Parse the query and assemble the name.
var parsed = shim.parseQuery(queryStr, this)
var name = (parsed.collection || 'other') + '/' + parsed.operation + suffix
// If the intstrumentation provides a name use it, otherwise construct a name
// from the parsed query string
var name = queryName ?
queryName + '/' + 'select' + suffix :
(parsed.collection || 'other') + '/' + parsed.operation + suffix

// Return the segment descriptor.
return {
Expand Down Expand Up @@ -827,6 +833,18 @@ function _extractQueryStr(fn, fnName, spec, ctx, args) {
return queryStr
}

function _extractQueryName(fn, fnName, spec, ctx, args) {
var queryName = spec.queryName
// Adds support for an instrumentation to specify
// a function to calculate a name that should be used
// for generating the transaction segment name for the query
if (!queryName || !this.isFunction(queryName)) {
return null
}

return queryName.call(ctx, this, fn, fnName, args)
}

/**
* Normalizes segment parameter values.
*
Expand Down