-
Notifications
You must be signed in to change notification settings - Fork 569
ref: Make logs, metrics go via scope #5213
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
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
270be59
ref: Make logs, metrics go via scope
sentrivana c46c32d
Merge branch 'master' into ivana/make-logs-and-metrics-go-via-scope
sentrivana 9be698f
Merge branch 'master' into ivana/make-logs-and-metrics-go-via-scope
sentrivana ddb5838
Merge branch 'master' into ivana/make-logs-and-metrics-go-via-scope
sentrivana 329ea2c
typing fixes
sentrivana 6c1897a
giving up on typing dispatches
sentrivana 8ffc78a
span_id is not an attribute anymore
sentrivana 2405282
move format_attributes to utils
sentrivana a0a603b
attr list values
sentrivana a2fee7c
.
sentrivana 87537f0
add link
sentrivana 8c32833
remove custom trace_id, span_id setting
sentrivana 3747c5f
rename, fix
sentrivana 4dc5dd8
.
sentrivana 6acb510
simplify
sentrivana 2bf0f3a
.
sentrivana 52bcfee
Merge branch 'master' into ivana/make-logs-and-metrics-go-via-scope
sentrivana c83d76c
.
sentrivana a62d90b
first attrs, then before_send
sentrivana 649c3ad
dont pass opts around
sentrivana 3fffa7f
simplify dispatcher
sentrivana 7ccbd5a
no support for array attributes yet
sentrivana d2cbf74
Merge branch 'master' into ivana/make-logs-and-metrics-go-via-scope
sentrivana 9d8b3d2
put preserialization back
sentrivana 0b25f46
preserialize after template, parameters
sentrivana 3eac621
Merge branch 'master' into ivana/make-logs-and-metrics-go-via-scope
sentrivana ef5f9fb
fix
sentrivana 603da41
Merge branch 'master' into ivana/make-logs-and-metrics-go-via-scope
sentrivana 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
Some comments aren't visible on the classic Files Changed page.
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
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
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 |
|---|---|---|
|
|
@@ -217,10 +217,10 @@ def is_active(self) -> bool: | |
| def capture_event(self, *args: "Any", **kwargs: "Any") -> "Optional[str]": | ||
| return None | ||
|
|
||
| def _capture_log(self, log: "Log") -> None: | ||
| def _capture_log(self, log: "Log", scope: "Scope") -> None: | ||
| pass | ||
|
|
||
| def _capture_metric(self, metric: "Metric") -> None: | ||
| def _capture_metric(self, metric: "Metric", scope: "Scope") -> None: | ||
| pass | ||
|
|
||
| def capture_session(self, *args: "Any", **kwargs: "Any") -> None: | ||
|
|
@@ -898,132 +898,41 @@ def capture_event( | |
|
|
||
| return return_value | ||
|
|
||
| def _capture_log(self, log: "Optional[Log]") -> None: | ||
| if not has_logs_enabled(self.options) or log is None: | ||
| def _capture_telemetry( | ||
|
Contributor
Author
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 now essentially a dispatcher on the |
||
| self, telemetry: "Optional[Union[Log, Metric]]", ty: str, scope: "Scope" | ||
| ) -> None: | ||
| # Capture attributes-based telemetry (logs, metrics, spansV2) | ||
| if telemetry is None: | ||
| return | ||
|
|
||
| current_scope = sentry_sdk.get_current_scope() | ||
| isolation_scope = sentry_sdk.get_isolation_scope() | ||
|
|
||
| log["attributes"]["sentry.sdk.name"] = SDK_INFO["name"] | ||
| log["attributes"]["sentry.sdk.version"] = SDK_INFO["version"] | ||
|
|
||
| server_name = self.options.get("server_name") | ||
| if server_name is not None and SPANDATA.SERVER_ADDRESS not in log["attributes"]: | ||
| log["attributes"][SPANDATA.SERVER_ADDRESS] = server_name | ||
|
|
||
| environment = self.options.get("environment") | ||
| if environment is not None and "sentry.environment" not in log["attributes"]: | ||
| log["attributes"]["sentry.environment"] = environment | ||
|
|
||
| release = self.options.get("release") | ||
| if release is not None and "sentry.release" not in log["attributes"]: | ||
| log["attributes"]["sentry.release"] = release | ||
|
|
||
| trace_context = current_scope.get_trace_context() | ||
| trace_id = trace_context.get("trace_id") | ||
| span_id = trace_context.get("span_id") | ||
|
|
||
| if trace_id is not None and log.get("trace_id") is None: | ||
| log["trace_id"] = trace_id | ||
|
|
||
| if span_id is not None and log.get("span_id") is None: | ||
| log["span_id"] = span_id | ||
|
|
||
| # The user, if present, is always set on the isolation scope. | ||
| if self.should_send_default_pii() and isolation_scope._user is not None: | ||
| for log_attribute, user_attribute in ( | ||
| ("user.id", "id"), | ||
| ("user.name", "username"), | ||
| ("user.email", "email"), | ||
| ): | ||
| if ( | ||
| user_attribute in isolation_scope._user | ||
| and log_attribute not in log["attributes"] | ||
| ): | ||
| log["attributes"][log_attribute] = isolation_scope._user[ | ||
| user_attribute | ||
| ] | ||
|
|
||
| # If debug is enabled, log the log to the console | ||
| debug = self.options.get("debug", False) | ||
| if debug: | ||
| logger.debug( | ||
| f"[Sentry Logs] [{log.get('severity_text')}] {log.get('body')}" | ||
| ) | ||
|
|
||
| before_send_log = get_before_send_log(self.options) | ||
| if before_send_log is not None: | ||
| log = before_send_log(log, {}) | ||
| scope.apply_to_telemetry(telemetry) | ||
|
|
||
| if log is None: | ||
| return | ||
| before_send = None | ||
| if ty == "log": | ||
| before_send = get_before_send_log(self.options) | ||
| elif ty == "metric": | ||
| before_send = get_before_send_metric(self.options) # type: ignore | ||
|
|
||
| if self.log_batcher: | ||
| self.log_batcher.add(log) | ||
| if before_send is not None: | ||
| telemetry = before_send(telemetry, {}) # type: ignore | ||
|
|
||
| def _capture_metric(self, metric: "Optional[Metric]") -> None: | ||
| if not has_metrics_enabled(self.options) or metric is None: | ||
| if telemetry is None: | ||
| return | ||
|
|
||
| current_scope = sentry_sdk.get_current_scope() | ||
| isolation_scope = sentry_sdk.get_isolation_scope() | ||
| batcher = None | ||
| if ty == "log": | ||
| batcher = self.log_batcher | ||
| elif ty == "metric": | ||
| batcher = self.metrics_batcher # type: ignore | ||
|
|
||
| metric["attributes"]["sentry.sdk.name"] = SDK_INFO["name"] | ||
| metric["attributes"]["sentry.sdk.version"] = SDK_INFO["version"] | ||
| if batcher is not None: | ||
| batcher.add(telemetry) # type: ignore | ||
|
|
||
| server_name = self.options.get("server_name") | ||
| if ( | ||
| server_name is not None | ||
| and SPANDATA.SERVER_ADDRESS not in metric["attributes"] | ||
| ): | ||
| metric["attributes"][SPANDATA.SERVER_ADDRESS] = server_name | ||
|
|
||
| environment = self.options.get("environment") | ||
| if environment is not None and "sentry.environment" not in metric["attributes"]: | ||
| metric["attributes"]["sentry.environment"] = environment | ||
|
|
||
| release = self.options.get("release") | ||
| if release is not None and "sentry.release" not in metric["attributes"]: | ||
| metric["attributes"]["sentry.release"] = release | ||
|
|
||
| trace_context = current_scope.get_trace_context() | ||
| 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 | ||
|
|
||
| if self.should_send_default_pii() and isolation_scope._user is not None: | ||
| for metric_attribute, user_attribute in ( | ||
| ("user.id", "id"), | ||
| ("user.name", "username"), | ||
| ("user.email", "email"), | ||
| ): | ||
| if ( | ||
| user_attribute in isolation_scope._user | ||
| and metric_attribute not in metric["attributes"] | ||
| ): | ||
| metric["attributes"][metric_attribute] = isolation_scope._user[ | ||
| user_attribute | ||
| ] | ||
|
|
||
| debug = self.options.get("debug", False) | ||
| if debug: | ||
| logger.debug( | ||
| f"[Sentry Metrics] [{metric.get('type')}] {metric.get('name')}: {metric.get('value')}" | ||
| ) | ||
|
|
||
| before_send_metric = get_before_send_metric(self.options) | ||
| if before_send_metric is not None: | ||
| metric = before_send_metric(metric, {}) | ||
|
|
||
| if metric is None: | ||
| return | ||
| def _capture_log(self, log: "Optional[Log]", scope: "Scope") -> None: | ||
| self._capture_telemetry(log, "log", scope) | ||
|
|
||
| if self.metrics_batcher: | ||
| self.metrics_batcher.add(metric) | ||
| def _capture_metric(self, metric: "Optional[Metric]", scope: "Scope") -> None: | ||
| self._capture_telemetry(metric, "metric", scope) | ||
|
|
||
| def capture_session( | ||
| self, | ||
|
|
||
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
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.
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.
scopehere is the merged global + iso + current scope.