Skip to content

Commit 0723c86

Browse files
committed
manually manage the span lifecycle instead of relying context manager
1 parent 34db73e commit 0723c86

File tree

1 file changed

+14
-1
lines changed
  • instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc

1 file changed

+14
-1
lines changed

instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc/_client.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,17 @@ def _intercept_server_stream(
200200
else:
201201
mutable_metadata = OrderedDict(metadata)
202202

203-
with self._start_span(client_info.full_method) as span:
203+
# Manually manage span lifecycle to avoid context issues with generators
204+
span = self._tracer.start_span(
205+
name=client_info.full_method,
206+
kind=trace.SpanKind.CLIENT,
207+
attributes={
208+
SpanAttributes.RPC_SYSTEM: "grpc",
209+
SpanAttributes.RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[0],
210+
},
211+
)
212+
213+
try:
204214
inject(mutable_metadata, setter=_carrier_setter)
205215
metadata = tuple(mutable_metadata.items())
206216
rpc_info = RpcInfo(
@@ -218,6 +228,9 @@ def _intercept_server_stream(
218228
span.set_status(Status(StatusCode.ERROR))
219229
span.set_attribute(RPC_GRPC_STATUS_CODE, err.code().value[0])
220230
raise err
231+
finally:
232+
span.end()
233+
221234

222235
def intercept_stream(
223236
self, request_or_iterator, metadata, client_info, invoker

0 commit comments

Comments
 (0)