Skip to content
Merged
Show file tree
Hide file tree
Changes from 79 commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
1162254
First version of helpers for better manual instrumentation
antonpirker Jul 30, 2025
4346db1
minor improvements
antonpirker Jul 30, 2025
bfc6626
A nicer implementation
antonpirker Jul 30, 2025
f193ecb
style
antonpirker Jul 30, 2025
8488a53
fixing setting of name
antonpirker Jul 30, 2025
5621f15
Some linting
antonpirker Jul 30, 2025
43ff0c6
Replaced original trace decorator with new_decorator
antonpirker Jul 30, 2025
6a5c88e
Cleanup
antonpirker Jul 30, 2025
74941d4
fix start child
antonpirker Jul 30, 2025
6d36b28
If no span, do nothing
antonpirker Jul 30, 2025
09d5646
change less code
antonpirker Jul 30, 2025
3020f92
comment
antonpirker Jul 30, 2025
acd5401
its description internally
antonpirker Jul 31, 2025
695dd06
move _set_span_attribuets into span
antonpirker Jul 31, 2025
5068c5b
typing
antonpirker Jul 31, 2025
98c8d42
.
antonpirker Jul 31, 2025
35e7809
Add enum for template
antonpirker Jul 31, 2025
a538d2a
Make some consts public
antonpirker Jul 31, 2025
241d5cf
Also make op part of public api
antonpirker Aug 1, 2025
33f79b8
sort
antonpirker Aug 1, 2025
dbbf3b4
Allow setting a key to None
antonpirker Aug 4, 2025
cfec8c0
Merge branch 'master' into antonpirker/manual-instrumentation
antonpirker Aug 4, 2025
c234dd4
typing
antonpirker Aug 4, 2025
cf5382f
Guess some attributes from input/response
antonpirker Aug 4, 2025
fdcdfdf
fix tool description
antonpirker Aug 4, 2025
9917808
Added more token names
antonpirker Aug 4, 2025
7c1076b
better span naming
antonpirker Aug 4, 2025
a6eed59
Tool input and output
antonpirker Aug 4, 2025
2011ab4
Check PII settings before adding tool input/output
antonpirker Aug 4, 2025
668a421
Better usage extraction
antonpirker Aug 4, 2025
d8b5838
cleanup
antonpirker Aug 4, 2025
94796c6
make attribute a string
antonpirker Aug 4, 2025
ee845b1
cleanup
antonpirker Aug 4, 2025
c2d47bc
renaming
antonpirker Aug 5, 2025
5e7d44b
Merge branch 'master' into antonpirker/manual-instrumentation
antonpirker Aug 5, 2025
4843674
update
antonpirker Aug 5, 2025
f0d3ab6
cleanup
antonpirker Aug 5, 2025
a75954e
Merge branch 'master' into antonpirker/manual-instrumentation
antonpirker Aug 5, 2025
58a1646
apidocs
antonpirker Aug 5, 2025
4002e86
Merge branch 'master' into antonpirker/manual-instrumentation
antonpirker Aug 5, 2025
989e0be
apidoc
antonpirker Aug 5, 2025
aa82162
apidocs
antonpirker Aug 5, 2025
0c3da75
typo
antonpirker Aug 5, 2025
55a1035
default value
antonpirker Aug 5, 2025
0b84d48
remove ai stuff
antonpirker Aug 5, 2025
35d8f12
remove template stuff
antonpirker Aug 5, 2025
10e82cd
remove
antonpirker Aug 5, 2025
c2d4e48
removed too much
antonpirker Aug 5, 2025
9a63d8c
cleanup
antonpirker Aug 5, 2025
c20429b
Apply suggestions from code review
antonpirker Aug 5, 2025
2d93a65
typing
antonpirker Aug 5, 2025
b0cc71a
typing
antonpirker Aug 5, 2025
42afdfa
moved up
antonpirker Aug 5, 2025
aa3c334
fix circular import
antonpirker Aug 6, 2025
dc63e43
linting
antonpirker Aug 6, 2025
de68868
AI Agents templates for trace decorator
antonpirker Aug 6, 2025
04b976a
docstring
antonpirker Aug 6, 2025
caafc14
Added test
antonpirker Aug 6, 2025
79768c6
More tests
antonpirker Aug 6, 2025
aad822e
Tests
antonpirker Aug 6, 2025
c7fb31e
tests
antonpirker Aug 6, 2025
e1e1b26
tests
antonpirker Aug 6, 2025
b95770b
tests
antonpirker Aug 6, 2025
e64c3a2
fiux
antonpirker Aug 6, 2025
bbeb136
cleanup
antonpirker Aug 6, 2025
6aab06c
Merge branch 'master' into manual-instrumentation2
antonpirker Aug 8, 2025
b294a54
mypy
antonpirker Aug 8, 2025
34a369a
Merge branch 'master' into manual-instrumentation2
antonpirker Aug 8, 2025
074fc53
Merge branch 'master' into manual-instrumentation2
antonpirker Aug 13, 2025
55289df
rename spantemplate.span to spantemplate.default
antonpirker Aug 13, 2025
baa1d78
Apply suggestions from code review
antonpirker Aug 13, 2025
48023db
Merge branch 'manual-instrumentation2' of github.com:getsentry/sentry…
antonpirker Aug 13, 2025
8e0b78d
dedupe code in _get_usage_attributes
antonpirker Aug 13, 2025
61158fb
dedupe _get_input_attributes
antonpirker Aug 13, 2025
c1405a2
dedupe _get_output_attributes
antonpirker Aug 13, 2025
2529d10
Cleanup
antonpirker Aug 13, 2025
ca3e67f
cleanup
antonpirker Aug 13, 2025
26fe6fc
error handling
antonpirker Aug 13, 2025
697ed9f
make it better
antonpirker Aug 13, 2025
2bd798a
comments
antonpirker Aug 13, 2025
a5b9294
fix
antonpirker Aug 13, 2025
22be202
fix
antonpirker Aug 13, 2025
5089695
whitespace
antonpirker Aug 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ class CompressionAlgo(Enum):
]


