Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa79c70

Browse files
committed
Handle streaming requests/responses in server
1 parent 82a78de commit aa79c70

File tree

1 file changed

+42
-62
lines changed
  • ext/opentelemetry-ext-grpc/src/opentelemetry/ext/grpc

1 file changed

+42
-62
lines changed

ext/opentelemetry-ext-grpc/src/opentelemetry/ext/grpc/_server.py

Lines changed: 42 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -197,69 +197,49 @@ def intercept_unary(self, request, servicer_context, server_info, handler):
197197

198198
return response
199199

200-
# # For RPCs that stream responses, the result can be a generator. To record
201-
# # the span across the generated responses and detect any errors, we wrap the
202-
# # result in a new generator that yields the response values.
203-
# def _intercept_server_stream(
204-
# self, request_or_iterator, servicer_context, server_info, handler
205-
# ):
206-
# with self._start_span(
207-
# servicer_context, server_info.full_method
208-
# ) as span:
209-
# rpc_info = RpcInfo(
210-
# full_method=server_info.full_method,
211-
# metadata=servicer_context.invocation_metadata(),
212-
# timeout=servicer_context.time_remaining(),
213-
# )
214-
# if not server_info.is_client_stream:
215-
# rpc_info.request = request_or_iterator
216-
# servicer_context = _OpenTelemetryServicerContext(
217-
# servicer_context, span
218-
# )
219-
# try:
220-
# result = handler(request_or_iterator, servicer_context)
221-
# for response in result:
222-
# yield response
223-
# except Exception:
224-
# e = sys.exc_info()[0]
225-
# span.set_tag("error", True)
226-
# span.log_kv({"event": "error", "error.object": e})
227-
# rpc_info.error = e
228-
# raise
229-
# _check_error_code(span, servicer_context, rpc_info)
200+
# For RPCs that stream responses, the result can be a generator. To record
201+
# the span across the generated responses and detect any errors, we wrap
202+
# the result in a new generator that yields the response values.
203+
def _intercept_server_stream(
204+
self, request_or_iterator, servicer_context, server_info, handler
205+
):
206+
with self._start_span(
207+
servicer_context, server_info.full_method
208+
) as span:
209+
rpc_info = RpcInfo(
210+
full_method=server_info.full_method,
211+
metadata=servicer_context.invocation_metadata(),
212+
timeout=servicer_context.time_remaining(),
213+
)
214+
if not server_info.is_client_stream:
215+
rpc_info.request = request_or_iterator
216+
servicer_context = _OpenTelemetryServicerContext(
217+
servicer_context, span
218+
)
219+
result = handler(request_or_iterator, servicer_context)
220+
for response in result:
221+
yield response
222+
_check_error_code(span, servicer_context, rpc_info)
230223

231224
def intercept_stream(
232225
self, request_or_iterator, servicer_context, server_info, handler
233226
):
234-
pass
235-
236-
# def intercept_stream(
237-
# self, request_or_iterator, servicer_context, server_info, handler
238-
# ):
239-
# """TODO doc and types"""
240-
# if server_info.is_server_stream:
241-
# return self._intercept_server_stream(
242-
# request_or_iterator, servicer_context, server_info, handler
243-
# )
244-
# with self._start_span(
245-
# servicer_context, server_info.full_method
246-
# ) as span:
247-
# rpc_info = RpcInfo(
248-
# full_method=server_info.full_method,
249-
# metadata=servicer_context.invocation_metadata(),
250-
# timeout=servicer_context.time_remaining(),
251-
# )
252-
# servicer_context = _OpenTelemetryServicerContext(
253-
# servicer_context, span
254-
# )
255-
# try:
256-
# response = handler(request_or_iterator, servicer_context)
257-
# except Exception:
258-
# e = sys.exc_info()[0]
259-
# span.set_tag("error", True)
260-
# span.log_kv({"event": "error", "error.object": e})
261-
# rpc_info.error = e
262-
# raise
263-
# _check_error_code(span, servicer_context, rpc_info)
264-
# rpc_info.response = response
265-
# return response
227+
if server_info.is_server_stream:
228+
return self._intercept_server_stream(
229+
request_or_iterator, servicer_context, server_info, handler
230+
)
231+
with self._start_span(
232+
servicer_context, server_info.full_method
233+
) as span:
234+
rpc_info = RpcInfo(
235+
full_method=server_info.full_method,
236+
metadata=servicer_context.invocation_metadata(),
237+
timeout=servicer_context.time_remaining(),
238+
)
239+
servicer_context = _OpenTelemetryServicerContext(
240+
servicer_context, span
241+
)
242+
response = handler(request_or_iterator, servicer_context)
243+
_check_error_code(span, servicer_context, rpc_info)
244+
rpc_info.response = response
245+
return response

0 commit comments

Comments
 (0)