-
Notifications
You must be signed in to change notification settings - Fork 422
refactor: Refactored dns instrumentation to subscribe to events emitted
#4224
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
Draft
bizob2828
wants to merge
2
commits into
newrelic:main
Choose a base branch
from
bizob2828:dns-migration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,16 @@ | ||
| /* | ||
| * Copyright 2026 New Relic Corporation. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| 'use strict' | ||
| module.exports = { | ||
| dns: [ | ||
| { | ||
| path: './dns/index', instrumentations: [] | ||
| }, | ||
| { | ||
| path: './dns/promises', instrumentations: [] | ||
| } | ||
| ] | ||
| } |
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,31 @@ | ||
| /* | ||
| * Copyright 2026 New Relic Corporation. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| 'use strict' | ||
| const RESOLVE_METHODS = [ | ||
| 'resolve', | ||
| 'resolve4', | ||
| 'resolve6', | ||
| 'resolveCname', | ||
| 'resolveMx', | ||
| 'resolveNaptr', | ||
| 'resolveNs', | ||
| 'resolvePtr', | ||
| 'resolveSrv', | ||
| 'resolveTxt', | ||
| // missing | ||
| // resolveAny | ||
| // resolveCaa | ||
| // resolveNaptr | ||
| // resolveSoa | ||
| // resolveTlsa | ||
| ] | ||
| const INSTRUMENTED_METHODS = [ | ||
| 'lookup', | ||
| 'reverse', | ||
| ...RESOLVE_METHODS | ||
| ] | ||
|
|
||
| module.exports = { INSTRUMENTED_METHODS, RESOLVE_METHODS } |
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,101 @@ | ||
| /* | ||
| * Copyright 2026 New Relic Corporation. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| 'use strict' | ||
| const { INSTRUMENTED_METHODS } = require('./constants') | ||
| // eslint-disable-next-line n/no-unsupported-features/node-builtins | ||
| const { tracingChannel } = require('node:diagnostics_channel') | ||
|
|
||
| class DnsSubscriber { | ||
| constructor({ agent, logger }) { | ||
| this.agent = agent | ||
| this.config = agent.config | ||
| this.logger = logger.child({ component: 'dns-subscriber' }) | ||
| this.prefix = 'nr:dns' | ||
| this.channels = this.buildChannels() | ||
| this.store = agent.tracer._contextManager._asyncLocalStorage | ||
| this.boundEnd = this.#end.bind(this) | ||
| this.boundAsyncEnd = this.#asyncEnd.bind(this) | ||
| } | ||
|
|
||
| buildChannels() { | ||
| return INSTRUMENTED_METHODS.map((method) => tracingChannel(`${this.prefix}:${method}`)) | ||
| } | ||
|
|
||
| get enabled() { | ||
| return this.config.instrumentation.dns.enabled === true | ||
| } | ||
|
|
||
| createSegment({ name, ctx }) { | ||
| const parent = ctx.segment | ||
|
|
||
| const segment = this.agent.tracer.createSegment({ | ||
| name, | ||
| parent, | ||
| transaction: ctx.transaction | ||
| }) | ||
|
|
||
| if (segment) { | ||
| segment.start() | ||
| return ctx.enterSegment({ segment }) | ||
| } | ||
| return ctx | ||
| } | ||
|
|
||
| enable() { | ||
| for (const channel of this.channels) { | ||
| channel.start.bindStore(this.store, (data) => { | ||
| const ctx = this.agent.tracer.getContext() | ||
| if (!ctx?.transaction?.isActive()) { | ||
| return ctx | ||
| } | ||
|
|
||
| return this.createSegment({ name: data.name, ctx }) | ||
| }) | ||
|
|
||
| channel.asyncStart.bindStore(this.store, (data) => { | ||
| const ctx = this.agent.tracer.getContext() | ||
| if (!ctx?.transaction?.isActive()) { | ||
| return ctx | ||
| } | ||
|
|
||
| const name = `Callback: ${data.callbackName}` | ||
| const newCtx = this.createSegment({ name, ctx }) | ||
| return newCtx | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #end() { | ||
| const ctx = this.agent.tracer.getContext() | ||
| ctx?.segment?.touch() | ||
| } | ||
|
|
||
| #asyncEnd(data) { | ||
| const ctx = this.agent.tracer.getContext() | ||
| ctx?.segment?.touch() | ||
| } | ||
|
|
||
| subscribe() { | ||
| for (const channel of this.channels) { | ||
| channel.subscribe({ end: this.boundEnd, asyncEnd: this.boundAsyncEnd }) | ||
| } | ||
| } | ||
|
|
||
| unsubscribe() { | ||
| for (const channel of this.channels) { | ||
| channel.unsubscribe({ end: this.boundEnd, asyncEnd: this.boundAsyncEnd }) | ||
| } | ||
| } | ||
|
|
||
| disable() { | ||
| for (const channel of this.channels) { | ||
| channel.start.unbindStore(this.store) | ||
| channel.asyncStart.unbindStore(this.store) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| module.exports = DnsSubscriber |
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,83 @@ | ||
| /* | ||
| * Copyright 2026 New Relic Corporation. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| 'use strict' | ||
| const { INSTRUMENTED_METHODS } = require('./constants') | ||
| // eslint-disable-next-line n/no-unsupported-features/node-builtins | ||
| const { tracingChannel } = require('node:diagnostics_channel') | ||
|
|
||
| class DnsPromisesSubscriber { | ||
| constructor({ agent, logger }) { | ||
| this.agent = agent | ||
| this.config = agent.config | ||
| this.logger = logger.child({ component: 'dns-promies-subscriber' }) | ||
| this.prefix = 'nr:dns:promises' | ||
| this.channels = this.buildChannels() | ||
| this.store = agent.tracer._contextManager._asyncLocalStorage | ||
| this.boundAsyncEnd = this.#asyncEnd.bind(this) | ||
| } | ||
|
|
||
| buildChannels() { | ||
| return INSTRUMENTED_METHODS.map((method) => tracingChannel(`${this.prefix}:${method}`)) | ||
| } | ||
|
|
||
| get enabled() { | ||
| return this.config.instrumentation.dns.enabled === true | ||
| } | ||
|
|
||
| createSegment({ name, ctx }) { | ||
| const parent = ctx.segment | ||
|
|
||
| const segment = this.agent.tracer.createSegment({ | ||
| name, | ||
| parent, | ||
| transaction: ctx.transaction | ||
| }) | ||
|
|
||
| if (segment) { | ||
| segment.start() | ||
| return ctx.enterSegment({ segment }) | ||
| } | ||
| return ctx | ||
| } | ||
|
|
||
| enable() { | ||
| for (const channel of this.channels) { | ||
| channel.start.bindStore(this.store, (data) => { | ||
| const ctx = this.agent.tracer.getContext() | ||
| if (!ctx?.transaction?.isActive()) { | ||
| return ctx | ||
| } | ||
|
|
||
| return this.createSegment({ name: data.name, ctx }) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #asyncEnd(data) { | ||
| const ctx = this.agent.tracer.getContext() | ||
| ctx?.segment?.touch() | ||
| } | ||
|
|
||
| subscribe() { | ||
| for (const channel of this.channels) { | ||
| channel.subscribe({ asyncEnd: this.boundAsyncEnd }) | ||
| } | ||
| } | ||
|
|
||
| unsubscribe() { | ||
| for (const channel of this.channels) { | ||
| channel.unsubscribe({ asyncEnd: this.boundAsyncEnd }) | ||
| } | ||
| } | ||
|
|
||
| disable() { | ||
| for (const channel of this.channels) { | ||
| channel.start.unbindStore(this.store) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| module.exports = DnsPromisesSubscriber |
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,80 @@ | ||
| /* | ||
| * Copyright 2026 New Relic Corporation. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| 'use strict' | ||
|
|
||
| /** | ||
| * Mock most methods so we can control the results. | ||
| * Wrap calling the callback in a `setImmediate` so it will | ||
| * properly emit both `end` and `asyncEnd` within the tracing channel | ||
| * @param {object} params to function | ||
| * @param {object} params.dns dns package | ||
| * @param {object} params.sandbox sinon sandbox | ||
| */ | ||
| module.exports = function mockDns({ dns, sandbox }) { | ||
| sandbox.stub(dns, 'reverse').callsFake((_addr, cb) => { | ||
| setImmediate(() => { | ||
| cb(null, ['localhost']) | ||
| }) | ||
| }) | ||
| sandbox.stub(dns.promises, 'reverse').callsFake(async() => Promise.resolve(['localhost'])) | ||
|
|
||
| sandbox.stub(dns, 'resolve').callsFake((_, cb) => { | ||
| setImmediate(() => { | ||
| cb(null, ['127.0.0.1']) | ||
| }) | ||
| }) | ||
| sandbox.stub(dns.Resolver.prototype, 'resolve').callsFake((_, cb) => { | ||
| setImmediate(() => { | ||
| cb(null, ['127.0.0.1']) | ||
| }) | ||
| }) | ||
| sandbox.stub(dns.promises, 'resolve').callsFake(async () => Promise.resolve(['127.0.0.1'])) | ||
| sandbox.stub(dns.promises.Resolver.prototype, 'resolve').callsFake(async () => Promise.resolve(['127.0.0.1'])) | ||
| sandbox.stub(dns, 'resolve4').callsFake((_, cb) => { | ||
| setImmediate(() => { | ||
| cb(null, ['127.0.0.1']) | ||
| }) | ||
| }) | ||
| sandbox.stub(dns.promises, 'resolve4').callsFake(async () => Promise.resolve(['127.0.0.1'])) | ||
| sandbox.stub(dns, 'resolve6').callsFake((_, cb) => { | ||
| setImmediate(() => { | ||
| cb(null, ['::1']) | ||
| }) | ||
| }) | ||
| sandbox.stub(dns.promises, 'resolve6').callsFake(async () => Promise.resolve(['::1'])) | ||
| const error = Error('boom') | ||
| error.code = 'ENODATA' | ||
| sandbox.stub(dns, 'resolveCname').callsFake((_, cb) => { | ||
| setImmediate(() => { | ||
| cb(error) | ||
| }) | ||
| }) | ||
| sandbox.stub(dns.promises, 'resolveCname').callsFake(async () => Promise.reject(error)) | ||
| sandbox.stub(dns, 'resolveMx').callsFake((_, cb) => { | ||
| setImmediate(() => { | ||
| cb(null, ['127.0.0.1']) | ||
| }) | ||
| }) | ||
| sandbox.stub(dns.promises, 'resolveMx').callsFake(async () => Promise.resolve(['127.0.0.1'])) | ||
| sandbox.stub(dns, 'resolveNs').callsFake((_, cb) => { | ||
| setImmediate(() => { | ||
| cb(null, ['a.iana-servers.net', 'b.iana-servers.net']) | ||
| }) | ||
| }) | ||
| sandbox.stub(dns.promises, 'resolveNs').callsFake(async () => Promise.resolve(['a.iana-servers.net', 'b.iana-servers.net'])) | ||
| sandbox.stub(dns, 'resolveTxt').callsFake((_, cb) => { | ||
| setImmediate(() => { | ||
| cb(null, ['one', 'two', 'three']) | ||
| }) | ||
| }) | ||
| sandbox.stub(dns.promises, 'resolveTxt').callsFake(async () => Promise.resolve(['one', 'two', 'three'])) | ||
| sandbox.stub(dns, 'resolveSrv').callsFake((_, cb) => { | ||
| setImmediate(() => { | ||
| cb(error) | ||
| }) | ||
| }) | ||
| sandbox.stub(dns.promises, 'resolveSrv').callsFake(async () => Promise.reject(error)) | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want?:
./subscribers/core/dns/config?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i guess we could because my next commit will have some sort of abstraction for core library instrumentation so i can live there as well