Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/grpc/_observability.py: 55%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

87 statements  

1# Copyright 2023 The gRPC 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 

15from __future__ import annotations 

16 

17import abc 

18import contextlib 

19import logging 

20import threading 

21from typing import Any, Generator, Generic, List, Optional, TypeVar 

22 

23from grpc._cython import cygrpc as _cygrpc 

24from grpc._typing import ChannelArgumentType 

25 

26_LOGGER = logging.getLogger(__name__) 

27 

28_channel = Any # _channel.py imports this module. 

29ClientCallTracerCapsule = TypeVar("ClientCallTracerCapsule") 

30ServerCallTracerFactoryCapsule = TypeVar("ServerCallTracerFactoryCapsule") 

31 

32_plugin_lock: threading.RLock = threading.RLock() 

33_OBSERVABILITY_PLUGIN: Optional["ObservabilityPlugin"] = None 

34_SERVICES_TO_EXCLUDE: List[bytes] = [ 

35 b"google.monitoring.v3.MetricService", 

36 b"google.devtools.cloudtrace.v2.TraceService", 

37] 

38 

39 

40class ServerCallTracerFactory: 

41 """An encapsulation of a ServerCallTracerFactory. 

42 

43 Instances of this class can be passed to a Channel as values for the 

44 grpc.experimental.server_call_tracer_factory option 

45 """ 

46 

47 def __init__(self, address): 

48 self._address = address 

49 

50 def __int__(self): 

51 return self._address 

52 

53 

54class ObservabilityPlugin( 

55 Generic[ClientCallTracerCapsule, ServerCallTracerFactoryCapsule], 

56 metaclass=abc.ABCMeta, 

57): 

58 """Abstract base class for observability plugin. 

59 

60 *This is a semi-private class that was intended for the exclusive use of 

61 the gRPC team.* 

62 

63 The ClientCallTracerCapsule and ClientCallTracerCapsule created by this 

64 plugin should be inject to gRPC core using observability_init at the 

65 start of a program, before any channels/servers are built. 

66 

67 Any future methods added to this interface cannot have the 

68 @abc.abstractmethod annotation. 

69 

70 Attributes: 

71 _stats_enabled: A bool indicates whether tracing is enabled. 

72 _tracing_enabled: A bool indicates whether stats(metrics) is enabled. 

73 _registered_methods: A set which stores the registered method names in 

74 bytes. 

75 """ 

76 

77 _tracing_enabled: bool = False 

78 _stats_enabled: bool = False 

79 

80 @abc.abstractmethod 

81 def create_client_call_tracer( 

82 self, method_name: bytes, target: bytes 

83 ) -> ClientCallTracerCapsule: 

84 """Creates a ClientCallTracerCapsule. 

85 

86 After register the plugin, if tracing or stats is enabled, this method 

87 will be called after a call was created, the ClientCallTracer created 

88 by this method will be saved to call context. 

89 

90 The ClientCallTracer is an object which implements `grpc_core::ClientCallTracer` 

91 interface and wrapped in a PyCapsule using `client_call_tracer` as name. 

92 

93 Args: 

94 method_name: The method name of the call in byte format. 

95 target: The channel target of the call in byte format. 

96 registered_method: Wether this method is pre-registered. 

97 

98 Returns: 

99 A PyCapsule which stores a ClientCallTracer object. 

100 """ 

101 raise NotImplementedError() 

102 

103 @abc.abstractmethod 

104 def delete_client_call_tracer( 

105 self, client_call_tracer: ClientCallTracerCapsule 

106 ) -> None: 

107 """Deletes the ClientCallTracer stored in ClientCallTracerCapsule. 

108 

109 After register the plugin, if tracing or stats is enabled, this method 

110 will be called at the end of the call to destroy the ClientCallTracer. 

111 

112 The ClientCallTracer is an object which implements `grpc_core::ClientCallTracer` 

113 interface and wrapped in a PyCapsule using `client_call_tracer` as name. 

114 

115 Args: 

116 client_call_tracer: A PyCapsule which stores a ClientCallTracer object. 

117 """ 

