Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/redis/observability/providers.py: 35%

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

96 statements  

1""" 

2OpenTelemetry provider management for redis-py. 

3 

4This module handles initialization and lifecycle management of OTel SDK components 

5including MeterProvider, TracerProvider (future), and LoggerProvider (future). 

6 

7Uses a singleton pattern - initialize once globally, all Redis clients use it automatically. 

8 

9Redis-py uses the global MeterProvider set by your application. Set it up before 

10initializing observability: 

11 

12 from opentelemetry import metrics 

13 from opentelemetry.sdk.metrics import MeterProvider 

14 

15 provider = MeterProvider(...) 

16 metrics.set_meter_provider(provider) 

17 

18 # Then initialize redis-py observability 

19 otel = get_observability_instance() 

20 otel.init(OTelConfig(enable_metrics=True)) 

21""" 

22 

23import logging 

24from typing import Callable, List, Optional 

25 

26from redis.observability.config import OTelConfig 

27 

28logger = logging.getLogger(__name__) 

29 

30# Optional imports - OTel SDK may not be installed 

31try: 

32 from opentelemetry.sdk.metrics import MeterProvider 

33 

34 OTEL_AVAILABLE = True 

35except ImportError: 

36 OTEL_AVAILABLE = False 

37 MeterProvider = None 

38 

39# Global singleton instance 

40_global_provider_manager: Optional["OTelProviderManager"] = None 

41 

42 

43class OTelProviderManager: 

44 """ 

45 Manages OpenTelemetry SDK providers and their lifecycle. 

46 

47 This class handles: 

48 - Getting the global MeterProvider set by the application 

49 - Configuring histogram bucket boundaries via Views 

50 - Graceful shutdown 

51 

52 Args: 

53 config: OTel configuration object 

54 """ 

55 

56 def __init__(self, config: OTelConfig): 

57 self.config = config 

58 self._meter_provider: Optional[MeterProvider] = None 

59 

60 def get_meter_provider(self) -> Optional[MeterProvider]: 

61 """ 

62 Get the global MeterProvider set by the application. 

63 

64 Returns: 

65 MeterProvider instance or None if metrics are disabled 

66 

67 Raises: 

68 ImportError: If OpenTelemetry is not installed 

69 RuntimeError: If metrics are enabled but no global MeterProvider is set 

70 """ 

71 if not self.config.is_enabled(): 

72 return None 

73 

74 # Lazy import - only import OTel when metrics are enabled 

75 try: 

76 from opentelemetry import metrics 

77 from opentelemetry.metrics import NoOpMeterProvider 

78 except ImportError: 

79 raise ImportError( 

80 "OpenTelemetry is not installed. Install it with:\n" 

81 " pip install opentelemetry-api opentelemetry-sdk opentelemetry-exporter-otlp-proto-http" 

82 ) 

83 

84 # Get the global MeterProvider 

85 if self._meter_provider is None: 

86 self._meter_provider = metrics.get_meter_provider() 

87 

88 # Check if it's a real provider (not NoOp) 

89 if isinstance(self._meter_provider, NoOpMeterProvider): 

90 raise RuntimeError( 

91 "Metrics are enabled but no global MeterProvider is configured.\n" 

92 "\n" 

93 "Set up OpenTelemetry before initializing redis-py observability:\n" 

94 "\n" 

95 " from opentelemetry import metrics\n" 

96 " from opentelemetry.sdk.metrics import MeterProvider\n" 

97 " from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader\n" 

98 " from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter\n" 

99 "\n" 

100 " # Create exporter\n" 

101 " exporter = OTLPMetricExporter(\n" 

102 " endpoint='http://localhost:4318/v1/metrics'\n" 

103 " )\n" 

104 "\n" 

105 " # Create reader\n" 

106 " reader = PeriodicExportingMetricReader(\n" 

107 " exporter=exporter,\n" 

108 " export_interval_millis=10000\n" 

109 " )\n" 

110 "\n" 

111 " # Create and set global provider\n" 

112 " provider = MeterProvider(metric_readers=[reader])\n" 

113 " metrics.set_meter_provider(provider)\n" 

114 "\n" 

115 " # Now initialize redis-py observability\n" 

116 " from redis.observability import get_observability_instance, OTelConfig\n" 

117 " otel = get_observability_instance()\n" 

118 " otel.init(OTelConfig(enable_metrics=True))\n" 

119 ) 

120 

121 logger.info("Using global MeterProvider from application") 

122 

123 return self._meter_provider 

124 

125 def shutdown(self, timeout_millis: int = 30000) -> bool: 

