-
Notifications
You must be signed in to change notification settings - Fork 588
ref: Add streaming trace decorator (7) #5594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: ivana/span-first-6-add-continue-and-new-trace
Are you sure you want to change the base?
Changes from all commits
941863e
e20d4fd
f2738ff
c974d3e
831adae
1dcf176
9b1e2f3
1006e7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,9 +14,12 @@ | |
| from sentry_sdk.utils import format_attribute, logger | ||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import Any, Optional, Union | ||
| from typing import Any, Callable, Optional, ParamSpec, TypeVar, Union | ||
| from sentry_sdk._types import Attributes, AttributeValue | ||
|
|
||
| P = ParamSpec("P") | ||
| R = TypeVar("R") | ||
|
|
||
|
|
||
| class SpanStatus(str, Enum): | ||
| OK = "ok" | ||
|
|
@@ -235,6 +238,14 @@ def __repr__(self) -> str: | |
| f"active={self._active})>" | ||
| ) | ||
|
|
||
| def __enter__(self) -> "StreamedSpan": | ||
| return self | ||
|
|
||
| def __exit__( | ||
| self, ty: "Optional[Any]", value: "Optional[Any]", tb: "Optional[Any]" | ||
| ) -> None: | ||
| pass | ||
sentrivana marked this conversation as resolved.
Show resolved
Hide resolved
sentrivana marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Context manager
|
||
|
|
||
| def get_attributes(self) -> "Attributes": | ||
| return self._attributes | ||
|
|
||
|
|
@@ -306,6 +317,14 @@ def __init__(self) -> None: | |
| def __repr__(self) -> str: | ||
| return f"<{self.__class__.__name__}(sampled={self.sampled})>" | ||
|
|
||
| def __enter__(self) -> "NoOpStreamedSpan": | ||
| return self | ||
|
|
||
| def __exit__( | ||
| self, ty: "Optional[Any]", value: "Optional[Any]", tb: "Optional[Any]" | ||
| ) -> None: | ||
| pass | ||
sentrivana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def get_attributes(self) -> "Attributes": | ||
| return {} | ||
|
|
||
|
|
@@ -349,3 +368,74 @@ def trace_id(self) -> str: | |
| @property | ||
| def sampled(self) -> "Optional[bool]": | ||
| return False | ||
|
|
||
|
|
||
| def trace( | ||
| func: "Optional[Callable[P, R]]" = None, | ||
| *, | ||
| name: "Optional[str]" = None, | ||
| attributes: "Optional[dict[str, Any]]" = None, | ||
| active: bool = True, | ||
| ) -> "Union[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]": | ||
| """ | ||
| Decorator to start a span around a function call. | ||
|
|
||
| This decorator automatically creates a new span when the decorated function | ||
| is called, and finishes the span when the function returns or raises an exception. | ||
|
|
||
| :param func: The function to trace. When used as a decorator without parentheses, | ||
| this is the function being decorated. When used with parameters (e.g., | ||
| ``@trace(op="custom")``, this should be None. | ||
| :type func: Callable or None | ||
|
|
||
| :param name: The human-readable name/description for the span. If not provided, | ||
| defaults to the function name. This provides more specific details about | ||
| what the span represents (e.g., "GET /api/users", "process_user_data"). | ||
| :type name: str or None | ||
|
|
||
| :param attributes: A dictionary of key-value pairs to add as attributes to the span. | ||
| Attribute values must be strings, integers, floats, or booleans. These | ||
| attributes provide additional context about the span's execution. | ||
| :type attributes: dict[str, Any] or None | ||
|
|
||
| :param active: Controls whether spans started while this span is running | ||
| will automatically become its children. That's the default behavior. If | ||
| you want to create a span that shouldn't have any children (unless | ||
| provided explicitly via the `parent_span` argument), set this to False. | ||
| :type active: bool | ||
|
|
||
| :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 | ||
|
|
||
| # Simple usage with default values | ||
| @sentry_sdk.trace | ||
| def process_data(): | ||
| # Function implementation | ||
| pass | ||
|
|
||
| # With custom parameters | ||
| @sentry_sdk.trace( | ||
| name="Get user data", | ||
| attributes={"postgres": True} | ||
| ) | ||
| def make_db_query(sql): | ||
| # Function implementation | ||
| pass | ||
| """ | ||
| from sentry_sdk.tracing_utils import create_streaming_span_decorator | ||
|
|
||
| decorator = create_streaming_span_decorator( | ||
| name=name, | ||
| attributes=attributes, | ||
| active=active, | ||
| ) | ||
|
|
||
| if func: | ||
| return decorator(func) | ||
| else: | ||
| return decorator | ||
sentrivana marked this conversation as resolved.
Show resolved
Hide resolved
sentrivana marked this conversation as resolved.
Show resolved
Hide resolved
|
||


Uh oh!
There was an error while loading. Please reload this page.