Skip to content
Closed
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
78 changes: 78 additions & 0 deletions lib/instrumentation/cassandra-driver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use strict'

var shimmer = require('../shimmer')
, logger = require('../logger')
.child({component : 'cassandra-driver'})
, record = require('../metrics/recorders/cassandra.js')
, parseSql = require('../db/parse-sql')
, CASSANDRA = require('../metrics/names').CASSANDRA


var INSTRUMENTED_OPERATIONS = [
'execute',
'batch',
'eachRow'
]

module.exports = function initialize(agent, cassandra) {
var tracer = agent.tracer

INSTRUMENTED_OPERATIONS.forEach(function cb_forEach(operation) {
shimmer.wrapMethod(cassandra && cassandra.Client && cassandra.Client.prototype,
'cassandra-driver.Client.prototype',
operation,
function wrapper(cmd) {
return tracer.segmentProxy(function wrapped(queries) {
if (!tracer.getTransaction() || arguments.length < 1) {
logger.trace("Not tracing cassandra-driver command due to no transaction state.")
return cmd.apply(this, arguments)
}

var transaction = tracer.getTransaction()
, args = tracer.slice(arguments)
, query = typeof queries === 'string' ? queries : queries[0]
, ps = parseSql(CASSANDRA.PREFIX, query)
, name = CASSANDRA.STATEMENT + this.keyspace + '.' + ps.model + '/' + ps.operation
, segment = tracer.addSegment(name, record)
, position = args.length - 1
, last = args[position]


logger.trace("Adding cassandra-driver command trace segment transaction %s.",
transaction.id)

// capture connection info for datastore instance metric
segment.port = this.port
segment.host = this.host

function finalize(target) {
return function cls_finalize() {
var returned = target.apply(this, arguments)
segment.end()
logger.trace("cassandra-driver command trace segment ended for transaction %s.",
transaction.id)

return returned
}
}

if (typeof last === 'function') {
args[position] = tracer.callbackProxy(finalize(last))
}
else if (Array.isArray(last) && typeof last[last.length - 1] === 'function') {
var callback = finalize(last[last.length - 1])
last[last.length - 1] = tracer.callbackProxy(callback)
}
else { // let's shove a callback in there for fun
args.push(function cb_push() {
segment.end()
logger.trace("cassandra-driver command trace segment ended for transaction %s.",
transaction.id)
})
}

return cmd.apply(this, args)
})
})
})
}
3 changes: 1 addition & 2 deletions lib/instrumentation/node-cassandra-cql.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var shimmer = require('../shimmer')
, logger = require('../logger')
.child({component : 'node-cassandra-cql'})
, record = require('../metrics/recorders/node-cassandra-cql.js')
, record = require('../metrics/recorders/cassandra.js')
, CASSANDRA = require('../metrics/names').CASSANDRA


Expand Down Expand Up @@ -34,7 +34,6 @@ module.exports = function initialize(agent, cassandracql) {
, position = args.length - 1
, last = args[position]


logger.trace("Adding cassandra-cql command trace segment transaction %s.",
transaction.id)

Expand Down
1 change: 1 addition & 0 deletions lib/instrumentations.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = function() {
'memcached',
'mongodb',
'mysql',
'cassandra-driver',
'node-cassandra-cql',
'pg',
'redis',
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@
"split": "*",
"tap": "*",
"through": "^2.3.6",
"wrench": "*"
"wrench": "*",
"cassandra-driver": "~1.0.2"
},
"repository": {
"type": "git",
Expand Down