Skip to content

fix(scores): skip sampling if OTEL sampler not available #1241

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 2 commits into from
Jul 4, 2025
Merged
Changes from all commits
Commits
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
30 changes: 18 additions & 12 deletions langfuse/_client/resource_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,20 +277,26 @@ def add_score_task(self, event: dict, *, force_sample: bool = False):
try:
# Sample scores with the same sampler that is used for tracing
tracer_provider = cast(TracerProvider, otel_trace_api.get_tracer_provider())
should_sample = force_sample or (
(
tracer_provider.sampler.should_sample(
parent_context=None,
trace_id=int(event["body"].trace_id, 16),
name="score",
).decision
== Decision.RECORD_AND_SAMPLE
if hasattr(event["body"], "trace_id")
should_sample = (
force_sample
or isinstance(
tracer_provider, otel_trace_api.ProxyTracerProvider
) # default to in-sample if otel sampler is not available
or (
(
tracer_provider.sampler.should_sample(
parent_context=None,
trace_id=int(event["body"].trace_id, 16),
name="score",
).decision
== Decision.RECORD_AND_SAMPLE
if hasattr(event["body"], "trace_id")
else True
)
if event["body"].trace_id
is not None # do not sample out session / dataset run scores
else True
)
if event["body"].trace_id
is not None # do not sample out session / dataset run scores
else True
)

if should_sample:
Expand Down
Loading