feat: storing logs in mongodb

This commit is contained in:
NarayanBavisetti 2024-02-16 12:56:11 +05:30
parent 7628419a26
commit 4ef82bad22
6 changed files with 78 additions and 19 deletions

View File

@ -31,3 +31,5 @@ USE_MINIO=1
# Nginx Configuration
NGINX_PORT=80
MONGO_DB_URL="mongodb://plane-mongodb:27017/"

View File

@ -0,0 +1,23 @@
from pymongo import MongoClient
def singleton(cls):
instances = {}
def wrapper(*args, **kwargs):
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]
return wrapper
@singleton
class Database:
db = None
client = None
def __init__(self, mongo_uri, database_name):
self.client = MongoClient(mongo_uri)
self.db = self.client[database_name]
def get_db(self):
return self.db

View File

@ -1,9 +1,14 @@
from plane.db.models import APIToken, APIActivityLog
# Python imports
import os
# Django imports
from plane.db.mongodb import Database
class APITokenLogMiddleware:
def __init__(self, get_response):
self.get_response = get_response
self.database = Database(os.environ.get("MONGO_DB_URL"), "plane")
def __call__(self, request):
request_body = request.body
@ -17,23 +22,31 @@ class APITokenLogMiddleware:
# If the API key is present, log the request
if api_key:
try:
APIActivityLog.objects.create(
token_identifier=api_key,
path=request.path,
method=request.method,
query_params=request.META.get("QUERY_STRING", ""),
headers=str(request.headers),
body=(
request_body.decode("utf-8") if request_body else None
),
response_body=(
response.content.decode("utf-8")
if response.content
else None
),
response_code=response.status_code,
ip_address=request.META.get("REMOTE_ADDR", None),
user_agent=request.META.get("HTTP_USER_AGENT", None),
db = self.database.get_db()
collection = db["api_activity_logs"]
_ = collection.insert_one(
{
"token_identifier": api_key,
"path": request.path,
"method": request.method,
"query_params": request.META.get("QUERY_STRING", ""),
"headers": str(request.headers),
"body": (
request_body.decode("utf-8")
if request_body
else None
),
"response_body": (
response.content.decode("utf-8")
if response.content
else None
),
"response_code": response.status_code,
"ip_address": request.META.get("REMOTE_ADDR", None),
"user_agent": request.META.get(
"HTTP_USER_AGENT", None
),
}
)
except Exception as e:

View File

@ -33,4 +33,4 @@ posthog==3.0.2
cryptography==42.0.0
lxml==4.9.3
boto3==1.28.40
pymongo==4.6.1

View File

@ -55,6 +55,7 @@ x-app-env : &app-env
- MINIO_ROOT_PASSWORD=${MINIO_ROOT_PASSWORD:-"secret-key"}
- BUCKET_NAME=${BUCKET_NAME:-uploads}
- FILE_SIZE_LIMIT=${FILE_SIZE_LIMIT:-5242880}
- MONGO_DB_URL=${MONGO_DB_URL:-"mongodb://plane-mongodb:27017/"}
@ -138,6 +139,14 @@ services:
command: postgres -c 'max_connections=1000'
volumes:
- pgdata:/var/lib/postgresql/data
plane-mongodb:
<<: *app-env
image: mongo:7.0.5
pull_policy: if_not_present
restart: unless-stopped
volumes:
- mongodbdata:/data/db
plane-redis:
<<: *app-env
@ -172,3 +181,4 @@ volumes:
pgdata:
redisdata:
uploads:
mongodbdata:

View File

@ -8,6 +8,7 @@ volumes:
redisdata:
uploads:
pgdata:
mongodbdata:
services:
@ -45,6 +46,16 @@ services:
- .env
environment:
PGDATA: /var/lib/postgresql/data
plane-mongodb:
image: mongo:7.0.5
restart: unless-stopped
networks:
- dev_env
volumes:
- mongodbdata:/data/db
env_file:
- .env
web:
build: