Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ Performance Monitoring

.. autofunction:: sentry_sdk.api.trace
.. autofunction:: sentry_sdk.api.continue_trace
.. autofunction:: sentry_sdk.api.new_trace
.. autofunction:: sentry_sdk.api.get_current_span
.. autofunction:: sentry_sdk.api.start_span
.. autofunction:: sentry_sdk.api.start_transaction


Distributed Tracing
Expand Down
4 changes: 2 additions & 2 deletions docs/apidocs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ API Docs
.. autoclass:: sentry_sdk.HttpTransport
:members:

.. autoclass:: sentry_sdk.tracing.Transaction
.. autoclass:: sentry_sdk.traces.StreamedSpan
:members:

.. autoclass:: sentry_sdk.tracing.Span
.. autoclass:: sentry_sdk.traces.NoOpStreamedSpan
:members:

.. autoclass:: sentry_sdk.session.Session
Expand Down
2 changes: 1 addition & 1 deletion sentry_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"capture_exception",
"capture_message",
"continue_trace",
"new_trace",
"flush",
"flush_async",
"get_baggage",
Expand All @@ -45,7 +46,6 @@
"set_tags",
"set_user",
"start_span",
"start_transaction",
"trace",
"monitor",
"logger",
Expand Down
1 change: 0 additions & 1 deletion sentry_sdk/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,6 @@ class DataCollection(TypedDict):
EventProcessor = Callable[[Event, Hint], Optional[Event]]
ErrorProcessor = Callable[[Event, ExcInfo], Optional[Event]]
BreadcrumbProcessor = Callable[[Breadcrumb, BreadcrumbHint], Optional[Breadcrumb]]
TransactionProcessor = Callable[[Event, Hint], Optional[Event]]
LogProcessor = Callable[[Log, Hint], Optional[Log]]

TracesSampler = Callable[[SamplingContext], Union[float, int, bool]]
Expand Down
3 changes: 1 addition & 2 deletions sentry_sdk/ai/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def record_token_usage(

if input_tokens_cached is not None:
span.set_attribute(
SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
input_tokens_cached,
SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, input_tokens_cached
)

if input_tokens_cache_write is not None:
Expand Down
84 changes: 18 additions & 66 deletions sentry_sdk/api.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import inspect
from typing import TYPE_CHECKING

from sentry_sdk import Client, tracing_utils
from sentry_sdk import Client, traces
from sentry_sdk._init_implementation import init
from sentry_sdk.crons import monitor
from sentry_sdk.scope import Scope, isolation_scope, new_scope
from sentry_sdk.tracing import NoOpSpan, Transaction, trace
from sentry_sdk.traces import trace

if TYPE_CHECKING:
from collections.abc import Mapping
Expand All @@ -19,19 +19,16 @@
overload,
)

from typing_extensions import Unpack

from sentry_sdk._types import (
Breadcrumb,
BreadcrumbHint,
Event,
ExcInfo,
Hint,
LogLevelStr,
SamplingContext,
)
from sentry_sdk.client import BaseClient
from sentry_sdk.tracing import Span, TransactionKwargs
from sentry_sdk.traces import StreamedSpan

T = TypeVar("T")
F = TypeVar("F", bound=Callable[..., Any])
Expand All @@ -50,6 +47,7 @@ def overload(x: "T") -> "T":
"capture_exception",
"capture_message",
"continue_trace",
"new_trace",
"flush",
"flush_async",
"get_baggage",
Expand All @@ -73,7 +71,6 @@ def overload(x: "T") -> "T":
"set_tags",
"set_user",
"start_span",
"start_transaction",
"trace",
"monitor",
"start_session",
Expand Down Expand Up @@ -273,59 +270,15 @@ async def flush_async(


@scopemethod
def start_span(
**kwargs: "Any",
) -> "Span":
return get_current_scope().start_span(**kwargs)


@scopemethod
def start_transaction(
transaction: "Optional[Transaction]" = None,
custom_sampling_context: "Optional[SamplingContext]" = None,
**kwargs: "Unpack[TransactionKwargs]",
) -> "Union[Transaction, NoOpSpan]":
"""
Start and return a transaction on the current scope.

Start an existing transaction if given, otherwise create and start a new
transaction with kwargs.

This is the entry point to manual tracing instrumentation.

A tree structure can be built by adding child spans to the transaction,
and child spans to other spans. To start a new child span within the
transaction or any span, call the respective `.start_child()` method.

Every child span must be finished before the transaction is finished,
otherwise the unfinished spans are discarded.
def start_span(**kwargs: "Any") -> "StreamedSpan":
return traces.start_span(**kwargs)

When used as context managers, spans and transactions are automatically
finished at the end of the `with` block. If not using context managers,
call the `.finish()` method.

When the transaction is finished, it will be sent to Sentry with all its
finished child spans.

:param transaction: The transaction to start. If omitted, we create and
start a new transaction.
:param custom_sampling_context: The transaction's custom sampling context.
:param kwargs: Optional keyword arguments to be passed to the Transaction
constructor. See :py:class:`sentry_sdk.tracing.Transaction` for
available arguments.
"""
return get_current_scope().start_transaction(
transaction, custom_sampling_context, **kwargs
)


def get_current_span(
scope: "Optional[Scope]" = None,
) -> "Optional[Span]":
def get_current_span(scope: "Optional[Scope]" = None) -> "Optional[StreamedSpan]":
"""
Returns the currently active span if there is one running, otherwise `None`
"""
return tracing_utils.get_current_span(scope)
return traces.get_current_span(scope)


def get_traceparent() -> "Optional[str]":
Expand All @@ -346,19 +299,18 @@ def get_baggage() -> "Optional[str]":
return None


def continue_trace(
environ_or_headers: "Dict[str, Any]",
op: "Optional[str]" = None,
name: "Optional[str]" = None,
source: "Optional[str]" = None,
origin: str = "manual",
) -> "Transaction":
def continue_trace(incoming: "Dict[str, Any]") -> None:
"""
Sets the propagation context from environment or headers and returns a transaction.
Sets the propagation context from environment or headers.
"""
return get_isolation_scope().continue_trace(
environ_or_headers, op, name, source, origin
)
return traces.continue_trace(incoming)


def new_trace() -> None:
"""
Resets the propagation context, forcing a new trace.
"""
return traces.new_trace()


@scopemethod
Expand Down
Loading
Loading