Skip to content

feat(opentelemetry-sdk-trace-base): add AlwaysRecordSampler#6168

Open
majanjua-amzn wants to merge 1 commit intoopen-telemetry:mainfrom
majanjua-amzn:main
Open

feat(opentelemetry-sdk-trace-base): add AlwaysRecordSampler#6168
majanjua-amzn wants to merge 1 commit intoopen-telemetry:mainfrom
majanjua-amzn:main

Conversation

@majanjua-amzn
Copy link
Copy Markdown
Contributor

@majanjua-amzn majanjua-amzn commented Nov 26, 2025

Which problem is this PR solving?

Part of open-telemetry/opentelemetry-specification#4698

Introduces a way for users to access the RECORD_ONLY functionality of the sampler, as currently all available samplers either provide a RECORD_AND_SAMPLE or DROP response.

Short description of the changes

  • Introduced new AlwaysRecordSampler
  • Added relevant unit tests

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Unit tests and extensively used in aws-otel-js-instrumentation

Checklist:

  • Followed the style guidelines of this project
  • Unit tests have been added
  • Documentation has been updated

@majanjua-amzn majanjua-amzn requested a review from a team as a code owner November 26, 2025 00:29
@codecov
Copy link
Copy Markdown

codecov Bot commented Nov 26, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.41%. Comparing base (167185a) to head (bd73ff8).
⚠️ Report is 123 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6168   +/-   ##
=======================================
  Coverage   95.40%   95.41%           
=======================================
  Files         317      318    +1     
  Lines        9439     9458   +19     
  Branches     2187     2190    +3     
=======================================
+ Hits         9005     9024   +19     
  Misses        434      434           
Files with missing lines Coverage Δ
...s/sampler-always-record/src/AlwaysRecordSampler.ts 100.00% <100.00%> (ø)

... and 2 files with indirect coverage changes

🚀 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.

@david-luna
Copy link
Copy Markdown
Contributor

Hi @majanjua-amzn

Thanks for the contribution to the opentelemetry-js project :)

If this new sampler is part of the spec. Is there any motivation to have it in a dedicated package instead of having it within @opentelemetry/sdk-trace-base ?

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Feb 9, 2026

This PR is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.

Copy link
Copy Markdown
Member

@dyladan dyladan left a comment

Choose a reason for hiding this comment

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

Seems pretty straightforward

Comment on lines +43 to +52
public static create(rootSampler: Sampler): AlwaysRecordSampler {
return new AlwaysRecordSampler(rootSampler);
}

private constructor(rootSampler: Sampler) {
if (rootSampler == null) {
throw new Error('rootSampler is null/undefined. It must be provided');
}
this.rootSampler = rootSampler;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why are we using a static factory method here? Seems like extra boilerplate

return `AlwaysRecordSampler{${this.rootSampler.toString()}}`;
}

wrapResultWithRecordOnlyResult(result: SamplingResult) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
wrapResultWithRecordOnlyResult(result: SamplingResult) {
private _wrapResultWithRecordOnlyResult(result: SamplingResult) {

Comment on lines +17 to +19
// Includes work from:
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

same question

github-merge-queue Bot pushed a commit to open-telemetry/opentelemetry-specification that referenced this pull request Mar 13, 2026
The `AlwaysRecord` sampler has been in our development since the
December 2025 release, and it has prototypes in the following languages:

* Java: open-telemetry/opentelemetry-java#7877
* Go: open-telemetry/opentelemetry-go#7714
* PHP: open-telemetry/opentelemetry-php#1834
* Javascript:
open-telemetry/opentelemetry-js#6168
* Python:
open-telemetry/opentelemetry-python#4823
* DotNet:
open-telemetry/opentelemetry-dotnet#6732 (got
closed but hopefully we bring it back)
Copy link
Copy Markdown
Member

@pichlermarc pichlermarc left a comment

Choose a reason for hiding this comment

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

@majanjua-amzn thanks for working on this. A few suggestions on the code, overall this implementation looks good.

suggestion: let's move this into @opentelemetry/sdk-trace-base from the get-go since the spec has been stablized already.

* limitations under the License.
*/

export { AlwaysRecordSampler } from './AlwaysRecordSampler';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

what we've started doing in new packages is not exporting the classes directly, and instead export factory functions like function createAlwaysRecordSampler(options: {rootSampler: Sampler}): Sampler {}, the reason for that is hiding the concrete type from the public export, which usually becomes incompatible with itself when new private properties are added to it.

suggestion: export a factory function instead.

let mockedSampler: Sampler;
let sampler: AlwaysRecordSampler;

describe('AlwaysRecordSamplerTest', () => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

suggestion: let's not use arrow functions with mocha. ref https://mochajs.org/features/arrow-functions/

Suggested change
describe('AlwaysRecordSamplerTest', () => {
describe('AlwaysRecordSamplerTest', function () {

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants