5
5
from enum import Enum
6
6
7
7
import sentry_sdk
8
- from sentry_sdk .consts import INSTRUMENTER , SPANSTATUS , SPANDATA
8
+ from sentry_sdk .consts import INSTRUMENTER , SPANSTATUS , SPANDATA , SPANTEMPLATE
9
9
from sentry_sdk .profiler .continuous_profiler import get_profiler_id
10
10
from sentry_sdk .utils import (
11
11
get_current_thread_meta ,
@@ -1365,8 +1365,10 @@ def _set_initial_sampling_decision(self, sampling_context):
1365
1365
if TYPE_CHECKING :
1366
1366
1367
1367
@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]]
1370
1372
# Handles: @trace() and @trace(op="custom")
1371
1373
pass
1372
1374
@@ -1377,8 +1379,10 @@ def trace(func):
1377
1379
pass
1378
1380
1379
1381
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]]]
1382
1386
"""
1383
1387
Decorator to start a child span around a function call.
1384
1388
@@ -1407,14 +1411,21 @@ def trace(func=None, *, op=None, name=None, attributes=None):
1407
1411
attributes provide additional context about the span's execution.
1408
1412
:type attributes: dict[str, Any] or None
1409
1413
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
+
1410
1421
:returns: When used as ``@trace``, returns the decorated function. When used as
1411
1422
``@trace(...)`` with parameters, returns a decorator function.
1412
1423
:rtype: Callable or decorator function
1413
1424
1414
1425
Example::
1415
1426
1416
1427
import sentry_sdk
1417
- from sentry_sdk.consts import OP
1428
+ from sentry_sdk.consts import OP, SPANTEMPLATE
1418
1429
1419
1430
# Simple usage with default values
1420
1431
@sentry_sdk.trace
@@ -1431,13 +1442,20 @@ def process_data():
1431
1442
def make_db_query(sql):
1432
1443
# Function implementation
1433
1444
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
1434
1451
"""
1435
1452
from sentry_sdk .tracing_utils import create_span_decorator
1436
1453
1437
1454
decorator = create_span_decorator (
1438
1455
op = op ,
1439
1456
name = name ,
1440
1457
attributes = attributes ,
1458
+ template = template ,
1441
1459
)
1442
1460
1443
1461
if func :
0 commit comments