118 raise NotImplementedError() 

119 

120 @abc.abstractmethod 

121 def save_trace_context( 

122 self, trace_id: str, span_id: str, is_sampled: bool 

123 ) -> None: 

124 """Saves the trace_id and span_id related to the current span. 

125 

126 After register the plugin, if tracing is enabled, this method will be 

127 called after the server finished sending response. 

128 

129 This method can be used to propagate census context. 

130 

131 Args: 

132 trace_id: The identifier for the trace associated with the span as a 

133 32-character hexadecimal encoded string, 

134 e.g. 26ed0036f2eff2b7317bccce3e28d01f 

135 span_id: The identifier for the span as a 16-character hexadecimal encoded 

136 string. e.g. 113ec879e62583bc 

137 is_sampled: A bool indicates whether the span is sampled. 

138 """ 

139 raise NotImplementedError() 

140 

141 @abc.abstractmethod 

142 def create_server_call_tracer_factory( 

143 self, 

144 *, 

145 xds: bool = False, 

146 ) -> Optional[ServerCallTracerFactoryCapsule]: 

147 """Creates a ServerCallTracerFactoryCapsule. 

148 

149 This method will be called at server initialization time to create a 

150 ServerCallTracerFactory, which will be registered to gRPC core. 

151 

152 The ServerCallTracerFactory is an object which implements 

153 `grpc_core::ServerCallTracerFactory` interface and wrapped in a PyCapsule 

154 using `server_call_tracer_factory` as name. 

155 

156 Args: 

157 xds: Whether the server is xds server. 

158 Returns: 

159 A PyCapsule which stores a ServerCallTracerFactory object. Or None if 

160 plugin decides not to create ServerCallTracerFactory. 

161 """ 

162 raise NotImplementedError() 

163 

164 @abc.abstractmethod 

165 def record_rpc_latency( 

166 self, method: str, target: str, rpc_latency: float, status_code: Any 

167 ) -> None: 

168 """Record the latency of the RPC. 

169 

170 After register the plugin, if stats is enabled, this method will be 

171 called at the end of each RPC. 

172 

173 Args: 

174 method: The fully-qualified name of the RPC method being invoked. 

175 target: The target name of the RPC method being invoked. 

176 rpc_latency: The latency for the RPC in seconds, equals to the time between 

177 when the client invokes the RPC and when the client receives the status. 

178 status_code: An element of grpc.StatusCode in string format representing the 

179 final status for the RPC. 

180 """ 

181 raise NotImplementedError() 

182 

183 def set_tracing(self, enable: bool) -> None: 

184 """Enable or disable tracing. 

185 

186 Args: 

187 enable: A bool indicates whether tracing should be enabled. 

188 """ 

189 self._tracing_enabled = enable 

190 

191 def set_stats(self, enable: bool) -> None: 

192 """Enable or disable stats(metrics). 

193 

194 Args: 

195 enable: A bool indicates whether stats should be enabled. 

196 """ 

197 self._stats_enabled = enable 

198 

199 def save_registered_method(self, method_name: bytes) -> None: 

200 """Saves the method name to registered_method list. 

201 

202 When exporting metrics, method name for unregistered methods will be replaced 

203 with 'other' by default. 

204 

205 Args: 

206 method_name: The method name in bytes. 

207 """ 

208 raise NotImplementedError() 

209 

210 @property 

211 def tracing_enabled(self) -> bool: 

212 return self._tracing_enabled 

213 

214 @property 

215 def stats_enabled(self) -> bool: 

216 return self._stats_enabled 

217 

218 @property 

219 def observability_enabled(self) -> bool: 

220 return self.tracing_enabled or self.stats_enabled 

221 

222 

223@contextlib.contextmanager 

224def get_plugin() -> Generator[Optional[ObservabilityPlugin], None, None]: 

225 """Get the ObservabilityPlugin in _observability module. 

226 

227 Returns: 

228 The ObservabilityPlugin currently registered with the _observability 

229 module. Or None if no plugin exists at the time of calling this method. 

230 """ 

