-
Notifications
You must be signed in to change notification settings - Fork 419
chore: Record subscriber usage metric #3626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
481b730
chore: Record subscriber usage metric
jsumners-nr 1beea6b
fixes
jsumners-nr 357caef
cover app root case
jsumners-nr b56edaa
address feedback
jsumners-nr 0a3c636
fix test
jsumners-nr 48a1c71
ensure metrics are recorded only once
jsumners-nr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -22,3 +22,6 @@ nr-security-home | |
| # benchmark results | ||
| benchmark_results | ||
| bin/.env | ||
|
|
||
| # Local developer resources: | ||
| local/ | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,39 @@ | ||
| /* | ||
| * Copyright 2025 New Relic Corporation. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| 'use strict' | ||
|
|
||
| module.exports = recordSupportabilityMetric | ||
|
|
||
| const semver = require('semver') | ||
| const { | ||
| FEATURES: { | ||
| INSTRUMENTATION: { SUBSCRIBER_USED } | ||
| } | ||
| } = require('#agentlib/metrics/names.js') | ||
|
|
||
| function recordSupportabilityMetric({ | ||
| agent, | ||
| moduleName, | ||
| moduleVersion = 'unknown' | ||
| } = {}) { | ||
| const major = moduleVersion === 'unknown' | ||
| ? semver.major(process.version) | ||
| : semver.major(moduleVersion) | ||
|
|
||
| let metric = agent.metrics.getOrCreateMetric( | ||
| `${SUBSCRIBER_USED}/${moduleName}/${major}` | ||
| ) | ||
| if (metric.callCount === 0) { | ||
| metric.incrementCallCount() | ||
| } | ||
|
|
||
| metric = agent.metrics.getOrCreateMetric( | ||
| `${SUBSCRIBER_USED}/${moduleName}` | ||
| ) | ||
| if (metric.callCount === 0) { | ||
| metric.incrementCallCount() | ||
| } | ||
| } |
This file contains hidden or 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,84 @@ | ||
| /* | ||
| * Copyright 2025 New Relic Corporation. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| 'use strict' | ||
|
|
||
| const path = require('node:path') | ||
| const defaultLogger = require('#agentlib/logger.js').child({ | ||
| component: 'resolve-module-version' | ||
| }) | ||
|
|
||
| const dcFuncFrame = /^\s*at Channel\.publish/ | ||
| const modPathReg = /at .+ \((.+):\d+:\d+\)/ | ||
|
|
||
| module.exports = resolveModuleVersion | ||
|
|
||
| /** | ||
| * Given a module name, attempt to read the version string from its | ||
| * associated package manifest. If the module is a built-in, or one that has | ||
| * been bundled with Node.js (e.g. `undici`), a package manifest will not be | ||
| * available. In this case, the string "unknown" will be returned. | ||
| * | ||
| * This version resolver assumes that it will be invoked through our | ||
| * diagnostics channel subscriber instrumentations. That is, it expects the | ||
| * call tree to be similar to: | ||
| * | ||
| * 1. some-module.function() | ||
| * 2. diagnostics_channel.publish() | ||
| * 3. subscriber.handler() | ||
| * | ||
| * @param {string} moduleSpecifier What would be passed to `resolve()`. | ||
| * @param {object} [deps] Optional dependencies. | ||
| * @param {object} [deps.logger] Agent logger instance. | ||
| * | ||
| * @returns {string} The version string from the package manifest or "unknown". | ||
| */ | ||
| function resolveModuleVersion(moduleSpecifier, { logger = defaultLogger } = {}) { | ||
| let pkgPath | ||
| // We'd prefer to use `require.resolve(moduleSpecifier)` here, but it gets | ||
| // a bit confused when there are non-standard module directories in play. | ||
| // Once we are able to refactor our "on require" metric recording to | ||
| // utilize `module.registerHooks`, we should be able to eliminate this | ||
| // slow algorithm. | ||
| const err = Error() | ||
| const stack = err.stack.split('\n') | ||
| do { | ||
| stack.shift() | ||
| } while (dcFuncFrame.test(stack[0]) === false && stack.length > 0) | ||
| const matches = modPathReg.exec(stack[1]) | ||
| pkgPath = matches?.[1] | ||
|
|
||
| if (!pkgPath) { | ||
| logger.warn( | ||
| { moduleSpecifier }, | ||
| 'Could not resolve module path. Possibly a built-in or Node.js bundled module.' | ||
| ) | ||
| return 'unknown' | ||
| } | ||
|
|
||
| const cwd = process.cwd() | ||
| let reachedCwd = false | ||
| let pkg | ||
| let base = path.dirname(pkgPath) | ||
| do { | ||
| try { | ||
| pkgPath = path.join(base, 'package.json') | ||
| pkg = require(pkgPath) | ||
| } catch { | ||
| base = path.resolve(path.join(base, '..')) | ||
| if (base === cwd) { | ||
| reachedCwd = true | ||
| } else if (reachedCwd === true) { | ||
| // We reached the supposed app root, attempted to load a manifest | ||
| // file in that location, and still couldn't find one. So we give up. | ||
| pkg = {} | ||
| } | ||
| } | ||
| } while (!pkg) | ||
|
|
||
| const version = pkg.version ?? 'unknown' | ||
| logger.trace({ moduleSpecifier, version }, 'Resolved package version.') | ||
| return version | ||
| } |
This file contains hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.