|
14 | 14 | from sentry_sdk.utils import format_attribute, logger |
15 | 15 |
|
16 | 16 | if TYPE_CHECKING: |
17 | | - from typing import Any, Optional, Union |
| 17 | + from typing import Any, Callable, Optional, ParamSpec, TypeVar, Union |
18 | 18 | from sentry_sdk._types import Attributes, AttributeValue |
19 | 19 |
|
| 20 | + P = ParamSpec("P") |
| 21 | + R = TypeVar("R") |
| 22 | + |
20 | 23 |
|
21 | 24 | class SpanStatus(str, Enum): |
22 | 25 | OK = "ok" |
@@ -342,3 +345,74 @@ def trace_id(self) -> str: |
342 | 345 | @property |
343 | 346 | def sampled(self) -> "Optional[bool]": |
344 | 347 | return False |
| 348 | + |
| 349 | + |
| 350 | +def trace( |
| 351 | + func: "Optional[Callable[P, R]]" = None, |
| 352 | + *, |
| 353 | + name: "Optional[str]" = None, |
| 354 | + attributes: "Optional[dict[str, Any]]" = None, |
| 355 | + active: bool = True, |
| 356 | +) -> "Union[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]": |
| 357 | + """ |
| 358 | + Decorator to start a span around a function call. |
| 359 | +
|
| 360 | + This decorator automatically creates a new span when the decorated function |
| 361 | + is called, and finishes the span when the function returns or raises an exception. |
| 362 | +
|
| 363 | + :param func: The function to trace. When used as a decorator without parentheses, |
| 364 | + this is the function being decorated. When used with parameters (e.g., |
| 365 | + ``@trace(op="custom")``, this should be None. |
| 366 | + :type func: Callable or None |
| 367 | +
|
| 368 | + :param name: The human-readable name/description for the span. If not provided, |
| 369 | + defaults to the function name. This provides more specific details about |
| 370 | + what the span represents (e.g., "GET /api/users", "process_user_data"). |
| 371 | + :type name: str or None |
| 372 | +
|
| 373 | + :param attributes: A dictionary of key-value pairs to add as attributes to the span. |
| 374 | + Attribute values must be strings, integers, floats, or booleans. These |
| 375 | + attributes provide additional context about the span's execution. |
| 376 | + :type attributes: dict[str, Any] or None |
| 377 | +
|
| 378 | + :param active: Controls whether spans started while this span is running |
| 379 | + will automatically become its children. That's the default behavior. If |
| 380 | + you want to create a span that shouldn't have any children (unless |
| 381 | + provided explicitly via the `parent_span` argument), set this to False. |
| 382 | + :type active: bool |
| 383 | +
|
| 384 | + :returns: When used as ``@trace``, returns the decorated function. When used as |
| 385 | + ``@trace(...)`` with parameters, returns a decorator function. |
| 386 | + :rtype: Callable or decorator function |
| 387 | +
|
| 388 | + Example:: |
| 389 | +
|
| 390 | + import sentry_sdk |
| 391 | +
|
| 392 | + # Simple usage with default values |
| 393 | + @sentry_sdk.trace |
| 394 | + def process_data(): |
| 395 | + # Function implementation |
| 396 | + pass |
| 397 | +
|
| 398 | + # With custom parameters |
| 399 | + @sentry_sdk.trace( |
| 400 | + name="Get user data", |
| 401 | + attributes={"postgres": True} |
| 402 | + ) |
| 403 | + def make_db_query(sql): |
| 404 | + # Function implementation |
| 405 | + pass |
| 406 | + """ |
| 407 | + from sentry_sdk.tracing_utils import create_streaming_span_decorator |
| 408 | + |
| 409 | + decorator = create_streaming_span_decorator( |
| 410 | + name=name, |
| 411 | + attributes=attributes, |
| 412 | + active=active, |
| 413 | + ) |
| 414 | + |
| 415 | + if func: |
| 416 | + return decorator(func) |
| 417 | + else: |
| 418 | + return decorator |
0 commit comments