Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
913f5bc
feat(configuration): add sampler parsing
MikeGoldsmith Feb 13, 2026
09143b0
update changelog
MikeGoldsmith Feb 13, 2026
a44f88c
fix(configuration): normalize null to undefined in sampler parsing
MikeGoldsmith Feb 13, 2026
785ca08
Merge branch 'main' of github.com:open-telemetry/opentelemetry-js int…
MikeGoldsmith Feb 16, 2026
48c5c8d
fix(configuration): enhance composite sampler parsing
MikeGoldsmith Feb 16, 2026
ce1a7a9
test(configuration): improve sampler parsing coverage
MikeGoldsmith Feb 16, 2026
1ca6a09
Merge branch 'main' of github.com:open-telemetry/opentelemetry-js int…
MikeGoldsmith Feb 17, 2026
11956e5
run linter
MikeGoldsmith Feb 17, 2026
5ba3faf
address review feedback on sampler parsing
MikeGoldsmith Mar 2, 2026
8e7708d
Merge branch 'main' of github.com:open-telemetry/opentelemetry-js int…
MikeGoldsmith Mar 2, 2026
f8e2b32
merge upstream/main into mike/parse-samplers
MikeGoldsmith Mar 9, 2026
7943dc1
move changelog entry to unreleased
MikeGoldsmith Mar 10, 2026
a629374
Merge branch 'main' of github.com:open-telemetry/opentelemetry-js int…
MikeGoldsmith Mar 10, 2026
236c4c3
Merge branch 'main' of github.com:open-telemetry/opentelemetry-js int…
MikeGoldsmith Mar 10, 2026
acb6fc3
fix(configuration): update sampler tests to use renamed OTEL_CONFIG_F…
MikeGoldsmith Mar 10, 2026
a1ba584
Merge branch 'main' of github.com:open-telemetry/opentelemetry-js int…
MikeGoldsmith Mar 13, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2

