Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion sentry_sdk/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import time
from typing import TYPE_CHECKING, Any

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.utils import capture_internal_exceptions, format_attribute

Expand Down Expand Up @@ -46,7 +48,7 @@ def _capture_log(

if kwargs:
# only attach template if there are parameters
attributes["sentry.message.template"] = format_attribute(template)
attributes[ATTRIBUTE_NAMES.SENTRY_MESSAGE_TEMPLATE] = format_attribute(template)

with capture_internal_exceptions():
body = template.format_map(_dict_default_key(kwargs))
Expand Down
36 changes: 19 additions & 17 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
from itertools import chain
from typing import TYPE_CHECKING

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk._types import AnnotatedValue
from sentry_sdk.attachments import Attachment
from sentry_sdk.consts import (
DEFAULT_MAX_BREADCRUMBS,
FALSE_VALUES,
SPANDATA,
)
from sentry_sdk.feature_flags import DEFAULT_FLAG_CAPACITY, FlagBuffer
from sentry_sdk.session import Session
Expand Down Expand Up @@ -343,31 +344,31 @@ def get_global_scope(cls) -> "Scope":
return _global_scope

def set_global_attributes(self) -> None:
self.set_attribute(SPANDATA.SENTRY_SDK_NAME, SDK_INFO["name"])
self.set_attribute(SPANDATA.SENTRY_SDK_VERSION, SDK_INFO["version"])
self.set_attribute(ATTRIBUTE_NAMES.SENTRY_SDK_NAME, SDK_INFO["name"])
self.set_attribute(ATTRIBUTE_NAMES.SENTRY_SDK_VERSION, SDK_INFO["version"])

self.set_attribute(
"process.runtime.name",
ATTRIBUTE_NAMES.PROCESS_RUNTIME_NAME,
platform.python_implementation(),
)
self.set_attribute(
"process.runtime.version",
ATTRIBUTE_NAMES.PROCESS_RUNTIME_VERSION,
f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}",
)

options = sentry_sdk.get_client().options

server_name = options.get("server_name")
if server_name:
self.set_attribute(SPANDATA.SERVER_ADDRESS, server_name)
self.set_attribute(ATTRIBUTE_NAMES.SERVER_ADDRESS, server_name)

environment = options.get("environment")
if environment:
self.set_attribute(SPANDATA.SENTRY_ENVIRONMENT, environment)
self.set_attribute(ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT, environment)

release = options.get("release")
if release:
self.set_attribute(SPANDATA.SENTRY_RELEASE, release)
self.set_attribute(ATTRIBUTE_NAMES.SENTRY_RELEASE, release)

@classmethod
def last_event_id(cls) -> "Optional[str]":
Expand Down Expand Up @@ -704,7 +705,8 @@ def set_transaction_name(self, name: str, source: "Optional[str]" = None) -> Non
self._span._segment.name = name
if source:
self._span._segment.set_attribute(
"sentry.segment.name.source", getattr(source, "value", source)
ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE,
getattr(source, "value", source),
)

if source:
Expand Down Expand Up @@ -734,18 +736,18 @@ def span(self, span: "Optional[Span]") -> None:

if type(span) is Span:
self._transaction = span.name
if span._attributes.get("sentry.segment.name.source"):
if span._attributes.get(ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE):
self._transaction_info["source"] = str(
span._attributes["sentry.segment.name.source"]
span._attributes[ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE]
)
return

if type(span) is NoOpSpan:
if span._name is not None:
self._transaction = span.name
if span._attributes.get("sentry.segment.name.source"):
if span._attributes.get(ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE):
self._transaction_info["source"] = str(
span._attributes["sentry.segment.name.source"]
span._attributes[ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE]
)

