|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from collections import OrderedDict |
| 16 | +import functools |
| 17 | + |
| 18 | +import grpc |
| 19 | +from grpc.aio import ClientCallDetails |
| 20 | + |
| 21 | +from opentelemetry import context, trace |
| 22 | +from opentelemetry.semconv.trace import SpanAttributes |
| 23 | +from opentelemetry.propagate import inject |
| 24 | +from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY |
| 25 | +from opentelemetry.instrumentation.grpc.version import __version__ |
| 26 | + |
| 27 | +from opentelemetry.trace.status import Status, StatusCode |
| 28 | + |
| 29 | +from opentelemetry.instrumentation.grpc._client import ( |
| 30 | + OpenTelemetryClientInterceptor, |
| 31 | + _carrier_setter, |
| 32 | +) |
| 33 | + |
| 34 | + |
| 35 | +def _unary_done_callback(span, code, details): |
| 36 | + def callback(call): |
| 37 | + try: |
| 38 | + span.set_attribute( |
| 39 | + SpanAttributes.RPC_GRPC_STATUS_CODE, |
| 40 | + code.value[0], |
| 41 | + ) |
| 42 | + if code != grpc.StatusCode.OK: |
| 43 | + span.set_status( |
| 44 | + Status( |
| 45 | + status_code=StatusCode.ERROR, |
| 46 | + description=details, |
| 47 | + ) |
| 48 | + ) |
| 49 | + finally: |
| 50 | + span.end() |
| 51 | + |
| 52 | + return callback |
| 53 | + |
| 54 | + |
| 55 | +class _BaseAioClientInterceptor(OpenTelemetryClientInterceptor): |
| 56 | + @staticmethod |
| 57 | + def propagate_trace_in_details(client_call_details): |
| 58 | + method = client_call_details.method.decode("utf-8") |
| 59 | + metadata = client_call_details.metadata |
| 60 | + if not metadata: |
| 61 | + mutable_metadata = OrderedDict() |
| 62 | + else: |
| 63 | + mutable_metadata = OrderedDict(metadata) |
| 64 | + |
| 65 | + inject(mutable_metadata, setter=_carrier_setter) |
| 66 | + metadata = tuple(mutable_metadata.items()) |
| 67 | + |
| 68 | + return ClientCallDetails( |
| 69 | + client_call_details.method, |
| 70 | + client_call_details.timeout, |
| 71 | + metadata, |
| 72 | + client_call_details.credentials, |
| 73 | + client_call_details.wait_for_ready, |
| 74 | + ) |
| 75 | + |
| 76 | + @staticmethod |
| 77 | + def add_error_details_to_span(span, exc): |
| 78 | + if isinstance(exc, grpc.RpcError): |
| 79 | + span.set_attribute( |
| 80 | + SpanAttributes.RPC_GRPC_STATUS_CODE, |
| 81 | + exc.code().value[0], |
| 82 | + ) |
| 83 | + span.set_status( |
| 84 | + Status( |
| 85 | + status_code=StatusCode.ERROR, |
| 86 | + description=f"{type(exc).__name__}: {exc}", |
| 87 | + ) |
| 88 | + ) |
| 89 | + span.record_exception(exc) |
| 90 | + |
| 91 | + async def _wrap_unary_response(self, continuation, span): |
| 92 | + try: |
| 93 | + call = await continuation() |
| 94 | + |
| 95 | + # code and details are both coroutines that need to be await-ed, |
| 96 | + # the callbacks added with add_done_callback do not allow async |
| 97 | + # code so we need to get the code and details here then pass them |
| 98 | + # to the callback. |
| 99 | + code = await call.code() |
| 100 | + details = await call.details() |
| 101 | + |
| 102 | + call.add_done_callback(_unary_done_callback(span, code, details)) |
| 103 | + |
| 104 | + return call |
| 105 | + except grpc.aio.AioRpcError as exc: |
| 106 | + self.add_error_details_to_span(span, exc) |
| 107 | + raise exc |
| 108 | + |
| 109 | + async def _wrap_stream_response(self, span, call): |
| 110 | + try: |
| 111 | + async for response in call: |
| 112 | + yield response |
| 113 | + except Exception as exc: |
| 114 | + self.add_error_details_to_span(span, exc) |
| 115 | + raise exc |
| 116 | + finally: |
| 117 | + span.end() |
| 118 | + |
| 119 | + |
| 120 | +class UnaryUnaryAioClientInterceptor( |
| 121 | + grpc.aio.UnaryUnaryClientInterceptor, |
| 122 | + _BaseAioClientInterceptor, |
| 123 | +): |
| 124 | + async def intercept_unary_unary( |
| 125 | + self, continuation, client_call_details, request |
| 126 | + ): |
| 127 | + if context.get_value(_SUPPRESS_INSTRUMENTATION_KEY): |
| 128 | + return await continuation(client_call_details, request) |
| 129 | + |
| 130 | + method = client_call_details.method.decode("utf-8") |
| 131 | + with self._start_span( |
| 132 | + method, |
| 133 | + end_on_exit=False, |
| 134 | + record_exception=False, |
| 135 | + set_status_on_exception=False, |
| 136 | + ) as span: |
| 137 | + new_details = self.propagate_trace_in_details(client_call_details) |
| 138 | + |
| 139 | + continuation_with_args = functools.partial( |
| 140 | + continuation, new_details, request |
| 141 | + ) |
| 142 | + return await self._wrap_unary_response( |
| 143 | + continuation_with_args, span |
| 144 | + ) |
| 145 | + |
| 146 | + |
| 147 | +class UnaryStreamAioClientInterceptor( |
| 148 | + grpc.aio.UnaryStreamClientInterceptor, |
| 149 | + _BaseAioClientInterceptor, |
| 150 | +): |
| 151 | + async def intercept_unary_stream( |
| 152 | + self, continuation, client_call_details, request |
| 153 | + ): |
| 154 | + if context.get_value(_SUPPRESS_INSTRUMENTATION_KEY): |
| 155 | + return await continuation(client_call_details, request) |
| 156 | + |
| 157 | + method = client_call_details.method.decode("utf-8") |
| 158 | + with self._start_span( |
| 159 | + method, |
| 160 | + end_on_exit=False, |
| 161 | + record_exception=False, |
| 162 | + set_status_on_exception=False, |
| 163 | + ) as span: |
| 164 | + new_details = self.propagate_trace_in_details(client_call_details) |
| 165 | + |
| 166 | + resp = await continuation(new_details, request) |
| 167 | + |
| 168 | + return self._wrap_stream_response(span, resp) |
| 169 | + |
| 170 | + |
| 171 | +class StreamUnaryAioClientInterceptor( |
| 172 | + grpc.aio.StreamUnaryClientInterceptor, |
| 173 | + _BaseAioClientInterceptor, |
| 174 | +): |
| 175 | + async def intercept_stream_unary( |
| 176 | + self, continuation, client_call_details, request_iterator |
| 177 | + ): |
| 178 | + if context.get_value(_SUPPRESS_INSTRUMENTATION_KEY): |
| 179 | + return await continuation(client_call_details, request_iterator) |
| 180 | + |
| 181 | + method = client_call_details.method.decode("utf-8") |
| 182 | + with self._start_span( |
| 183 | + method, |
| 184 | + end_on_exit=False, |
| 185 | + record_exception=False, |
| 186 | + set_status_on_exception=False, |
| 187 | + ) as span: |
| 188 | + new_details = self.propagate_trace_in_details(client_call_details) |
| 189 | + |
| 190 | + continuation_with_args = functools.partial( |
| 191 | + continuation, new_details, request_iterator |
| 192 | + ) |
| 193 | + return await self._wrap_unary_response( |
| 194 | + continuation_with_args, span |
| 195 | + ) |
| 196 | + |
| 197 | + |
| 198 | +class StreamStreamAioClientInterceptor( |
| 199 | + grpc.aio.StreamStreamClientInterceptor, |
| 200 | + _BaseAioClientInterceptor, |
| 201 | +): |
| 202 | + async def intercept_stream_stream( |
| 203 | + self, continuation, client_call_details, request_iterator |
| 204 | + ): |
| 205 | + if context.get_value(_SUPPRESS_INSTRUMENTATION_KEY): |
| 206 | + return await continuation(client_call_details, request_iterator) |
| 207 | + |
| 208 | + method = client_call_details.method.decode("utf-8") |
| 209 | + with self._start_span( |
| 210 | + method, |
| 211 | + end_on_exit=False, |
| 212 | + record_exception=False, |
| 213 | + set_status_on_exception=False, |
| 214 | + ) as span: |
| 215 | + new_details = self.propagate_trace_in_details(client_call_details) |
| 216 | + |
| 217 | + resp = await continuation(new_details, request_iterator) |
| 218 | + |
| 219 | + return self._wrap_stream_response(span, resp) |
0 commit comments