-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #540 from Bot-detector/develop
Release
- Loading branch information
Showing
19 changed files
with
1,701 additions
and
167 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,9 +1,12 @@ | ||
repos: | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v2.3.0 | ||
- repo: https://github.com/astral-sh/ruff-pre-commit | ||
# Ruff version. | ||
rev: v0.7.4 | ||
hooks: | ||
- id: check-yaml | ||
- repo: https://github.com/psf/black | ||
rev: 22.10.0 | ||
hooks: | ||
- id: black | ||
# Run the linter. | ||
- id: ruff | ||
types_or: [python, pyi] | ||
args: [--fix] | ||
# Run the formatter. | ||
- id: ruff-format | ||
types_or: [python, pyi] |
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.12 |
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,24 @@ | ||
[project] | ||
name = "core-api" | ||
version = "0.1.0" | ||
description = "Add your description here" | ||
readme = "README.md" | ||
requires-python = ">=3.10" | ||
dependencies = [ | ||
"aiohttp>=3.9.5", | ||
"asyncmy==0.2.8", | ||
"fastapi[standard]>=0.115.5", | ||
"pandas>=2.0.3", | ||
"prometheus-client>=0.21.0", | ||
"python-dotenv==1.0.0", | ||
"sqlalchemy==2.0.19", | ||
"starlette-prometheus>=0.9.0", | ||
] | ||
|
||
[dependency-groups] | ||
dev = [ | ||
"httpx>=0.28.0", | ||
"pytest-asyncio>=0.24.0", | ||
"pytest>=8.3.3", | ||
"ruff>=0.8.1", | ||
] |
Binary file not shown.
Binary file not shown.
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
from fastapi import APIRouter | ||
|
||
from src.api.legacy import legacy, legacy_debug | ||
from src.api.legacy import legacy | ||
|
||
router = APIRouter() | ||
|
||
router.include_router(legacy.router) | ||
router.include_router(legacy_debug.router) | ||
# router.include_router(legacy_debug.router) |
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 @@ | ||
from . import config, logging | ||
|
||
__all__ = ["logging", "config"] |
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,4 @@ | ||
from .logging import LoggingMiddleware | ||
from .metrics import PrometheusMiddleware | ||
|
||
__all__ = ["LoggingMiddleware", "PrometheusMiddleware"] |
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,28 @@ | ||
import logging | ||
import time | ||
|
||
from fastapi import Request | ||
from starlette.middleware.base import BaseHTTPMiddleware | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class LoggingMiddleware(BaseHTTPMiddleware): | ||
async def dispatch(self, request: Request, call_next): | ||
start_time = time.perf_counter() | ||
response = await call_next(request) | ||
process_time = time.perf_counter() - start_time | ||
|
||
query_params_list = [ | ||
(key, value if key != "token" else "***") | ||
for key, value in request.query_params.items() | ||
] | ||
|
||
logger.info( | ||
{ | ||
"url": request.url.path, | ||
"params": query_params_list, | ||
"process_time": f"{process_time:.4f}", | ||
} | ||
) | ||
return response |
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,38 @@ | ||
from prometheus_client.metrics import Counter, Histogram | ||
|
||
import time | ||
from starlette.middleware.base import BaseHTTPMiddleware | ||
from fastapi import Request | ||
|
||
# Define Prometheus metrics | ||
REQUEST_COUNT = Counter( | ||
"request_count", "Total number of requests", ["method", "endpoint", "http_status"] | ||
) | ||
REQUEST_LATENCY = Histogram( | ||
"request_latency_seconds", "Latency of requests in seconds", ["method", "endpoint"] | ||
) | ||
|
||
|
||
# Middleware for Prometheus metrics logging | ||
class PrometheusMiddleware(BaseHTTPMiddleware): | ||
async def dispatch(self, request: Request, call_next): | ||
REQUEST_COUNT.labels( | ||
method=request.method, | ||
endpoint=request.url.path | ||
).inc() | ||
|
||
# Start timer for request latency | ||
start_time = time.perf_counter() | ||
|
||
# Process request | ||
response = await call_next(request) | ||
|
||
# Calculate request latency | ||
latency = time.perf_counter() - start_time | ||
|
||
REQUEST_LATENCY.labels( | ||
method=request.method, | ||
endpoint=request.url.path, | ||
).observe(latency) | ||
|
||
return response |
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,44 @@ | ||
import json | ||
import logging | ||
|
||
|
||
# Configure JSON logging | ||
class JsonFormatter(logging.Formatter): | ||
def format(self, record): | ||
log_record = { | ||
"ts": self.formatTime(record, self.datefmt), | ||
"lvl": record.levelname, | ||
"name": record.name, | ||
# "module": record.module, | ||
"func": record.funcName, | ||
"line": record.lineno, | ||
"msg": record.getMessage(), | ||
} | ||
if record.exc_info: | ||
log_record["exception"] = self.formatException(record.exc_info) | ||
return json.dumps(log_record) | ||
|
||
|
||
class IgnoreSQLWarnings(logging.Filter): | ||
def filter(self, record): | ||
ignore_messages = ["Unknown table", "Duplicate entry"] | ||
# Check if any of the ignore messages are in the log record message | ||
if any(msg in record.getMessage() for msg in ignore_messages): | ||
return False # Don't log | ||
return True # Log | ||
|
||
|
||
# Set up the logger | ||
handler = logging.StreamHandler() | ||
handler.setFormatter(JsonFormatter()) | ||
|
||
logging.basicConfig(level=logging.INFO, handlers=[handler]) | ||
|
||
# set imported loggers to warning | ||
# logging.getLogger("requests").setLevel(logging.DEBUG) | ||
# logging.getLogger("urllib3").setLevel(logging.DEBUG) | ||
# logging.getLogger("uvicorn").setLevel(logging.DEBUG) | ||
# logging.getLogger("apscheduler").setLevel(logging.WARNING) | ||
# logging.getLogger("aiomysql").setLevel(logging.ERROR) | ||
# logging.getLogger("asyncmy").setLevel(logging.ERROR) | ||
# logging.getLogger("aiokafka").setLevel(logging.WARNING) |
Oops, something went wrong.