Closed lakshaythareja closed 2 years ago
Yes! 🙂 See https://github.com/snok/asgi-correlation-id/issues/7 - that should have a few examples you can use 👍 Feel free to re-open if you get stuck
As i mentioned the issue above solves for correlation id in a request and not parent and current process id for celery tasks. I need help there. @sondrelg
Ah, sorry, it's too early in the morning 😄 I'll try to find the time to look into this later today
I think something like this might do the trick:
import logging
import sys
from uuid import uuid4
import uvicorn
from asgi_correlation_id import CorrelationIdMiddleware
from asgi_correlation_id.context import correlation_id, celery_parent_id, celery_current_id
from fastapi import FastAPI
from loguru import logger
app = FastAPI()
app.add_middleware(CorrelationIdMiddleware)
logger.remove()
def correlation_id_filter(record):
celery_current_id.set(uuid4()) # just for testing - remove these
celery_parent_id.set(uuid4()) # just for testing - remove these
record['correlation_id'] = correlation_id.get()
record['celery_parent_id'] = str(celery_parent_id.get())[:6]
record['celery_current_id'] = str(celery_current_id.get())[:6]
return record
fmt = "<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> <level>{level:<8}</level> <red>{correlation_id}</red> <red>{celery_parent_id}-{celery_current_id}</red> <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>"
logger.add(
sys.stderr,
format=fmt,
level=logging.DEBUG,
filter=correlation_id_filter,
)
@app.get('/')
def index():
logger.info("Request with id")
return 'OK'
if __name__ == '__main__':
uvicorn.run(app)
This is what it looks like in my local environment
It worked thanks.
I use loguru for logging, saw your example for adding the correlation id for requests. But I am unable to add parent and current process id for celery tasks in loguru. Can you help?