|
30 | 30 | DEFAULT_ADD_FULL_STACK, |
31 | 31 | DEFAULT_MAX_STACK_FRAMES, |
32 | 32 | DEFAULT_MAX_VALUE_LENGTH, |
| 33 | + SPANDATA, |
33 | 34 | EndpointType, |
34 | 35 | ) |
35 | 36 | from sentry_sdk._types import Annotated, AnnotatedValue, SENSITIVE_DATA_SUBSTITUTE |
@@ -1726,6 +1727,85 @@ def is_sentry_url(client, url): |
1726 | 1727 | ) |
1727 | 1728 |
|
1728 | 1729 |
|
| 1730 | +def serialize_attribute(value): |
| 1731 | + # type: (Any) -> AttributeValue |
| 1732 | + # check for allowed primitives |
| 1733 | + if isinstance(value, (int, str, float, bool)): |
| 1734 | + return value |
| 1735 | + |
| 1736 | + # lists are allowed too, as long as they don't mix types |
| 1737 | + if isinstance(value, (list, tuple)): |
| 1738 | + for type_ in (int, str, float, bool): |
| 1739 | + if all(isinstance(item, type_) for item in value): |
| 1740 | + return list(value) |
| 1741 | + |
| 1742 | + return safe_repr(value) |
| 1743 | + |
| 1744 | + |
| 1745 | +def attribute_value_to_transport_format(value): |
| 1746 | + # type: (Any) -> dict[str, bool | str | int | float] |
| 1747 | + if isinstance(value, bool): |
| 1748 | + return {"value": value, "type": "boolean"} |
| 1749 | + |
| 1750 | + if isinstance(value, int): |
| 1751 | + return {"value": value, "type": "integer"} |
| 1752 | + |
| 1753 | + if isinstance(value, float): |
| 1754 | + return {"value": value, "type": "double"} |
| 1755 | + |
| 1756 | + if isinstance(value, str): |
| 1757 | + return {"value": value, "type": "string"} |
| 1758 | + |
| 1759 | + return {"value": safe_repr(value), "type": "string"} |
| 1760 | + |
| 1761 | + |
| 1762 | +def get_default_attributes(): |
| 1763 | + # type: () -> Attributes |
| 1764 | + # TODO[ivana]: standardize attr names into an enum/take from sentry convs |
| 1765 | + from sentry_sdk.client import SDK_INFO |
| 1766 | + from sentry_sdk.scope import should_send_default_pii |
| 1767 | + |
| 1768 | + attributes = {} |
| 1769 | + |
| 1770 | + attributes["sentry.sdk.name"] = SDK_INFO["name"] |
| 1771 | + attributes["sentry.sdk.version"] = SDK_INFO["version"] |
| 1772 | + |
| 1773 | + options = sentry_sdk.get_client().options |
| 1774 | + |
| 1775 | + server_name = options.get("server_name") |
| 1776 | + if server_name is not None: |
| 1777 | + attributes[SPANDATA.SERVER_ADDRESS] = server_name |
| 1778 | + |
| 1779 | + environment = options.get("environment") |
| 1780 | + if environment is not None: |
| 1781 | + attributes["sentry.environment"] = environment |
| 1782 | + |
| 1783 | + release = options.get("release") |
| 1784 | + if release is not None: |
| 1785 | + attributes["sentry.release"] = release |
| 1786 | + |
| 1787 | + thread_id, thread_name = get_current_thread_meta() |
| 1788 | + if thread_id is not None: |
| 1789 | + attributes["thread.id"] = thread_id |
| 1790 | + if thread_name is not None: |
| 1791 | + attributes["thread.name"] = thread_name |
| 1792 | + |
| 1793 | + # The user, if present, is always set on the isolation scope. |
| 1794 | + # TODO[ivana]: gate behing PII? |
| 1795 | + if should_send_default_pii(): |
| 1796 | + isolation_scope = sentry_sdk.get_isolation_scope() |
| 1797 | + if isolation_scope._user is not None: |
| 1798 | + for attribute, user_attribute in ( |
| 1799 | + ("user.id", "id"), |
| 1800 | + ("user.name", "username"), |
| 1801 | + ("user.email", "email"), |
| 1802 | + ): |
| 1803 | + if attribute in isolation_scope._user: |
| 1804 | + attributes[attribute] = isolation_scope._user[user_attribute] |
| 1805 | + |
| 1806 | + return attributes |
| 1807 | + |
| 1808 | + |
1729 | 1809 | def _generate_installed_modules(): |
1730 | 1810 | # type: () -> Iterator[Tuple[str, str]] |
1731 | 1811 | try: |
@@ -2076,20 +2156,3 @@ def get_before_send_metric(options): |
2076 | 2156 | return options.get("before_send_metric") or options["_experiments"].get( |
2077 | 2157 | "before_send_metric" |
2078 | 2158 | ) |
2079 | | - |
2080 | | - |
2081 | | -def format_attribute_value(value): |
2082 | | - # type: (Any) -> dict[str, bool | str | int | float] |
2083 | | - if isinstance(value, bool): |
2084 | | - return {"value": value, "type": "boolean"} |
2085 | | - |
2086 | | - if isinstance(value, int): |
2087 | | - return {"value": value, "type": "integer"} |
2088 | | - |
2089 | | - if isinstance(value, float): |
2090 | | - return {"value": value, "type": "double"} |
2091 | | - |
2092 | | - if isinstance(value, str): |
2093 | | - return {"value": value, "type": "string"} |
2094 | | - |
2095 | | - return {"value": safe_repr(value), "type": "string"} |
|
0 commit comments