Skip to content

Commit 460f141

Browse files
authored
Fix blocking of gRPC stream-to-stream requests when elasticapm is enabled (#1967)
* Fix gprc support with streaming requests (#1966) * Update version.py
1 parent c7bde0a commit 460f141

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

elasticapm/contrib/grpc/async_server_interceptor.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,18 @@
3333
import grpc
3434

3535
import elasticapm
36-
from elasticapm.contrib.grpc.server_interceptor import _ServicerContextWrapper, _wrap_rpc_behavior, get_trace_parent
36+
from elasticapm.contrib.grpc.server_interceptor import _ServicerContextWrapper, get_trace_parent
3737

3838

3939
class _AsyncServerInterceptor(grpc.aio.ServerInterceptor):
4040
async def intercept_service(self, continuation, handler_call_details):
41-
def transaction_wrapper(behavior, request_streaming, response_streaming):
42-
async def _interceptor(request_or_iterator, context):
43-
if request_streaming or response_streaming: # only unary-unary is supported
44-
return behavior(request_or_iterator, context)
41+
def wrap_unary_unary(behavior):
42+
async def _interceptor(request, context):
4543
tp = get_trace_parent(handler_call_details)
4644
client = elasticapm.get_client()
4745
transaction = client.begin_transaction("request", trace_parent=tp)
4846
try:
49-
result = behavior(request_or_iterator, _ServicerContextWrapper(context, transaction))
47+
result = behavior(request, _ServicerContextWrapper(context, transaction))
5048

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

6664
return _interceptor
6765

68-
return _wrap_rpc_behavior(await continuation(handler_call_details), transaction_wrapper)
66+
handler = await continuation(handler_call_details)
67+
if handler.request_streaming or handler.response_streaming:
68+
return handler
69+
70+
return grpc.unary_unary_rpc_method_handler(
71+
wrap_unary_unary(handler.unary_unary),
72+
request_deserializer=handler.request_deserializer,
73+
response_serializer=handler.response_serializer,
74+
)

0 commit comments

Comments
 (0)