Skip to content

Commit 35e7809

Browse files
committed
Add enum for template
1 parent 98c8d42 commit 35e7809

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
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: 11 additions & 7 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.tracing_utils import create_span_decorator
1111
from sentry_sdk.utils import (
@@ -1348,18 +1348,22 @@ def _set_initial_sampling_decision(self, sampling_context):
13481348
if TYPE_CHECKING:
13491349

13501350
@overload
1351-
def trace(func=None, *, template="span", op=None, name=None, attributes=None):
1352-
# type: (Optional[Callable[P, R]], str, Optional[str], Optional[str], Optional[dict[str, Any]]) -> Union[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]
1351+
def trace(
1352+
func=None, *, template=SpanTemplate.SPAN, op=None, name=None, attributes=None
1353+
):
1354+
# type: (Optional[Callable[P, R]], ..., Optional[str], Optional[str], Optional[dict[str, Any]]) -> Union[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]
13531355
pass
13541356

13551357
@overload
1356-
def trace(func, *, template="span", op=None, name=None, attributes=None):
1357-
# type: (Callable[P, R], str, Optional[str], Optional[str], Optional[dict[str, Any]]) -> Union[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]
1358+
def trace(func, *, template=SpanTemplate.SPAN, op=None, name=None, attributes=None):
1359+
# type: (Callable[P, R], ..., Optional[str], Optional[str], Optional[dict[str, Any]]) -> Union[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]
13581360
pass
13591361

13601362

1361-
def trace(func=None, *, template="span", op=None, name=None, attributes=None):
1362-
# type: (Optional[Callable[P, R]], str, Optional[str], Optional[str], Optional[dict[str, Any]]) -> Union[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]
1363+
def trace(
1364+
func=None, *, template=SpanTemplate.SPAN, op=None, name=None, attributes=None
1365+
):
1366+
# type: (Optional[Callable[P, R]], ..., Optional[str], Optional[str], Optional[dict[str, Any]]) -> Union[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]
13631367
"""
13641368
Decorator to start a child span.
13651369

sentry_sdk/tracing_utils.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -831,41 +831,41 @@ def _sample_rand_range(parent_sampled, sample_rate):
831831

832832

833833
def _get_span_name(template, name):
834-
# type: (str, str) -> str
834+
# type: (Union[str, "SpanTemplate"], str) -> str
835835
"""
836836
Get the name of the span based on the template and the name.
837837
"""
838838
span_name = name
839839

840-
if template == "ai_chat":
840+
if template == SpanTemplate.AI_CHAT:
841841
span_name = "chat [MODEL]"
842-
elif template == "ai_agent":
842+
elif template == SpanTemplate.AI_AGENT:
843843
span_name = f"invoke_agent {name}"
844-
elif template == "ai_tool":
844+
elif template == SpanTemplate.AI_TOOL:
845845
span_name = f"execute_tool {name}"
846846

847847
return span_name
848848

849849

850850
def _get_span_op(template):
851-
# type: (str) -> str
851+
# type: (Union[str, "SpanTemplate"]) -> str
852852
"""
853853
Get the operation of the span based on the template.
854854
"""
855855
op = OP.FUNCTION
856856

857-
if template == "ai_chat":
857+
if template == SpanTemplate.AI_CHAT:
858858
op = OP.GEN_AI_CHAT
859-
elif template == "ai_agent":
859+
elif template == SpanTemplate.AI_AGENT:
860860
op = OP.GEN_AI_INVOKE_AGENT
861-
elif template == "ai_tool":
861+
elif template == SpanTemplate.AI_TOOL:
862862
op = OP.GEN_AI_EXECUTE_TOOL
863863

864864
return op
865865

866866

867867
def _set_input_attributes(span, template, args, kwargs):
868-
# type: (Span, str, tuple[Any, ...], dict[str, Any]) -> None
868+
# type: (Span, Union[str, "SpanTemplate"], tuple[Any, ...], dict[str, Any]) -> None
869869
"""
870870
Set span input attributes based on the given template.
871871
@@ -878,16 +878,16 @@ def _set_input_attributes(span, template, args, kwargs):
878878

879879
# TODO: Implement actual input parameters for those templates :)
880880

881-
if template == "ai_agent":
881+
if template == SpanTemplate.AI_AGENT:
882882
attributes = {
883883
SPANDATA.GEN_AI_OPERATION_NAME: "invoke_agent",
884884
SPANDATA.GEN_AI_AGENT_NAME: span.description,
885885
}
886-
elif template == "ai_chat":
886+
elif template == SpanTemplate.AI_CHAT:
887887
attributes = {
888888
SPANDATA.GEN_AI_OPERATION_NAME: "chat",
889889
}
890-
elif template == "ai_tool":
890+
elif template == SpanTemplate.AI_TOOL:
891891
attributes = {
892892
SPANDATA.GEN_AI_OPERATION_NAME: "execute_tool",
893893
SPANDATA.GEN_AI_TOOL_NAME: span.description,
@@ -897,7 +897,7 @@ def _set_input_attributes(span, template, args, kwargs):
897897

898898

899899
def _set_output_attributes(span, template, result):
900-
# type: (Span, str, Any) -> None
900+
# type: (Span, Union[str, "SpanTemplate"], Any) -> None
901901
"""
902902
Set span output attributes based on the given template.
903903
@@ -913,7 +913,7 @@ def _set_output_attributes(span, template, result):
913913

914914

915915
def create_span_decorator(template, op=None, name=None, attributes=None):
916-
# type: (str, Optional[str], Optional[str], Optional[dict[str, Any]]) -> Any
916+
# type: (Union[str, "SpanTemplate"], Optional[str], Optional[str], Optional[dict[str, Any]]) -> Any
917917
"""
918918
Create a span decorator that can wrap both sync and async functions.
919919
@@ -1013,6 +1013,7 @@ def sync_wrapper(*args, **kwargs):
10131013
LOW_QUALITY_TRANSACTION_SOURCES,
10141014
SENTRY_TRACE_HEADER_NAME,
10151015
)
1016+
from sentry_sdk.consts import SpanTemplate
10161017

10171018
if TYPE_CHECKING:
10181019
from sentry_sdk.tracing import Span

0 commit comments

Comments
 (0)