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
132 changes: 40 additions & 92 deletions sentry_sdk/integrations/grpc/aio/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

try:
from google.protobuf.message import Message
Expand Down Expand Up @@ -50,54 +49,29 @@ async def intercept_unary_unary(
) -> "Union[UnaryUnaryCall, Message]":
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 await continuation(client_call_details, request)
with sentry_sdk.traces.start_span(
name="unary unary call to %s" % method.decode(),
attributes={
"sentry.op": OP.GRPC_CLIENT,
"sentry.origin": SPAN_ORIGIN,
SPANDATA.RPC_METHOD: method.decode(),
},
) as span:
client_call_details = (
self._update_client_call_details_metadata_from_scope(
client_call_details
)
)

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

return response
else:
with sentry_sdk.start_span(
op=OP.GRPC_CLIENT,
name="unary unary call to %s" % method.decode(),
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
)
)
if sentry_sdk.traces.get_current_span() is None:
client_call_details = self._update_client_call_details_metadata_from_scope(
client_call_details
)
return await continuation(client_call_details, request)

with sentry_sdk.traces.start_span(
name="unary unary call to %s" % method.decode(),
attributes={
"sentry.op": OP.GRPC_CLIENT,
"sentry.origin": SPAN_ORIGIN,
SPANDATA.RPC_METHOD: method.decode(),
},
) as span:
client_call_details = self._update_client_call_details_metadata_from_scope(
client_call_details
)

response = await continuation(client_call_details, request)
status_code = await response.code()
span.set_data("code", status_code.name)
response = await continuation(client_call_details, request)
status_code = await response.code()
span.set_attribute(SPANDATA.RPC_RESPONSE_STATUS_CODE, status_code.name)

return response
return response


class SentryUnaryStreamClientInterceptor(
Expand All @@ -112,49 +86,23 @@ async def intercept_unary_stream(
) -> "Union[AsyncIterable[Any], UnaryStreamCall]":
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 await continuation(client_call_details, request)
with sentry_sdk.traces.start_span(
name="unary stream call to %s" % method.decode(),
attributes={
"sentry.op": OP.GRPC_CLIENT,
"sentry.origin": SPAN_ORIGIN,
SPANDATA.RPC_METHOD: method.decode(),
},
) as span:
client_call_details = (
self._update_client_call_details_metadata_from_scope(
client_call_details
)
)

response = await continuation(client_call_details, request)

return response
else:
with sentry_sdk.start_span(
op=OP.GRPC_CLIENT,
name="unary stream call to %s" % method.decode(),
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 = await continuation(client_call_details, request)
# status_code = await response.code()
# span.set_data("code", status_code)
if sentry_sdk.traces.get_current_span() is None:
client_call_details = self._update_client_call_details_metadata_from_scope(
client_call_details
)
return await continuation(client_call_details, request)

with sentry_sdk.traces.start_span(
name="unary stream call to %s" % method.decode(),
attributes={
"sentry.op": OP.GRPC_CLIENT,
"sentry.origin": SPAN_ORIGIN,
SPANDATA.RPC_METHOD: method.decode(),
},
):
client_call_details = self._update_client_call_details_metadata_from_scope(
client_call_details
)

return response
response = await continuation(client_call_details, request)
return response
76 changes: 24 additions & 52 deletions sentry_sdk/integrations/grpc/aio/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
from sentry_sdk.utils import event_from_exception

if TYPE_CHECKING:
Expand Down Expand Up @@ -54,57 +52,31 @@ async def wrapped(request: "Any", context: "ServicerContext") -> "Any":
if not name:
return await handler(request, context)

span_streaming = has_span_streaming_enabled(
sentry_sdk.get_client().options
# What if the headers are empty?
sentry_sdk.traces.continue_trace(
dict(context.invocation_metadata())
)
if span_streaming:
# What if the headers are empty?
sentry_sdk.traces.continue_trace(
dict(context.invocation_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 await handler.unary_unary(request, context)
except AbortError:
raise
except Exception as exc:
event, hint = event_from_exception(
exc,
mechanism={"type": "grpc", "handled": False},
)
sentry_sdk.capture_event(event, hint=hint)
raise
else:
# What if the headers are empty?
transaction = sentry_sdk.continue_trace(
dict(context.invocation_metadata()),
op=OP.GRPC_SERVER,
name=name,
source=TransactionSource.CUSTOM,
origin=SPAN_ORIGIN,
)

with sentry_sdk.start_transaction(transaction=transaction):
try:
return await handler.unary_unary(request, context)
except AbortError:
raise
except Exception as exc:
event, hint = event_from_exception(
exc,
mechanism={"type": "grpc", "handled": False},
)
sentry_sdk.capture_event(event, hint=hint)
raise

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 await handler.unary_unary(request, context)
except AbortError:
raise
except Exception as exc:
event, hint = event_from_exception(
exc,
mechanism={"type": "grpc", "handled": False},
)
sentry_sdk.capture_event(event, hint=hint)
raise

elif not handler.request_streaming and handler.response_streaming:
handler_factory = grpc.unary_stream_rpc_method_handler
Expand Down
Loading