* feat(sampler-composite): add ComposableAnnotatingSampler and ComposableRuleBasedSampler [#6305](https://github.com/open-telemetry/opentelemetry-js/pull/6305) @trentm
* feat(configuration): parse config for rc 3 [#6304](https://github.com/open-telemetry/opentelemetry-js/pull/6304) @maryliag
* feat(configuration): add sampler configuration parsing support [#6409](https://github.com/open-telemetry/opentelemetry-js/pull/6409) @MikeGoldsmith
Comment thread
MikeGoldsmith marked this conversation as resolved.
Outdated
* feat(instrumentation): use the `internals: true` option with import-in-the-middle hook, allowing instrumentations to hook internal files in ES modules [#6344](https://github.com/open-telemetry/opentelemetry-js/pull/6344) @trentm

### :bug: Bug Fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class EnvironmentConfigFactory implements ConfigFactory {
setAttributeLimits(this._config);
setPropagators(this._config);
setTracerProvider(this._config);
setSampler(this._config);
Comment thread
MikeGoldsmith marked this conversation as resolved.
Outdated
setMeterProvider(this._config);
setLoggerProvider(this._config);
}
Expand Down Expand Up @@ -160,6 +161,55 @@ export function setPropagators(config: ConfigurationModel): void {
}
}

export function setSampler(config: ConfigurationModel): void {
const sampler = getStringFromEnv('OTEL_TRACES_SAMPLER');
const arg = getStringFromEnv('OTEL_TRACES_SAMPLER_ARG');

if (!sampler || !config.tracer_provider) {
return;
}

const ratio = arg ? parseFloat(arg) : 1.0;

switch (sampler) {
case 'always_on':
config.tracer_provider.sampler = { always_on: {} };
break;

case 'always_off':
config.tracer_provider.sampler = { always_off: {} };
break;

case 'traceidratio':
config.tracer_provider.sampler = {
trace_id_ratio_based: { ratio },
};
break;

case 'parentbased_always_on':
config.tracer_provider.sampler = {
parent_based: { root: { always_on: {} } },
};
break;

case 'parentbased_always_off':
config.tracer_provider.sampler = {
parent_based: { root: { always_off: {} } },
};
break;

case 'parentbased_traceidratio':
config.tracer_provider.sampler = {
parent_based: { root: { trace_id_ratio_based: { ratio } } },
};
break;

default:
diag.warn(`Unknown sampler type: ${sampler}`);
break;
}
}

export function setTracerProvider(config: ConfigurationModel): void {
const exportersType = Array.from(
new Set(getStringListFromEnv('OTEL_TRACES_EXPORTER'))
Expand Down
120 changes: 120 additions & 0 deletions experimental/packages/configuration/src/FileConfigFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
} from './models/commonModel';
import {
initializeDefaultTracerProviderConfiguration,
Sampler,
SpanExporter,
SpanProcessor,
TracerProvider,
Expand Down Expand Up @@ -294,6 +295,120 @@ export function setPropagator(
}
}

function parseSampler(sampler: Sampler): Sampler {
const samplerType = Object.keys(sampler)[0];
let parsedSampler: Sampler = {};

switch (samplerType) {
case 'always_on':
parsedSampler = { always_on: sampler['always_on'] ?? undefined };
Comment thread
maryliag marked this conversation as resolved.
break;

case 'always_off':
parsedSampler = { always_off: sampler['always_off'] ?? undefined };
break;

case 'trace_id_ratio_based': {
const s = sampler['trace_id_ratio_based'];
parsedSampler = {
trace_id_ratio_based: {
ratio: getNumberFromConfigFile(s?.ratio) ?? 1.0,
},
};
break;
}

case 'parent_based': {
const s = sampler['parent_based'];
if (s) {
parsedSampler = { parent_based: {} };
if (s.root) {
parsedSampler.parent_based!.root = parseSampler(s.root);
}
if (s.remote_parent_sampled) {
parsedSampler.parent_based!.remote_parent_sampled = parseSampler(
s.remote_parent_sampled
);
}
if (s.remote_parent_not_sampled) {
parsedSampler.parent_based!.remote_parent_not_sampled = parseSampler(
s.remote_parent_not_sampled
);
}
if (s.local_parent_sampled) {
parsedSampler.parent_based!.local_parent_sampled = parseSampler(
s.local_parent_sampled
);
}
if (s.local_parent_not_sampled) {
parsedSampler.parent_based!.local_parent_not_sampled = parseSampler(
s.local_parent_not_sampled
);
}
}
break;
}

case 'probability': {
const s = sampler['probability'];
parsedSampler = {
probability: {
ratio: getNumberFromConfigFile(s?.ratio) ?? 1.0,
},
};
break;
}

case 'probability/development': {
const s = sampler['probability/development'];
parsedSampler = {
'probability/development': {
ratio: getNumberFromConfigFile(s?.ratio) ?? 1.0,
},
};
break;
}

case 'composite/development': {
const s = sampler['composite/development'];
if (s) {
parsedSampler = { 'composite/development': {} };
if (s.samplers && s.samplers.length > 0) {
parsedSampler['composite/development']!.samplers = s.samplers.map(
(childSampler: Sampler) => parseSampler(childSampler)
);
}
if (s.composite) {
parsedSampler['composite/development']!.composite = s.composite;
}
if (s.rule_based) {
const rb = s.rule_based;
parsedSampler['composite/development']!.rule_based = {};
if (rb.fallback_sampler) {
parsedSampler[
'composite/development'
]!.rule_based!.fallback_sampler = parseSampler(rb.fallback_sampler);
}
if (rb.rules) {
parsedSampler['composite/development']!.rule_based!.rules =
rb.rules.map(rule => ({
...rule,
sampler: rule.sampler ? parseSampler(rule.sampler) : undefined,
}));
}
if (rb.span_kind) {
parsedSampler['composite/development']!.rule_based!.span_kind =
getStringFromConfigFile(rb.span_kind);
}
}
}
break;
}
}

return parsedSampler;
}

function getConfigHeaders(
h?: NameStringValuePair[]
): NameStringValuePair[] | null {
Expand Down Expand Up @@ -507,6 +622,11 @@ export function setTracerProvider(
}
}

// Sampler
Comment thread
MikeGoldsmith marked this conversation as resolved.
if (tracerProvider['sampler']) {
config.tracer_provider.sampler = parseSampler(tracerProvider['sampler']);
}

// Processors
for (let i = 0; i < tracerProvider['processors'].length; i++) {
const processorType = Object.keys(tracerProvider['processors'][i])[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ export interface Sampler {
* Configure sampler to be trace_id_ratio_based.
*/
trace_id_ratio_based?: TraceIdRatioBasedSampler;

/**
* Configure sampler to be probability (experimental).
*/
probability?: ProbabilitySampler;
Comment thread
MikeGoldsmith marked this conversation as resolved.
Outdated

/**
* Configure sampler to be probability/development.
*/
'probability/development'?: ProbabilitySampler;
Comment thread
MikeGoldsmith marked this conversation as resolved.
Outdated

/**
* Configure sampler to be composite/development.
*/
'composite/development'?: CompositeSampler;
}

export interface ParentBasedSampler {
Expand Down Expand Up @@ -155,6 +170,73 @@ export interface TraceIdRatioBasedSampler {
ratio?: number;
}

export interface ProbabilitySampler {
Comment thread
MikeGoldsmith marked this conversation as resolved.
Outdated
/**
* Configure probability ratio.
Comment thread
MikeGoldsmith marked this conversation as resolved.
*/
ratio?: number;
}

export interface RuleBasedSamplerRule {
/**
* Configure attribute values matching condition.
*/
attribute_values?: {
key?: string;
values?: string[];
};
/**
* Configure attribute patterns matching condition.
*/
attribute_patterns?: {
key?: string;
included?: string[];
excluded?: string[];
};
/**
* Configure parent matching condition.
*/
parent?: string[];
/**
* Configure span kinds matching condition.
*/
span_kinds?: string[];
/**
* Configure sampler to use when rule matches.
*/
sampler?: Sampler;
}

export interface RuleBasedSampler {
Comment thread
MikeGoldsmith marked this conversation as resolved.
Outdated
/**
* Configure fallback sampler.
*/
fallback_sampler?: Sampler;
/**
* Configure rules.
*/
rules?: RuleBasedSamplerRule[];
/**
* Configure span kind.
*/
span_kind?: string;
}

export interface CompositeSampler {
/**
* Configure list of samplers.
*/
samplers?: Sampler[];
/**
* Configure composite type.
*/
composite?: 'rule_based';
/**
* Configure rule_based sampler.
*/
rule_based?: RuleBasedSampler;
}

export interface SimpleSpanProcessor {
/**
* Configure exporter.
Expand Down
Loading