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

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

81 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 injected 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: Whether 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 save_trace_context( 

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

106 ) -> None: 

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

108 

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

110 called after the server finished sending response. 

111 

112 This method can be used to propagate census context. 

113 

114 Args: 

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

116 32-character hexadecimal encoded string, 

117 e.g. 26ed0036f2eff2b7317bccce3e28d01f 

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

119 string. e.g. 113ec879e62583bc 

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

121 """ 

122 raise NotImplementedError() 

123 

124 @abc.abstractmethod 

125 def create_server_call_tracer_factory( 

126 self, 

127 *, 

128 xds: bool = False, 

129 ) -> Optional[ServerCallTracerFactoryCapsule]: 

130 """Creates a ServerCallTracerFactoryCapsule. 

131 

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

133 ServerCallTracerFactory, which will be registered to gRPC core. 

134 

135 The ServerCallTracerFactory is an object which implements 

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

137 using `server_call_tracer_factory` as name. 

138 

139 Args: 

140 xds: Whether the server is xds server. 

141 Returns: 

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

143 plugin decides not to create ServerCallTracerFactory. 

144 """ 

145 raise NotImplementedError() 

146 

147 @abc.abstractmethod 

148 def record_rpc_latency( 

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

150 ) -> None: 

151 """Record the latency of the RPC. 

152 

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

154 called at the end of each RPC. 

155 

156 Args: 

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

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

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

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

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

162 final status for the RPC. 

163 """ 

164 raise NotImplementedError() 

165 

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

167 """Enable or disable tracing. 

168 

169 Args: 

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

171 """ 

172 self._tracing_enabled = enable 

173 

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

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

176 

177 Args: 

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

179 """ 

180 self._stats_enabled = enable 

181 

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

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

184 

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

186 with 'other' by default. 

187 

188 Args: 

189 method_name: The method name in bytes. 

190 """ 

191 raise NotImplementedError() 

192 

193 @property 

194 def tracing_enabled(self) -> bool: 

195 return self._tracing_enabled 

196 

197 @property 

198 def stats_enabled(self) -> bool: 

199 return self._stats_enabled 

200 

201 @property 

202 def observability_enabled(self) -> bool: 

203 return self.tracing_enabled or self.stats_enabled 

204 

205 

206@contextlib.contextmanager 

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

208 """Get the ObservabilityPlugin in _observability module. 

209 

210 Returns: 

211 The ObservabilityPlugin currently registered with the _observability 

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

213 """ 

214 with _plugin_lock: 

215 yield _OBSERVABILITY_PLUGIN 

216 

217 

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

219 """Save ObservabilityPlugin to _observability module. 

220 

221 Args: 

222 observability_plugin: The ObservabilityPlugin to save. 

223 

224 Raises: 

225 ValueError: If an ObservabilityPlugin was already registered at the 

226 time of calling this method. 

227 """ 

228 global _OBSERVABILITY_PLUGIN # pylint: disable=global-statement 

229 with _plugin_lock: 

230 if observability_plugin and _OBSERVABILITY_PLUGIN: 

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

232 _OBSERVABILITY_PLUGIN = observability_plugin 

233 

234 

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

236 """Initialize observability with provided ObservabilityPlugin. 

237 

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

239 channels/servers are built. 

240 

241 Args: 

242 observability_plugin: The ObservabilityPlugin to use. 

243 

244 Raises: 

245 ValueError: If an ObservabilityPlugin was already registered at the 

246 time of calling this method. 

247 """ 

248 set_plugin(observability_plugin) 

249 

250 

251def observability_deinit() -> None: 

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

253 ServerCallTracerFactory 

254 

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

256 it's possible to re-initialize again. 

257 """ 

258 set_plugin(None) 

259 _cygrpc.clear_server_call_tracer_factory() 

260 

261 

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

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

264 

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

266 

267 Args: 

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

269 RPC. 

270 """ 

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

272 for exclude_prefix in _SERVICES_TO_EXCLUDE: 

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

274 return 

275 with get_plugin() as plugin: 

276 if plugin and plugin.stats_enabled: 

277 rpc_latency_s = state.rpc_end_time - state.rpc_start_time 

278 rpc_latency_ms = rpc_latency_s * 1000 

279 plugin.record_rpc_latency( 

280 state.method, state.target, rpc_latency_ms, state.code 

281 ) 

282 

283 

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

285 with get_plugin() as plugin: 

286 if plugin and plugin.stats_enabled: 

287 server_call_tracer_factory_address = ( 

288 _cygrpc.get_server_call_tracer_factory_address(plugin, xds) 

289 ) 

290 if server_call_tracer_factory_address: 

291 return ( 

292 ( 

293 "grpc.experimental.server_call_tracer_factory", 

294 ServerCallTracerFactory( 

295 server_call_tracer_factory_address 

296 ), 

297 ), 

298 ) 

299 return ()