Coverage for tests/prometheus/basic.py: 99%

83 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-17 21:20 +0000

1import asyncio 

2from typing import TYPE_CHECKING, Any, cast 

3 

4import pytest 

5from dirty_equals import IsList, IsPositiveFloat, IsStr 

6from prometheus_client import CollectorRegistry 

7 

8from faststream import Context 

9from faststream.exceptions import IgnoredException, RejectMessage 

10from faststream.message import AckStatus 

11from faststream.prometheus import MetricsSettingsProvider 

12from faststream.prometheus.consts import ( 

13 PROCESSING_STATUS_BY_ACK_STATUS, 

14 PROCESSING_STATUS_BY_HANDLER_EXCEPTION_MAP, 

15) 

16from faststream.prometheus.middleware import PrometheusMiddleware 

17from faststream.prometheus.types import ProcessingStatus, PublishingStatus 

18from tests.brokers.base.basic import BaseTestcaseConfig 

19from tests.prometheus.utils import ( 

20 get_published_messages_duration_seconds_metric, 

21 get_published_messages_exceptions_metric, 

22 get_published_messages_metric, 

23 get_received_messages_in_process_metric, 

24 get_received_messages_metric, 

25 get_received_messages_size_bytes_metric, 

26 get_received_processed_messages_duration_seconds_metric, 

27 get_received_processed_messages_exceptions_metric, 

28 get_received_processed_messages_metric, 

29) 

30 

31if TYPE_CHECKING: 

32 from collections.abc import Callable 

33 

34 

35class PrometheusTestcaseConfig(BaseTestcaseConfig[Any]): 

36 def get_middleware(self, **kwargs: Any) -> PrometheusMiddleware[Any, Any]: 

37 raise NotImplementedError 

38 

39 def get_settings_provider(self) -> MetricsSettingsProvider[Any, Any]: 

40 raise NotImplementedError 

41 

42 

43@pytest.mark.asyncio() 

44class LocalPrometheusTestcase(PrometheusTestcaseConfig): 

45 @pytest.mark.parametrize( 

46 ( 

47 "status", 

48 "exception_class", 

49 ), 

50 ( 

51 pytest.param( 

52 AckStatus.ACKED, 

53 RejectMessage, 

54 id="acked status with reject message exception", 

55 ), 

56 pytest.param( 

57 AckStatus.ACKED, 

58 Exception, 

59 id="acked status with not handler exception", 

60 ), 

61 pytest.param( 

62 AckStatus.ACKED, 

63 None, 

64 id="acked status without exception", 

65 ), 

66 pytest.param( 

67 AckStatus.NACKED, 

68 None, 

69 id="nacked status without exception", 

70 ), 

71 pytest.param( 

72 AckStatus.REJECTED, 

73 None, 

74 id="rejected status without exception", 

75 ), 

76 pytest.param( 

77 AckStatus.ACKED, 

78 IgnoredException, 

79 id="acked status with ignore exception", 

80 ), 

81 ), 

82 ) 

83 async def test_metrics( 

84 self, 

85 queue: str, 

86 status: AckStatus, 

87 exception_class: type[Exception] | None, 

88 event: asyncio.Event, 

89 ) -> None: 

90 registry = CollectorRegistry() 

91 custom_labels: dict[str, str | Callable[[Any], str]] = { 

92 "static": "pupupu", 

93 "dynamic": lambda x: "papapa", 

94 } 

95 middleware = self.get_middleware(registry=registry, custom_labels=custom_labels) 

96 

97 broker = self.get_broker(apply_types=True, middlewares=(middleware,)) 

98 

99 args, kwargs = self.get_subscriber_params(queue) 

100 

101 message = None 

102 

103 @broker.subscriber(*args, **kwargs) 

104 async def handler(m: Any = Context("message")) -> None: 

105 event.set() 

106 

107 nonlocal message 

108 message = m 

109 

110 if exception_class: 

111 raise exception_class 

112 

113 if status == AckStatus.ACKED: 

