-
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
base: master
Are you sure you want to change the base?
Changes from all commits
270be59
c46c32d
9be698f
ddb5838
329ea2c
6c1897a
8ffc78a
2405282
a0a603b
a2fee7c
87537f0
8c32833
3747c5f
4dc5dd8
6acb510
2bf0f3a
52bcfee
c83d76c
a62d90b
649c3ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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: | ||
|
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.
|
||
| 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_getter = { | ||
| "log": lambda: get_before_send_log(self.options), | ||
| "metric": lambda: get_before_send_metric(self.options), | ||
| }.get(ty) | ||
|
Comment on lines
+910
to
+913
Contributor
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. Could this return a function instead of a lambda that returns a function?
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. imo this causes useless allocations, just use an if else |
||
|
|
||
| if self.log_batcher: | ||
| self.log_batcher.add(log) | ||
| if before_send_getter is not None: | ||
| before_send = before_send_getter() | ||
| if before_send is not None: | ||
| telemetry = before_send(telemetry, {}) # type: ignore[arg-type] | ||
|
|
||
| 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: "Optional[Union[LogBatcher, MetricsBatcher]]" = { | ||
| "log": self.log_batcher, | ||
| "metric": self.metrics_batcher, | ||
| }.get(ty) | ||
|
|
||
| metric["attributes"]["sentry.sdk.name"] = SDK_INFO["name"] | ||
| metric["attributes"]["sentry.sdk.version"] = SDK_INFO["version"] | ||
| if batcher: | ||
| batcher.add(telemetry) # type: ignore[arg-type] | ||
|
|
||
| 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, | ||
|
|
||
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.
I'm probably out of the loop on this. According to the develop docs we don't currently support array attributes.
Either we should update the develop docs or this type is too permissive, at least when it comes to what services after the SDK support.
https://develop.sentry.dev/sdk/telemetry/logs/#default-attributes