Skip to content

Commit bb87cd8

Browse files
authored
refactor: Updated cassandra-driver instrumentation to subscribe to events emitted. (newrelic#3372)
1 parent 0abad35 commit bb87cd8

16 files changed

+470
-143
lines changed

lib/instrumentation/cassandra-driver.js

Lines changed: 0 additions & 131 deletions
This file was deleted.

lib/instrumentations.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ module.exports = function instrumentations() {
2323
'aws-sdk': { module: './instrumentation/aws-sdk' },
2424
bluebird: { type: InstrumentationDescriptor.TYPE_PROMISE },
2525
bunyan: { type: InstrumentationDescriptor.TYPE_GENERIC },
26-
'cassandra-driver': { type: InstrumentationDescriptor.TYPE_DATASTORE },
2726
connect: { type: InstrumentationDescriptor.TYPE_WEB_FRAMEWORK },
2827
express: { type: InstrumentationDescriptor.TYPE_WEB_FRAMEWORK },
2928
fastify: { type: InstrumentationDescriptor.TYPE_WEB_FRAMEWORK },

lib/subscriber-configs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// The expected export of these files is:
99
// 'package-name': [ { path: 'subscriberPath', instrumentations: [] }, ... ]
1010
const subscribers = {
11+
...require('./subscribers/cassandra-driver/config'),
1112
...require('./subscribers/elasticsearch/config'),
1213
...require('./subscribers/ioredis/config'),
1314
...require('./subscribers/mcp-sdk/config'),
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2025 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
'use strict'
7+
8+
const DbQuerySubscriber = require('../db-query')
9+
10+
/**
11+
* Given the arguments for Cassandra's `batch` method, this finds the first
12+
* query in the batch.
13+
*
14+
* @param {Array} args - original arguments passed to the batch function
15+
* @returns {string} The query for this batch request.
16+
*/
17+
function findBatchQueryArg(args) {
18+
const sql = (args[0] && args[0][0]) || ''
19+
return sql.query || sql
20+
}
21+
22+
/**
23+
* Subscribes to the `batch` event in `cassandra-driver`
24+
* and extracts relevant information from the queries.
25+
*/
26+
class CassandraBatchSubscriber extends DbQuerySubscriber {
27+
constructor({ agent, logger, channelName = 'nr_batch' }) {
28+
super({ agent, logger, channelName, packageName: 'cassandra-driver', system: 'Cassandra' })
29+
this.events = ['asyncEnd']
30+
this.isBatch = true
31+
}
32+
33+
handler(data, ctx) {
34+
const { self, arguments: args } = data
35+
this.queryString = findBatchQueryArg(args)
36+
this.setParameters(self)
37+
return super.handler(data, ctx)
38+
}
39+
40+
setParameters(self) {
41+
this.parameters = {}
42+
this.parameters.product = this.system
43+
this.parameters.database_name = self?.keyspace
44+
this.parameters.host = self?.options?.contactPoints?.[0]
45+
this.parameters.port_path_or_id = self?.options?.protocolOptions?.port
46+
}
47+
}
48+
49+
module.exports = CassandraBatchSubscriber
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2025 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
'use strict'
7+
8+
const DbOperationSubscriber = require('../db-operation')
9+
10+
/**
11+
* Subscribes to the `connect` event in `cassandra-driver`'s `Client`.
12+
*/
13+
class CassandraConnectSubscriber extends DbOperationSubscriber {
14+
constructor({ agent, logger, channelName = 'nr_connect' }) {
15+
super({ agent, logger, channelName, packageName: 'cassandra-driver', system: 'Cassandra' })
16+
this.events = ['asyncEnd']
17+
this.operation = 'connect'
18+
}
19+
20+
handler(data, ctx) {
21+
const { self } = data
22+
this.setParameters(self)
23+
return super.handler(data, ctx)
24+
}
25+
26+
setParameters(self) {
27+
this.parameters = {}
28+
this.parameters.product = this.system
29+
this.parameters.database_name = self?.keyspace
30+
this.parameters.host = self?.options?.contactPoints?.[0]
31+
this.parameters.port_path_or_id = self?.options?.protocolOptions?.port
32+
}
33+
}
34+
35+
module.exports = CassandraConnectSubscriber
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2025 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
'use strict'
7+
8+
const DbQuerySubscriber = require('../db-query')
9+
10+
/**
11+
* Subscribes to the `execute` event in `cassandra-driver`
12+
* and extracts relevant information from the query.
13+
*/
14+
class CassandraEachRowSubscriber extends DbQuerySubscriber {
15+
constructor({ agent, logger, channelName = 'nr_eachRow' }) {
16+
super({ agent, logger, channelName, packageName: 'cassandra-driver', system: 'Cassandra' })
17+
this.events = ['asyncEnd']
18+
}
19+
20+
handler(data, ctx) {
21+
const { self, arguments: args } = data
22+
this.queryString = args?.[0]
23+
this.setParameters(self)
24+
return super.handler(data, ctx)
25+
}
26+
27+
setParameters(self) {
28+
this.parameters = {}
29+
this.parameters.product = this.system
30+
this.parameters.database_name = self?.keyspace
31+
this.parameters.host = self?.controlConnection?.connection?.address
32+
this.parameters.port_path_or_id = self?.controlConnection?.connection?.port
33+
}
34+
}
35+
36+
module.exports = CassandraEachRowSubscriber
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2025 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
'use strict'
7+
8+
const DbQuerySubscriber = require('../db-query')
9+
10+
/**
11+
* Subscribes to the `execute` event in `cassandra-driver`
12+
* and extracts relevant information from the query.
13+
*/
14+
class CassandraExecuteSubscriber extends DbQuerySubscriber {
15+
constructor({ agent, logger, channelName = 'nr_execute' }) {
16+
super({ agent, logger, channelName, packageName: 'cassandra-driver', system: 'Cassandra' })
17+
this.events = ['asyncEnd']
18+
}
19+
20+
handler(data, ctx) {
21+
const { self, arguments: args } = data
22+
this.queryString = args?.[0]
23+
this.setParameters(self)
24+
return super.handler(data, ctx)
25+
}
26+
27+
setParameters(self) {
28+
this.parameters = {}
29+
this.parameters.product = this.system
30+
this.parameters.database_name = self?.keyspace
31+
this.parameters.host = self?.controlConnection?.connection?.address
32+
this.parameters.port_path_or_id = self?.controlConnection?.connection?.port
33+
}
34+
}
35+
36+
module.exports = CassandraExecuteSubscriber
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2025 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
'use strict'
7+
8+
const DbOperationSubscriber = require('../db-operation')
9+
10+
/**
11+
* Subscribes to the `shutdown` event in `cassandra-driver`.
12+
*/
13+
class CassandraShutdownSubscriber extends DbOperationSubscriber {
14+
constructor({ agent, logger, channelName = 'nr_shutdown' }) {
15+
super({ agent, logger, channelName, packageName: 'cassandra-driver', system: 'Cassandra' })
16+
this.events = ['asyncStart', 'asyncEnd']
17+
this.operation = 'shutdown'
18+
}
19+
20+
handler(data, ctx) {
21+
const { self } = data
22+
this.setParameters(self)
23+
return super.handler(data, ctx)
24+
}
25+
26+
setParameters() {
27+
this.parameters = {}
28+
this.parameters.product = this.system
29+
}
30+
}
31+
32+
module.exports = CassandraShutdownSubscriber

0 commit comments

Comments
 (0)