-
Notifications
You must be signed in to change notification settings - Fork 384
/
elasticsearch.tap.js
600 lines (558 loc) · 22.7 KB
/
elasticsearch.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/*
* 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 params = require('../../lib/params')
const urltils = require('../../../lib/util/urltils')
const crypto = require('crypto')
const { readFile } = require('fs/promises')
const semver = require('semver')
const DB_INDEX = `test-${randomString()}`
const DB_INDEX_2 = `test2-${randomString()}`
const DB_INDEX_3 = `test3-${randomString()}`
const SEARCHTERM_1 = randomString()
function randomString() {
return crypto.randomBytes(5).toString('hex')
}
// request bodies are structured differently in ElasticSearch v7.x vs v8.x
function setRequestBody(body, version) {
if (semver.lt(version, '8.0.0')) {
return { body }
}
return body
}
function setBulkBody(body, version) {
if (semver.lt(version, '8.0.0')) {
return {
refresh: true,
body
}
}
return {
refresh: true,
operations: body
}
}
function setMsearch(body, version) {
if (semver.lt(version, '8.0.0')) {
return { body }
}
return {
searches: body
}
}
test('Elasticsearch instrumentation', (t) => {
t.autoend()
let METRIC_HOST_NAME = null
let HOST_ID = null
let agent
let client
let pkgVersion
t.before(async () => {
// Determine version. ElasticSearch v7 did not export package, so we have to read the file
// instead of requiring it, as we can with 8+.
const pkg = await readFile(`${__dirname}/node_modules/@elastic/elasticsearch/package.json`)
;({ version: pkgVersion } = JSON.parse(pkg.toString()))
agent = helper.instrumentMockedAgent()
METRIC_HOST_NAME = urltils.isLocalhost(params.elastic_host)
? agent.config.getHostnameSafe()
: params.elastic_host
HOST_ID = METRIC_HOST_NAME + '/' + params.elastic_port
// need to capture attributes
agent.config.attributes.enabled = true
const { Client } = require('@elastic/elasticsearch')
client = new Client({
node: `http://${params.elastic_host}:${params.elastic_port}`
})
return Promise.all([
client.indices.create({ index: DB_INDEX }),
client.indices.create({ index: DB_INDEX_2 })
])
})
t.afterEach(() => {
agent.queries.clear()
})
t.teardown(() => {
agent && helper.unloadAgent(agent)
return Promise.all([
client.indices.delete({ index: DB_INDEX }),
client.indices.delete({ index: DB_INDEX_2 })
])
})
t.test('should be able to record creating an index', async (t) => {
const index = `test-index-${randomString()}`
t.teardown(async () => {
await client.indices.delete({ index })
})
await helper.runInTransaction(agent, async function transactionInScope(transaction) {
t.ok(transaction, 'transaction should be visible')
await client.indices.create({ index })
const trace = transaction.trace
t.ok(trace?.root?.children?.[0], 'trace, trace root, and first child should exist')
const firstChild = trace.root.children[0]
t.equal(
firstChild.name,
`Datastore/statement/ElasticSearch/${index}/index.create`,
'should record index PUT as create'
)
})
})
t.test('should record bulk operations', async (t) => {
await helper.runInTransaction(agent, async function transactionInScope(transaction) {
const operations = [
{ index: { _index: DB_INDEX } },
{ title: 'First Bulk Doc', body: 'Content of first bulk document' },
{ index: { _index: DB_INDEX } },
{ title: 'Second Bulk Doc', body: 'Content of second bulk document.' },
{ index: { _index: DB_INDEX } },
{ title: 'Third Bulk Doc', body: 'Content of third bulk document.' },
{ index: { _index: DB_INDEX } },
{ title: 'Fourth Bulk Doc', body: 'Content of fourth bulk document.' },
{ index: { _index: DB_INDEX_2 } },
{ title: 'Fifth Bulk Doc', body: 'Content of fifth bulk document' },
{ index: { _index: DB_INDEX_2 } },
{
title: 'Sixth Bulk Doc',
body: `Content of sixth bulk document. Has search term: ${SEARCHTERM_1}`
},
{ index: { _index: DB_INDEX_2 } },
{ title: 'Seventh Bulk Doc', body: 'Content of seventh bulk document.' },
{ index: { _index: DB_INDEX_2 } },
{ title: 'Eighth Bulk Doc', body: 'Content of eighth bulk document.' }
]
await client.bulk(setBulkBody(operations, pkgVersion))
t.ok(transaction, 'transaction should still be visible after bulk create')
const trace = transaction.trace
t.ok(trace?.root?.children?.[0], 'trace, trace root, and first child should exist')
const firstChild = trace.root.children[0]
t.equal(
firstChild.name,
'Datastore/statement/ElasticSearch/any/bulk.create',
'should record bulk operation'
)
})
})
t.test('should record bulk operations triggered by client helpers', async (t) => {
await helper.runInTransaction(agent, async function transactionInScope(transaction) {
const operations = [
{ title: 'Ninth Bulk Doc from helpers', body: 'Content of ninth bulk document' },
{ title: 'Tenth Bulk Doc from helpers', body: 'Content of tenth bulk document.' },
{ title: 'Eleventh Bulk Doc from helpers', body: 'Content of eleventh bulk document.' },
{ title: 'Twelfth Bulk Doc from helpers', body: 'Content of twelfth bulk document.' },
{
title: 'Thirteenth Bulk Doc from helpers',
body: 'Content of thirteenth bulk document'
},
{
title: 'Fourteenth Bulk Doc from helpers',
body: 'Content of fourteenth bulk document.'
},
{ title: 'Fifteenth Bulk Doc from helpers', body: 'Content of fifteenth bulk document.' },
{ title: 'Sixteenth Bulk Doc from helpers', body: 'Content of sixteenth bulk document.' }
]
await client.helpers.bulk({
datasource: operations,
onDocument() {
return {
index: { _index: DB_INDEX_2 }
}
},
refreshOnCompletion: true
})
t.ok(transaction, 'transaction should still be visible after bulk create')
const trace = transaction.trace
t.ok(trace?.root?.children?.[0], 'trace, trace root, and first child should exist')
t.ok(trace?.root?.children?.[1], 'trace, trace root, and second child should exist')
// helper interface results in a first child of timers.setTimeout, with the second child related to the operation
const secondChild = trace.root.children[1]
t.equal(
secondChild.name,
'Datastore/statement/ElasticSearch/any/bulk.create',
'should record bulk operation'
)
})
})
t.test('should record search with query string', async function (t) {
// enable slow queries
agent.config.transaction_tracer.explain_threshold = 0
agent.config.transaction_tracer.record_sql = 'raw'
agent.config.slow_sql.enabled = true
await helper.runInTransaction(agent, async function transactionInScope(transaction) {
const expectedQuery = { q: SEARCHTERM_1 }
const search = await client.search({ index: DB_INDEX_2, ...expectedQuery })
t.ok(search, 'search should return a result')
t.ok(transaction, 'transaction should still be visible after search')
const trace = transaction.trace
t.ok(trace?.root?.children?.[0], 'trace, trace root, and first child should exist')
const firstChild = trace.root.children[0]
t.match(
firstChild.name,
`Datastore/statement/ElasticSearch/${DB_INDEX_2}/search`,
'querystring search should be recorded as a search'
)
const attrs = firstChild.getAttributes()
t.match(attrs.product, 'ElasticSearch')
t.match(attrs.host, METRIC_HOST_NAME)
transaction.end()
t.ok(agent.queries.samples.size > 0, 'there should be a query sample')
for (const query of agent.queries.samples.values()) {
t.ok(query.total > 0, 'the samples should have positive duration')
t.match(
query.trace.query,
JSON.stringify(expectedQuery),
'expected query string should have been used'
)
}
})
})
t.test('should record search with request body', async function (t) {
// enable slow queries
agent.config.transaction_tracer.explain_threshold = 0
agent.config.transaction_tracer.record_sql = 'raw'
agent.config.slow_sql.enabled = true
await helper.runInTransaction(agent, async function transactionInScope(transaction) {
// We expect this content in the trace of the request, but the request body is different in 7 v 8.
const expectedQuery = { query: { match: { body: 'document' } } }
const requestBody = setRequestBody(expectedQuery, pkgVersion)
const search = await client.search({ index: DB_INDEX, ...requestBody })
t.ok(search, 'search should return a result')
t.ok(transaction, 'transaction should still be visible after search')
const trace = transaction.trace
t.ok(trace?.root?.children?.[0], 'trace, trace root, and first child should exist')
const firstChild = trace.root.children[0]
t.match(
firstChild.name,
`Datastore/statement/ElasticSearch/${DB_INDEX}/search`,
'search index is specified, so name shows it'
)
const attrs = firstChild.getAttributes()
t.equal(attrs.product, 'ElasticSearch')
t.equal(attrs.host, METRIC_HOST_NAME)
t.equal(attrs.port_path_or_id, `${params.elastic_port}`)
// TODO: update once instrumentation is properly setting database name
t.equal(attrs.database_name, 'unknown')
transaction.end()
t.ok(agent.queries.samples.size > 0, 'there should be a query sample')
for (const query of agent.queries.samples.values()) {
t.ok(query.total > 0, 'the samples should have positive duration')
t.match(
query.trace.query,
JSON.stringify({ ...expectedQuery }),
'expected query body should have been recorded'
)
}
})
})
t.test('should record search across indices', async function (t) {
// enable slow queries
agent.config.transaction_tracer.explain_threshold = 0
agent.config.transaction_tracer.record_sql = 'raw'
agent.config.slow_sql.enabled = true
await helper.runInTransaction(agent, async function transactionInScope(transaction) {
const expectedQuery = { query: { match: { body: 'document' } } }
const requestBody = setRequestBody(expectedQuery, pkgVersion)
const search = await client.search({ ...requestBody })
t.ok(search, 'search should return a result')
t.ok(transaction, 'transaction should still be visible after search')
const trace = transaction.trace
t.ok(trace?.root?.children?.[0], 'trace, trace root, and first child should exist')
const firstChild = trace.root.children[0]
t.match(
firstChild.name,
'Datastore/statement/ElasticSearch/any/search',
'child name on all indices should show search'
)
const attrs = firstChild.getAttributes()
t.match(attrs.product, 'ElasticSearch')
t.match(attrs.host, METRIC_HOST_NAME)
transaction.end()
t.ok(agent.queries.samples.size > 0, 'there should be a query sample')
for (const query of agent.queries.samples.values()) {
t.ok(query.total > 0, 'the samples should have positive duration')
t.match(
query.trace.query,
JSON.stringify({ ...expectedQuery }),
'expected query body should have been recorded'
)
}
})
})
t.test('should record msearch', async function (t) {
agent.config.transaction_tracer.explain_threshold = 0
agent.config.transaction_tracer.record_sql = 'raw'
agent.config.slow_sql.enabled = true
await helper.runInTransaction(agent, async function transactionInScope(transaction) {
const expectedQuery = [
{}, // cross-index searches have can have an empty metadata section
{ query: { match: { body: SEARCHTERM_1 } } },
{},
{ query: { match: { body: 'bulk' } } }
]
const requestBody = setMsearch(expectedQuery, pkgVersion)
const search = await client.msearch(requestBody)
// 7 and 8 have different result responses
let results = search?.responses
if (!search?.responses && semver.lt(pkgVersion, '8.0.0')) {
results = search?.body?.responses
}
t.ok(results, 'msearch should return results')
t.equal(results?.length, 2, 'there should be two responses--one per search')
t.equal(results?.[0]?.hits?.hits?.length, 1, 'first search should return one result')
t.equal(results?.[1]?.hits?.hits?.length, 10, 'second search should return ten results')
t.ok(transaction, 'transaction should still be visible after search')
const trace = transaction.trace
t.ok(trace?.root?.children?.[0], 'trace, trace root, and first child should exist')
const firstChild = trace.root.children[0]
t.match(
firstChild.name,
'Datastore/statement/ElasticSearch/any/msearch.create',
'child name should show msearch'
)
const attrs = firstChild.getAttributes()
t.match(attrs.product, 'ElasticSearch')
t.match(attrs.host, METRIC_HOST_NAME)
transaction.end()
t.ok(agent.queries.samples.size > 0, 'there should be a query sample')
for (const query of agent.queries.samples.values()) {
t.ok(query.total > 0, 'the samples should have positive duration')
t.match(
query.trace.query,
JSON.stringify(expectedQuery),
'expected msearch query should have been recorded'
)
}
})
})
t.test('should record msearch via helpers', async function (t) {
agent.config.transaction_tracer.explain_threshold = 0
agent.config.transaction_tracer.record_sql = 'raw'
agent.config.slow_sql.enabled = true
await helper.runInTransaction(agent, async function transactionInScope(transaction) {
const m = client.helpers.msearch()
const searchA = await m.search({}, { query: { match: { body: SEARCHTERM_1 } } })
const searchB = await m.search({}, { query: { match: { body: 'bulk' } } })
const resultsA = searchA?.body?.hits
const resultsB = searchB?.body?.hits
t.ok(resultsA, 'msearch for sixth should return results')
t.ok(resultsB, 'msearch for bulk should return results')
t.equal(resultsA?.hits?.length, 1, 'first search should return one result')
t.equal(resultsB?.hits?.length, 10, 'second search should return ten results')
t.ok(transaction, 'transaction should still be visible after search')
const trace = transaction.trace
t.ok(trace?.root?.children?.[0], 'trace, trace root, and first child should exist')
const firstChild = trace.root.children[0]
t.match(
firstChild.name,
'timers.setTimeout',
'helpers, for some reason, generates a setTimeout metric first'
)
transaction.end()
t.ok(agent.queries.samples.size > 0, 'there should be a query sample')
for (const query of agent.queries.samples.values()) {
// which query gets captured in helper.msearch is non-deterministic
t.ok(query.total > 0, 'the samples should have positive duration')
}
})
})
t.test('should create correct metrics', async function (t) {
const id = `key-${randomString()}`
t.plan(28)
await helper.runInTransaction(agent, async function transactionInScope(transaction) {
const documentProp = setRequestBody(
{
document: {
title: 'second document',
body: 'body of the second document'
}
},
pkgVersion
)
await client.index({
index: DB_INDEX,
id,
...documentProp
})
// check metrics/methods for "exists" queries
await client.exists({ id, index: DB_INDEX })
await client.get({ id, index: DB_INDEX })
const searchQuery = setRequestBody({ query: { match: { body: 'document' } } }, pkgVersion)
await client.search(searchQuery)
await client.delete({ id, index: DB_INDEX })
transaction.end()
const unscoped = transaction.metrics.unscoped
const expected = {
'Datastore/all': 5,
'Datastore/allWeb': 5,
'Datastore/ElasticSearch/all': 5,
'Datastore/ElasticSearch/allWeb': 5,
'Datastore/operation/ElasticSearch/doc.create': 1,
'Datastore/operation/ElasticSearch/doc.get': 1,
'Datastore/operation/ElasticSearch/doc.exists': 1,
'Datastore/operation/ElasticSearch/search': 1,
[`Datastore/statement/ElasticSearch/${DB_INDEX}/doc.create`]: 1,
[`Datastore/statement/ElasticSearch/${DB_INDEX}/doc.get`]: 1,
[`Datastore/statement/ElasticSearch/${DB_INDEX}/doc.exists`]: 1,
[`Datastore/statement/ElasticSearch/${DB_INDEX}/doc.delete`]: 1,
'Datastore/statement/ElasticSearch/any/search': 1
}
expected['Datastore/instance/ElasticSearch/' + HOST_ID] = 5
checkMetrics(t, unscoped, expected)
})
})
t.test('should not add instance attributes/metrics when disabled', async function (t) {
t.plan(4)
// disable
agent.config.datastore_tracer.instance_reporting.enabled = false
agent.config.datastore_tracer.database_name_reporting.enabled = false
await helper.runInTransaction(agent, async function transactionInScope(transaction) {
const documentProp = setRequestBody(
{
document: {
title: 'third document title',
body: 'body of the third document'
}
},
pkgVersion
)
await client.index({
index: DB_INDEX,
id: 'testkey3',
...documentProp
})
const createSegment = transaction.trace.root.children[0]
const attributes = createSegment.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/ElasticSearch/' + HOST_ID],
undefined,
'should not have instance metric'
)
})
})
t.test('edge cases', async (t) => {
await helper.runInTransaction(agent, async function transactionInScope(transaction) {
try {
await client.indices.create({ index: '_search' })
} catch (e) {
t.ok(e, 'should not be able to create an index named _search')
}
const firstChild = transaction?.trace?.root?.children[0]
t.equal(
firstChild.name,
'Datastore/statement/ElasticSearch/_search/index.create',
'should record the attempted index creation without altering the index name'
)
})
})
t.test('index existence check should not error', async (t) => {
await helper.runInTransaction(agent, async function transactionInScope() {
try {
await client.indices.exists({ index: DB_INDEX })
} catch (e) {
t.notOk(e, 'should be able to check for index existence')
}
})
})
})
test('Elasticsearch uninstrumented behavior, to check helpers', { skip: false }, (t) => {
t.autoend()
let client
// eslint-disable-next-line no-unused-vars
let pkgVersion
t.before(async () => {
// Determine version. ElasticSearch v7 did not export package, so we have to read the file
// instead of requiring it, as we can with 8+.
const pkg = await readFile(`${__dirname}/node_modules/@elastic/elasticsearch/package.json`)
;({ version: pkgVersion } = JSON.parse(pkg.toString()))
const { Client } = require('@elastic/elasticsearch')
client = new Client({
node: `http://${params.elastic_host}:${params.elastic_port}`
})
return Promise.all([client.indices.create({ index: DB_INDEX_3 })])
})
t.teardown(() => {
return Promise.all([client.indices.delete({ index: DB_INDEX_3 })])
})
t.test('should record bulk operations triggered by client helpers', async (t) => {
const operations = [
{
title: 'Uninstrumented First Bulk Doc from helpers',
body: 'Content of uninstrumented first bulk document'
},
{
title: 'Uninstrumented Second Bulk Doc from helpers',
body: 'Content of uninstrumented second bulk document.'
},
{
title: 'Uninstrumented Third Bulk Doc from helpers',
body: 'Content of uninstrumented third bulk document.'
},
{
title: 'Uninstrumented Fourth Bulk Doc from helpers',
body: 'Content of uninstrumented fourth bulk document.'
},
{
title: 'Uninstrumented Fifth Bulk Doc from helpers',
body: 'Content of uninstrumented fifth bulk document'
},
{
title: 'Uninstrumented Sixth Bulk Doc from helpers',
body: 'Content of uninstrumented sixth bulk document.'
},
{
title: 'Uninstrumented Seventh Bulk Doc from helpers',
body: 'Content of uninstrumented seventh bulk document.'
},
{
title: 'Uninstrumented Eighth Bulk Doc from helpers',
body: 'Content of uninstrumented eighth bulk document.'
}
]
const result = await client.helpers.bulk({
datasource: operations,
onDocument() {
return {
index: { _index: DB_INDEX_3 }
}
}
// refreshOnCompletion: true
}) // setBulkBody(operations, pkgVersion)
t.ok(result, 'We should have been able to create bulk entries without error')
t.equal(result.total, 8, 'We should have been inserted eight records')
})
t.test('should be able to check bulk insert with msearch via helpers', async function (t) {
const m = client.helpers.msearch()
const searchA = await m.search({ index: DB_INDEX_3 }, { query: { match: { body: 'sixth' } } })
const searchB = await m.search(
{ index: DB_INDEX_3 },
{ query: { match: { body: 'uninstrumented' } } }
)
const resultsA = searchA?.body?.hits
const resultsB = searchB?.body?.hits
t.ok(resultsA, 'msearch should return a response for A')
t.ok(resultsB, 'msearch should return results for B')
// some versions of helper msearch seem not to return results for the first search.
// t.equal(resultsA?.hits?.length, 1, 'first search should return one result')
t.equal(resultsB?.hits?.length, 8, 'second search should return eight results')
})
})
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
)
}
})
}