Skip to content

Commit

Permalink
feat!: Removed support for redis < 2.6.0 (#2405)
Browse files Browse the repository at this point in the history
  • Loading branch information
bizob2828 authored Jul 24, 2024
1 parent 321244c commit e2c0a31
Showing 1 changed file with 13 additions and 59 deletions.
72 changes: 13 additions & 59 deletions lib/instrumentation/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

'use strict'

const hasOwnProperty = require('../util/properties').hasOwn
const stringify = require('json-stringify-safe')
const {
OperationSpec,
Expand All @@ -20,15 +19,19 @@ module.exports = function initialize(_agent, redis, _moduleName, shim) {

shim.setDatastore(shim.REDIS)

if (proto.internal_send_command) {
registerInternalSendCommand(shim, proto)
} else {
registerSendCommand(shim, proto)
if (!proto.internal_send_command) {
shim.logger.warn(
'New Relic Node.js agent no longer supports redis < 2.6.0, current version %s. Please downgrade to v11 for support, if needed',
shim.pkgVersion
)
return
}

registerInternalSendCommand(shim, proto)
}

/**
* Instrumentation used in versions of redis > 2.6.1 < 4 to record all redis commands
* Instrumentation used in versions of redis >= 2.6.0 < 4 to record all redis commands
*
* @param {Shim} shim instance of shim
* @param {object} proto RedisClient prototype
Expand All @@ -40,7 +43,7 @@ function registerInternalSendCommand(shim, proto) {
function wrapInternalSendCommand(shim, _, __, args) {
const commandObject = args[0]
const keys = commandObject.args
const parameters = getInstanceParameters(shim, this)
const parameters = getInstanceParameters(this)

parameters.key = stringifyKeys(shim, keys)

Expand Down Expand Up @@ -68,34 +71,6 @@ function registerInternalSendCommand(shim, proto) {
)
}

/**
* Instrumentation used in versions of redis < 2.6.1 to record all redis commands
*
* @param {Shim} shim instance of shim
* @param {object} proto RedisClient prototype
*/
function registerSendCommand(shim, proto) {
shim.recordOperation(proto, 'send_command', function wrapSendCommand(shim, _, __, args) {
const [command, keys] = args
const parameters = getInstanceParameters(shim, this)

parameters.key = stringifyKeys(shim, keys)

return new OperationSpec({
name: command || 'other',
parameters,
callback: function bindCallback(shim, _f, _n, segment) {
const last = args[args.length - 1]
if (shim.isFunction(last)) {
shim.bindCallbackSegment(null, args, shim.LAST, segment)
} else if (shim.isArray(last) && shim.isFunction(last[last.length - 1])) {
shim.bindCallbackSegment(null, last, shim.LAST, segment)
}
}
})
})
}

function stringifyKeys(shim, keys) {
let key = null
if (keys && keys.length && !shim.isFunction(keys)) {
Expand All @@ -111,35 +86,14 @@ function stringifyKeys(shim, keys) {
}

/**
* Captures the necessary datastore parameters based on the specific version of redis
* Captures the necessary datastore parameters from redis client
*
* @param {Shim} shim instance of shim
* @param {object} client instance of redis client
* @returns {object} datastore parameters
*/
function getInstanceParameters(shim, client) {
if (hasOwnProperty(client, 'connection_options')) {
// for redis 2.4.0 - 2.6.2
return doCapture(client, client.connection_options)
} else if (hasOwnProperty(client, 'connectionOption')) {
// for redis 0.12 - 2.2.5
return doCapture(client, client.connectionOption)
} else if (hasOwnProperty(client, 'options')) {
// for redis 2.3.0 - 2.3.1
return doCapture(client, client.options)
}
shim.logger.debug('Could not access instance attributes on connection.')
return doCapture()
}
function getInstanceParameters(client = {}) {
const opts = client?.connection_options

/**
* Extracts the relevant datastore parameters
*
* @param {object} client instance of redis client
* @param {object} opts options for the client instance
* @returns {object} datastore parameters
*/
function doCapture(client = {}, opts = {}) {
return new DatastoreParameters({
host: opts.host || 'localhost',
port_path_or_id: opts.path || opts.port || '6379',
Expand Down

0 comments on commit e2c0a31

Please sign in to comment.