8000 Implement new continue_trace and make WSGI work by sl0thentr0py · Pull Request #3460 · getsentry/sentry-python · GitHub
[go: up one dir, main page]

Skip to content

Implement new continue_trace and make WSGI work #3460

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

Merged
merged 2 commits into from
Aug 26, 2024
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
14 changes: 8 additions & 6 deletions sentry_sdk/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
from contextlib import contextmanager

from sentry_sdk import tracing_utils, Client
from sentry_sdk._init_implementation import init
Expand All @@ -23,6 +24,7 @@
from typing import Callable
from typing import TypeVar
from typing import Union
from typing import Generator

from typing_extensions import Unpack

Expand Down Expand Up @@ -336,11 +338,11 @@ def get_baggage():
return None


def continue_trace(environ_or_headers, op=None, name=None, source=None, origin=None):
# type: (Dict[str, Any], Optional[str], Optional[str], Optional[str], Optional[str]) -> Transaction
@contextmanager
def continue_trace(environ_or_headers):
# type: (Dict[str, Any]) -> Generator[None, None, None]
"""
Sets the propagation context from environment or headers and returns a transaction.
Sets the propagation context from environment or headers to continue an incoming trace.
"""
return get_isolation_scope().continue_trace(
environ_or_headers, op, name, source, origin
)
with get_isolation_scope().continue_trace(environ_or_headers):
yield
10 changes: 9 additions & 1 deletion sentry_sdk/integrations/opentelemetry/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from sentry_sdk._types import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Tuple, Optional, Generator
from typing import Tuple, Optional, Generator, Dict, Any


class PotelScope(Scope):
8000 Expand Down Expand Up @@ -58,6 +58,14 @@ def _get_isolation_scope(cls):
scopes = cls._get_scopes()
return scopes[1] if scopes else None

@contextmanager
def continue_trace(self, environ_or_headers):
# type: (Dict[str, Any]) -> Generator[None, None, None]
with new_scope() as scope:
scope.generate_propagation_context(environ_or_headers)
# TODO-neel-potel add remote span on context
yield


_INITIAL_CURRENT_SCOPE = PotelScope(ty=ScopeType.CURRENT)
_INITIAL_ISOLATION_SCOPE = PotelScope(ty=ScopeType.ISOLATION)
Expand Down
39 changes: 19 additions & 20 deletions sentry_sdk/integrations/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,26 +91,25 @@ def __call__(self, environ, start_response):
)
)

transaction = continue_trace(
environ,
op=OP.HTTP_SERVER,
name="generic WSGI request",
source=TRANSACTION_SOURCE_ROUTE,
origin=self.span_origin,
)

with sentry_sdk.start_transaction(
transaction, custom_sampling_context={"wsgi_environ": environ}
):
try:
response = self.app(
environ,
partial(
_sentry_start_response, start_response, transaction
),
)
except BaseException:
reraise(*_capture_exception())
with continue_trace(environ):
with sentry_sdk.start_transaction(
op=OP.HTTP_SERVER,
name="generic WSGI request",
source=TRANSACTION_SOURCE_ROUTE,
origin=self.span_origin,
custom_sampling_context={"wsgi_environ": environ},
) as transaction:
try:
response = self.app(
environ,
partial(
_sentry_start_response,
start_response,
transaction,
),
)
except BaseException:
reraise(*_capture_exception())
finally:
_wsgi_middleware_applied.set(False)

Expand Down
30 changes: 28 additions & 2 deletions sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class SpanKwargs(TypedDict, total=False):
scope: "sentry_sdk.Scope"
"""The scope to use for this span. If not provided, we use the current scope."""

origin: str
origin: Optional[str]
"""
The origin of the span.
See https://develop.sentry.dev/sdk/performance/trace-origin/
Expand Down Expand Up @@ -1401,6 +1401,32 @@ def source(self, value):
# type: (str) -> None
pass

@property
def start_timestamp(self):
# type: () -> Optional[datetime]
start_time = self._otel_span.start_time
if start_time is None:
return None

from sentry_sdk.integrations.opentelemetry.utils import (
convert_from_otel_timestamp,
)

return convert_from_otel_timestamp(start_time)

@property
def timestamp(self):
# type: () -> Optional[datetime]
end_time = self._otel_span.end_time
if end_time is None:
return None

from sentry_sdk.integrations.opentelemetry.utils import (
convert_from_otel_timestamp,
)

return convert_from_otel_timestamp(end_time)

def start_child(self, **kwargs):
# type: (str, **Any) -> POTelSpan
kwargs.setdefault("sampled", self.sampled)
Expand Down Expand Up @@ -1485,7 +1511,7 @@ def set_status(self, status):
otel_description = None
else:
otel_status = StatusCode.ERROR
otel_description = status.value
otel_description = status

self._otel_span.set_status(otel_status, otel_description)

Expand Down
Loading
0