Skip to content
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

Fix blocking of gRPC stream-to-stream requests when elasticapm is enabled #1967

Merged
merged 2 commits into from
Feb 13, 2024
Merged
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
20 changes: 13 additions & 7 deletions elasticapm/contrib/grpc/async_server_interceptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,18 @@
import grpc

import elasticapm
from elasticapm.contrib.grpc.server_interceptor import _ServicerContextWrapper, _wrap_rpc_behavior, get_trace_parent
from elasticapm.contrib.grpc.server_interceptor import _ServicerContextWrapper, get_trace_parent


class _AsyncServerInterceptor(grpc.aio.ServerInterceptor):
async def intercept_service(self, continuation, handler_call_details):
def transaction_wrapper(behavior, request_streaming, response_streaming):
async def _interceptor(request_or_iterator, context):
if request_streaming or response_streaming: # only unary-unary is supported
return behavior(request_or_iterator, context)
def wrap_unary_unary(behavior):
async def _interceptor(request, context):
tp = get_trace_parent(handler_call_details)
client = elasticapm.get_client()
transaction = client.begin_transaction("request", trace_parent=tp)
try:
result = behavior(request_or_iterator, _ServicerContextWrapper(context, transaction))
result = behavior(request, _ServicerContextWrapper(context, transaction))

# This is so we can support both sync and async rpc functions
if inspect.isawaitable(result):
Expand All @@ -65,4 +63,12 @@ async def _interceptor(request_or_iterator, context):

return _interceptor

return _wrap_rpc_behavior(await continuation(handler_call_details), transaction_wrapper)
handler = await continuation(handler_call_details)
if handler.request_streaming or handler.response_streaming:
return handler

return grpc.unary_unary_rpc_method_handler(
wrap_unary_unary(handler.unary_unary),
request_deserializer=handler.request_deserializer,
response_serializer=handler.response_serializer,
)
Loading