44from enum import Enum
55
66import sentry_sdk
7- from sentry_sdk .consts import INSTRUMENTER , SPANSTATUS , SPANDATA
7+ from sentry_sdk .consts import INSTRUMENTER , SPANSTATUS , SPANDATA , SPANTEMPLATE
88from sentry_sdk .profiler .continuous_profiler import get_profiler_id
99from sentry_sdk .utils import (
1010 get_current_thread_meta ,
@@ -1364,8 +1364,10 @@ def _set_initial_sampling_decision(self, sampling_context):
13641364if TYPE_CHECKING :
13651365
13661366 @overload
1367- def trace (func = None , * , op = None , name = None , attributes = None ):
1368- # type: (None, Optional[str], Optional[str], Optional[dict[str, Any]]) -> Callable[[Callable[P, R]], Callable[P, R]]
1367+ def trace (
1368+ func = None , * , op = None , name = None , attributes = None , template = SPANTEMPLATE .DEFAULT
1369+ ):
1370+ # type: (None, Optional[str], Optional[str], Optional[dict[str, Any]], SPANTEMPLATE) -> Callable[[Callable[P, R]], Callable[P, R]]
13691371 # Handles: @trace() and @trace(op="custom")
13701372 pass
13711373
@@ -1376,8 +1378,10 @@ def trace(func):
13761378 pass
13771379
13781380
1379- def trace (func = None , * , op = None , name = None , attributes = None ):
1380- # type: (Optional[Callable[P, R]], Optional[str], Optional[str], Optional[dict[str, Any]]) -> Union[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]
1381+ def trace (
1382+ func = None , * , op = None , name = None , attributes = None , template = SPANTEMPLATE .DEFAULT
1383+ ):
1384+ # 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]]]
13811385 """
13821386 Decorator to start a child span around a function call.
13831387
@@ -1406,14 +1410,21 @@ def trace(func=None, *, op=None, name=None, attributes=None):
14061410 attributes provide additional context about the span's execution.
14071411 :type attributes: dict[str, Any] or None
14081412
1413+ :param template: The type of span to create. This determines what kind of
1414+ span instrumentation and data collection will be applied. Use predefined
1415+ constants from :py:class:`sentry_sdk.consts.SPANTEMPLATE`.
1416+ The default is `SPANTEMPLATE.DEFAULT` which is the right choice for most
1417+ use cases.
1418+ :type template: :py:class:`sentry_sdk.consts.SPANTEMPLATE`
1419+
14091420 :returns: When used as ``@trace``, returns the decorated function. When used as
14101421 ``@trace(...)`` with parameters, returns a decorator function.
14111422 :rtype: Callable or decorator function
14121423
14131424 Example::
14141425
14151426 import sentry_sdk
1416- from sentry_sdk.consts import OP
1427+ from sentry_sdk.consts import OP, SPANTEMPLATE
14171428
14181429 # Simple usage with default values
14191430 @sentry_sdk.trace
@@ -1430,13 +1441,20 @@ def process_data():
14301441 def make_db_query(sql):
14311442 # Function implementation
14321443 pass
1444+
1445+ # With a custom template
1446+ @sentry_sdk.trace(template=SPANTEMPLATE.AI_TOOL)
1447+ def calculate_interest_rate(amount, rate, years):
1448+ # Function implementation
1449+ pass
14331450 """
14341451 from sentry_sdk .tracing_utils import create_span_decorator
14351452
14361453 decorator = create_span_decorator (
14371454 op = op ,
14381455 name = name ,
14391456 attributes = attributes ,
1457+ template = template ,
14401458 )
14411459
14421460 if func :
0 commit comments