-
Notifications
You must be signed in to change notification settings - Fork 831
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): simple rate limiting with slowapi (#2903)
- Loading branch information
Showing
9 changed files
with
145 additions
and
37 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
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,8 @@ | ||
# https://slowapi.readthedocs.io/en/latest/#fastapi | ||
from slowapi import Limiter | ||
from slowapi.util import get_remote_address | ||
|
||
from keep.api.core.config import config | ||
|
||
limiter_enabled = config("KEEP_USE_LIMITER", default="false", cast=bool) | ||
limiter = Limiter(key_func=get_remote_address, enabled=limiter_enabled) |
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,51 +1,39 @@ | ||
import os | ||
|
||
from prometheus_client import CollectorRegistry, Counter, Gauge, Summary, multiprocess | ||
from prometheus_client import Counter, Gauge, Summary | ||
|
||
PROMETHEUS_MULTIPROC_DIR = os.environ.get("PROMETHEUS_MULTIPROC_DIR", "/tmp/prometheus") | ||
os.makedirs(PROMETHEUS_MULTIPROC_DIR, exist_ok=True) | ||
|
||
METRIC_PREFIX = "keep_" | ||
|
||
# Create a single registry for all metrics | ||
registry = CollectorRegistry() | ||
multiprocess.MultiProcessCollector(registry, path=PROMETHEUS_MULTIPROC_DIR) | ||
|
||
# Process event metrics | ||
events_in_counter = Counter( | ||
f"{METRIC_PREFIX}events_in_total", | ||
"Total number of events received", | ||
registry=registry, | ||
) | ||
events_out_counter = Counter( | ||
f"{METRIC_PREFIX}events_processed_total", | ||
"Total number of events processed", | ||
registry=registry, | ||
) | ||
events_error_counter = Counter( | ||
f"{METRIC_PREFIX}events_error_total", | ||
"Total number of events with error", | ||
registry=registry, | ||
) | ||
processing_time_summary = Summary( | ||
f"{METRIC_PREFIX}processing_time_seconds", | ||
"Average time spent processing events", | ||
registry=registry, | ||
) | ||
|
||
# Running tasks metrics | ||
running_tasks_gauge = Gauge( | ||
f"{METRIC_PREFIX}running_tasks_current", | ||
"Current number of running tasks", | ||
registry=registry, | ||
multiprocess_mode="livesum", | ||
) | ||
|
||
# Per-process running tasks metrics | ||
running_tasks_by_process_gauge = Gauge( | ||
f"{METRIC_PREFIX}running_tasks_by_process", | ||
"Current number of running tasks per process", | ||
labelnames=["pid"], | ||
registry=registry, | ||
multiprocess_mode="livesum", | ||
) |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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