@@ -1338,43 +1338,85 @@ def _set_initial_sampling_decision(self, sampling_context):
13381338if TYPE_CHECKING :
13391339
13401340 @overload
1341- def trace (func = None ):
1342- # type: (None) -> Callable[[Callable[P, R]], Callable[P, R]]
1341+ def trace (func = None , * , op = None , name = None , attributes = None ):
1342+ # type: (None, Optional[str], Optional[str], Optional[dict[str, Any]]) -> Callable[[Callable[P, R]], Callable[P, R]]
1343+ # Handles: @trace() and @trace(op="custom")
13431344 pass
13441345
13451346 @overload
13461347 def trace (func ):
13471348 # type: (Callable[P, R]) -> Callable[P, R]
1349+ # Handles: @trace
13481350 pass
13491351
13501352
1351- def trace (func = None ):
1352- # type: (Optional[Callable[P, R]]) -> Union[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]
1353+ def trace (func = None , * , op = None , name = None , attributes = None ):
1354+ # type: (Optional[Callable[P, R]], Optional[str], Optional[str], Optional[dict[str, Any]] ) -> Union[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]
13531355 """
1354- Decorator to start a child span under the existing current transaction.
1355- If there is no current transaction, then nothing will be traced.
1356+ Decorator to start a child span around a function call.
13561357
1357- .. code-block::
1358- :caption: Usage
1358+ This decorator automatically creates a new span when the decorated function
1359+ is called, and finishes the span when the function returns or raises an exception.
1360+
1361+ :param func: The function to trace. When used as a decorator without parentheses,
1362+ this is the function being decorated. When used with parameters (e.g.,
1363+ ``@trace(op="custom")``, this should be None.
1364+ :type func: Callable or None
1365+
1366+ :param op: The operation name for the span. This is a high-level description
1367+ of what the span represents (e.g., "http.client", "db.query").
1368+ You can use predefined constants from :py:class:`sentry_sdk.consts.OP`
1369+ or provide your own string. If not provided, a default operation will
1370+ be assigned based on the template.
1371+ :type op: str or None
1372+
1373+ :param name: The human-readable name/description for the span. If not provided,
1374+ defaults to the function name. This provides more specific details about
1375+ what the span represents (e.g., "GET /api/users", "process_user_data").
1376+ :type name: str or None
1377+
1378+ :param attributes: A dictionary of key-value pairs to add as attributes to the span.
1379+ Attribute values must be strings, integers, floats, or booleans. These
1380+ attributes provide additional context about the span's execution.
1381+ :type attributes: dict[str, Any] or None
1382+
1383+ :returns: When used as ``@trace``, returns the decorated function. When used as
1384+ ``@trace(...)`` with parameters, returns a decorator function.
1385+ :rtype: Callable or decorator function
1386+
1387+ Example::
13591388
13601389 import sentry_sdk
1390+ from sentry_sdk.consts import OP
13611391
1392+ # Simple usage with default values
13621393 @sentry_sdk.trace
1363- def my_function():
1364- ...
1394+ def process_data():
1395+ # Function implementation
1396+ pass
13651397
1366- @sentry_sdk.trace
1367- async def my_async_function():
1368- ...
1398+ # With custom parameters
1399+ @sentry_sdk.trace(
1400+ op=OP.DB_QUERY,
1401+ name="Get user data",
1402+ attributes={"postgres": True}
1403+ )
1404+ def make_db_query(sql):
1405+ # Function implementation
1406+ pass
13691407 """
1370- from sentry_sdk .tracing_utils import start_child_span_decorator
1408+ from sentry_sdk .tracing_utils import create_span_decorator
1409+
1410+ decorator = create_span_decorator (
1411+ op = op ,
1412+ name = name ,
1413+ attributes = attributes ,
1414+ )
13711415
1372- # This patterns allows usage of both @sentry_traced and @sentry_traced(...)
1373- # See https://stackoverflow.com/questions/52126071/decorator-with-arguments-avoid-parenthesis-when-no-arguments/52126278
13741416 if func :
1375- return start_child_span_decorator (func )
1417+ return decorator (func )
13761418 else :
1377- return start_child_span_decorator
1419+ return decorator
13781420
13791421
13801422# Circular imports
0 commit comments