-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix/feat: register metrics only once andd add hit-miss
- Loading branch information
Showing
3 changed files
with
34 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,38 @@ | ||
from typing import Optional | ||
|
||
from prometheus_client import Histogram | ||
from prometheus_client import Counter, Histogram | ||
|
||
from cashews import cache | ||
from cashews._typing import Middleware | ||
from cashews.backends.interface import Backend | ||
from cashews.commands import Command | ||
|
||
_DEFAULT_METRIC = Histogram( | ||
"cashews_operations_latency_seconds", | ||
"Latency of different operations with a cache", | ||
labelnames=["operation", "backend_class", "tag"], | ||
) | ||
_HIT_MISS = Counter( | ||
"cashews_get_operation", | ||
"Count of hits or missed GET operations", | ||
labelnames=["result", "backend_class", "tag"], | ||
) | ||
|
||
def create_metrics_middleware(latency_metric: Optional[Histogram] = None, with_tag: bool = False) -> Middleware: | ||
_DEFAULT_METRIC = Histogram( | ||
"cashews_operations_latency_seconds", | ||
"Latency of different operations with a cache", | ||
labelnames=["operation", "backend_class"] if not with_tag else ["operation", "backend_class", "tag"], | ||
) | ||
_latency_metric = latency_metric or _DEFAULT_METRIC | ||
|
||
def create_metrics_middleware(with_tag: bool = False) -> Middleware: | ||
async def metrics_middleware(call, cmd: Command, backend: Backend, *args, **kwargs): | ||
with _latency_metric.time() as metric: | ||
metric.labels(operation=cmd.value, backend_class=backend.__class__.__name__) | ||
with _DEFAULT_METRIC.time() as metric: | ||
tag = "" | ||
|
||
if with_tag and "key" in kwargs: | ||
tags = cache.get_key_tags(kwargs["key"]) | ||
tag = next((t for t in tags), None) | ||
if tag: | ||
metric.labels(tag=tag) | ||
return await call(*args, **kwargs) | ||
tag = next((t for t in tags), "") | ||
metric.labels(operation=cmd.value, backend_class=backend.__class__.__name__, tag=tag) | ||
result = await call(*args, **kwargs) | ||
if cmd is Command.GET: | ||
if result is not kwargs["default"]: | ||
op_result = "hit" | ||
else: | ||
op_result = "miss" | ||
_HIT_MISS.labels(result=op_result, backend_class=backend.__class__.__name__, tag=tag).inc() | ||
return result | ||
|
||
return metrics_middleware |
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