Skip to content
Open
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
140 changes: 43 additions & 97 deletions sentry_sdk/integrations/grpc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.integrations import DidNotEnable
from sentry_sdk.integrations.grpc.consts import SPAN_ORIGIN
from sentry_sdk.tracing_utils import has_span_streaming_enabled

if TYPE_CHECKING:
from typing import Any, Callable, Iterable, Iterator, Union
Expand All @@ -31,54 +30,27 @@ def intercept_unary_unary(
) -> "_UnaryOutcome":
method = client_call_details.method

span_streaming = has_span_streaming_enabled(sentry_sdk.get_client().options)
if span_streaming:
if sentry_sdk.traces.get_current_span() is None:
client_call_details = (
self._update_client_call_details_metadata_from_scope(
client_call_details
)
)
return continuation(client_call_details, request)
with sentry_sdk.traces.start_span(
name="unary unary call to %s" % method,
attributes={
"sentry.op": OP.GRPC_CLIENT,
"sentry.origin": SPAN_ORIGIN,
SPANDATA.RPC_METHOD: method,
},
) as span:
client_call_details = (
self._update_client_call_details_metadata_from_scope(
client_call_details
)
)

response = continuation(client_call_details, request)
span.set_attribute(
SPANDATA.RPC_RESPONSE_STATUS_CODE, response.code().name
)

return response
else:
with sentry_sdk.start_span(
op=OP.GRPC_CLIENT,
name="unary unary call to %s" % method,
origin=SPAN_ORIGIN,
) as span:
span.set_data("type", "unary unary")
span.set_data("method", method)

client_call_details = (
self._update_client_call_details_metadata_from_scope(
client_call_details
)
)

response = continuation(client_call_details, request)
span.set_data("code", response.code().name)

return response
if sentry_sdk.traces.get_current_span() is None:
client_call_details = self._update_client_call_details_metadata_from_scope(
client_call_details
)
return continuation(client_call_details, request)
with sentry_sdk.traces.start_span(
name="unary unary call to %s" % method,
attributes={
"sentry.op": OP.GRPC_CLIENT,
"sentry.origin": SPAN_ORIGIN,
SPANDATA.RPC_METHOD: method,
},
) as span:
client_call_details = self._update_client_call_details_metadata_from_scope(
client_call_details
)

response = continuation(client_call_details, request)
span.set_attribute(SPANDATA.RPC_RESPONSE_STATUS_CODE, response.code().name)

return response

def intercept_unary_stream(
self: "ClientInterceptor",
Expand All @@ -88,55 +60,29 @@ def intercept_unary_stream(
) -> "Union[Iterator[Message], Call]":
method = client_call_details.method

span_streaming = has_span_streaming_enabled(sentry_sdk.get_client().options)
response: "UnaryStreamCall"
if span_streaming:
if sentry_sdk.traces.get_current_span() is None:
client_call_details = (
self._update_client_call_details_metadata_from_scope(
client_call_details
)
)
return continuation(client_call_details, request)
with sentry_sdk.traces.start_span(
name="unary stream call to %s" % method,
attributes={
"sentry.op": OP.GRPC_CLIENT,
"sentry.origin": SPAN_ORIGIN,
SPANDATA.RPC_METHOD: method,
},
) as span:
client_call_details = (
self._update_client_call_details_metadata_from_scope(
client_call_details
)
)

response = continuation(client_call_details, request)
# Setting code on unary-stream leads to execution getting stuck
# span.set_data("code", response.code().name)

return response
else:
with sentry_sdk.start_span(
op=OP.GRPC_CLIENT,
name="unary stream call to %s" % method,
origin=SPAN_ORIGIN,
) as span:
span.set_data("type", "unary stream")
span.set_data("method", method)

client_call_details = (
self._update_client_call_details_metadata_from_scope(
client_call_details
)
)

response = continuation(client_call_details, request)
# Setting code on unary-stream leads to execution getting stuck
# span.set_data("code", response.code().name)

return response
if sentry_sdk.traces.get_current_span() is None:
client_call_details = self._update_client_call_details_metadata_from_scope(
client_call_details
)
return continuation(client_call_details, request)
with sentry_sdk.traces.start_span(
name="unary stream call to %s" % method,
attributes={
"sentry.op": OP.GRPC_CLIENT,
"sentry.origin": SPAN_ORIGIN,
SPANDATA.RPC_METHOD: method,
},
):
client_call_details = self._update_client_call_details_metadata_from_scope(
client_call_details
)

response = continuation(client_call_details, request)
# Setting code on unary-stream leads to execution getting stuck
# span.set_data("code", response.code().name)

return response

@staticmethod
def _update_client_call_details_metadata_from_scope(
Expand Down
47 changes: 14 additions & 33 deletions sentry_sdk/integrations/grpc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from sentry_sdk.integrations import DidNotEnable
from sentry_sdk.integrations.grpc.consts import SPAN_ORIGIN
from sentry_sdk.traces import SegmentNameSource
from sentry_sdk.tracing import TransactionSource
from sentry_sdk.tracing_utils import has_span_streaming_enabled

if TYPE_CHECKING:
from typing import Callable, Optional
Expand Down Expand Up @@ -48,39 +46,22 @@ def behavior(request: "Message", context: "ServicerContext") -> "Message":
if name:
metadata = dict(context.invocation_metadata())

span_streaming = has_span_streaming_enabled(
sentry_sdk.get_client().options
)
if span_streaming:
sentry_sdk.traces.continue_trace(metadata)
sentry_sdk.traces.continue_trace(metadata)

with sentry_sdk.traces.start_span(
name=name,
attributes={
"sentry.op": OP.GRPC_SERVER,
"sentry.segment.name.source": SegmentNameSource.CUSTOM.value,
"sentry.origin": SPAN_ORIGIN,
},
parent_span=None,
):
try:
return handler.unary_unary(request, context)
except BaseException as e:
raise e
else:
transaction = sentry_sdk.continue_trace(
metadata,
op=OP.GRPC_SERVER,
name=name,
source=TransactionSource.CUSTOM,
origin=SPAN_ORIGIN,
)
with sentry_sdk.traces.start_span(
name=name,
attributes={
"sentry.op": OP.GRPC_SERVER,
"sentry.segment.name.source": SegmentNameSource.CUSTOM.value,
"sentry.origin": SPAN_ORIGIN,
},
parent_span=None,
):
try:
return handler.unary_unary(request, context)
except BaseException as e:
raise e

with sentry_sdk.start_transaction(transaction=transaction):
try:
return handler.unary_unary(request, context)
except BaseException as e:
raise e
else:
return handler.unary_unary(request, context)

Expand Down
Loading
Loading