-
Notifications
You must be signed in to change notification settings - Fork 404
/
Copy pathredis-v4.tap.js
278 lines (234 loc) · 9.87 KB
/
redis-v4.tap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
'use strict'
const tap = require('tap')
const test = tap.test
const helper = require('../../lib/agent_helper')
const { removeMatchedModules } = require('../../lib/cache-buster')
const params = require('../../lib/params')
const urltils = require('../../../lib/util/urltils')
// Indicates unique database in Redis. 0-15 supported.
const DB_INDEX = 2
test('Redis instrumentation', function (t) {
t.autoend()
let METRIC_HOST_NAME = null
let HOST_ID = null
let agent
let client
t.beforeEach(async function () {
agent = helper.instrumentMockedAgent()
const redis = require('redis')
client = redis.createClient({ socket: { port: params.redis_port, host: params.redis_host } })
await client.connect()
await client.flushAll()
await client.select(DB_INDEX)
// eslint-disable-next-line new-cap
METRIC_HOST_NAME = urltils.isLocalhost(params.redis_host)
? agent.config.getHostnameSafe()
: params.redis_host
HOST_ID = METRIC_HOST_NAME + '/' + params.redis_port
// need to capture attributes
agent.config.attributes.enabled = true
})
t.afterEach(async function () {
agent && helper.unloadAgent(agent)
if (client) {
await client.flushAll()
await client.disconnect()
}
// must purge require cache of redis related instrumentation
// otherwise it will not re-register on subsequent test runs
removeMatchedModules(/redis/)
})
t.test('should find Redis calls in the transaction trace', function (t) {
t.notOk(agent.getTransaction(), 'no transaction should be in play')
helper.runInTransaction(agent, async function transactionInScope() {
const transaction = agent.getTransaction()
t.ok(transaction, 'transaction should be visible')
const ok = await client.set('testkey', 'arglbargle')
t.ok(agent.getTransaction(), 'transaction should still be visible')
t.ok(ok, 'everything should be peachy after setting')
const value = await client.get('testkey')
t.ok(agent.getTransaction(), 'transaction should still still be visible')
t.equal(value, 'arglbargle', 'redis client should still work')
const trace = transaction.trace
t.ok(trace, 'trace should exist')
t.ok(trace.root, 'root element should exist')
t.equal(trace.root.children.length, 2, 'there should be only two children of the root')
const setSegment = trace.root.children[0]
const setAttributes = setSegment.getAttributes()
t.ok(setSegment, 'trace segment for set should exist')
t.equal(setSegment.name, 'Datastore/operation/Redis/set', 'should register the set')
t.equal(setAttributes.key, '"testkey"', 'should have the set key as a attribute')
t.equal(setSegment.children.length, 0, 'set should have no children')
const getSegment = trace.root.children[1]
const getAttributes = getSegment.getAttributes()
t.ok(getSegment, 'trace segment for get should exist')
t.equal(getSegment.name, 'Datastore/operation/Redis/get', 'should register the get')
t.equal(getAttributes.key, '"testkey"', 'should have the get key as a attribute')
t.ok(getSegment.timer.hrDuration, 'trace segment should have ended')
t.end()
})
})
t.test('should create correct metrics', function (t) {
t.notOk(agent.getTransaction(), 'no transaction should be in play')
helper.runInTransaction(agent, async function transactionInScope() {
const transaction = agent.getTransaction()
await client.set('testkey', 'arglbargle')
await client.get('testkey')
transaction.end()
const unscoped = transaction.metrics.unscoped
const expected = {
'Datastore/all': 2,
'Datastore/allWeb': 2,
'Datastore/Redis/all': 2,
'Datastore/Redis/allWeb': 2,
'Datastore/operation/Redis/set': 1,
'Datastore/operation/Redis/get': 1
}
checkMetrics(t, unscoped, expected)
t.end()
})
})
t.test('should handle multi commands', function (t) {
helper.runInTransaction(agent, async function transactionInScope() {
const transaction = agent.getTransaction()
const results = await client.multi().set('multi-key', 'multi-value').get('multi-key').exec()
t.same(results, ['OK', 'multi-value'], 'should return expected results')
transaction.end()
const unscoped = transaction.metrics.unscoped
const expected = {
'Datastore/all': 4,
'Datastore/allWeb': 4,
'Datastore/Redis/all': 4,
'Datastore/Redis/allWeb': 4,
'Datastore/operation/Redis/multi': 1,
'Datastore/operation/Redis/set': 1,
'Datastore/operation/Redis/get': 1,
'Datastore/operation/Redis/exec': 1
}
expected['Datastore/instance/Redis/' + HOST_ID] = 4
checkMetrics(t, unscoped, expected)
t.end()
})
})
t.test('should add `key` attribute to trace segment', function (t) {
t.notOk(agent.getTransaction(), 'no transaction should be in play')
agent.config.attributes.enabled = true
helper.runInTransaction(agent, async function () {
await client.set('saveme', 'foobar')
const segment = agent.tracer.getSegment().children[0]
t.equal(segment.getAttributes().key, '"saveme"', 'should have `key` attribute')
t.end()
})
})
t.test('should not add `key` attribute to trace segment', function (t) {
t.notOk(agent.getTransaction(), 'no transaction should be in play')
agent.config.attributes.enabled = false
helper.runInTransaction(agent, async function () {
await client.set('saveme', 'foobar')
const segment = agent.tracer.getSegment().children[0]
t.notOk(segment.getAttributes().key, 'should not have `key` attribute')
t.end()
})
})
t.test('should add datastore instance attributes to trace segments', function (t) {
t.notOk(agent.getTransaction(), 'no transaction should be in play')
// Enable.
agent.config.datastore_tracer.instance_reporting.enabled = true
agent.config.datastore_tracer.database_name_reporting.enabled = true
helper.runInTransaction(agent, async function transactionInScope() {
const transaction = agent.getTransaction()
await client.set('testkey', 'arglbargle')
const trace = transaction.trace
const setSegment = trace.root.children[0]
const attributes = setSegment.getAttributes()
t.equal(attributes.host, METRIC_HOST_NAME, 'should have host as attribute')
t.equal(
attributes.port_path_or_id,
String(params.redis_port),
'should have port as attribute'
)
t.equal(attributes.database_name, String(DB_INDEX), 'should have database id as attribute')
t.equal(attributes.product, 'Redis', 'should have product attribute')
t.end()
})
})
t.test('should not add instance attributes/metrics when disabled', function (t) {
t.notOk(agent.getTransaction(), 'no transaction should be in play')
// disable
agent.config.datastore_tracer.instance_reporting.enabled = false
agent.config.datastore_tracer.database_name_reporting.enabled = false
helper.runInTransaction(agent, async function transactionInScope() {
const transaction = agent.getTransaction()
await client.set('testkey', 'arglbargle')
const setSegment = transaction.trace.root.children[0]
const attributes = setSegment.getAttributes()
t.equal(attributes.host, undefined, 'should not have host attribute')
t.equal(attributes.port_path_or_id, undefined, 'should not have port attribute')
t.equal(attributes.database_name, undefined, 'should not have db name attribute')
transaction.end()
const unscoped = transaction.metrics.unscoped
t.equal(
unscoped['Datastore/instance/Redis/' + HOST_ID],
undefined,
'should not have instance metric'
)
t.end()
})
})
t.test('should follow selected database', function (t) {
t.notOk(agent.getTransaction(), 'no transaction should be in play')
let transaction = null
const SELECTED_DB = 3
helper.runInTransaction(agent, async function (tx) {
transaction = tx
await client.set('select:test:key', 'foo')
t.ok(agent.getTransaction(), 'should not lose transaction state')
await client.select(SELECTED_DB)
t.ok(agent.getTransaction(), 'should not lose transaction state')
await client.set('select:test:key:2', 'bar')
t.ok(agent.getTransaction(), 'should not lose transaction state')
transaction.end()
verify()
t.end()
})
function verify() {
const setSegment1 = transaction.trace.root.children[0]
const selectSegment = transaction.trace.root.children[1]
const setSegment2 = transaction.trace.root.children[2]
t.equal(setSegment1.name, 'Datastore/operation/Redis/set', 'should register the first set')
t.equal(
setSegment1.getAttributes().database_name,
String(DB_INDEX),
'should have the starting database id as attribute for the first set'
)
t.equal(selectSegment.name, 'Datastore/operation/Redis/select', 'should register the select')
t.equal(
selectSegment.getAttributes().database_name,
String(DB_INDEX),
'should have the starting database id as attribute for the select'
)
t.equal(setSegment2.name, 'Datastore/operation/Redis/set', 'should register the second set')
t.equal(
setSegment2.getAttributes().database_name,
String(SELECTED_DB),
'should have the selected database id as attribute for the second set'
)
}
})
})
function checkMetrics(t, metrics, expected) {
Object.keys(expected).forEach(function (name) {
t.ok(metrics[name], 'should have metric ' + name)
if (metrics[name]) {
t.equal(
metrics[name].callCount,
expected[name],
'should have ' + expected[name] + ' calls for ' + name
)
}
})
}