-
Notifications
You must be signed in to change notification settings - Fork 818
feat(traceloop-sdk): add sampling_rate parameter to control trace volume #3409
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
Open
Nikhil172913832
wants to merge
3
commits into
traceloop:main
Choose a base branch
from
Nikhil172913832:fixing_issue_2109
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| """Tests for sampling_rate parameter functionality.""" | ||
|
|
||
| import pytest | ||
| from opentelemetry.sdk.trace.sampling import TraceIdRatioBased | ||
| from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter | ||
| from traceloop.sdk import Traceloop | ||
| from traceloop.sdk.tracing.tracing import TracerWrapper | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def clean_tracer_wrapper(): | ||
| """Fixture to manage TracerWrapper global state.""" | ||
| original_instance = None | ||
| if hasattr(TracerWrapper, "instance"): | ||
| original_instance = TracerWrapper.instance | ||
| del TracerWrapper.instance | ||
|
|
||
| yield | ||
|
|
||
| if hasattr(TracerWrapper, "instance"): | ||
| del TracerWrapper.instance | ||
| if original_instance is not None: | ||
| TracerWrapper.instance = original_instance | ||
|
|
||
|
|
||
| class TestSamplingRate: | ||
| """Test class for sampling_rate parameter functionality.""" | ||
|
|
||
| def test_init_with_sampling_rate(self, clean_tracer_wrapper): | ||
| """Test initialization with sampling_rate parameter.""" | ||
| exporter = InMemorySpanExporter() | ||
|
|
||
| client = Traceloop.init( | ||
| app_name="test-sampling-rate", | ||
| sampling_rate=0.5, | ||
| exporter=exporter, | ||
| disable_batch=True | ||
| ) | ||
|
|
||
| assert client is None | ||
| assert hasattr(TracerWrapper, "instance") | ||
| assert TracerWrapper.instance is not None | ||
|
|
||
| # Verify that a tracer provider was created with a sampler | ||
| tracer_provider = TracerWrapper.instance._TracerWrapper__tracer_provider | ||
| assert tracer_provider is not None | ||
| assert tracer_provider.sampler is not None | ||
|
|
||
| def test_sampling_rate_validation(self, clean_tracer_wrapper): | ||
| """Test that sampling_rate validates input range.""" | ||
| exporter = InMemorySpanExporter() | ||
|
|
||
| with pytest.raises(ValueError, match="sampling_rate must be between 0.0 and 1.0"): | ||
| Traceloop.init( | ||
| app_name="test-invalid-sampling-rate", | ||
| sampling_rate=1.5, | ||
| exporter=exporter, | ||
| disable_batch=True | ||
| ) | ||
|
|
||
| with pytest.raises(ValueError, match="sampling_rate must be between 0.0 and 1.0"): | ||
| Traceloop.init( | ||
| app_name="test-invalid-sampling-rate", | ||
| sampling_rate=-0.1, | ||
| exporter=exporter, | ||
| disable_batch=True | ||
| ) | ||
|
|
||
| def test_sampling_rate_and_sampler_conflict(self, clean_tracer_wrapper): | ||
| """Test that providing both sampling_rate and sampler raises an error.""" | ||
| exporter = InMemorySpanExporter() | ||
| sampler = TraceIdRatioBased(0.5) | ||
|
|
||
| with pytest.raises(ValueError, match="Cannot specify both 'sampler' and 'sampling_rate'"): | ||
| Traceloop.init( | ||
| app_name="test-conflict", | ||
| sampling_rate=0.5, | ||
| sampler=sampler, | ||
| exporter=exporter, | ||
| disable_batch=True | ||
| ) | ||
|
|
||
| def test_sampling_rate_edge_cases(self, clean_tracer_wrapper): | ||
| """Test sampling_rate with edge case values (0.0 and 1.0).""" | ||
| exporter = InMemorySpanExporter() | ||
|
|
||
| # Test 0.0 (no sampling) | ||
| client = Traceloop.init( | ||
| app_name="test-sampling-zero", | ||
| sampling_rate=0.0, | ||
| exporter=exporter, | ||
| disable_batch=True | ||
| ) | ||
| assert client is None | ||
| assert hasattr(TracerWrapper, "instance") | ||
|
|
||
| tracer_provider = TracerWrapper.instance._TracerWrapper__tracer_provider | ||
| assert tracer_provider.sampler is not None | ||
|
|
||
| del TracerWrapper.instance | ||
|
|
||
| # Test 1.0 (full sampling) | ||
| client = Traceloop.init( | ||
| app_name="test-sampling-one", | ||
| sampling_rate=1.0, | ||
| exporter=exporter, | ||
| disable_batch=True | ||
| ) | ||
| assert client is None | ||
| assert hasattr(TracerWrapper, "instance") | ||
|
|
||
| tracer_provider = TracerWrapper.instance._TracerWrapper__tracer_provider | ||
| assert tracer_provider.sampler is not None | ||
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 |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ | |
| from typing import Callable, List, Optional, Set, Union | ||
| from colorama import Fore | ||
| from opentelemetry.sdk.trace import SpanProcessor, ReadableSpan | ||
| from opentelemetry.sdk.trace.sampling import Sampler | ||
| from opentelemetry.sdk.trace.sampling import Sampler, TraceIdRatioBased | ||
| from opentelemetry.sdk.trace.export import SpanExporter | ||
| from opentelemetry.sdk.metrics.export import MetricExporter | ||
| from opentelemetry.sdk._logs.export import LogExporter | ||
|
|
@@ -62,6 +62,7 @@ def init( | |
| processor: Optional[Union[SpanProcessor, List[SpanProcessor]]] = None, | ||
| propagator: TextMapPropagator = None, | ||
| sampler: Optional[Sampler] = None, | ||
| sampling_rate: Optional[float] = None, | ||
| traceloop_sync_enabled: bool = False, | ||
| should_enrich_metrics: bool = True, | ||
| resource_attributes: dict = {}, | ||
|
|
@@ -136,6 +137,19 @@ def init( | |
|
|
||
| print(Fore.RESET) | ||
|
|
||
| if sampling_rate is not None and sampler is not None: | ||
| raise ValueError( | ||
| "Cannot specify both 'sampler' and 'sampling_rate'. " | ||
| "Please use only one of these parameters." | ||
| ) | ||
|
|
||
| if sampling_rate is not None: | ||
| if not 0.0 <= sampling_rate <= 1.0: | ||
| raise ValueError( | ||
| f"sampling_rate must be between 0.0 and 1.0, got {sampling_rate}" | ||
| ) | ||
| sampler = TraceIdRatioBased(sampling_rate) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a no-op unless you also set it in the tracer provider: |
||
|
|
||
| # Tracer init | ||
| resource_attributes.update({SERVICE_NAME: app_name}) | ||
| TracerWrapper.set_static_params( | ||
|
|
||
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.
Don't use these low-quality autogenerated test - they don't really test anything. You should run a real-world test - like the other ones we have here