From 17f24cb08656ade3de9e7d326d117875a2ad0a89 Mon Sep 17 00:00:00 2001 From: HgO Date: Sat, 29 Oct 2022 14:18:16 +0200 Subject: [PATCH] create custom logger to log at debug level webhook requests --- docker/Dockerfile | 8 +++++--- docker/docker-compose.yml | 3 ++- matrix_alertbot/webhook.py | 35 ++++++++++++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 9511044..88e8f1b 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -92,18 +92,20 @@ ENV PATH="$VIRTUAL_ENV/bin:$PATH" WORKDIR "${PROJECT_DIR}" +# Install any native runtime dependencies +RUN apk add --no-cache libstdc++ + # Copy python dependencies from the "builder" container COPY --from=builder "${PROJECT_DIR}" "${PROJECT_DIR}" # Copy libolm from the "builder" container COPY --from=builder /usr/local/lib/libolm* /usr/local/lib/ -# Install any native runtime dependencies -RUN apk add --no-cache libstdc++ - # Specify a volume that holds the config file, SQLite3 database, # and the matrix-nio store VOLUME ["/data"] # Start the bot CMD ["matrix-alertbot", "/data/config.yaml"] + +EXPOSE 8080 diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 53687db..bcec427 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -40,6 +40,8 @@ services: restart: always networks: - matrix-alertbot + ports: + - 8080:8080 volumes: - matrix-alertbot:/data # Used for allowing connections to homeservers hosted on the host machine @@ -48,4 +50,3 @@ services: # Defaults to 127.0.0.1 and is set in docker/.env extra_hosts: - "localhost:${HOST_IP_ADDRESS}" - diff --git a/matrix_alertbot/webhook.py b/matrix_alertbot/webhook.py index 791c41e..b093343 100644 --- a/matrix_alertbot/webhook.py +++ b/matrix_alertbot/webhook.py @@ -4,6 +4,7 @@ import logging import prometheus_client from aiohttp import ClientError, web, web_request +from aiohttp.abc import AbstractAccessLogger from aiohttp_prometheus_exporter.handler import metrics from aiohttp_prometheus_exporter.middleware import prometheus_middleware_factory from diskcache import Cache @@ -24,6 +25,38 @@ logger = logging.getLogger(__name__) routes = web.RouteTableDef() +class AccessLogger(AbstractAccessLogger): + def log( + self, + request: web_request.BaseRequest, + response: web.StreamResponse, + time: float, + ) -> None: + if request is None: + remote_address = "-" + request_info = "-" + referer_header = "-" + user_agent_header = "-" + else: + if request.remote is None: + remote_address = "-" + else: + remote_address = request.remote + + request_info = ( + f"{request.method} {request.path_qs} " + f"HTTP/{request.version.major}.{request.version.minor}" + ) + + referer_header = request.headers.get("Referer", "-") + user_agent_header = request.headers.get("User-Agent", "-") + + self.logger.debug( + f'{remote_address} "{request_info}" {response.status} ' + f'{response.body_length} "{referer_header}" "{user_agent_header}"' + ) + + @routes.get("/health") async def get_health(request: web_request.Request) -> web.Response: return web.Response(status=200) @@ -153,7 +186,7 @@ class Webhook: ) self.app.router.add_get("/metrics", metrics()) - self.runner = web.AppRunner(self.app) + self.runner = web.AppRunner(self.app, access_log_class=AccessLogger) self.config = config self.address = config.address