126 """ 

127 Shutdown observability and flush any pending metrics. 

128 

129 Note: We don't shutdown the global MeterProvider since it's owned by the application. 

130 We only force flush pending metrics. 

131 

132 Args: 

133 timeout_millis: Maximum time to wait for flush 

134 

135 Returns: 

136 True if flush was successful, False otherwise 

137 """ 

138 logger.debug( 

139 "Flushing metrics before shutdown (not shutting down global MeterProvider)" 

140 ) 

141 return self.force_flush(timeout_millis=timeout_millis) 

142 

143 def force_flush(self, timeout_millis: int = 30000) -> bool: 

144 """ 

145 Force flush any pending metrics from the global MeterProvider. 

146 

147 Args: 

148 timeout_millis: Maximum time to wait for flush 

149 

150 Returns: 

151 True if flush was successful, False otherwise 

152 """ 

153 if self._meter_provider is None: 

154 return True 

155 

156 # NoOpMeterProvider doesn't have force_flush method 

157 if not hasattr(self._meter_provider, "force_flush"): 

158 logger.debug("MeterProvider does not support force_flush, skipping") 

159 return True 

160 

161 try: 

162 logger.debug("Force flushing metrics from global MeterProvider") 

163 self._meter_provider.force_flush(timeout_millis=timeout_millis) 

164 return True 

165 except Exception as e: 

166 logger.error(f"Error flushing metrics: {e}") 

167 return False 

168 

169 def __enter__(self): 

170 """Context manager entry.""" 

171 return self 

172 

173 def __exit__(self, _exc_type, _exc_val, _exc_tb): 

174 """Context manager exit - shutdown provider.""" 

175 self.shutdown() 

176 

177 def __repr__(self) -> str: 

178 return f"OTelProviderManager(config={self.config})" 

179 

180 

181# Singleton instance class 

182 

183 

184class ObservabilityInstance: 

185 """ 

186 Singleton instance for managing OpenTelemetry observability. 

187 

188 This class follows the singleton pattern similar to Glide's GetOtelInstance(). 

189 Use GetObservabilityInstance() to get the singleton instance, then call init() 

190 to initialize observability. 

191 

192 Example: 

193 >>> from redis.observability.config import OTelConfig 

194 >>> 

195 >>> # Get singleton instance 

196 >>> otel = get_observability_instance() 

197 >>> 

198 >>> # Initialize once at app startup 

199 >>> otel.init(OTelConfig()) 

200 >>> 

201 >>> # All Redis clients now automatically collect metrics 

202 >>> import redis 

203 >>> r = redis.Redis(host='localhost', port=6379) 

204 >>> r.set('key', 'value') # Metrics collected automatically 

205 """ 

206 

207 def __init__(self): 

208 self._provider_manager: Optional[OTelProviderManager] = None 

209 

210 def init(self, config: OTelConfig) -> "ObservabilityInstance": 

211 """ 

212 Initialize OpenTelemetry observability globally for all Redis clients. 

213 

214 This should be called once at application startup. After initialization, 

215 all Redis clients will automatically collect and export metrics without 

216 needing any additional configuration. 

217 

218 Safe to call multiple times - will shutdown previous instance before 

219 initializing a new one. 

220 

221 Args: 

222 config: OTel configuration object 

223 

224 Returns: 

225 Self for method chaining 

226 

227 Example: 

228 >>> otel = get_observability_instance() 

229 >>> otel.init(OTelConfig()) 

230 """ 

231 if self._provider_manager is not None: 

232 logger.warning( 

233 "Observability already initialized. Shutting down previous instance." 

234 ) 

235 self._provider_manager.shutdown() 

236 

237 self._provider_manager = OTelProviderManager(config) 

238 

239 # Both recorders cache the resolved collector - including the answer 

240 # "observability is disabled" - so that disabled recording costs two 

241 # global reads on the command hot path. Initializing (or 

242 # re-initializing) observability must drop those caches: a client that 

243 # recorded before init() would otherwise stay silent, and a re-init 

244 # with a different config would keep the previous collector. 

245 _reset_recorder_collectors() 

246 

247 logger.info("Observability initialized") 

248 

249 return self 

250 

251 def is_enabled(self) -> bool: 

252 """ 

253 Check if observability is enabled. 

254 

255 Returns: 

256 True if observability is initialized and metrics are enabled 

257 

258 Example: 

259 >>> otel = get_observability_instance() 

260 >>> if otel.is_enabled(): 

261 ... print("Metrics are being collected") 

262 """ 

263 return ( 

264 self._provider_manager is not None 

265 and self._provider_manager.config.is_enabled() 

266 ) 

267 

268 def get_provider_manager(self) -> Optional[OTelProviderManager]: 