def set_tag(self, key: str, value: "Any") -> None:
Expand Down Expand Up @@ -1439,10 +1441,10 @@ def _apply_user_attributes_to_telemetry(
return

for attribute_name, user_attribute in (
("user.id", "id"),
("user.name", "username"),
("user.email", "email"),
("user.ip_address", "ip_address"),
(ATTRIBUTE_NAMES.USER_ID, "id"),
(ATTRIBUTE_NAMES.USER_NAME, "username"),
(ATTRIBUTE_NAMES.USER_EMAIL, "email"),
(ATTRIBUTE_NAMES.USER_IP_ADDRESS, "ip_address"),
):
if (
user_attribute in self._user
Expand Down
69 changes: 41 additions & 28 deletions sentry_sdk/traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
from enum import Enum
from typing import TYPE_CHECKING

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.consts import SPANDATA
from sentry_sdk.profiler.continuous_profiler import (
get_profiler_id,
try_autostart_continuous_profiler,
Expand Down Expand Up @@ -211,8 +212,8 @@ def __init__(
self._name: str = name
self._active: bool = active
self._attributes: "Attributes" = {
"sentry.origin": "manual",
"sentry.trace_lifecycle": "stream",
ATTRIBUTE_NAMES.SENTRY_ORIGIN: "manual",
ATTRIBUTE_NAMES.SENTRY_TRACE_LIFECYCLE: "stream",
}

if attributes:
Expand Down Expand Up @@ -322,19 +323,23 @@ def _end(self, end_timestamp: "Optional[Union[float, datetime]]" = None) -> None
# Set attributes from the segment. These are set on span end on purpose
# so that we have the best chance to capture the segment's final name
# (since it might change during its lifetime)
self.set_attribute("sentry.segment.id", self._segment.span_id)
self.set_attribute("sentry.segment.name", self._segment.name)
self.set_attribute(ATTRIBUTE_NAMES.SENTRY_SEGMENT_ID, self._segment.span_id)
self.set_attribute(ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME, self._segment.name)

# Set conversation ID if one is set and this is a correct kind of span
conversation_id = self._scope.get_conversation_id()
if conversation_id:
has_ai_operation_name = SPANDATA.GEN_AI_OPERATION_NAME in self._attributes
op = self._attributes.get("sentry.op")
has_ai_operation_name = (
ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME in self._attributes
)
op = self._attributes.get(ATTRIBUTE_NAMES.SENTRY_OP)
has_gen_ai_op = isinstance(op, str) and (
op.startswith("ai.") or op.startswith("gen_ai.")
)
if has_ai_operation_name or has_gen_ai_op:
self.set_attribute("gen_ai.conversation.id", conversation_id)
self.set_attribute(
ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID, conversation_id
)

# Set the end timestamp
if end_timestamp is not None:
Expand Down Expand Up @@ -457,10 +462,10 @@ def _update_active_thread(self) -> None:
thread_id, thread_name = get_current_thread_meta()

if thread_id is not None:
self.set_attribute(SPANDATA.THREAD_ID, str(thread_id))
self.set_attribute(ATTRIBUTE_NAMES.THREAD_ID, str(thread_id))

if thread_name is not None:
self.set_attribute(SPANDATA.THREAD_NAME, thread_name)
self.set_attribute(ATTRIBUTE_NAMES.THREAD_NAME, thread_name)

def _dynamic_sampling_context(self) -> "dict[str, str]":
return self._segment._get_baggage().dynamic_sampling_context()
Expand Down Expand Up @@ -516,16 +521,16 @@ def _get_trace_context(self) -> "dict[str, Any]":
"dynamic_sampling_context": self._dynamic_sampling_context(),
}

if "sentry.op" in self._attributes:
context["op"] = self._attributes["sentry.op"]
if "sentry.origin" in self._attributes:
context["origin"] = self._attributes["sentry.origin"]
if ATTRIBUTE_NAMES.SENTRY_OP in self._attributes:
context["op"] = self._attributes[ATTRIBUTE_NAMES.SENTRY_OP]
if ATTRIBUTE_NAMES.SENTRY_ORIGIN in self._attributes:
context["origin"] = self._attributes[ATTRIBUTE_NAMES.SENTRY_ORIGIN]

return context

def _set_profile_id(self, profiler_id: "Optional[str]") -> None:
if profiler_id is not None:
self.set_attribute("sentry.profiler_id", profiler_id)
self.set_attribute(ATTRIBUTE_NAMES.SENTRY_PROFILER_ID, profiler_id)

def _start_profile(self) -> None:
if not self._is_segment():
Expand All @@ -541,19 +546,23 @@ def _set_segment_attributes(self) -> None:

client = sentry_sdk.get_client()

self.set_attribute(SPANDATA.SENTRY_PLATFORM, "python")
self.set_attribute(SPANDATA.PROCESS_COMMAND_ARGS, sys.argv)
self.set_attribute(ATTRIBUTE_NAMES.SENTRY_PLATFORM, "python")
self.set_attribute(ATTRIBUTE_NAMES.PROCESS_COMMAND_ARGS, sys.argv)
self.set_attribute(
SPANDATA.SENTRY_SDK_INTEGRATIONS, sorted(client.integrations.keys())
ATTRIBUTE_NAMES.SENTRY_SDK_INTEGRATIONS, sorted(client.integrations.keys())
)
if not self.get_attributes().get("sentry.segment.name.source"):
if not self.get_attributes().get(ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE):
self.set_attribute(
"sentry.segment.name.source", SegmentNameSource.CUSTOM.value
ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE,
SegmentNameSource.CUSTOM.value,
)

if client.options.get("dist") and SPANDATA.SENTRY_DIST not in self._attributes:
if (
client.options.get("dist")
and ATTRIBUTE_NAMES.SENTRY_DIST not in self._attributes
):
self.set_attribute(
SPANDATA.SENTRY_DIST, str(client.options["dist"]).strip()
ATTRIBUTE_NAMES.SENTRY_DIST, str(client.options["dist"]).strip()
)

def _to_json(self) -> "SpanJSON":
Expand Down Expand Up @@ -604,9 +613,13 @@ def __init__(
) -> None:
self._name = name # type: ignore[assignment]
self._attributes = {}
if attributes is not None and "sentry.segment.name.source" in attributes:
if (
attributes is not None
and ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE in attributes
):
self.set_attribute(
"sentry.segment.name.source", attributes["sentry.segment.name.source"]
ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE,
attributes[ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE],
)

self._span_id: "Optional[str]" = None
Expand Down Expand Up @@ -685,17 +698,17 @@ def get_attributes(self) -> "Attributes":
return {}

def set_attribute(self, key: str, value: "AttributeValue") -> None:
if key != "sentry.segment.name.source":
if key != ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE:
return

super().set_attribute("sentry.segment.name.source", value)
super().set_attribute(ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE, value)

def set_attributes(self, attributes: "Attributes") -> None:
for key, value in attributes.items():
if key != "sentry.segment.name.source":
if key != ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE:
continue

self.set_attribute("sentry.segment.name.source", value)
self.set_attribute(ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE, value)

def remove_attribute(self, key: str) -> None:
pass
Expand Down
36 changes: 20 additions & 16 deletions sentry_sdk/tracing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
from typing import TYPE_CHECKING
from urllib.parse import quote, unquote

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.consts import OP
from sentry_sdk.data_collection import (
_apply_data_collection_filtering_to_query_string,
)
Expand Down Expand Up @@ -138,13 +140,15 @@ def record_sql_queries(

additional_attributes = {}
if query is not None:
additional_attributes["db.query.text"] = query
additional_attributes[ATTRIBUTE_NAMES.DB_QUERY_TEXT] = query

with sentry_sdk.start_span(
name="<unknown SQL query>" if query is None else query,
attributes={
"sentry.origin": span_origin,
"sentry.op": span_op_override_value if span_op_override_value else OP.DB,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: span_origin,
ATTRIBUTE_NAMES.SENTRY_OP: span_op_override_value
if span_op_override_value
else OP.DB,
**additional_attributes,
},
) as span:
Expand Down Expand Up @@ -188,14 +192,14 @@ def get_url_attributes(

url_full = parsed_url.url
if query:
attributes[SPANDATA.URL_QUERY] = query
attributes[ATTRIBUTE_NAMES.URL_QUERY] = query
url_full += "?" + query

if parsed_url.fragment:
attributes[SPANDATA.URL_FRAGMENT] = parsed_url.fragment
attributes[ATTRIBUTE_NAMES.URL_FRAGMENT] = parsed_url.fragment
url_full += "#" + parsed_url.fragment

attributes[SPANDATA.URL_FULL] = url_full
attributes[ATTRIBUTE_NAMES.URL_FULL] = url_full

return attributes

Expand Down Expand Up @@ -271,14 +275,14 @@ def add_source(
except Exception:
lineno = None
if lineno is not None:
span.set_attribute("code.line.number", lineno)
span.set_attribute(ATTRIBUTE_NAMES.CODE_LINE_NUMBER, lineno)

try:
namespace = frame.f_globals.get("__name__")
except Exception:
namespace = None
if namespace is not None:
span.set_attribute(SPANDATA.CODE_NAMESPACE, namespace)
span.set_attribute(ATTRIBUTE_NAMES.CODE_NAMESPACE, namespace)

filepath = _get_frame_module_abs_path(frame)
if filepath is not None:
Expand All @@ -290,15 +294,15 @@ def add_source(
in_app_path = filepath

if in_app_path is not None:
span.set_attribute("code.file.path", in_app_path)
span.set_attribute(ATTRIBUTE_NAMES.CODE_FILE_PATH, in_app_path)

try:
code_function = frame.f_code.co_name
except Exception:
code_function = None

if code_function is not None:
span.set_attribute(SPANDATA.CODE_FUNCTION, frame.f_code.co_name)
span.set_attribute(ATTRIBUTE_NAMES.CODE_FUNCTION, frame.f_code.co_name)


def add_query_source(
Expand Down Expand Up @@ -759,7 +763,7 @@ def populate_from_segment(cls, segment: "Span") -> "Baggage":
sentry_items["org_id"] = client.parsed_dsn.org_id

if (
segment.get_attributes().get("sentry.segment.name.source")
segment.get_attributes().get(ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE)
not in LOW_QUALITY_SEGMENT_SOURCES
) and segment.name:
sentry_items["transaction"] = segment.name
Expand Down Expand Up @@ -906,8 +910,8 @@ def span_decorator(f: "Any") -> "Any":
Decorator to create a span for the given function.
"""
new_attributes = dict(attributes) if attributes else {}
if "sentry.op" not in new_attributes:
new_attributes["sentry.op"] = OP.FUNCTION
if ATTRIBUTE_NAMES.SENTRY_OP not in new_attributes:
new_attributes[ATTRIBUTE_NAMES.SENTRY_OP] = OP.FUNCTION

@functools.wraps(f)
async def async_wrapper(*args: "Any", **kwargs: "Any") -> "Any":
Expand Down Expand Up @@ -1105,12 +1109,12 @@ def _make_sampling_decision(
"trace_id": propagation_context.trace_id,
"span_id": None,
"parent_span_id": propagation_context.parent_span_id,
"op": attributes.get("sentry.op"),
"op": attributes.get(ATTRIBUTE_NAMES.SENTRY_OP),
"name": name,
"description": name,
"start_timestamp": None,
"timestamp": None,
"source": attributes.get("sentry.segment.name.source"),
"source": attributes.get(ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE),
"sampled": None,
"data": attributes,
},
Expand Down
Loading
Loading