-
Notifications
You must be signed in to change notification settings - Fork 568
feat(tracemetrics): Add sample_rate to metrics api calls #5070
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
k-fish
wants to merge
4
commits into
master
Choose a base branch
from
feat/tracemetrics/add-sample-rate-function
base: master
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.
+179
−12
Open
Changes from all commits
Commits
Show all changes
4 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 |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| ) | ||
| from sentry_sdk.serializer import serialize | ||
| from sentry_sdk.tracing import trace | ||
| from sentry_sdk.tracing_utils import _generate_sample_rand | ||
| from sentry_sdk.transport import BaseHttpTransport, make_transport | ||
| from sentry_sdk.consts import ( | ||
| SPANDATA, | ||
|
|
@@ -181,7 +182,9 @@ class BaseClient: | |
|
|
||
| def __init__(self, options=None): | ||
| # type: (Optional[Dict[str, Any]]) -> None | ||
| self.options = options if options is not None else DEFAULT_OPTIONS # type: Dict[str, Any] | ||
| self.options = ( | ||
| options if options is not None else DEFAULT_OPTIONS | ||
| ) # type: Dict[str, Any] | ||
|
|
||
| self.transport = None # type: Optional[Transport] | ||
| self.monitor = None # type: Optional[Monitor] | ||
|
|
@@ -614,7 +617,9 @@ def _prepare_event( | |
| event_scrubber.scrub_event(event) | ||
|
|
||
| if scope is not None and scope._gen_ai_original_message_count: | ||
| spans = event.get("spans", []) # type: List[Dict[str, Any]] | AnnotatedValue | ||
| spans = event.get( | ||
| "spans", [] | ||
| ) # type: List[Dict[str, Any]] | AnnotatedValue | ||
| if isinstance(spans, list): | ||
| for span in spans: | ||
| span_id = span.get("span_id", None) | ||
|
|
@@ -1000,6 +1005,50 @@ def _capture_metric(self, metric): | |
| current_scope = sentry_sdk.get_current_scope() | ||
| isolation_scope = sentry_sdk.get_isolation_scope() | ||
|
|
||
| # Determine trace_id and span_id using the same logic as the original metrics.py | ||
| trace_id = None | ||
| span_id = None | ||
| if current_scope.span is not None: | ||
| trace_id = current_scope.span.trace_id | ||
| span_id = current_scope.span.span_id | ||
| elif current_scope._propagation_context is not None: | ||
| trace_id = current_scope._propagation_context.trace_id | ||
| span_id = current_scope._propagation_context.span_id | ||
|
|
||
| sample_rate = metric["attributes"].get("sentry.client_sample_rate") | ||
| if sample_rate is not None: | ||
| sample_rate = float(sample_rate) | ||
|
|
||
| # Always validate sample_rate range, regardless of trace context | ||
| if sample_rate <= 0.0 or sample_rate > 1.0: | ||
| if self.transport is not None: | ||
| self.transport.record_lost_event( | ||
| "invalid_sample_rate", | ||
| data_category="trace_metric", | ||
| quantity=1, | ||
| ) | ||
| return | ||
|
|
||
| # If there's no trace context, remove the sample_rate attribute and continue | ||
| if trace_id is None: | ||
| del metric["attributes"]["sentry.client_sample_rate"] | ||
| else: | ||
| # There is a trace context, apply sampling logic | ||
| if sample_rate < 1.0: | ||
| sample_rand = _generate_sample_rand(trace_id) | ||
| if sample_rand >= sample_rate: | ||
| if self.transport is not None: | ||
| self.transport.record_lost_event( | ||
| "sample_rate", | ||
| data_category="trace_metric", | ||
| quantity=1, | ||
| ) | ||
| return | ||
|
|
||
| # If sample_rate is 1.0, remove the attribute as it's implied | ||
| if sample_rate == 1.0: | ||
| del metric["attributes"]["sentry.client_sample_rate"] | ||
|
|
||
| metric["attributes"]["sentry.sdk.name"] = SDK_INFO["name"] | ||
| metric["attributes"]["sentry.sdk.version"] = SDK_INFO["version"] | ||
|
|
||
|
|
@@ -1011,10 +1060,6 @@ def _capture_metric(self, metric): | |
| if release is not None and "sentry.release" not in metric["attributes"]: | ||
| metric["attributes"]["sentry.release"] = release | ||
|
|
||
| trace_context = current_scope.get_trace_context() | ||
|
Member
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. please do not revert this and use the underlying |
||
| trace_id = trace_context.get("trace_id") | ||
| span_id = trace_context.get("span_id") | ||
|
|
||
| metric["trace_id"] = trace_id or "00000000-0000-0000-0000-000000000000" | ||
| if span_id is not None: | ||
| metric["span_id"] = span_id | ||
|
|
||
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
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.
Would also add a debug log.