Skip to content

Commit 58a1646

Browse files
committed
apidocs
1 parent a75954e commit 58a1646

File tree

1 file changed

+58
-6
lines changed

1 file changed

+58
-6
lines changed

sentry_sdk/tracing.py

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,13 +1358,65 @@ def trace(
13581358
):
13591359
# type: (Optional[Callable[P, R]], SPANTEMPLATE, Optional[str], Optional[str], Optional[dict[str, Any]]) -> Union[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]
13601360
"""
1361-
Decorator to start a child span.
1361+
Decorator to start a child span around a function call.
1362+
1363+
This decorator automatically creates a new span when the decorated function
1364+
is called, and finishes the span when the function returns or raises an exception.
1365+
1366+
:param func: The function to trace. When used as a decorator without parentheses,
1367+
this is the function being decorated. When used with parameters (e.g.,
1368+
``@trace(op="custom")``, this should be None.
1369+
:type func: Callable or None
1370+
1371+
:param template: The type of span to create. This determines what kind of
1372+
span instrumentation and data collection will be applied. Use predefined
1373+
constants from :py:class:`sentry_sdk.consts.SPANTEMPLATE`.
1374+
The default is `SPANTEMPLATE.SPAN` which is the right choice for most
1375+
use cases.
1376+
:type template: :py:class:`sentry_sdk.consts.SPANTEMPLATE`
1377+
1378+
:param op: The operation name for the span. This is a high-level description
1379+
of what the span represents (e.g., "http.client", "db.query").
1380+
You can use predefined constants from :py:class:`sentry_sdk.consts.OP`
1381+
or provide your own string. If not provided, a default operation will
1382+
be assigned based on the template.
1383+
:type op: str or None
1384+
1385+
:param name: The human-readable name/description for the span. If not provided,
1386+
defaults to the function name. This provides more specific details about
1387+
what the span represents (e.g., "GET /api/users", "process_user_data").
1388+
:type name: str or None
1389+
1390+
:param attributes: A dictionary of key-value pairs to add as attributes to the span.
1391+
Attribute values must be strings, integers, floats, or booleans. These
1392+
attributes provide additional context about the span's execution.
1393+
:type attributes: dict[str, Any] or None
1394+
1395+
:returns: When used as ``@trace``, returns the decorated function. When used as
1396+
``@trace(...)`` with parameters, returns a decorator function.
1397+
:rtype: Callable or decorator function
1398+
1399+
Example::
1400+
1401+
import sentry_sdk
1402+
from sentry_sdk.consts import OP, SPANTEMPLATE
1403+
1404+
# Simple usage with default template
1405+
@sentry_sdk.trace
1406+
def process_data():
1407+
# Function implementation
1408+
pass
13621409
1363-
:param func: The function to trace.
1364-
:param template: The type of span to create.
1365-
:param op: The operation of the span.
1366-
:param name: The name of the span. (defaults to the function name)
1367-
:param attributes: Additional attributes to set on the span.
1410+
# With custom parameters
1411+
@sentry_sdk.trace(
1412+
template=SPANTEMPLATE.AI_CHAT,
1413+
op=OP.GEN_AI_CHAT,
1414+
name="user_chat_completion",
1415+
attributes={"model": "gpt-4", "temperature": 0.7}
1416+
)
1417+
def generate_response(prompt):
1418+
# Function implementation
1419+
pass
13681420
"""
13691421
decorator = create_span_decorator(
13701422
template=template,

0 commit comments

Comments
 (0)