class SPANTEMPLATE(str, Enum):
DEFAULT = "default"
AI_AGENT = "ai_agent"
AI_TOOL = "ai_tool"
AI_CHAT = "ai_chat"

def __str__(self):
# type: () -> str
return self.value


class INSTRUMENTER:
SENTRY = "sentry"
OTEL = "otel"
Expand Down
30 changes: 24 additions & 6 deletions sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from enum import Enum

import sentry_sdk
from sentry_sdk.consts import INSTRUMENTER, SPANSTATUS, SPANDATA
from sentry_sdk.consts import INSTRUMENTER, SPANSTATUS, SPANDATA, SPANTEMPLATE
from sentry_sdk.profiler.continuous_profiler import get_profiler_id
from sentry_sdk.utils import (
get_current_thread_meta,
Expand Down Expand Up @@ -1365,8 +1365,10 @@ def _set_initial_sampling_decision(self, sampling_context):
if TYPE_CHECKING:

@overload
def trace(func=None, *, op=None, name=None, attributes=None):
# type: (None, Optional[str], Optional[str], Optional[dict[str, Any]]) -> Callable[[Callable[P, R]], Callable[P, R]]
def trace(
func=None, *, op=None, name=None, attributes=None, template=SPANTEMPLATE.DEFAULT
):
# type: (None, Optional[str], Optional[str], Optional[dict[str, Any]], SPANTEMPLATE) -> Callable[[Callable[P, R]], Callable[P, R]]
# Handles: @trace() and @trace(op="custom")
pass

Expand All @@ -1377,8 +1379,10 @@ def trace(func):
pass


def trace(func=None, *, op=None, name=None, attributes=None):
# type: (Optional[Callable[P, R]], Optional[str], Optional[str], Optional[dict[str, Any]]) -> Union[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]
def trace(
func=None, *, op=None, name=None, attributes=None, template=SPANTEMPLATE.DEFAULT
):
# 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]]]
"""
Decorator to start a child span around a function call.

Expand Down Expand Up @@ -1407,14 +1411,21 @@ def trace(func=None, *, op=None, name=None, attributes=None):
attributes provide additional context about the span's execution.
:type attributes: dict[str, Any] or None

:param template: The type of span to create. This determines what kind of
span instrumentation and data collection will be applied. Use predefined
constants from :py:class:`sentry_sdk.consts.SPANTEMPLATE`.
The default is `SPANTEMPLATE.DEFAULT` which is the right choice for most
use cases.
:type template: :py:class:`sentry_sdk.consts.SPANTEMPLATE`

:returns: When used as ``@trace``, returns the decorated function. When used as
``@trace(...)`` with parameters, returns a decorator function.
:rtype: Callable or decorator function

Example::

import sentry_sdk
from sentry_sdk.consts import OP
from sentry_sdk.consts import OP, SPANTEMPLATE

# Simple usage with default values
@sentry_sdk.trace
Expand All @@ -1431,13 +1442,20 @@ def process_data():
def make_db_query(sql):
# Function implementation
pass

# With a custom template
@sentry_sdk.trace(template=SPANTEMPLATE.AI_TOOL)
def calculate_interest_rate(amount, rate, years):
# Function implementation
pass
"""
from sentry_sdk.tracing_utils import create_span_decorator

decorator = create_span_decorator(
op=op,
name=name,
attributes=attributes,
template=template,
)

if func:
Expand Down
Loading
Loading