114 await message.ack() 

115 elif status == AckStatus.NACKED: 

116 await message.nack() 

117 elif status == AckStatus.REJECTED: 117 ↛ exitline 117 didn't return from function 'handler' because the condition on line 117 was always true

118 await message.reject() 

119 

120 async with broker: 

121 await broker.start() 

122 tasks = ( 

123 asyncio.create_task(broker.publish("hello", queue)), 

124 asyncio.create_task(event.wait()), 

125 ) 

126 await asyncio.wait(tasks, timeout=self.timeout) 

127 

128 assert event.is_set() 

129 self.assert_metrics( 

130 registry=registry, 

131 message=message, 

132 exception_class=exception_class, 

133 custom_labels={ 

134 "static": "pupupu", 

135 "dynamic": "papapa", 

136 }, 

137 ) 

138 

139 def assert_metrics( 

140 self, 

141 *, 

142 registry: CollectorRegistry, 

143 message: Any, 

144 exception_class: type[Exception] | None, 

145 custom_labels: dict[str, str], 

146 ) -> None: 

147 settings_provider = self.get_settings_provider() 

148 consume_attrs = settings_provider.get_consume_attrs_from_message(message) 

149 

150 received_messages_metric = get_received_messages_metric( 

151 metrics_prefix="faststream", 

152 app_name="faststream", 

153 broker=settings_provider.messaging_system, 

154 queue=consume_attrs["destination_name"], 

155 messages_amount=consume_attrs["messages_count"], 

156 custom_labels=custom_labels, 

157 ) 

158 

159 received_messages_size_bytes_metric = get_received_messages_size_bytes_metric( 

160 metrics_prefix="faststream", 

161 app_name="faststream", 

162 broker=settings_provider.messaging_system, 

163 queue=consume_attrs["destination_name"], 

164 buckets=( 

165 2.0**4, 

166 2.0**6, 

167 2.0**8, 

168 2.0**10, 

169 2.0**12, 

170 2.0**14, 

171 2.0**16, 

172 2.0**18, 

173 2.0**20, 

174 2.0**22, 

175 2.0**24, 

176 float("inf"), 

177 ), 

178 size=consume_attrs["message_size"], 

179 messages_amount=1, 

180 custom_labels=custom_labels, 

181 ) 

182 

183 received_messages_in_process_metric = get_received_messages_in_process_metric( 

184 metrics_prefix="faststream", 

185 app_name="faststream", 

186 broker=settings_provider.messaging_system, 

187 queue=consume_attrs["destination_name"], 

188 messages_amount=0, 

189 custom_labels=custom_labels, 

190 ) 

191 

192 received_processed_messages_duration_seconds_metric = ( 

193 get_received_processed_messages_duration_seconds_metric( 

194 metrics_prefix="faststream", 

195 app_name="faststream", 

196 broker=settings_provider.messaging_system, 

197 queue=consume_attrs["destination_name"], 

198 duration=cast("float", IsPositiveFloat), 

199 custom_labels=custom_labels, 

200 ) 

201 ) 

202 

203 status = ProcessingStatus.acked 

204 

205 if exception_class: 

206 status = ( 

207 PROCESSING_STATUS_BY_HANDLER_EXCEPTION_MAP.get(exception_class) 

208 or ProcessingStatus.error 

209 ) 

210 elif message.committed: 

211 status = PROCESSING_STATUS_BY_ACK_STATUS[message.committed] 

212 

213 received_processed_messages_metric = get_received_processed_messages_metric( 

214 metrics_prefix="faststream", 

215 app_name="faststream", 

216 broker=settings_provider.messaging_system, 

217 queue=consume_attrs["destination_name"], 

218 messages_amount=consume_attrs["messages_count"], 

219 status=status, 

220 custom_labels=custom_labels, 

221 ) 

222 

223 exception_type: str | None = None 

224 

225 if exception_class and not issubclass(exception_class, IgnoredException): 

226 exception_type = exception_class.__name__ 

227 

