-
Notifications
You must be signed in to change notification settings - Fork 384
/
datastore-shim.js
850 lines (777 loc) · 27.3 KB
/
datastore-shim.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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
'use strict'
const dbutil = require('../db/utils')
const hasOwnProperty = require('../util/properties').hasOwn
const logger = require('../logger').child({ component: 'DatastoreShim' })
const metrics = require('../metrics/names')
const parseSql = require('../db/query-parsers/sql')
const ParsedStatement = require('../db/parsed-statement')
const Shim = require('./shim')
const urltils = require('../util/urltils')
const util = require('util')
/**
* An enumeration of well-known datastores so that new instrumentations can use
* the same names we already use for first-party instrumentation.
*
* Each of these values is also exposed directly on the DatastoreShim class as
* static members.
*
* @readonly
* @memberof DatastoreShim
* @enum {string}
*/
const DATASTORE_NAMES = {
CASSANDRA: 'Cassandra',
DYNAMODB: 'DynamoDB',
ELASTICSEARCH: 'ElasticSearch',
MEMCACHED: 'Memcache',
MONGODB: 'MongoDB',
MYSQL: 'MySQL',
NEPTUNE: 'Neptune',
POSTGRES: 'Postgres',
REDIS: 'Redis',
PRISMA: 'Prisma'
}
/**
* Default value for unknown instance parameters.
*
* @readonly
* @private
*/
const INSTANCE_UNKNOWN = 'unknown'
const defaultParsers = {
SQL: parseSql
}
/**
* Pre-defined query parsers for well-known languages.
*
* Each of these values is also exposed directly on the DatastoreShim class as
* static members.
*
* @readonly
* @memberof DatastoreShim
* @enum {string}
*/
const QUERY_PARSERS = {
SQL_PARSER: 'SQL'
}
/**
* Constructs a shim associated with the given agent instance, specialized for
* instrumenting datastores.
*
* @class
* @augments Shim
* @classdesc A helper class for wrapping datastore modules.
* @param {Agent} agent The agent this shim will use.
* @param {string} moduleName The name of the module being instrumented.
* @param {string} resolvedName The full path to the loaded module.
* @param {string} shimName Used to persist shim ids across different shim instances.
* @param {string} pkgVersion version of module
* @see Shim
* @see DatastoreShim.DATASTORE_NAMES
*/
function DatastoreShim(agent, moduleName, resolvedName, shimName, pkgVersion) {
Shim.call(this, agent, moduleName, resolvedName, shimName, pkgVersion)
this._logger = logger.child({ module: moduleName })
this.queryParser = defaultParsers[this.SQL_PARSER]
}
module.exports = DatastoreShim
util.inherits(DatastoreShim, Shim)
// Add constants on the shim for the well-known datastores.
DatastoreShim.DATASTORE_NAMES = DATASTORE_NAMES
Object.keys(DATASTORE_NAMES).forEach(function defineDatastoreMetricEnum(dsName) {
Shim.defineProperty(DatastoreShim, dsName, DATASTORE_NAMES[dsName])
Shim.defineProperty(DatastoreShim.prototype, dsName, DATASTORE_NAMES[dsName])
})
// Add constants on the shim for the provided query parsers.
DatastoreShim.QUERY_PARSERS = QUERY_PARSERS
Object.keys(QUERY_PARSERS).forEach(function defineQueryParserEnum(qpName) {
Shim.defineProperty(DatastoreShim, qpName, QUERY_PARSERS[qpName])
Shim.defineProperty(DatastoreShim.prototype, qpName, QUERY_PARSERS[qpName])
})
DatastoreShim.prototype.setDatastore = setDatastore
DatastoreShim.prototype.recordOperation = recordOperation
DatastoreShim.prototype.recordQuery = recordQuery
DatastoreShim.prototype.recordBatchQuery = recordBatchQuery
DatastoreShim.prototype.parseQuery = parseQuery
DatastoreShim.prototype.setParser = setParser
DatastoreShim.prototype.bindRowCallbackSegment = bindRowCallbackSegment
DatastoreShim.prototype.captureInstanceAttributes = captureInstanceAttributes
DatastoreShim.prototype.getDatabaseNameFromUseQuery = getDatabaseNameFromUseQuery
// -------------------------------------------------------------------------- //
/**
* @callback QuerySpecFunction
* @summary
* Used for determining information about a query when it can not be simply
* found in the arguments.
* @param {Shim} shim
* The shim this function was passed to.
* @param {Function} func
* The function being recorded.
* @param {string} name
* The name of the function.
* @param {Array.<*>} args
* The arguments being passed into the function.
* @returns {QuerySpec} The spec for how this query should be recorded.
* @see DatastoreShim#recordQuery
* @see DatastoreShim#recordBatchQuery
* @see QuerySpec
*/
/**
* @callback QueryFunction
* @summary
* Pulls the query argument out from an array of arguments.
* @param {Shim} shim
* The shim this function was passed to.
* @param {Function} func
* The function being recorded.
* @param {string} name
* The name of the function.
* @param {Array.<*>} args
* The arguments being passed into the function.
* @returns {string} The query string from the arguments list.
* @see QuerySpec
* @see QuerySpecFunction
*/
/**
* @callback QueryParserFunction
* @summary
* Used to parse queries to extract the basic information about it.
* @param {string} query - The query to be parsed.
* @returns {ParsedQueryData} An object containing the basic information about
* the query.
* @see DatastoreShim#setParser
* @see ParsedQueryData
*/
/**
* @interface OperationSpec
* @description
* Describes the interface for an operation function.
* @property {string} [name]
* The name for this operation. If omitted, the operation function's name will
* used instead.
* @property {DatastoreParameters} [parameters]
* Extra parameters to be set on the metric for the operation.
* @property {boolean} [record=true]
* Indicates if the operation should be recorded as a metric. A segment will be
* created even if this is `false`.
* @property {number|CallbackBindFunction} [callback]
* If a number, it is the offset in the arguments array for the operation's
* callback argument. If it is a function, it should perform the segment
* binding to the callback.
* @property {boolean} [promise=false]
* If `true`, the return value will be wrapped as a Promise.
* @see DatastoreShim#recordOperation
* @see QuerySpec
* @see DatastoreParameters
*/
/**
* @interface QuerySpec
* @augments OperationSpec
* @description
* Describes the interface for a query function. Extends {@link OperationSpec}
* with query-specific parameters.
* @property {boolean} [stream=false]
* If `true`, the return value will be wrapped as a stream.
* @property {number|string|QueryFunction} query
* If a number, it is the offset in the arguments array for the query string
* argument. If a string, it is the query being executed. If a function, it
* will be passed the arguments and must return the query string.
* @see DatastoreShim#recordQuery
* @see DatastoreShim#recordBatchQuery
* @see QuerySpecFunction
* @see QueryFunction
* @see OperationSpec
* @see DatastoreParameters
*/
/**
* @interface DatastoreParameters
* @description
* Extra parameters which may be added to an operation or query segment. All of
* these properties are optional.
* @property {string} host
* The host of the database server being interacted with. If provided, along
* with `port_path_or_id`, then an instance metric will also be generated for
* this database.
* @property {number|string} port_path_or_id
* The port number or path to domain socket used to connect to the database
* server.
* @property {string} database_name
* The name of the database being queried or operated on.
* @see OperationSpec
* @see QuerySpec
*/
/**
* @interface ParsedQueryData
* @description
* Returned by a {@link QueryParserFunction}, this information is used to
* generate the name for recording datastore queries.
* @property {string} operation
* The datastore operation such as `SELECT` or `UPDATE`.
* @property {string} collection
* The collection being queried. This would be the table name from a SQL
* statement or the collection name in a MongoDB query.
* @property {string} [query]
* The query with any sensitive information redacted and comments removed.
* @see DatastoreShim#setParser
* @see QueryParserFunction
*/
// -------------------------------------------------------------------------- //
/**
* Sets the vendor the module implements.
*
* This is used to determine the names for metrics and segments. If a string is
* passed, metric names will be generated using that name.
*
* This method *MUST* be called to use any methods that generate
* segments or metrics.
*
* @memberof DatastoreShim.prototype
* @param {string} datastore
* The name of this datastore. Use one of the well-known constants listed in
* {@link DatastoreShim.DATASTORE_NAMES} if available for the datastore.
* @see DatastoreShim.DATASTORE_NAMES
* @see DatastoreShim#recordBatchQuery
* @see DatastoreShim#recordQuery
* @see DatastoreShim#recordOperation
* @see DatastoreShim#parseQuery
*/
function setDatastore(datastore) {
this._metrics = {
PREFIX: datastore,
STATEMENT: metrics.DB.STATEMENT + '/' + datastore + '/',
OPERATION: metrics.DB.OPERATION + '/' + datastore + '/',
INSTANCE: metrics.DB.INSTANCE + '/' + datastore + '/',
ALL: metrics.DB.PREFIX + datastore + '/' + metrics.ALL
}
this._datastore = datastore
this._logger = this._logger.child({ datastore: this._metrics.PREFIX })
this.logger.trace({ metrics: this._metrics }, 'Datastore metric names set')
}
/**
* Sets the query parser used by this shim instance.
*
* @memberof DatastoreShim.prototype
* @param {string|QueryParserFunction} parser
* The string used to look up a default parser or the function used to parse
* queries. It is recommended that you use one of the well-known constants if
* available in the {@link DatastoreShim.QUERY_PARSERS}.
* @see DatastoreShim.QUERY_PARSERS
* @see QueryParserFunction
* @see ParsedQueryData
*/
function setParser(parser) {
if (this.isString(parser)) {
const newParser = defaultParsers[parser]
if (newParser) {
this.queryParser = newParser
} else {
this.logger.debug(
'Attempted to set the query parser to invalid parser %s, not setting new parser',
parser
)
}
} else if (this.isFunction(parser)) {
this.queryParser = parser
} else {
this.logger.trace('Received invalid parser (%s)', parser)
}
}
/**
* Wraps the given properties as datastore operations that should be recorded.
*
* - `recordOperation(nodule, properties, opSpec)`
* - `recordOperation(func, opSpec)`
*
* The resulting wrapped methods will record their actions using the datastore
* `OPERATION` metric.
*
* NOTE: Calling this method before {@link DatastoreShim#setDatastore}
* will result in an exception.
*
* @memberof DatastoreShim.prototype
* @param {object | Function} nodule
* The source for the properties to wrap, or a single function to wrap.
* @param {string|Array.<string>} [properties]
* One or more properties to wrap. If omitted, the `nodule` parameter is
* assumed to be the function to wrap.
* @param {OperationSpec|SegmentFunction} opSpec
* The spec for this operation function.
* @returns {object | Function} The first parameter to this function, after
* wrapping it or its properties.
* @see Shim#wrap
* @see Shim#record
* @see OperationSpec
* @see SegmentFunction
*/
function recordOperation(nodule, properties, opSpec) {
if (this.isObject(properties) && !this.isArray(properties)) {
// operation(func, opSpec)
opSpec = properties
properties = null
}
if (!opSpec) {
opSpec = Object.create(null)
}
return this.record(nodule, properties, function opRecorder(shim, fn, fnName, args) {
shim.logger.trace('Recording datastore operation "%s"', fnName)
// Derive the segment information, starting from some defaults
let segDesc = null
if (shim.isFunction(opSpec)) {
segDesc = opSpec.call(this, shim, fn, fnName, args)
} else {
segDesc = {
name: fnName || 'other',
opaque: false,
after: null,
promise: null,
...opSpec
}
}
if (hasOwnProperty(segDesc, 'parameters')) {
_normalizeParameters.call(shim, segDesc.parameters)
}
// Adjust the segment name with the metric prefix and add a recorder.
if (!hasOwnProperty(segDesc, 'record') || segDesc.record !== false) {
segDesc.name = shim._metrics.OPERATION + segDesc.name
segDesc.recorder = _recordOperationMetrics.bind(shim)
segDesc.internal = true
}
// And done.
return segDesc
})
}
/**
* Wraps the given properties as datastore query that should be recorded.
*
* - `recordQuery(nodule, properties, querySpec)`
* - `recordQuery(func, querySpec)`
*
* The resulting wrapped methods will record their actions using the datastore
* `STATEMENT` metric.
*
* NOTE: Calling this method before {@link DatastoreShim#setDatastore}
* will result in an exception.
*
* @memberof DatastoreShim.prototype
* @param {object | Function} nodule
* The source for the properties to wrap, or a single function to wrap.
* @param {string|Array.<string>} [properties]
* One or more properties to wrap. If omitted, the `nodule` parameter is
* assumed to be the function to wrap.
* @param {QuerySpec|QuerySpecFunction} querySpec
* The spec for this query function.
* @returns {object | Function} The first parameter to this function, after
* wrapping it or its properties.
* @see Shim#wrap
* @see Shim#record
* @see DatastoreShim#recordBatchQuery
* @see QuerySpec
* @see QuerySpecFunction
*/
function recordQuery(nodule, properties, querySpec) {
return _recordQuery.call(this, '', nodule, properties, querySpec)
}
/**
* Just like {@link DatastoreShim#recordQuery}, but with a `batch` suffix for
* the recorded metric.
*
* - `recordBatchQuery(nodule, properties, querySpec)`
* - `recordBatchQuery(func, querySpec)`
*
* The resulting wrapped methods will record their actions using the datastore
* `STATEMENT` metric with a `/batch` suffix.
*
* NOTE: Calling this method before {@link DatastoreShim#setDatastore}
* will result in an exception.
*
* @memberof DatastoreShim.prototype
* @param {object | Function} nodule
* The source for the properties to wrap, or a single function to wrap.
* @param {string|Array.<string>} [properties]
* One or more properties to wrap. If omitted, the `nodule` parameter is
* assumed to be the function to wrap.
* @param {QuerySpec|QuerySpecFunction} querySpec
* The spec for this query function.
* @returns {object | Function} The first parameter to this function, after
* wrapping it or its properties.
* @see Shim#wrap
* @see Shim#record
* @see DatastoreShim#recordQuery
* @see QuerySpec
* @see QuerySpecFunction
*/
function recordBatchQuery(nodule, properties, querySpec) {
return _recordQuery.call(this, '/batch', nodule, properties, querySpec)
}
/**
* Parses the given query to extract information for any metrics that will be
* created.
*
* NOTE: Calling this method before {@link DatastoreShim#setDatastore}
* will result in an exception.
*
* @memberof DatastoreShim.prototype
* @param {string} query - The query to parse.
* @param {object} nodule - Context for the queryParse to run under.
* @returns {ParsedStatement} The parsed query object.
* @see DatastoreShim#setParser
*/
function parseQuery(query, nodule) {
const parsed = this.queryParser.call(nodule, query)
let collection = parsed.collection
// strip enclosing special characters from collection (table) name
if (typeof collection === 'string' && collection.length > 2) {
if (/^[\[{'"`]/.test(collection)) {
collection = collection.substr(1)
}
if (/[\]}'"`]$/.test(collection)) {
collection = collection.substr(0, collection.length - 1)
}
}
const queryRecorded =
this.agent.config.transaction_tracer.record_sql === 'raw' ||
this.agent.config.transaction_tracer.record_sql === 'obfuscated'
return new ParsedStatement(
this._metrics.PREFIX,
parsed.operation,
collection,
queryRecorded ? parsed.query : null
)
}
/**
* Wraps the callback in an arguments array with one that is bound to a segment.
*
* - `bindRowCallbackSegment(args, cbIdx [, parentSegment])`
*
* @memberof DatastoreShim.prototype
* @param {Array} args
* The arguments array to replace the callback in.
* @param {number} cbIdx
* The index of the callback in the arguments array.
* @param {TraceSegment} [parentSegment]
* Optional. The segment to be the parent row callback's segment. Defaults to
* the segment active when the row callback is first called.
*/
function bindRowCallbackSegment(args, cbIdx, parentSegment) {
const idx = this.normalizeIndex(args.length, cbIdx)
if (idx === null) {
this.logger.debug('Not binding row callback, invalid cbIdx %s', cbIdx)
return
}
// Pull out the callback and make sure it is a function.
const cb = args[idx]
if (!this.isFunction(cb)) {
this.logger.debug('Argument %d is not a function, not binding row callback', cbIdx)
return cb
}
this.logger.trace('Wrapping argument %d as a row callback.', cbIdx)
// We have a little state to maintain through potentially multiple calls.
let callCounter = 0
let segment = null
const segmentName = 'Callback: ' + this.getName(cb)
const shim = this
const wrapper = this.bindSegment(function rowCallbackWrapper() {
// The first time this row callback is fired we want to touch the parent
// segment and create the callback segment.
if (++callCounter === 1) {
const realParent = parentSegment || shim.getSegment()
realParent && realParent.touch()
segment = shim.createSegment(segmentName, realParent)
if (segment) {
segment.async = false
}
}
// Update the segment name and run the actual callback.
if (segment) {
segment.addAttribute('count', callCounter)
}
return shim.applySegment(cb, segment, true, this, arguments)
}, parentSegment)
shim.assignOriginal(wrapper, cb, true)
args[idx] = wrapper
}
/**
* Normalizes and adds datastore instance attributes to the current segment.
*
* If the current segment was not created by this shim then no action is taken.
*
* @memberof DatastoreShim.prototype
* @param {string} host - The name of the database host.
* @param {number|string} port - The port, path, or ID of the database server.
* @param {string} database - The name of the database in use.
*/
function captureInstanceAttributes(host, port, database) {
// See if we are currently in a segment created by us.
const segment = this.getSegment()
if (!segment || segment.shim !== this) {
this.logger.trace(
'Not adding db instance metric attributes to segment %j',
segment && segment.name
)
return
}
this.logger.trace('Adding db instance attributes to segment %j', segment.name)
// Normalize the instance attributes.
const attributes = _normalizeParameters.call(this, {
host,
port_path_or_id: port,
database_name: database
})
for (const key in attributes) {
if (attributes[key]) {
segment.addAttribute(key, attributes[key])
}
}
}
/**
* Parses the database name from a `USE` SQL query.
*
* @memberof DatastoreShim.prototype
* @param {string} query - The SQL query to parse the database name from.
* @returns {?string} The name of the database if it could be parsed, otherwise
* `null`.
*/
function getDatabaseNameFromUseQuery(query) {
return dbutil.extractDatabaseChangeFromUse(query)
}
// -------------------------------------------------------------------------- //
/**
* Wraps the given properties as datastore query that should be recorded.
*
* - `_recordQuery(suffix, nodule, properties, querySpec)`
* - `_recordQuery(suffix, func, querySpec)`
*
* The resulting wrapped methods will record their actions using the datastore
* `STATEMENT` metric.
*
* @private
* @this DatastoreShim
* @param {string} suffix
* Suffix to be added to the segment name.
* @param {object | Function} nodule
* The source for the properties to wrap, or a single function to wrap.
* @param {string|Array.<string>} [properties]
* One or more properties to wrap. If omitted, the `nodule` parameter is
* assumed to be the function to wrap.
* @param {QuerySpec|QueryFunction} querySpec
* The spec for this query function.
* @returns {object | Function} The first parameter to this function, after
* wrapping it or its properties.
* @see Shim#wrap
* @see Shim#record
* @see DatastoreShim#recordQuery
* @see DatastoreShim#recordBatchQuery
* @see QuerySpec
* @see QuerySpecFunction
*/
function _recordQuery(suffix, nodule, properties, querySpec) {
if (this.isObject(properties) && !this.isArray(properties)) {
// _recordQuery(suffix, func, querySpec)
querySpec = properties
properties = null
}
if (!querySpec) {
this.logger.debug('Missing query spec for recordQuery, not wrapping.')
return nodule
}
return this.record(nodule, properties, function queryRecord(shim, fn, fnName, args) {
shim.logger.trace('Determining query information for %j', fnName)
let queryDesc = querySpec
if (shim.isFunction(querySpec)) {
queryDesc = querySpec.call(this, shim, fn, fnName, args) || Object.create(null)
}
// Set some default values, in case they're missing.
queryDesc = {
name: fnName,
callback: null,
rowCallback: null,
stream: null,
after: null,
promise: null,
opaque: false,
inContext: null,
...queryDesc
}
const parameters = _normalizeParameters.call(shim, queryDesc.parameters || Object.create(null))
// If we're not actually recording this, then just return the segment
// descriptor now.
if (queryDesc?.record === false) {
return {
internal: false,
...queryDesc,
parameters
}
}
// Fetch the query string.
const queryStr = _extractQueryStr.call(shim, fn, fnName, queryDesc, this, args)
if (!shim.isString(queryStr)) {
return null
}
// Parse the query and assemble the name.
const parsed = shim.parseQuery(queryStr, this)
const name = (parsed.collection || 'other') + '/' + parsed.operation + suffix
shim.logger.trace('Found and parsed query %s -> %s', parsed.type, name)
// Return the segment descriptor.
return {
internal: true,
...queryDesc,
// This name and parameters might override those in the original
// queryDesc.
name: shim._metrics.STATEMENT + name,
parameters,
recorder: function queryRecorder(segment, scope) {
if (segment) {
parsed.recordMetrics(segment, scope)
}
}
}
})
}
/**
* Records all the metrics required for database operations.
*
* - `_recordOperationMetrics(segment [, scope])`
*
* @private
* @this DatastoreShim
* @implements {MetricFunction}
* @param {TraceSegment} segment - The segment being recorded.
* @param {string} [scope] - The scope of the segment.
* @see DatastoreShim#recordOperation
* @see MetricFunction
*/
function _recordOperationMetrics(segment, scope) {
if (!segment) {
return
}
const duration = segment.getDurationInMillis()
const exclusive = segment.getExclusiveDurationInMillis()
const transaction = segment.transaction
const type = transaction.isWeb() ? 'allWeb' : 'allOther'
const operation = segment.name
if (scope) {
transaction.measure(operation, scope, duration, exclusive)
}
transaction.measure(operation, null, duration, exclusive)
transaction.measure(metrics.DB.PREFIX + type, null, duration, exclusive)
transaction.measure(metrics.DB.ALL, null, duration, exclusive)
transaction.measure(this._metrics.ALL, null, duration, exclusive)
transaction.measure(
metrics.DB.PREFIX + this._metrics.PREFIX + '/' + type,
null,
duration,
exclusive
)
const attributes = segment.getAttributes()
if (attributes.host && attributes.port_path_or_id) {
const instanceName = [
metrics.DB.INSTANCE,
this._metrics.PREFIX,
attributes.host,
attributes.port_path_or_id
].join('/')
transaction.measure(instanceName, null, duration, exclusive)
}
}
/**
* Extracts the query string from the arguments according to the given spec.
*
* - `_extractQueryStr(fn, fnName, spec, ctx, args)`
*
* @private
* @this DatastoreShim
* @param {Function} fn - The query function to be executed.
* @param {string} fnName - The name of the query function.
* @param {QuerySpec} spec - The query spec.
* @param {*} ctx - The context of the query function's execution.
* @param {Array} args - The arguments for the query function.
* @returns {?string} The query from the arguments if found, otherwise `null`.
*/
function _extractQueryStr(fn, fnName, spec, ctx, args) {
let queryStr = spec.query
if (this.isNumber(queryStr)) {
const queryIdx = this.normalizeIndex(args.length, queryStr)
if (queryIdx === null) {
this.logger.debug('Invalid query index %d of %d', queryStr, args.length)
return null
}
queryStr = args[queryIdx]
} else if (this.isFunction(queryStr)) {
queryStr = queryStr.call(ctx, this, fn, fnName, args)
}
return queryStr
}
/**
* Normalizes segment parameter values.
*
* - `_normalizeParameters([parameters])`
*
* Removes disabled parameters and corrects other values, such as changing host
* from `localhost` to the actual host name.
*
* @private
* @this DatastoreShim
* @param {object} [parameters={}] - The segment parameters to clean up.
* @returns {object} - The normalized segment parameters.
*/
function _normalizeParameters(parameters) {
parameters = parameters || Object.create(null)
const config = this.agent.config
const dsTracerConf = config.datastore_tracer
parameters.product = parameters.product || this._datastore
_normalizeDatabaseName(parameters, dsTracerConf)
_normalizeInstanceInformation(parameters, dsTracerConf, config)
return parameters
}
/**
* Normalizes the database name from segment parameter values.
*
* @private
* @param {object} parameters - The segment parameters to clean up.
* @param {object} dsTracerConf - The datastore tracer configuration
*/
function _normalizeDatabaseName(parameters, dsTracerConf) {
// Add database name if provided and enabled.
if (!dsTracerConf.database_name_reporting.enabled) {
delete parameters.database_name
} else if (hasOwnProperty(parameters, 'database_name') && parameters.database_name !== false) {
parameters.database_name =
typeof parameters.database_name === 'number'
? String(parameters.database_name)
: parameters.database_name || INSTANCE_UNKNOWN
}
}
/**
* Normalizes the database instance information from segment parameter
* values: host and the port/path/id.
*
* @private
* @param {object} parameters - The segment parameters to clean up.
* @param {object} dsTracerConf - The datastore tracer configuration
* @param {object} config - The agent configuration
*/
function _normalizeInstanceInformation(parameters, dsTracerConf, config) {
// Add instance information if enabled.
if (!dsTracerConf.instance_reporting.enabled) {
delete parameters.host
delete parameters.port_path_or_id
} else {
// Determine appropriate defaults for host and port.
if (hasOwnProperty(parameters, 'port_path_or_id')) {
parameters.port_path_or_id = String(parameters.port_path_or_id || INSTANCE_UNKNOWN)
}
if (hasOwnProperty(parameters, 'host')) {
if (parameters.host && urltils.isLocalhost(parameters.host)) {
parameters.host = config.getHostnameSafe(parameters.host)
}
// Config's default name of a host is `UNKNOWN_BOX`.
if (!parameters.host || parameters.host === 'UNKNOWN_BOX') {
parameters.host = INSTANCE_UNKNOWN
}
}
}
}