Skip to content

Add request_id and user_id logging in nginx and services #10

@ArtemSBulgakov

Description

@ArtemSBulgakov

Describe the feature

We want to trace users requests to debug issues. For this, we need to add user ids and request ids to each log line: both in Nginx logs and FastAPI logs.

Suggested solution

Пример log_format:
(блок http)

    log_format main '[$time_local] "$http_x_forwarded_for" $remote_addr '
                    '"$server_name" "$request" $status '
                    '"$http_referer" "$http_user_agent" '
                    '$body_bytes_sent $request_time '
                    'auth_user="$remote_user" '
                    'session_id="$session_id"';

Пример пробрасывания переменных в сервис:
(блок server)

    proxy_set_header X-Request-ID $requestid;

Пример возврата переменных клиенту:
(блок server)

    add_header X-Request-ID $requestid;

Пример куки для трекинга браузера:
(блок http)

    map $cookie_session_id $session_id {
        "" $request_id.$msec;
        default $cookie_session_id;
    }
    add_header Set-Cookie "session_id=$session_id; Path=/; HttpOnly; SameSite=Strict; Max-Age=31536000";

Also FastAPI should add header for user id, and Nginx can use this header in log.

FastAPI logging should also print user ids and request ids.

Additional context

No response

Metadata

Metadata

Labels

No labels
No labels
No fields configured for Feature.

Projects

Status
🔖 Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions