-
Notifications
You must be signed in to change notification settings - Fork 946
feat(sampler-composite): add experimental implementation of composite sampling spec #5839
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 10 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
33485d3
feat(trace): add experimental implementation of draft consistent samp…
anuraaga 0a967ac
Update changelog
anuraaga 1fbc1dc
git add
anuraaga 0f1d33e
Drift
anuraaga b41131b
Fix folder name
anuraaga 38ecc13
Fix
anuraaga acc3070
Cleanup
anuraaga 63c6c06
Rename package
anuraaga 3119308
README
anuraaga a6df902
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
anuraaga 2b5e7c5
Cleanup
anuraaga 7ced0a5
Factory functions
anuraaga 7f7afdc
Doc
anuraaga 47c3fd4
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
anuraaga 61656b5
Hard refresh lock
anuraaga 861ac86
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
anuraaga 6592630
Cleanup
anuraaga 98e5cd8
Update CHANGELOG.md
anuraaga b7a34e6
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
anuraaga fd5c24a
Merge branch 'consistent-sampling' of https://github.com/anuraaga/ope…
anuraaga af4bc3e
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
anuraaga 00a00f9
Force merge package lock
anuraaga 02c8bce
Add precision TODO
anuraaga 04809a3
Finish
anuraaga ffc9bc4
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
anuraaga 21ad9dc
Update experimental/CHANGELOG.md
trentm 535292e
Merge branch 'main' into consistent-sampling
trentm 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,2 @@ | ||
| build | ||
| src/generated | ||
trentm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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,8 @@ | ||
| module.exports = { | ||
| "env": { | ||
| "mocha": true, | ||
| "commonjs": true, | ||
| "shared-node-browser": true | ||
| }, | ||
| ...require('../../../eslint.base.js') | ||
| } |
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,63 @@ | ||
| # OpenTelemetry Composite Sampling | ||
|
|
||
| [![NPM Published Version][npm-img]][npm-url] | ||
| [![Apache License][license-image]][license-image] | ||
|
|
||
| **Note: This is an experimental package under active development. New releases may include breaking changes.** | ||
|
|
||
| This package provides implementations of composite samplers that propagate sampling information across a trace. | ||
| This implements the [experimental specification][probability-sampling]. | ||
|
|
||
| Currently `ComposableRuleBased` an `ComposableAnnotating` are not implemented. | ||
|
|
||
| ## Quick Start | ||
|
|
||
| To get started you will need to install a compatible OpenTelemetry SDK. | ||
|
|
||
| ### Install Peer Dependencies | ||
|
|
||
| ```sh | ||
| npm install @opentelemetry/sdk-trace-node # Or web | ||
trentm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| ### Samplers | ||
|
|
||
| This module exports samplers that follow the general behavior of the standard SDK samplers, but ensuring | ||
| it is consistent across a trace by using the tracestate header. Notably, the tracestate can be examined | ||
| in exported spans to reconstruct population metrics. | ||
|
|
||
| ```typescript | ||
| import { | ||
| CompositeSampler, | ||
| ComposableAlwaysOffSampler, | ||
| ComposableAlwaysOnSampler, | ||
| ComposableParentThresholdSampler, | ||
| ComposableTraceIDRatioBasedSampler, | ||
| } from '@opentelemetry/sampler-composite'; | ||
|
|
||
| // never sample | ||
| const sampler = new CompositeSampler(new ComposableAlwaysOffSampler()); | ||
| // always sample | ||
| const sampler = new CompositeSampler(new ComposableAlwaysOnSampler()); | ||
| // follow the parent, or otherwise sample with a probability if root | ||
| const sampler = new CompositeSampler( | ||
| new ComposableParentThresholdSampler(new ComposableTraceIDRatioBasedSampler(0.3))); | ||
trentm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| ## Useful links | ||
|
|
||
| - For more information on OpenTelemetry, visit: <https://opentelemetry.io/> | ||
| - For more about OpenTelemetry JavaScript: <https://github.com/open-telemetry/opentelemetry-js> | ||
| - For help or feedback on this project, join us in [GitHub Discussions][discussions-url] | ||
|
|
||
| ## License | ||
|
|
||
| Apache 2.0 - See [LICENSE][license-url] for more information. | ||
|
|
||
| [discussions-url]: https://github.com/open-telemetry/opentelemetry-js/discussions | ||
| [license-url]: https://github.com/open-telemetry/opentelemetry-js/blob/main/LICENSE | ||
| [license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat | ||
| [npm-url]: https://www.npmjs.com/package/@opentelemetry/sampler-composite | ||
| [npm-img]: https://badge.fury.io/js/%40opentelemetry%sampler-composite.svg | ||
|
|
||
| [probability-sampling]: https://opentelemetry.io/docs/specs/otel/trace/tracestate-probability-sampling/ | ||
trentm marked this conversation as resolved.
Show resolved
Hide resolved
|
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,24 @@ | ||
| /*! | ||
| * Copyright The OpenTelemetry Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| const karmaWebpackConfig = require('../../../karma.webpack'); | ||
| const karmaBaseConfig = require('../../../karma.base'); | ||
|
|
||
| module.exports = (config) => { | ||
| config.set(Object.assign({}, karmaBaseConfig, { | ||
| webpack: karmaWebpackConfig | ||
| })) | ||
| }; |
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,79 @@ | ||||||
| { | ||||||
| "name": "@opentelemetry/sampler-composite", | ||||||
| "private": false, | ||||||
| "publishConfig": { | ||||||
| "access": "public" | ||||||
| }, | ||||||
| "version": "0.203.0", | ||||||
|
||||||
| "version": "0.203.0", | |
| "version": "0.205.0", |
trentm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
trentm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
trentm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
trentm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
trentm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
trentm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
trentm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
trentm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
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,35 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| import type { ComposableSampler, SamplingIntent } from './types'; | ||
| import { INVALID_THRESHOLD } from './util'; | ||
|
|
||
| const intent: SamplingIntent = { | ||
| threshold: INVALID_THRESHOLD, | ||
| thresholdReliable: false, | ||
| }; | ||
|
|
||
| /** | ||
| * A composable sampler that does not sample any span. | ||
| */ | ||
| export class ComposableAlwaysOffSampler implements ComposableSampler { | ||
| getSamplingIntent(): SamplingIntent { | ||
| return intent; | ||
| } | ||
|
|
||
| toString(): string { | ||
| return 'ComposableAlwaysOffSampler'; | ||
| } | ||
| } |
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,35 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| import type { ComposableSampler, SamplingIntent } from './types'; | ||
| import { MIN_THRESHOLD } from './util'; | ||
|
|
||
| const intent: SamplingIntent = { | ||
| threshold: MIN_THRESHOLD, | ||
| thresholdReliable: true, | ||
| }; | ||
|
|
||
| /** | ||
| * A composable sampler that samples all span. | ||
| */ | ||
| export class ComposableAlwaysOnSampler implements ComposableSampler { | ||
| getSamplingIntent(): SamplingIntent { | ||
| return intent; | ||
| } | ||
|
|
||
| toString(): string { | ||
| return 'ComposableAlwaysOnSampler'; | ||
| } | ||
| } |
108 changes: 108 additions & 0 deletions
108
experimental/packages/sampler-composite/src/composite.ts
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,108 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| import { | ||
| Context, | ||
| SpanKind, | ||
| Attributes, | ||
| Link, | ||
| TraceState, | ||
| } from '@opentelemetry/api'; | ||
| import { TraceState as CoreTraceState } from '@opentelemetry/core'; | ||
| import { | ||
| Sampler, | ||
| SamplingDecision, | ||
| SamplingResult, | ||
| } from '@opentelemetry/sdk-trace-base'; | ||
| import { ComposableSampler } from './types'; | ||
| import { getSpanContext } from '../../../../api/src/trace/context-utils'; | ||
| import { parseOtelTraceState, serializeTraceState } from './tracestate'; | ||
| import { | ||
| INVALID_THRESHOLD, | ||
| isValidRandomValue, | ||
| isValidThreshold, | ||
| } from './util'; | ||
|
|
||
| export class CompositeSampler implements Sampler { | ||
| constructor(private readonly delegate: ComposableSampler) {} | ||
|
|
||
| shouldSample( | ||
| context: Context, | ||
| traceId: string, | ||
| spanName: string, | ||
| spanKind: SpanKind, | ||
| attributes: Attributes, | ||
| links: Link[] | ||
| ): SamplingResult { | ||
| const spanContext = getSpanContext(context); | ||
|
|
||
| const traceState = spanContext?.traceState; | ||
| const otTraceState = parseOtelTraceState(traceState); | ||
|
|
||
| const intent = this.delegate.getSamplingIntent( | ||
| context, | ||
| traceId, | ||
| spanName, | ||
| spanKind, | ||
| attributes, | ||
| links | ||
| ); | ||
|
|
||
| let adjustedCountCorrect = false; | ||
| let sampled = false; | ||
| if (isValidThreshold(intent.threshold)) { | ||
| adjustedCountCorrect = intent.thresholdReliable; | ||
| let randomness: bigint; | ||
| if (isValidRandomValue(otTraceState.randomValue)) { | ||
| randomness = otTraceState.randomValue; | ||
| } else { | ||
| // Use last 56 bits of trace_id as randomness. | ||
| randomness = BigInt(`0x${traceId.slice(-14)}`); | ||
| } | ||
| sampled = intent.threshold <= randomness; | ||
| } | ||
|
|
||
| const decision = sampled | ||
| ? SamplingDecision.RECORD_AND_SAMPLED | ||
| : SamplingDecision.NOT_RECORD; | ||
| if (sampled && adjustedCountCorrect) { | ||
| otTraceState.threshold = intent.threshold; | ||
| } else { | ||
| otTraceState.threshold = INVALID_THRESHOLD; | ||
| } | ||
|
|
||
| const otts = serializeTraceState(otTraceState); | ||
|
|
||
| let newTraceState: TraceState | undefined; | ||
| if (traceState) { | ||
| newTraceState = traceState; | ||
| if (intent.updateTraceState) { | ||
| newTraceState = intent.updateTraceState(newTraceState); | ||
| } | ||
| } | ||
| if (otts) { | ||
| if (!newTraceState) { | ||
| newTraceState = new CoreTraceState(); | ||
| } | ||
| newTraceState = newTraceState.set('ot', otts); | ||
| } | ||
|
|
||
| return { | ||
| decision, | ||
| attributes: intent.attributes, | ||
| traceState: newTraceState, | ||
| }; | ||
| } | ||
| } | ||
trentm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 The OpenTelemetry Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| export { ComposableAlwaysOffSampler } from './alwaysoff'; | ||
| export { ComposableAlwaysOnSampler } from './alwayson'; | ||
| export { ComposableTraceIDRatioBasedSampler } from './traceidratio'; | ||
| export { ComposableParentThresholdSampler } from './parentthreshold'; | ||
| export { CompositeSampler } from './composite'; | ||
| export type { ComposableSampler, SamplingIntent } from './types'; |
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.
[This is a general question. I'm commenting here for lack of a good place to mention it.]
My head is spinning from the inconsistent (har har) use of
CompositeorComposableorConsistentusage in class and interface names in the spec, OTEP, and implementations. And also, a little less so, in some class name changes, e.g.ConsistentFixedThresholdin the OTEP vs.ComposableTraceIDRatioBasedin the spec. I haven't read through this PR yet, but I'm guessing other API diffs between OTEP and spec will show up, e.g.threshold_reliablein the spec vs.IsAdjustedCountReliablein the OTEP.While I don't know that the names used in the spec need to be the same names as in the implementation APIs, approaching the same names is likely desired. Are we implementing the spec here, or the Java
consistent56implementation, which seems to follow the OTEP naming (which makes sense as it was probably developed before the spec renamings)?I'm happy to go asking about this in the OTel channels.
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.
Thanks - I will read the spec and align with it as much as possible, sorry for missing it
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'm curious what Java (and the Rust/Go implementations that are linked to from open-telemetry/opentelemetry-specification#4466) will do. I.e. will they change their implementations to use the class names in the spec? Or change the spec to use
Consistent-prefix?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.
Looking at the rust and go implementations (esp. the go implementation README: https://github.com/jmacd/go-sampler) I'm pretty certain
CompositeSamplerandComposable*Samplernaming is the intent.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 asked for clarification in #otel-sampling: https://cloud-native.slack.com/archives/C027DS6GZD3/p1755299016130699
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.
Issue/answer on the naming: open-telemetry/opentelemetry-specification#4640
Basically: "So the short answer is that the Specification names should be used.", but "do not strongly believe that all SDKs need the same exact names".
So, whatever names are used by a particular SDK is fine.
(Note that I would expect the OTel schema for its configuration data model would use the spec names for these samplers, when these new samplers are added to the schema. At that point there is some value to users and maintainers if the configuration names match the implementation names.)