Skip to content

Commit 1804955

Browse files
antonpirkerszokeasaurusrexsentrivana
authored
feat(tracing): AI Agents templates for @trace decorator (#4676)
Adding a `template` parameter to the `@trace` decorator to make it easier to manually create spans for insights modules. Currently there are three templates supported `ai_agent`, `ai_tool`, and `ai_chat`. --------- Co-authored-by: Daniel Szoke <[email protected]> Co-authored-by: Ivana Kellyer <[email protected]>
1 parent 46eb82c commit 1804955

File tree

4 files changed

+555
-13
lines changed

4 files changed

+555
-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+
DEFAULT = "default"
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,
@@ -1365,8 +1365,10 @@ def _set_initial_sampling_decision(self, sampling_context):
13651365
if 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

Comments
 (0)