Skip to content

Commit de68868

Browse files
committed
AI Agents templates for trace decorator
1 parent dc63e43 commit de68868

File tree

3 files changed

+280
-13
lines changed

3 files changed

+280
-13
lines changed

sentry_sdk/consts.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ class CompressionAlgo(Enum):
100100
]
101101

102102

103+
class SPANTEMPLATE(str, Enum):
104+
SPAN = "span"
105+
AI_AGENT = "ai_agent"
106+
AI_TOOL = "ai_tool"
107+
AI_CHAT = "ai_chat"
108+
109+
def __str__(self):
110+
# type: () -> str
111+
return self.value
112+
113+
103114
class INSTRUMENTER:
104115
SENTRY = "sentry"
105116
OTEL = "otel"

sentry_sdk/tracing.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from enum import Enum
66

77
import sentry_sdk
8-
from sentry_sdk.consts import INSTRUMENTER, SPANSTATUS, SPANDATA
8+
from sentry_sdk.consts import INSTRUMENTER, SPANSTATUS, SPANDATA, SPANTEMPLATE
99
from sentry_sdk.profiler.continuous_profiler import get_profiler_id
1010
from sentry_sdk.utils import (
1111
get_current_thread_meta,
@@ -1340,8 +1340,10 @@ def _set_initial_sampling_decision(self, sampling_context):
13401340
if TYPE_CHECKING:
13411341

13421342
@overload
1343-
def trace(func=None, *, op=None, name=None, attributes=None):
1344-
# type: (None, Optional[str], Optional[str], Optional[dict[str, Any]]) -> Callable[[Callable[P, R]], Callable[P, R]]
1343+
def trace(
1344+
func=None, *, op=None, name=None, attributes=None, template=SPANTEMPLATE.SPAN
1345+
):
1346+
# type: (None, Optional[str], Optional[str], Optional[dict[str, Any]], SPANTEMPLATE) -> Callable[[Callable[P, R]], Callable[P, R]]
13451347
# Handles: @trace() and @trace(op="custom")
13461348
pass
13471349

@@ -1352,8 +1354,10 @@ def trace(func):
13521354
pass
13531355

13541356

1355-
def trace(func=None, *, op=None, name=None, attributes=None):
1356-
# type: (Optional[Callable[P, R]], Optional[str], Optional[str], Optional[dict[str, Any]]) -> Union[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]
1357+
def trace(
1358+
func=None, *, op=None, name=None, attributes=None, template=SPANTEMPLATE.SPAN
1359+
):
1360+
# 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]]]
13571361
"""
13581362
Decorator to start a child span around a function call.
13591363
@@ -1382,14 +1386,21 @@ def trace(func=None, *, op=None, name=None, attributes=None):
13821386
attributes provide additional context about the span's execution.
13831387
:type attributes: dict[str, Any] or None
13841388
1389+
:param template: The type of span to create. This determines what kind of
1390+
span instrumentation and data collection will be applied. Use predefined
1391+
constants from :py:class:`sentry_sdk.consts.SPANTEMPLATE`.
1392+
The default is `SPANTEMPLATE.SPAN` which is the right choice for most
1393+
use cases.
1394+
:type template: :py:class:`sentry_sdk.consts.SPANTEMPLATE`
1395+
13851396
:returns: When used as ``@trace``, returns the decorated function. When used as
13861397
``@trace(...)`` with parameters, returns a decorator function.
13871398
:rtype: Callable or decorator function
13881399
13891400
Example::
13901401
13911402
import sentry_sdk
1392-
from sentry_sdk.consts import OP
1403+
from sentry_sdk.consts import OP, SPANTEMPLATE
13931404
13941405
# Simple usage with default values
13951406
@sentry_sdk.trace
@@ -1406,13 +1417,20 @@ def process_data():
14061417
def make_db_query(sql):
14071418
# Function implementation
14081419
pass
1420+
1421+
# With a custom template
1422+
@sentry_sdk.trace(template=SPANTEMPLATE.AI_TOOL)
1423+
def calculate_interest_rate(amount, rate, years):
1424+
# Function implementation
1425+
pass
14091426
"""
14101427
from sentry_sdk.tracing_utils import create_span_decorator
14111428

14121429
decorator = create_span_decorator(
14131430
op=op,
14141431
name=name,
14151432
attributes=attributes,
1433+
template=template,
14161434
)
14171435

14181436
if func:

0 commit comments

Comments
 (0)