Skip to content

Conversation

@bizob2828
Copy link
Member

Description

This PR encapsulates all the sampling decision executions into a Samplers class. It instantiates up to 7 samplers and applies the correct one depending on your configuration and where a sampling decision is made. For a majority of the sampling decisions, the will be made with samplers.applySamplingDecision which run both full and partial granularity root samplers depending on your configuration. In the case of accepting a DT trace from an upstream parent, it will run both full and partial granularity remoteParentSampled or remoteParentNotSampled depending on the traceparent or tracestate sampled flag. Lastly, the case of accepting a DT trace via the newrelic header, it will run both fulll and partial granularity remoteParentSampled and remoteParentNotSampled from the newrelic sampled flag.

A few nuances to call out:

Related Issues

Closes #3536

@codecov
Copy link

codecov bot commented Dec 1, 2025

Codecov Report

❌ Patch coverage is 99.64664% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 97.66%. Comparing base (44f51dd) to head (5233912).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
lib/samplers/sampler.js 87.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3544      +/-   ##
==========================================
- Coverage   97.77%   97.66%   -0.11%     
==========================================
  Files         427      427              
  Lines       56099    56244     +145     
  Branches        1        1              
==========================================
+ Hits        54853    54933      +80     
- Misses       1246     1311      +65     
Flag Coverage Δ
integration-tests-cjs-20.x 74.02% <80.91%> (+0.02%) ⬆️
integration-tests-cjs-22.x 74.05% <80.91%> (+0.01%) ⬆️
integration-tests-cjs-24.x 74.81% <80.91%> (+0.01%) ⬆️
integration-tests-esm-20.x 52.64% <56.89%> (+0.01%) ⬆️
integration-tests-esm-22.x 52.69% <56.89%> (+0.01%) ⬆️
integration-tests-esm-24.x ?
unit-tests-20.x 88.79% <99.64%> (+0.05%) ⬆️
unit-tests-22.x 88.83% <99.64%> (+0.06%) ⬆️
unit-tests-24.x 88.84% <99.64%> (+0.06%) ⬆️
versioned-tests-20.x 80.93% <69.61%> (-0.33%) ⬇️
versioned-tests-22.x 80.94% <69.61%> (-0.33%) ⬇️
versioned-tests-24.x 80.87% <69.61%> (-0.33%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Contributor

@amychisholm03 amychisholm03 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may have missed it in test/unit/samplers/index.test.js, but I do not see an integration test testing the following scenario that we've commonly brought up (I see the unit test that affirms the "effective ratio" addition):

If there are 100 traces, agent.samplers.root.ratio = 0.4 and agent.samplers.partialRoot.ratio = 0.2, will there be ~40 full traces and ~20 partial traces?


class Samplers {
constructor(agent) {
this.fullEnabled = agent.config.distributed_tracing.sampler.full_granularity.enabled
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have to check for distributed_tracing.enabled here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to change too much here. But previously we always assigned priority and sampled. Then before we generate span events we check if DT is enabled here

@bizob2828
Copy link
Member Author

I may have missed it in test/unit/samplers/index.test.js, but I do not see an integration test testing the following scenario that we've commonly brought up (I see the unit test that affirms the "effective ratio" addition):

If there are 100 traces, agent.samplers.root.ratio = 0.4 and agent.samplers.partialRoot.ratio = 0.2, will there be ~40 full traces and ~20 partial traces?

Do we need a test? As you mentioned I have tests that assert the ratio is assigned correctly. We also have a full suite of tests for one ratio sampler

@bizob2828
Copy link
Member Author

I may have missed it in test/unit/samplers/index.test.js, but I do not see an integration test testing the following scenario that we've commonly brought up (I see the unit test that affirms the "effective ratio" addition):
If there are 100 traces, agent.samplers.root.ratio = 0.4 and agent.samplers.partialRoot.ratio = 0.2, will there be ~40 full traces and ~20 partial traces?

Do we need a test? As you mentioned I have tests that assert the ratio is assigned correctly. We also have a full suite of tests for one ratio sampler

Actually let me clean up the ratio unit tests, they are misleading. and I'll add a test for this

Copy link
Contributor

@amychisholm03 amychisholm03 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we'd have tests to test other full/partial sampler combinations, but I think the full ratio and partial ratio test is good for now

@bizob2828
Copy link
Member Author

I'm going to merge this but I have a question out about how to handle assigning priority when DT is disabled

@bizob2828 bizob2828 merged commit 1535a82 into newrelic:main Dec 2, 2025
24 of 25 checks passed
@github-project-automation github-project-automation bot moved this from Needs PR Review to Done: Issues recently completed in Node.js Engineering Board Dec 2, 2025
@bizob2828 bizob2828 deleted the partial-samplers branch December 2, 2025 18:18
Copy link
Contributor

@jsumners-nr jsumners-nr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's all looking reasonable to me. I have a few comments, but none that would have been blockers on this.

const sampler = isSampled ? this.remoteParentSampled : this.remoteParentNotSampled
const partialSampler = isSampled ? this.partialRemoteParentSampled : this.partialRemoteParentNotSampled
if (this.fullEnabled) {
if (sampler?.toString() === 'AdaptiveSampler') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that you've implemented a literal toString method on these objects:

toString() {
return 'AdaptiveSampler'
}

These implementations are a bit odd because they seem to be attempting to provide "type" inference support. Whereas a typical toString override would actually return some sort of string representation of the object. Consider the following:

'use strict'

class Foo {
  toString() {
    return 'Foo'
  }
}

const foo = new Foo()
console.log(
  Object.prototype.toString.call(foo)
)
console.log(
  foo.toString()
)


class Bar {
  get [Symbol.toStringTag]() { return 'Bar' }
  toString() { return 'Bar' }
}

const bar = new Bar()
console.log(
  Object.prototype.toString.call(bar)
)
console.log(
  bar.toString()
)

The output of that script is:

[object Object]
Foo
[object Bar]
Bar

Which is to say, the overrides in these methods are contrary to what one would expect if they are familiar with the canonical way of determining the "type" of an object (using Object.prototype.toString).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't implement this. it was in the previous iterations. I agree but didn't change it in here

Comment on lines +6 to +18
'use strict'

const test = require('node:test')
const assert = require('node:assert')

const Config = require('#agentlib/config/index.js')
const Samplers = require('#agentlib/samplers/index.js')
const AdaptiveSampler = require('#agentlib/samplers/adaptive-sampler.js')
const AlwaysOnSampler = require('#agentlib/samplers/always-on-sampler.js')
const AlwaysOffSampler = require('#agentlib/samplers/always-off-sampler.js')
const TraceIdRatioBasedSampler = require('#agentlib/samplers/ratio-based-sampler.js')

function beforeEach(ctx) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am so happy to see this clean organization of imports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done: Issues recently completed

Development

Successfully merging this pull request may close these issues.

Add partial granularity samplers

3 participants