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