-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feature/#19-elk
- Loading branch information
Showing
16 changed files
with
227 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
input { | ||
udp { | ||
codec => "json" | ||
port => "5044" | ||
} | ||
} | ||
filter { | ||
} | ||
output { | ||
stdout { | ||
} | ||
elasticsearch { | ||
hosts => [ "${ES_HOST}" ] | ||
index => "ugc.%{+YYYY.MM}" | ||
} | ||
} |
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 |
---|---|---|
|
@@ -26,3 +26,6 @@ motor==3.0.0 | |
|
||
gunicorn==20.1.0 | ||
uvloop==0.16.0 | ||
loguru==0.6.0 | ||
ecs-logging==2.0.0 | ||
python-logstash==0.4.8 |
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 typing import Final | ||
|
||
REQUEST_ID_HEADER: Final[str] = "X-Request-Id" |
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,33 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
from typing import TYPE_CHECKING | ||
|
||
from logstash import LogstashHandler | ||
from loguru import logger | ||
|
||
if TYPE_CHECKING: | ||
from loguru import Logger, Record | ||
|
||
|
||
def init_logstash_handler(host: str, port: int, version: int) -> LogstashHandler: | ||
logstash_handler = LogstashHandler(host=host, port=port, version=version) | ||
yield logstash_handler | ||
|
||
|
||
def init_logger(handler: logging.Handler, log_format: str, level: str | int) -> Logger: | ||
logger.add( | ||
handler, | ||
format=log_format, | ||
level=level, | ||
filter=request_id_filter, | ||
) | ||
yield logger | ||
|
||
|
||
def request_id_filter(record: Record) -> bool: | ||
"""Добавление поля `request_id` к записи в лог.""" | ||
extra_fields = record.get("extra") | ||
request_id = extra_fields.get("request_id") | ||
record["request_id"] = request_id | ||
return True |
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,23 @@ | ||
from typing import Awaitable, Callable | ||
|
||
from loguru import logger | ||
|
||
from aiohttp import web | ||
|
||
from ugc.common.constants import REQUEST_ID_HEADER | ||
from ugc.common.exceptions import RequiredHeaderMissingError | ||
|
||
|
||
@web.middleware | ||
async def request_id_middleware(request: web.Request, handler: Callable[..., Awaitable]) -> web.Response: | ||
"""Обработка обязательного заголовка `X-Request-Id`.""" | ||
request_id = request.headers.get(REQUEST_ID_HEADER) | ||
if not request_id: | ||
raise RequiredHeaderMissingError(message="Required header `X-Request-Id` is missing") | ||
_add_extra_logging_field(request_id) | ||
response = await handler(request) | ||
return response | ||
|
||
|
||
def _add_extra_logging_field(request_id: str) -> None: | ||
logger.configure(extra={"request_id": request_id}) |
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