269 """ 

270 Get the provider manager instance. 

271 

272 Returns: 

273 The provider manager, or None if not initialized 

274 

275 Example: 

276 >>> otel = get_observability_instance() 

277 >>> manager = otel.get_provider_manager() 

278 >>> if manager is not None: 

279 ... print(f"Observability enabled: {manager.config.is_enabled()}") 

280 """ 

281 return self._provider_manager 

282 

283 def shutdown(self, timeout_millis: int = 30000) -> bool: 

284 """ 

285 Shutdown observability and flush any pending metrics. 

286 

287 This should be called at application shutdown to ensure all metrics 

288 are exported before the application exits. 

289 

290 Args: 

291 timeout_millis: Maximum time to wait for shutdown 

292 

293 Returns: 

294 True if shutdown was successful 

295 

296 Example: 

297 >>> otel = get_observability_instance() 

298 >>> # At application shutdown 

299 >>> otel.shutdown() 

300 """ 

301 if self._provider_manager is None: 

302 logger.debug("Observability not initialized, nothing to shutdown") 

303 return True 

304 

305 success = self._provider_manager.shutdown(timeout_millis) 

306 self._provider_manager = None 

307 logger.info("Observability shutdown") 

308 

309 return success 

310 

311 def force_flush(self, timeout_millis: int = 30000) -> bool: 

312 """ 

313 Force flush all pending metrics immediately. 

314 

315 Useful for testing or when you want to ensure metrics are exported 

316 before a specific point in your application. 

317 

318 Args: 

319 timeout_millis: Maximum time to wait for flush 

320 

321 Returns: 

322 True if flush was successful 

323 

324 Example: 

325 >>> otel = get_observability_instance() 

326 >>> # Execute some Redis commands 

327 >>> r.set('key', 'value') 

328 >>> # Force flush metrics immediately 

329 >>> otel.force_flush() 

330 """ 

331 if self._provider_manager is None: 

332 logger.debug("Observability not initialized, nothing to flush") 

333 return True 

334 

335 return self._provider_manager.force_flush(timeout_millis) 

336 

337 

338# Global singleton instance 

339_observability_instance: Optional[ObservabilityInstance] = None 

340 

341# Cache-invalidation hooks registered by the recorders themselves. 

342# 

343# Both recorders cache the resolved collector - including the answer 

344# "observability is disabled" - so that disabled recording stays cheap on the 

345# command hot path. Initializing (or re-initializing) observability has to drop 

346# those caches: a client that recorded before init() would otherwise stay 

347# silent, and a re-init with a different config would keep the previous 

348# collector. 

349# 

350# The recorders import this module for ``get_observability_instance``, so they 

351# register their ``reset_collector`` here at import time instead of this module 

352# importing them. That keeps every import at module scope: a sync-only 

353# application must not pull in redis.asyncio, and there is no cycle because the 

354# dependency only ever runs in this direction. 

355_reset_hooks: List[Callable[[], None]] = [] 

356 

357 

358def _register_reset_hook(hook: Callable[[], None]) -> None: 

359 """Register a recorder's collector-cache invalidation hook. 

360 

361 Called at import time by each recorder module. Builtin functions compare by 

362 identity, so registering the same hook twice is a no-op. 

363 """ 

364 if hook not in _reset_hooks: 

365 _reset_hooks.append(hook) 

366 

367 

368def _reset_recorder_collectors() -> None: 

369 """Drop the collector caches of every recorder that has been imported. 

370 

371 A recorder registers its hook when its module is imported, and a recorder 

372 only has a cache to drop once it has been imported, so the registered hooks 

373 are exactly the caches that can be stale. 

374 """ 

375 for hook in list(_reset_hooks): 

376 hook() 

377 

378 

379def get_observability_instance() -> ObservabilityInstance: 

380 """ 

381 Get the global observability singleton instance. 

382 

383 This is the Pythonic way to get the singleton instance. 

384 

385 Returns: 

386 The global ObservabilityInstance singleton 

387 

388 Example: 

389 >>> 

390 >>> otel = get_observability_instance() 

391 >>> otel.init(OTelConfig()) 

392 """ 

393 global _observability_instance 

394 

395 if _observability_instance is None: 

396 _observability_instance = ObservabilityInstance() 

397 

398 return _observability_instance 

399 

400 

401def reset_observability_instance() -> None: 

402 """ 

403 Reset the global observability singleton instance. 

404 

405 This is primarily used for testing and benchmarking to ensure 

406 a clean state between test runs. 

407 

408 Warning: 

409 This will shutdown any active provider manager and reset 

410 the global state. Use with caution in production code. 

411 """ 

412 global _observability_instance 

413 

414 if _observability_instance is not None: 

415 _observability_instance.shutdown() 

416 _observability_instance = None