-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1840ac8
commit 119dc8a
Showing
10 changed files
with
199 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from typing import TYPE_CHECKING | ||
|
||
from faststream.prometheus.middleware import BasePrometheusMiddleware | ||
from faststream.redis.prometheus.provider import attributes_provider_factory | ||
|
||
if TYPE_CHECKING: | ||
from prometheus_client import CollectorRegistry | ||
|
||
|
||
class RedisPrometheusMiddleware(BasePrometheusMiddleware): | ||
def __init__( | ||
self, | ||
*, | ||
registry: "CollectorRegistry", | ||
): | ||
super().__init__( | ||
settings_provider_factory=attributes_provider_factory, | ||
registry=registry, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from typing import TYPE_CHECKING, Optional, Union | ||
|
||
from faststream.prometheus.provider import ( | ||
ConsumeAttrs, | ||
MetricsSettingsProvider, | ||
) | ||
|
||
if TYPE_CHECKING: | ||
from faststream.broker.message import StreamMessage | ||
from faststream.types import AnyDict | ||
|
||
|
||
class BaseRedisMetricsSettingsProvider(MetricsSettingsProvider["AnyDict"]): | ||
def __init__(self): | ||
self.messaging_system = "redis" | ||
|
||
def get_publish_destination_name_from_kwargs( | ||
self, | ||
kwargs: "AnyDict", | ||
) -> str: | ||
return self._get_destination(kwargs) | ||
|
||
@staticmethod | ||
def _get_destination(kwargs: "AnyDict") -> str: | ||
return kwargs.get("channel") or kwargs.get("list") or kwargs.get("stream") or "" | ||
|
||
|
||
class RedisMetricsSettingsProvider(BaseRedisMetricsSettingsProvider): | ||
def get_consume_attrs_from_message( | ||
self, | ||
msg: "StreamMessage[AnyDict]", | ||
) -> ConsumeAttrs: | ||
return { | ||
"destination_name": self._get_destination(msg.raw_message), | ||
"message_size": len(msg.body), | ||
"messages_count": 1, | ||
} | ||
|
||
|
||
class BatchRedisMetricsSettingsProvider(BaseRedisMetricsSettingsProvider): | ||
def get_consume_attrs_from_message( | ||
self, | ||
msg: "StreamMessage[AnyDict]", | ||
) -> ConsumeAttrs: | ||
return { | ||
"destination_name": self._get_destination(msg.raw_message), | ||
"message_size": len(msg.body), | ||
"messages_count": len(msg._decoded_body), | ||
} | ||
|
||
|
||
def attributes_provider_factory( | ||
msg: Optional["AnyDict"], | ||
) -> Union[ | ||
RedisMetricsSettingsProvider, | ||
BatchRedisMetricsSettingsProvider, | ||
]: | ||
if msg is not None and msg.get("type", "").startswith("b"): | ||
return BatchRedisMetricsSettingsProvider() | ||
else: | ||
return RedisMetricsSettingsProvider() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import pytest | ||
|
||
pytest.importorskip("aio_pika") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import asyncio | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
from prometheus_client import CollectorRegistry | ||
|
||
from faststream.redis import ListSub, RedisBroker, RedisMessage | ||
from faststream.redis.prometheus.middleware import RedisPrometheusMiddleware | ||
from tests.brokers.redis.test_consume import TestConsume | ||
from tests.brokers.redis.test_publish import TestPublish | ||
from tests.prometheus.basic import LocalPrometheusTestcase | ||
|
||
|
||
@pytest.mark.redis | ||
class TestPrometheus(LocalPrometheusTestcase): | ||
broker_class = RedisBroker | ||
middleware_class = RedisPrometheusMiddleware | ||
message_class = RedisMessage | ||
|
||
async def test_metrics_batch( | ||
self, | ||
event: asyncio.Event, | ||
queue: str, | ||
): | ||
middleware = self.middleware_class(registry=CollectorRegistry()) | ||
metrics_mock = Mock() | ||
middleware._metrics = metrics_mock | ||
|
||
broker = self.broker_class(middlewares=(middleware,)) | ||
|
||
args, kwargs = self.get_subscriber_params(list=ListSub(queue, batch=True)) | ||
|
||
message_class = self.message_class | ||
message = None | ||
|
||
@broker.subscriber(*args, **kwargs) | ||
async def handler(m: message_class): | ||
event.set() | ||
|
||
nonlocal message | ||
message = m | ||
|
||
async with broker: | ||
await broker.start() | ||
tasks = ( | ||
asyncio.create_task(broker.publish_batch("hello", "world", list=queue)), | ||
asyncio.create_task(event.wait()), | ||
) | ||
await asyncio.wait(tasks, timeout=self.timeout) | ||
|
||
assert event.is_set() | ||
self.assert_consume_metrics( | ||
metrics=metrics_mock, message=message, exception_class=None | ||
) | ||
self.assert_publish_metrics(metrics=metrics_mock) | ||
|
||
|
||
@pytest.mark.redis | ||
class TestPublishWithPrometheus(TestPublish): | ||
def get_broker(self, apply_types: bool = False): | ||
return RedisBroker( | ||
middlewares=(RedisPrometheusMiddleware(registry=CollectorRegistry()),), | ||
apply_types=apply_types, | ||
) | ||
|
||
|
||
@pytest.mark.redis | ||
class TestConsumeWithTelemetry(TestConsume): | ||
def get_broker(self, apply_types: bool = False): | ||
return RedisBroker( | ||
middlewares=(RedisPrometheusMiddleware(registry=CollectorRegistry()),), | ||
apply_types=apply_types, | ||
) |