|
| 1 | +/* |
| 2 | + * Copyright 2026 New Relic Corporation. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict' |
| 7 | +const DbOperationSubscriber = require('../db-operation') |
| 8 | +const { redisClientOpts } = require('../../symbols') |
| 9 | + |
| 10 | +/** |
| 11 | + * Listens to events on `RedisCommandQueue.addCommand` to create |
| 12 | + * the segment with necessary datastore parameters for a given Redis |
| 13 | + * operation. |
| 14 | + * |
| 15 | + * Relies on `ctx[redisClientOpts]` being set for the `host`, `port_path_or_id`, |
| 16 | + * and `database` parameters. |
| 17 | + */ |
| 18 | +module.exports = class CmdQueueAddCmdSubscriber extends DbOperationSubscriber { |
| 19 | + constructor({ agent, logger, packageName = '@redis/client' }) { |
| 20 | + super({ agent, logger, packageName, channelName: 'nr_addCommand', system: 'Redis' }) |
| 21 | + this.events = ['asyncEnd'] |
| 22 | + } |
| 23 | + |
| 24 | + handler(data, ctx) { |
| 25 | + const { arguments: args } = data |
| 26 | + const [cmd, key, value] = args[0] |
| 27 | + this.setParameters({ ctx, key, value }) |
| 28 | + this.operation = cmd?.toLowerCase() || 'other' |
| 29 | + return super.handler(data, ctx) |
| 30 | + } |
| 31 | + |
| 32 | + setParameters({ ctx, key, value }) { |
| 33 | + const clientParams = ctx[redisClientOpts] ?? {} |
| 34 | + this.parameters = Object.assign({}, clientParams) |
| 35 | + this.parameters.product = this.system |
| 36 | + |
| 37 | + if (this.agent.config.attributes.enabled) { |
| 38 | + if (key) { |
| 39 | + this.parameters.key = JSON.stringify(key) |
| 40 | + } |
| 41 | + if (value) { |
| 42 | + this.parameters.value = JSON.stringify(value) |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | +} |
0 commit comments