231 with _plugin_lock: 

232 yield _OBSERVABILITY_PLUGIN 

233 

234 

235def set_plugin(observability_plugin: Optional[ObservabilityPlugin]) -> None: 

236 """Save ObservabilityPlugin to _observability module. 

237 

238 Args: 

239 observability_plugin: The ObservabilityPlugin to save. 

240 

241 Raises: 

242 ValueError: If an ObservabilityPlugin was already registered at the 

243 time of calling this method. 

244 """ 

245 global _OBSERVABILITY_PLUGIN # pylint: disable=global-statement 

246 with _plugin_lock: 

247 if observability_plugin and _OBSERVABILITY_PLUGIN: 

248 raise ValueError("observability_plugin was already set!") 

249 _OBSERVABILITY_PLUGIN = observability_plugin 

250 

251 

252def observability_init(observability_plugin: ObservabilityPlugin) -> None: 

253 """Initialize observability with provided ObservabilityPlugin. 

254 

255 This method have to be called at the start of a program, before any 

256 channels/servers are built. 

257 

258 Args: 

259 observability_plugin: The ObservabilityPlugin to use. 

260 

261 Raises: 

262 ValueError: If an ObservabilityPlugin was already registered at the 

263 time of calling this method. 

264 """ 

265 set_plugin(observability_plugin) 

266 

267 

268def observability_deinit() -> None: 

269 """Clear the observability context, including ObservabilityPlugin and 

270 ServerCallTracerFactory 

271 

272 This method have to be called after exit observability context so that 

273 it's possible to re-initialize again. 

274 """ 

275 set_plugin(None) 

276 _cygrpc.clear_server_call_tracer_factory() 

277 

278 

279def delete_call_tracer(client_call_tracer_capsule: Any) -> None: 

280 """Deletes the ClientCallTracer stored in ClientCallTracerCapsule. 

281 

282 This method will be called at the end of the call to destroy the ClientCallTracer. 

283 

284 The ClientCallTracer is an object which implements `grpc_core::ClientCallTracer` 

285 interface and wrapped in a PyCapsule using `client_call_tracer` as the name. 

286 

287 Args: 

288 client_call_tracer_capsule: A PyCapsule which stores a ClientCallTracer object. 

289 """ 

290 with get_plugin() as plugin: 

291 if plugin and plugin.observability_enabled: 

292 plugin.delete_client_call_tracer(client_call_tracer_capsule) 

293 

294 

295def maybe_record_rpc_latency(state: "_channel._RPCState") -> None: 

296 """Record the latency of the RPC, if the plugin is registered and stats is enabled. 

297 

298 This method will be called at the end of each RPC. 

299 

300 Args: 

301 state: a grpc._channel._RPCState object which contains the stats related to the 

302 RPC. 

303 """ 

304 # TODO(xuanwn): use channel args to exclude those metrics. 

305 for exclude_prefix in _SERVICES_TO_EXCLUDE: 

306 if exclude_prefix in state.method.encode("utf8"): 

307 return 

308 with get_plugin() as plugin: 

309 if plugin and plugin.stats_enabled: 

310 rpc_latency_s = state.rpc_end_time - state.rpc_start_time 

311 rpc_latency_ms = rpc_latency_s * 1000 

312 plugin.record_rpc_latency( 

313 state.method, state.target, rpc_latency_ms, state.code 

314 ) 

315 

316 

317def create_server_call_tracer_factory_option(xds: bool) -> ChannelArgumentType: 

318 with get_plugin() as plugin: 

319 if plugin and plugin.stats_enabled: 

320 server_call_tracer_factory_address = ( 

321 _cygrpc.get_server_call_tracer_factory_address(plugin, xds) 

322 ) 

323 if server_call_tracer_factory_address: 

324 return ( 

325 ( 

326 "grpc.experimental.server_call_tracer_factory", 

327 ServerCallTracerFactory( 

328 server_call_tracer_factory_address 

329 ), 

330 ), 

331 ) 

332 return ()