[SDK] Implement ComposableSampler
ComposableSampler are a recent addition in the specifications.
Implement support in the SDK.
https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#compositesampler
This issue is available for anyone to work on. Make sure to reference this issue in your pull request. :sparkles: Thank you for your contribution! :sparkles:
interested in picking this up, quick scoping question first.
#3966 adds the config-side DTOs under sdk/configuration/ and wires sdk_builder.cc to instantiate composable variants. reading #4028 as the trace-side piece: actual sampler classes under sdk/src/trace/samplers/ (ComposableAlwaysOn/Off, ComposableParentThreshold, ComposableProbability, ComposableRuleBased) plus the composition semantics from the spec. does that split match what you had in mind?
also: one PR covering all variants + composition, or split per variant? whichever is easier to review.
@mateenali66
interested in picking this up, quick scoping question first.
Thanks for volunteering.
#3966 adds the config-side DTOs under
sdk/configuration/and wiressdk_builder.ccto instantiate composable variants. reading #4028 as the trace-side piece: actual sampler classes undersdk/src/trace/samplers/(ComposableAlwaysOn/Off, ComposableParentThreshold, ComposableProbability, ComposableRuleBased) plus the composition semantics from the spec. does that split match what you had in mind?
Yes.
#3966 is now merged, so the next part is to implement the SDK logic itself, under sdk/src/trace/samplers/.
also: one PR covering all variants + composition, or split per variant? whichever is easier to review.
One PR is more practical.
Expect slow/no replies next week, but I will be available for review/guidance after that.
starting to sketch this. before committing to the rest of the variants and tests, wanted to float the interface shapes so you can redline anything early.
proposed types
struct SamplingIntent
{
uint64_t threshold_value = 0; // 56-bit value in low bits; R >= T keeps
bool has_threshold = false; // false = drop unconditionally
bool threshold_reliable = false; // true = write "ot"/"th:" back
AttributesProvider attributes_provider{}; // invoked only on keep
TraceStateProvider trace_state_provider{}; // applied before CompositeSampler writes "th:"
};
class ComposableSampler
{
public:
virtual SamplingIntent GetSamplingIntent(
const trace::SpanContext &parent_context,
trace::TraceId trace_id,
nostd::string_view name,
trace::SpanKind span_kind,
const common::KeyValueIterable &attributes,
const trace::SpanContextKeyValueIterable &links) noexcept = 0;
virtual nostd::string_view GetDescription() const noexcept = 0;
};
class CompositeSampler : public Sampler {
// owns the SamplingIntent -> SamplingResult translation
// owns the "ot" tracestate write-back ("th:", "rv:")
};
open questions
- threshold representation. going with
uint64_t threshold_value + bool has_threshold. consideredstd::optional<uint64_t>but the rest of the SDK avoids<optional>for ABI reasons. OK? - tracestate "ot" codec. putting the parse/serialize helpers in a private
sdk/src/trace/samplers/ot_tracestate.cchelper. not exporting them. alternative is a public util undersdk/common/if you anticipate reuse from non-sampler code. preference? - ComposableParentThreshold root param. the spec says a
rootdelegate is required; I'll pass it asstd::shared_ptr<ComposableSampler>to allow the caller to share it. any issue with shared vs unique ownership? - ComposableRuleBased rules. modeling each rule as
{ Predicate(attrs, name, kind) -> bool; std::shared_ptr<ComposableSampler> sampler; }. predicate type isstd::function<bool(...)>. good withstd::function, or would you rather have an abstractPredicatebase class to avoidstd::functionheap allocs on the hot path? - factory pattern. following the existing
*Factory::Create()convention for each of the 5 variants +CompositeSamplerFactory. anything you'd rather I do differently? - sdk_builder.cc. the FIXME-SDKs in #3966 currently fall back to
AlwaysOn/TraceIdRatioBased. once the real classes land, I'll wrap them inCompositeSamplerthere. or do you wantsdk_builderto expose bothComposableSampler-only and wrapped-Samplerpaths?
the four header files (base, composite, always_on, always_off) exist locally as a starting point. I'll push the rest of the variants once 1-4 above are settled. no rush, happy to wait until you are back in the loop next week.
So far the composable samplers have been implemented in the SDK along with YAML parsing of their models. The missing piece is to fill in the Visit methods to create the samplers from the sampler configs within the SdkBuilder and add some tests. Help still wanted!
See: https://github.com/open-telemetry/opentelemetry-cpp/blob/76d18c99f32bc4bb24dbe196c6a64d51b23c6733/sdk/src/configuration/sdk_builder.cc#L438