-
Notifications
You must be signed in to change notification settings - Fork 421
refactor: Break samplers into classes and store them on agent.sampler.*
#3527
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
4 commits
Select commit
Hold shift + click to select a range
b070d13
Break samplers into classes; introduce the concept of `agent.sampler.*`
amychisholm03 e5c99f1
`determineSampler` unit tests
amychisholm03 175691c
remove `Transaction._calculatePriority` as the name is misleading now
amychisholm03 7d0d13b
Clarify README
amychisholm03 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| # Sampling at New Relic | ||
|
|
||
| The New Relic agent supports a robust sampling decision making interface. This is a work-in-progress feature. | ||
|
|
||
| ## Config | ||
|
|
||
| Customers configure how they would like their transactions to be sampled under our `distributed_tracing` section in our config. Remember, sampling will only apply if a customer has `distributed_tracing.enabled` set to `true`. | ||
|
|
||
| ### Types | ||
|
|
||
| - A "sampler mode" refers to the following config sections: `distributed_tracing.sampler`, `distributed_tracing.sampler.full_granularity`, and `distributed_tracing.sampler.partial_granularity`. They are defined by the three sections: `root`, `remote_parent_sampled`, and `remote_parent_not_sampled`. | ||
| - A "sampler section" refers to `root`, `remote_parent_sampled`, or `remote_parent_not_sampled` within a particular sampler mode. The config defined at this section, i.e. `SAMPLER_TYPE: SAMPLER_SUBOPTION?`, describes the sampling decision for that particular scenario within that mode. | ||
| - `root`: This is the main sampler for traces originating from the current service. | ||
| - `remote_parent_sampled`: The sampler for when the upstream service has sampled the trace. | ||
| - `remote_parent_not_sampled`: The sampler for when the upstream service has not sampled the trace. | ||
|
|
||
| NOTE: `distributed_tracing.sampler` only exists for backward compatiability and may be deprecated in favor of `distributed_tracing.sampler.full_granularity`. For now, `full_granularity` will take precedence over the old path. | ||
|
|
||
| ### Full Config in Accordance to Spec | ||
|
|
||
| ```yaml | ||
| ... | ||
| (Serverless DT attributes; they may be defined under distributed_tracing instead) | ||
| account_id: string (unset by default, only set when using Serverless Mode) | ||
| trusted_account_key: string (unset by default, only set when using Serverless Mode) | ||
| primary_application_id: string (unset by default, only set when using Serverless Mode) | ||
| ... | ||
| distributed_tracing: | ||
| enabled: boolean (default true) | ||
| exclude_newrelic_header: boolean (default false) | ||
| enable_success_metrics (OPTIONAL): boolean (default true, set to false to disable supportability metrics) | ||
| sampler: (section for sampling config options for different scenarios) | ||
| adaptive_sampling_target (see note on Sampling Target below) | ||
| root: (when the trace originates from the current service) | ||
| ${SAMPLER_TYPE} (See `Sampler Options` below) | ||
| ${SAMPLER_SUBOPTION} | ||
| remote_parent_sampled: (when the upstream service has sampled the trace) | ||
| ${SAMPLER_TYPE} (See `Sampler Options` below) | ||
| ${SAMPLER_SUBOPTION} | ||
| remote_parent_not_sampled: (when the upstream service has not sampled the trace) | ||
| ${SAMPLER_TYPE} | ||
| ${SAMPLER_SUBOPTION} | ||
| full_granularity: | ||
| enabled | ||
| root: (when the trace originates from the current service) | ||
| ${SAMPLER_TYPE} (See `Sampler Options` below) | ||
| ${SAMPLER_SUBOPTION} | ||
| remote_parent_sampled: (when the upstream service has sampled the trace) | ||
| ${SAMPLER_TYPE} (See `Sampler Options` below) | ||
| ${SAMPLER_SUBOPTION} | ||
| remote_parent_not_sampled: (when the upstream service has not sampled the trace) | ||
| ${SAMPLER_TYPE} | ||
| ${SAMPLER_SUBOPTION} | ||
| partial_granularity: | ||
| enabled | ||
| type ("reduced", "essential", "compact") | ||
| root: (when the trace originates from the current service) | ||
| ${SAMPLER_TYPE} (See `Sampler Options` below) | ||
| ${SAMPLER_SUBOPTION} | ||
| remote_parent_sampled: (when the upstream service has sampled the trace) | ||
| ${SAMPLER_TYPE} (See `Sampler Options` below) | ||
| ${SAMPLER_SUBOPTION} | ||
| remote_parent_not_sampled: (when the upstream service has not sampled the trace) | ||
| ${SAMPLER_TYPE} | ||
| ${SAMPLER_SUBOPTION} | ||
| ... | ||
| ``` | ||
|
|
||
| ## Solution | ||
|
|
||
| There are three sampler modes, each with three sampler sections, resulting in potentially nine different sampling decisions that the agent would have to support. We create a new `Sampler` instance (`AdaptiveSampler`, `AlwaysOnSampler`, `AlwaysOffSampler`, or `TraceIdRatioBasedSampler`, defined in this folder) for each of these sampler modes' sections. | ||
|
|
||
| `agent.sampler` would be defined as: | ||
|
|
||
| * `agent.sampler.fullGranularity.root` | ||
| * `agent.sampler.fullGranularity.remoteParentSampled` | ||
| * `agent.sampler.fullGranularity.remoteParentNotSampled` | ||
| * `agent.sampler.partialGranularity.root` | ||
| * `agent.sampler.partialGranularity.remoteParentSampled` | ||
| * `agent.sampler.partialGranularity.remoteParentNotSampled` | ||
|
|
||
| These fields currently exist (before core tracing was implemented); `agent.sampler.fullGranularity.*` will take precedence over these fields: | ||
|
|
||
| * `agent.sampler.root` | ||
| * `agent.sampler.remoteParentSampled` | ||
| * `agent.sampler.remoteParentNotSampled` | ||
|
|
||
| These samplers have a `applySamplingDecision({transaction})` function, which `Transaction` calls (in `lib/transaction/index.js`) to update its `sampled` field and therefore its `priority`. | ||
|
|
||
| Unlike the other samplers, the `AdaptiveSampler` must share state with other `AdaptiveSamplers` with the same `sampling_target`, which complicates our seperate sampler instances approach. This will be fixed shortly, and this document will be updated to describe that solution. |
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 2025 New Relic Corporation. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| const Sampler = require('./sampler') | ||
|
|
||
| class AlwaysOffSampler extends Sampler { | ||
| applySamplingDecision({ transaction }) { | ||
| if (!transaction) return | ||
| transaction.priority = 0 | ||
| transaction.sampled = false | ||
| } | ||
| } | ||
|
|
||
| module.exports = AlwaysOffSampler |
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 2025 New Relic Corporation. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| const Sampler = require('./sampler') | ||
|
|
||
| class AlwaysOnSampler extends Sampler { | ||
| applySamplingDecision({ transaction }) { | ||
| if (!transaction) return | ||
| transaction.priority = 2.0 | ||
| transaction.sampled = true | ||
| } | ||
| } | ||
|
|
||
| module.exports = AlwaysOnSampler |
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,42 @@ | ||
| /* | ||
| * Copyright 2025 New Relic Corporation. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| const AdaptiveSampler = require('./adaptive-sampler') | ||
| const AlwaysOffSampler = require('./always-off-sampler') | ||
| const AlwaysOnSampler = require('./always-on-sampler') | ||
|
|
||
| /** | ||
| * A helper function for the Agent to determine what sampler | ||
| * to use and will log messages about the chosen sampler. | ||
| * @param {*} params Function parameters. | ||
| * @param {Agent} params.agent The New Relic agent instance. | ||
| * @param {object} params.config The entire agent config. | ||
| * @param {string} params.sampler The sampler type to use: 'root', 'remote_parent_sampled', or 'remote_parent_not_sampled'. | ||
| * @returns {Sampler} A Sampler e.g. AdaptiveSampler | ||
| */ | ||
| function determineSampler({ agent, config, sampler = 'root' }) { | ||
| const samplerDefinition = config?.distributed_tracing?.sampler?.[sampler] | ||
|
|
||
| // Always on? | ||
| if (samplerDefinition === 'always_on') { | ||
| return new AlwaysOnSampler() | ||
| } | ||
|
|
||
| // Always off? | ||
| if (samplerDefinition === 'always_off') { | ||
| return new AlwaysOffSampler() | ||
| } | ||
|
|
||
| // Our default ^.^ | ||
| // We'll ignore https://github.com/newrelic/node-newrelic/issues/3519 for now 0.o | ||
| return new AdaptiveSampler({ | ||
| agent, | ||
| serverless: config.serverless_mode.enabled, | ||
| period: config.sampling_target_period_in_seconds * 1000, | ||
| target: config.sampling_target // getter/setter for distributed_tracing.sampler.adaptive_sampling_target | ||
| }) | ||
| } | ||
|
|
||
| module.exports = determineSampler |
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,22 @@ | ||
| /* | ||
| * Copyright 2025 New Relic Corporation. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| /** | ||
| * @private | ||
| * @interface | ||
| */ | ||
| class Sampler { | ||
| /** | ||
| * Sets `priority` and `sampled` on the transaction | ||
| * in respect to this sampler's decision. | ||
| * @param {object} params to make the sampling decision with | ||
| * @param {Transaction} params.transaction the transaction to update | ||
| */ | ||
| applySamplingDecision({ transaction }) { | ||
| throw new Error('must implement applySamplingDecision for %s', transaction) | ||
| } | ||
| } | ||
|
|
||
| module.exports = Sampler |
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.
So the transactionSampler is always the root sampler?
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.
Yes, it was.
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.
Though this
updateSamplingTargetfunction does reveal another thing we will have to resolve with the sharing ofAdaptiveSamplerstates, but that's out of scope for this PR.