|
2 | 2 |
|
3 | 3 | import fastapi |
4 | 4 | import litestar |
| 5 | +import pytest |
5 | 6 | from fastapi.testclient import TestClient as FastAPITestClient |
| 7 | +from faststream.redis import RedisBroker, TestRedisBroker |
| 8 | +from faststream.redis.prometheus import RedisPrometheusMiddleware |
6 | 9 | from litestar import status_codes |
7 | 10 | from litestar.middleware.base import DefineMiddleware |
8 | 11 | from litestar.testing import TestClient as LitestarTestClient |
9 | 12 |
|
10 | | -from microbootstrap import FastApiPrometheusConfig, LitestarPrometheusConfig |
| 13 | +from microbootstrap import FastApiPrometheusConfig, FastStreamSettings, LitestarPrometheusConfig |
11 | 14 | from microbootstrap.bootstrappers.fastapi import FastApiPrometheusInstrument |
| 15 | +from microbootstrap.bootstrappers.faststream import FastStreamBootstrapper |
12 | 16 | from microbootstrap.bootstrappers.litestar import LitestarPrometheusInstrument |
13 | | -from microbootstrap.instruments.prometheus_instrument import BasePrometheusConfig, PrometheusInstrument |
| 17 | +from microbootstrap.config.faststream import FastStreamConfig |
| 18 | +from microbootstrap.instruments.prometheus_instrument import ( |
| 19 | + BasePrometheusConfig, |
| 20 | + FastStreamPrometheusConfig, |
| 21 | + PrometheusInstrument, |
| 22 | +) |
| 23 | +from tests.utils import check_is_metrics_has_labels, create_test_redis_subscriber |
14 | 24 |
|
15 | 25 |
|
16 | 26 | def test_prometheus_is_ready(minimal_base_prometheus_config: BasePrometheusConfig) -> None: |
@@ -85,3 +95,58 @@ def test_fastapi_prometheus_bootstrap_working(minimal_fastapi_prometheus_config: |
85 | 95 | ) |
86 | 96 | assert response.status_code == status_codes.HTTP_200_OK |
87 | 97 | assert response.text |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.parametrize( |
| 101 | + ("custom_labels", "expected_label_keys"), |
| 102 | + [ |
| 103 | + ({"test_label": "test_value"}, {"test_label"}), |
| 104 | + ({}, {"method", "handler", "status"}), |
| 105 | + ], |
| 106 | +) |
| 107 | +def test_fastapi_prometheus_custom_labels( |
| 108 | + minimal_fastapi_prometheus_config: FastApiPrometheusConfig, |
| 109 | + custom_labels: dict[str, str], |
| 110 | + expected_label_keys: set[str], |
| 111 | +) -> None: |
| 112 | + minimal_fastapi_prometheus_config.prometheus_custom_labels = custom_labels |
| 113 | + prometheus_instrument: typing.Final = FastApiPrometheusInstrument(minimal_fastapi_prometheus_config) |
| 114 | + |
| 115 | + fastapi_application = fastapi.FastAPI() |
| 116 | + fastapi_application = prometheus_instrument.bootstrap_after(fastapi_application) |
| 117 | + |
| 118 | + response: typing.Final = FastAPITestClient(app=fastapi_application).get( |
| 119 | + minimal_fastapi_prometheus_config.prometheus_metrics_path |
| 120 | + ) |
| 121 | + |
| 122 | + assert response.status_code == status_codes.HTTP_200_OK |
| 123 | + assert check_is_metrics_has_labels(expected_label_keys) |
| 124 | + |
| 125 | + |
| 126 | +@pytest.mark.parametrize( |
| 127 | + ("custom_labels", "expected_label_keys"), |
| 128 | + [ |
| 129 | + ({"test_label": "test_value"}, {"test_label"}), |
| 130 | + ({}, {"app_name", "broker", "handler"}), |
| 131 | + ], |
| 132 | +) |
| 133 | +async def test_faststream_prometheus_custom_labels( |
| 134 | + minimal_faststream_prometheus_config: FastStreamPrometheusConfig, |
| 135 | + custom_labels: dict[str, str], |
| 136 | + expected_label_keys: set[str], |
| 137 | +) -> None: |
| 138 | + minimal_faststream_prometheus_config.prometheus_custom_labels = custom_labels |
| 139 | + minimal_faststream_prometheus_config.prometheus_middleware_cls = RedisPrometheusMiddleware # type: ignore[assignment] |
| 140 | + |
| 141 | + broker: typing.Final = RedisBroker() |
| 142 | + ( |
| 143 | + FastStreamBootstrapper(FastStreamSettings()) |
| 144 | + .configure_application(FastStreamConfig(broker=broker)) |
| 145 | + .configure_instrument(minimal_faststream_prometheus_config) |
| 146 | + .bootstrap() |
| 147 | + ) |
| 148 | + create_test_redis_subscriber(broker, topic="test-topic") |
| 149 | + |
| 150 | + async with TestRedisBroker(broker) as tb: |
| 151 | + await tb.publish({"foo": "bar"}, "test-topic") |
| 152 | + assert check_is_metrics_has_labels(expected_label_keys) |
0 commit comments