@@ -175,69 +175,19 @@ def client_request_hook(span: Any, request: Any) -> None:
175175
176176_grpc_client_request_hook = _make_grpc_client_request_hook ()
177177
178- # Mapping of standard gRPC integer status codes to their canonical status name strings.
179- # Used when stock gRPC wire spans encounter errors, guaranteeing mapping even in environments
180- # where the optional `grpc` package is not installed (e.g. REST-only environments).
181- _GRPC_INT_STATUS_CODE_TO_NAME = {
182- 0 : "OK" ,
183- 1 : "CANCELLED" ,
184- 2 : "UNKNOWN" ,
185- 3 : "INVALID_ARGUMENT" ,
186- 4 : "DEADLINE_EXCEEDED" ,
187- 5 : "NOT_FOUND" ,
188- 6 : "ALREADY_EXISTS" ,
189- 7 : "PERMISSION_DENIED" ,
190- 8 : "RESOURCE_EXHAUSTED" ,
191- 9 : "FAILED_PRECONDITION" ,
192- 10 : "ABORTED" ,
193- 11 : "OUT_OF_RANGE" ,
194- 12 : "UNIMPLEMENTED" ,
195- 13 : "INTERNAL" ,
196- 14 : "UNAVAILABLE" ,
197- 15 : "DATA_LOSS" ,
198- 16 : "UNAUTHENTICATED" ,
199- }
200-
201178
202179def _grpc_client_response_hook (span : Any , response : Any ) -> None :
203180 """OpenTelemetry gRPC client response hook to record response status code.
204181
182+ Note: Upstream OpenTelemetry gRPC instrumentation only invokes this response_hook
183+ on successful RPC invocations. Failed RPCs raise an exception before this hook is reached.
184+
205185 Args:
206186 span: The OpenTelemetry span.
207187 response: The gRPC response object or details.
208188 """
209- if span is None or not hasattr (span , "set_attribute" ):
210- return
211-
212- status = getattr (span , "status" , None )
213- status_code = getattr (status , "status_code" , None )
214- try :
215- from opentelemetry .trace .status import StatusCode
216-
217- if status_code == StatusCode .ERROR :
218- span_attrs = (
219- getattr (span , "attributes" , None )
220- or getattr (span , "_attributes" , None )
221- or {}
222- )
223- grpc_code = span_attrs .get ("rpc.grpc.status_code" )
224- if grpc_code is not None :
225- from google .api_core import exceptions
226-
227- name = None
228- if grpc_code in exceptions ._INT_TO_GRPC_CODE :
229- name = exceptions ._INT_TO_GRPC_CODE [grpc_code ].name
230- elif grpc_code in _GRPC_INT_STATUS_CODE_TO_NAME :
231- name = _GRPC_INT_STATUS_CODE_TO_NAME [grpc_code ]
232- if name :
233- span .set_attribute ("rpc.response.status_code" , name )
234- return
235- span .set_attribute ("rpc.response.status_code" , "ERROR" )
236- return
237- except Exception :
238- pass
239-
240- span .set_attribute ("rpc.response.status_code" , "OK" )
189+ if span is not None and hasattr (span , "set_attribute" ):
190+ span .set_attribute ("rpc.response.status_code" , "OK" )
241191
242192
243193def _get_tracer_provider (
0 commit comments