opentelemetry-ruby icon indicating copy to clipboard operation
opentelemetry-ruby copied to clipboard

Set sampler other than the default

Open Asafb26 opened this issue 4 years ago • 8 comments

Hello, I can't find and setter to the sampler in TracerProvider, Is there any option to change the default sampler within the SDK's configuration?

Asafb26 avatar Aug 19 '20 10:08 Asafb26

https://github.com/open-telemetry/opentelemetry-ruby/blob/a1b57a747341831508166e3dd6dcce5a1a7e48d1/sdk/lib/opentelemetry/sdk/trace/config/trace_config.rb#L49 and https://github.com/open-telemetry/opentelemetry-ruby/blob/a1b57a747341831508166e3dd6dcce5a1a7e48d1/sdk/lib/opentelemetry/sdk/trace/tracer_provider.rb#L15

So something like:

sampler = OpenTelemetry::SDK::Trace::Samplers.parent_or_else(OpenTelemetry::SDK::Trace::Samplers.probability(0.001))
trace_config = OpenTelemetry::SDK::Trace::Config::TraceConfig.new(sampler: sampler)
OpenTelemetry.tracer_provider.active_trace_config = trace_config

The heavily nested namespaces are decidedly unpleasant when you see them like this 😞 . The spec is less prescriptive than it used to be about configuration, so we should carefully consider whether this should be more convenient. To my mind, I'd prefer to configure the tracer_provider directly, e.g.:

OpenTelemetry.tracer_provider.sampler = OpenTelemetry::SDK::Trace::Samplers.parent_or_else(OpenTelemetry::SDK::Trace::Samplers.probability(0.001))

There's still heavy nesting of the Samplers namespace - I don't have good ideas of how to fix that - I played around with a DSL, but it seems less direct and more complicated, and not as useful if you have custom samplers.

I suppose we could tweak the interface of parent_or_else to handle all the built-in samplers:

OpenTelemetry.tracer_provider.sampler = OpenTelemetry::SDK::Trace::Samplers.parent_or_else(probability: 0.001)
OpenTelemetry.tracer_provider.sampler = OpenTelemetry::SDK::Trace::Samplers.parent_or_else(:always_on)
OpenTelemetry.tracer_provider.sampler = OpenTelemetry::SDK::Trace::Samplers.parent_or_else(:always_off)
OpenTelemetry.tracer_provider.sampler = OpenTelemetry::SDK::Trace::Samplers.parent_or_else(MyCustomSampler.new)

Note that the spec for Probability Sampler has changed since our implementation, and it no longer needs to handle the parent case -- that's expected to be done by wrapping it in parent_or_else.

fbogsany avatar Aug 19 '20 18:08 fbogsany

Thanks, what about exposing the sampler as an SDK configuration method? something like

OpenTelemetry::SDK.configure do |config|
  config.sampler OpenTelemetry::SDK::Trace::Samplers.parent_or_probability(0.001)
  ...
end

Asafb26 avatar Aug 19 '20 21:08 Asafb26

Thanks, what about exposing the sampler as an SDK configuration method?

We can do that, but the spec also allows the sampler to be replaced after configuration (MAY rather than MUST, but we already support it). Also, I think we want to be clear that this is the trace sampler.

Anyway, I'll address the SDK configuration in a separate PR.

fbogsany avatar Aug 20 '20 01:08 fbogsany

And actually, I think we want to make the configuration even less wordy by providing helpers for the standard samplers:

OpenTelemetry::SDK.configure do |c|
  c.trace_sampler = c.parent_or_else(c.probability(0.001))
  ...
end

fbogsany avatar Aug 20 '20 01:08 fbogsany

Is this implemented yet? i.e. is there now a way to set overall sampling probability in the global OpenTelemetry::SDK configuration?

johnnyshields avatar Jul 19 '22 18:07 johnnyshields

@johnnyshields I actually don't think the configurator block supports this yet - I believe the best way to do it globally is via environment variables. See the test suite here for examples: https://github.com/open-telemetry/opentelemetry-ruby/blob/0f7d7858aa413aea10db430187393e166df937b2/sdk/test/opentelemetry/sdk/trace/tracer_provider_test.rb#L20

@fbogsany This is probably something we should still plan on doing, yeah?

ahayworth avatar Jul 19 '22 22:07 ahayworth

@ahayworth thank you. Would be great if configurator could support it as well.

johnnyshields avatar Jul 20 '22 18:07 johnnyshields

👋 This issue has been marked as stale because it has been open with no activity. You can: comment on the issue or remove the stale label to hold stale off for a while, add the keep label to hold stale off permanently, or do nothing. If you do nothing this issue will be closed eventually by the stale bot.

github-actions[bot] avatar Apr 27 '24 01:04 github-actions[bot]