forked from newrelic/node-newrelic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: moved recorders to
lib/metrics/recorders
(newrelic#2666)
Co-authored-by: Bob Evans <[email protected]>
- Loading branch information
1 parent
bd4f7ff
commit d8dfe84
Showing
7 changed files
with
180 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
const metrics = require('../names') | ||
|
||
/** | ||
* 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) | ||
} | ||
} | ||
|
||
module.exports = recordOperationMetrics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
const { DB, ALL } = require('../names') | ||
const { DESTINATIONS } = require('../../config/attribute-filter') | ||
|
||
/** | ||
* @this ParsedStatement | ||
* @param {TraceSegment} segment - The segment being recorded. | ||
* @param {string} [scope] - The scope of the segment. | ||
*/ | ||
|
||
function recordQueryMetrics(segment, scope) { | ||
const duration = segment.getDurationInMillis() | ||
const exclusive = segment.getExclusiveDurationInMillis() | ||
const transaction = segment.transaction | ||
const type = transaction.isWeb() ? DB.WEB : DB.OTHER | ||
const thisTypeSlash = this.type + '/' | ||
const operation = DB.OPERATION + '/' + thisTypeSlash + this.operation | ||
|
||
// Note, an operation metric should _always_ be created even if the action was | ||
// a statement. This is part of the spec. | ||
|
||
// Rollups | ||
transaction.measure(operation, null, duration, exclusive) | ||
transaction.measure(DB.PREFIX + type, null, duration, exclusive) | ||
transaction.measure(DB.PREFIX + thisTypeSlash + type, null, duration, exclusive) | ||
transaction.measure(DB.PREFIX + thisTypeSlash + ALL, null, duration, exclusive) | ||
transaction.measure(DB.ALL, null, duration, exclusive) | ||
|
||
// If we can parse the SQL statement, create a 'statement' metric, and use it | ||
// as the scoped metric for transaction breakdowns. Otherwise, skip the | ||
// 'statement' metric and use the 'operation' metric as the scoped metric for | ||
// transaction breakdowns. | ||
let collection | ||
if (this.collection) { | ||
collection = DB.STATEMENT + '/' + thisTypeSlash + this.collection + '/' + this.operation | ||
transaction.measure(collection, null, duration, exclusive) | ||
if (scope) { | ||
transaction.measure(collection, scope, duration, exclusive) | ||
} | ||
} else if (scope) { | ||
transaction.measure(operation, scope, duration, exclusive) | ||
} | ||
|
||
// This recorder is side-effectful Because we are depending on the recorder | ||
// setting the transaction name, recorders must always be run before generating | ||
// the final transaction trace | ||
segment.name = collection || operation | ||
|
||
// Datastore instance metrics. | ||
const attributes = segment.attributes.get(DESTINATIONS.TRANS_SEGMENT) | ||
if (attributes.host && attributes.port_path_or_id) { | ||
const instanceName = | ||
DB.INSTANCE + '/' + thisTypeSlash + attributes.host + '/' + attributes.port_path_or_id | ||
transaction.measure(instanceName, null, duration, exclusive) | ||
} | ||
|
||
if (this.raw) { | ||
segment.transaction.agent.queries.add(segment, this.type.toLowerCase(), this.raw, this.trace) | ||
} | ||
} | ||
|
||
module.exports = recordQueryMetrics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
/** | ||
* Creates a recorder for middleware metrics. | ||
* | ||
* @private | ||
* @param {object} _shim instance of shim | ||
* @param {string} metricName name of metric | ||
* @returns {Function} recorder for middleware | ||
*/ | ||
function makeMiddlewareRecorder(_shim, metricName) { | ||
return function middlewareMetricRecorder(segment, scope) { | ||
const duration = segment.getDurationInMillis() | ||
const exclusive = segment.getExclusiveDurationInMillis() | ||
const transaction = segment.transaction | ||
|
||
if (scope) { | ||
transaction.measure(metricName, scope, duration, exclusive) | ||
} | ||
transaction.measure(metricName, null, duration, exclusive) | ||
} | ||
} | ||
|
||
module.exports = makeMiddlewareRecorder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.