Context manager that records endpoint latency and increments request count. Yields a :class:`RequestMetricsContext` whose ``feature_count`` and ``feature_view_count`` attributes can be updated inside the block. The final values are used when recording the histogram and counter in ``
(
endpoint: str, feature_count: str = "", feature_view_count: str = ""
)
| 317 | |
| 318 | @contextmanager |
| 319 | def track_request_latency( |
| 320 | endpoint: str, feature_count: str = "", feature_view_count: str = "" |
| 321 | ): |
| 322 | """Context manager that records endpoint latency and increments request count. |
| 323 | |
| 324 | Yields a :class:`RequestMetricsContext` whose ``feature_count`` and |
| 325 | ``feature_view_count`` attributes can be updated inside the block. |
| 326 | The final values are used when recording the histogram and counter |
| 327 | in ``finally``, so labels are accurate even when they depend on |
| 328 | work done inside the block. |
| 329 | |
| 330 | Gated by the ``request`` category flag. |
| 331 | """ |
| 332 | ctx = RequestMetricsContext(feature_count, feature_view_count) |
| 333 | if not _config.request: |
| 334 | yield ctx |
| 335 | return |
| 336 | |
| 337 | start = time.monotonic() |
| 338 | status_label = "success" |
| 339 | try: |
| 340 | yield ctx |
| 341 | except Exception: |
| 342 | status_label = "error" |
| 343 | raise |
| 344 | finally: |
| 345 | elapsed = time.monotonic() - start |
| 346 | request_latency.labels( |
| 347 | endpoint=endpoint, |
| 348 | feature_count=ctx.feature_count, |
| 349 | feature_view_count=ctx.feature_view_count, |
| 350 | ).observe(elapsed) |
| 351 | request_count.labels(endpoint=endpoint, status=status_label).inc() |
| 352 | |
| 353 | |
| 354 | def track_online_features_entities(entity_count: int): |