228 received_processed_messages_exceptions_metric = ( 

229 get_received_processed_messages_exceptions_metric( 

230 metrics_prefix="faststream", 

231 app_name="faststream", 

232 broker=settings_provider.messaging_system, 

233 queue=consume_attrs["destination_name"], 

234 exception_type=exception_type, 

235 exceptions_amount=consume_attrs["messages_count"], 

236 custom_labels=custom_labels, 

237 ) 

238 ) 

239 

240 published_messages_metric = get_published_messages_metric( 

241 metrics_prefix="faststream", 

242 app_name="faststream", 

243 broker=settings_provider.messaging_system, 

244 queue=cast("str", IsStr), 

245 status=PublishingStatus.success, 

246 messages_amount=consume_attrs["messages_count"], 

247 custom_labels=custom_labels, 

248 ) 

249 

250 published_messages_duration_seconds_metric = ( 

251 get_published_messages_duration_seconds_metric( 

252 metrics_prefix="faststream", 

253 app_name="faststream", 

254 broker=settings_provider.messaging_system, 

255 queue=cast("str", IsStr), 

256 duration=cast("float", IsPositiveFloat), 

257 custom_labels=custom_labels, 

258 ) 

259 ) 

260 

261 published_messages_exceptions_metric = get_published_messages_exceptions_metric( 

262 metrics_prefix="faststream", 

263 app_name="faststream", 

264 broker=settings_provider.messaging_system, 

265 queue=cast("str", IsStr), 

266 exception_type=None, 

267 custom_labels=custom_labels, 

268 ) 

269 

270 expected_metrics = IsList( 

271 received_messages_metric, 

272 received_messages_size_bytes_metric, 

273 received_messages_in_process_metric, 

274 received_processed_messages_metric, 

275 received_processed_messages_duration_seconds_metric, 

276 received_processed_messages_exceptions_metric, 

277 published_messages_metric, 

278 published_messages_duration_seconds_metric, 

279 published_messages_exceptions_metric, 

280 check_order=False, 

281 ) 

282 real_metrics = list(registry.collect()) 

283 

284 assert real_metrics == expected_metrics 

285 

286 

287class LocalRPCPrometheusTestcase(LocalPrometheusTestcase): 

288 @pytest.mark.asyncio() 

289 async def test_rpc_request(self, queue: str, event: asyncio.Event) -> None: 

290 registry = CollectorRegistry() 

291 

292 middleware = self.get_middleware(registry=registry) 

293 

294 broker = self.get_broker(apply_types=True, middlewares=(middleware,)) 

295 

296 message = None 

297 

298 @broker.subscriber(queue) 

299 async def handle(m: Any = Context("message")) -> Any: 

300 event.set() 

301 

302 nonlocal message 

303 message = m 

304 

305 return "" 

306 

307 async with self.patch_broker(broker) as br: 

308 await br.start() 

309 

310 await asyncio.wait_for( 

311 br.request("", queue), 

312 timeout=3, 

313 ) 

314 

315 assert event.is_set() 

316 

317 self.assert_metrics( 

318 registry=registry, 

319 message=message, 

320 exception_class=None, 

321 custom_labels={}, 

322 ) 

323 

324 

325class LocalMetricsSettingsProviderTestcase(PrometheusTestcaseConfig): 

326 messaging_system: str 

327 

328 def test_messaging_system(self) -> None: 

329 provider = self.get_settings_provider() 

330 assert provider.messaging_system == self.messaging_system 

331 

332 def test_one_registry_for_some_middlewares(self) -> None: 

333 registry = CollectorRegistry() 

334 

335 middleware_1 = self.get_middleware(registry=registry) 

336 middleware_2 = self.get_middleware(registry=registry) 

337 self.get_broker(middlewares=(middleware_1,)) 

338 self.get_broker(middlewares=(middleware_2,)) 

339 

340 assert ( 

341 middleware_1._metrics_container.received_messages_total 

342 is middleware_2._metrics_container.received_messages_total 

343 )