Skip to content

Commit

Permalink
Merge pull request #226 from ParkenDD/loki-sending-in-celery
Browse files Browse the repository at this point in the history
fix loki in celery, ruff mocked service
  • Loading branch information
the-infinity authored Dec 26, 2024
2 parents 5de6a3c + 16b5a9e commit 1fb1cb8
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 9 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,3 @@ jobs:
# https://beta.ruff.rs/docs/usage/#github-action
# uses: chartboost/ruff-action@v1
run: ruff check --output-format github ./webapp ./tests ./migrations

- name: format using ruff formatter
# We could also use the official GitHub Actions integration.
# https://beta.ruff.rs/docs/usage/#github-action
# uses: chartboost/ruff-action@v1
run: ruff check ./webapp ./tests ./migrations
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
# Changelog

## 0.16.3

Released 2024-12-26

### Fixes

* [Fixes an issue with logs not sent to Loki in Celery](https://github.com/ParkenDD/park-api-v3/pull/226)


## 0.16.2

Released 2024-11-28

### Fixes

* [Fixes an issue with Celery Tasks sometimes preventing static data imports](https://github.com/ParkenDD/park-api-v3/pull/224)


Expand Down
25 changes: 25 additions & 0 deletions dev/test_services/mocked_loki/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
Copyright 2024 binary butterfly GmbH
Use of this source code is governed by an MIT-style license that can be found in the LICENSE.txt.
"""

from flask import Flask, make_response, request


def empty_json_response():
response = make_response('')
response.mimetype = 'application/json'
return response


app = Flask('mocked_loki')


@app.post('/loki/api/v1/push')
def loki_push_logs():
print(f'<< {request.data}')
return empty_json_response(), 204


if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ x-flask-defaults: &flask-defaults
rabbitmq:
condition: service_healthy

# Define reusable defaults for mocked services
x-mocked-service-defaults: &mocked-service-defaults
image: park-api:local-dev
command: ["python", "/app/app.py"]
user: *local-user

# Define actual services (containers)
services:
Expand Down Expand Up @@ -76,6 +81,10 @@ services:
timeout: 1s
retries: 20

mocked-loki:
<<: *mocked-service-defaults
volumes:
- ./dev/test_services/mocked_loki:/app

volumes:
postgresql:
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ filterwarnings = [


[tool.ruff]

lint.select = [
"F", # pyflakes
"I", # isort
Expand Down Expand Up @@ -77,6 +78,7 @@ exclude = [
"dist",
"node_modules",
"venv",
"dev/*"
]

line-length = 120
Expand Down
5 changes: 3 additions & 2 deletions webapp/common/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Logger:
config_helper: ConfigHelper
context_helper: ContextHelper
logger: PythonLogger
local_handler: Optional[LocalFileHandler] = None
local_file_handler: Optional[LocalFileHandler] = None
stdout_handler: Optional[StdoutHandler] = None
loki_handler: Optional[LokiQueueHandler] = None
Expand All @@ -32,11 +33,11 @@ def __init__(self, config_helper: ConfigHelper, context_helper: ContextHelper):
self.logger = logging.getLogger('app')
self.logger.setLevel(logging.INFO)

self.local_handler = LocalFileHandler(config_helper=config_helper)
self.local_handler = LocalFileHandler(config_helper=self.config_helper)
self.logger.addHandler(self.local_handler)

if self.config_helper.get('LOKI_ENABLED'):
self.loki_handler = LokiQueueHandler(config_helper=config_helper)
self.loki_handler = LokiQueueHandler(config_helper=self.config_helper)
self.logger.addHandler(self.loki_handler)

if self.config_helper.get('STDOUT_LOGGING_ENABLED'):
Expand Down
4 changes: 3 additions & 1 deletion webapp/common/logging/loki_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
Use of this source code is governed by an MIT-style license that can be found in the LICENSE.txt.
"""

import logging
import os
from logging import Handler, LogRecord
from logging.handlers import QueueHandler, QueueListener
from queue import Queue
from multiprocessing import Queue
from typing import Optional

from webapp.common.config import ConfigHelper
Expand Down Expand Up @@ -60,6 +61,7 @@ def __init__(self, config_helper: ConfigHelper):

self.queue = Queue()
super().__init__(self.queue)
self.setLevel(logging.INFO)

if config_helper.get('LOKI_USER') and config_helper.get('LOKI_PASSWORD'):
auth = BasicAuth(config_helper.get('LOKI_USER'), config_helper.get('LOKI_PASSWORD'))
Expand Down

0 comments on commit 1fb1cb8

Please sign in to comment.