55from enum import Enum
66
77import sentry_sdk
8- from sentry_sdk .consts import INSTRUMENTER , SPANSTATUS , SPANDATA
8+ from sentry_sdk .consts import INSTRUMENTER , SPANSTATUS , SPANDATA , SPANTEMPLATE
99from sentry_sdk .profiler .continuous_profiler import get_profiler_id
1010from sentry_sdk .utils import (
1111 get_current_thread_meta ,
@@ -1365,8 +1365,10 @@ def _set_initial_sampling_decision(self, sampling_context):
13651365if TYPE_CHECKING :
13661366
13671367 @overload
1368- def trace (func = None , * , op = None , name = None , attributes = None ):
1369- # type: (None, Optional[str], Optional[str], Optional[dict[str, Any]]) -> Callable[[Callable[P, R]], Callable[P, R]]
1368+ def trace (
1369+ func = None , * , op = None , name = None , attributes = None , template = SPANTEMPLATE .DEFAULT
1370+ ):
1371+ # type: (None, Optional[str], Optional[str], Optional[dict[str, Any]], SPANTEMPLATE) -> Callable[[Callable[P, R]], Callable[P, R]]
13701372 # Handles: @trace() and @trace(op="custom")
13711373 pass
13721374
@@ -1377,8 +1379,10 @@ def trace(func):
13771379 pass
13781380
13791381
1380- def trace (func = None , * , op = None , name = None , attributes = None ):
1381- # type: (Optional[Callable[P, R]], Optional[str], Optional[str], Optional[dict[str, Any]]) -> Union[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]
1382+ def trace (
1383+ func = None , * , op = None , name = None , attributes = None , template = SPANTEMPLATE .DEFAULT
1384+ ):
1385+ # type: (Optional[Callable[P, R]], Optional[str], Optional[str], Optional[dict[str, Any]], SPANTEMPLATE) -> Union[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]
13821386 """
13831387 Decorator to start a child span around a function call.
13841388
@@ -1407,14 +1411,21 @@ def trace(func=None, *, op=None, name=None, attributes=None):
14071411 attributes provide additional context about the span's execution.
14081412 :type attributes: dict[str, Any] or None
14091413
1414+ :param template: The type of span to create. This determines what kind of
1415+ span instrumentation and data collection will be applied. Use predefined
1416+ constants from :py:class:`sentry_sdk.consts.SPANTEMPLATE`.
1417+ The default is `SPANTEMPLATE.DEFAULT` which is the right choice for most
1418+ use cases.
1419+ :type template: :py:class:`sentry_sdk.consts.SPANTEMPLATE`
1420+
14101421 :returns: When used as ``@trace``, returns the decorated function. When used as
14111422 ``@trace(...)`` with parameters, returns a decorator function.
14121423 :rtype: Callable or decorator function
14131424
14141425 Example::
14151426
14161427 import sentry_sdk
1417- from sentry_sdk.consts import OP
1428+ from sentry_sdk.consts import OP, SPANTEMPLATE
14181429
14191430 # Simple usage with default values
14201431 @sentry_sdk.trace
@@ -1431,13 +1442,20 @@ def process_data():
14311442 def make_db_query(sql):
14321443 # Function implementation
14331444 pass
1445+
1446+ # With a custom template
1447+ @sentry_sdk.trace(template=SPANTEMPLATE.AI_TOOL)
1448+ def calculate_interest_rate(amount, rate, years):
1449+ # Function implementation
1450+ pass
14341451 """
14351452 from sentry_sdk .tracing_utils import create_span_decorator
14361453
14371454 decorator = create_span_decorator (
14381455 op = op ,
14391456 name = name ,
14401457 attributes = attributes ,
1458+ template = template ,
14411459 )
14421460
14431461 if func :
0 commit comments