Skip to content

Commit

Permalink
chore: add user info in uwsgi request logs
Browse files Browse the repository at this point in the history
Refs KER-367
  • Loading branch information
danipran committed Jun 4, 2024
1 parent 683d26d commit cd37436
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ ENV PYTHONUNBUFFERED True

# less & netcat-openbsd are there for in-container manual debugging
# kerrokantasi needs gdal
RUN apt-get update && apt-get install -y postgresql-client less netcat-openbsd gettext locales gdal-bin python3-gdal
RUN apt-get update \
&& apt-get install -y postgresql-client less netcat-openbsd gettext locales gdal-bin python3-gdal \
&& uwsgi --build-plugin /app/deploy/escape_json.c

# we need the Finnish locale built
RUN sed -i 's/^# *\(fi_FI.UTF-8\)/\1/' /etc/locale.gen
Expand Down
42 changes: 42 additions & 0 deletions deploy/escape_json.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
https://github.com/velebit-ai/uwsgi-json-logging-plugin/blob/4edb5cc59013b18f32658195b328afb2ac21b2d2/escape_json.c
uWSGI plugin that creates custom json-escaped logging variables.
build plugin with `uwsgi --build-plugin <filename.c>`
and use it with `uwsgi --plugin <filename_plugin.so> ...`
*/
#include <uwsgi.h>


static ssize_t uwsgi_lf_json_uri(struct wsgi_request *wsgi_req, char **buf) {
long pos = offsetof(struct wsgi_request, uri);
long pos_len = offsetof(struct wsgi_request, uri_len);
char **var = (char **) (((char *) wsgi_req) + pos);
uint16_t *varlen = (uint16_t *) (((char *) wsgi_req) + pos_len);

char *e_json = uwsgi_malloc((*varlen * 2) + 1);
escape_json(*var, *varlen, e_json);
*buf = e_json;
return strlen(*buf);
}

static ssize_t uwsgi_lf_json_host(struct wsgi_request *wsgi_req, char **buf) {
long pos = offsetof(struct wsgi_request, host);
long pos_len = offsetof(struct wsgi_request, host_len);
char **var = (char **) (((char *) wsgi_req) + pos);
uint16_t *varlen = (uint16_t *) (((char *) wsgi_req) + pos_len);

char *e_json = uwsgi_malloc((*varlen * 2) + 1);
escape_json(*var, *varlen, e_json);
*buf = e_json;
return strlen(*buf);
}

static void register_logchunks() {
uwsgi_register_logchunk("json_uri", uwsgi_lf_json_uri, 1);
uwsgi_register_logchunk("json_host", uwsgi_lf_json_host, 1);
}

struct uwsgi_plugin escape_json_plugin = {
.name = "escape_json",
.on_load = register_logchunks,
};
5 changes: 5 additions & 0 deletions deploy/uwsgi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ uwsgi:
ignore-sigpipe: true
ignore-write-errors: true
disable-write-exception: true

logger-req: stdio
remote_addr: "%(addr)", "x_forwarded_for":"%(var.HTTP_X_FORWARDED_FOR)", "request_id":"%(var.HTTP_X_REQUEST_ID)", "remote_user":"%(user)", "bytes_sent":%(size), "request_time":%(secs), "status":%(status), "host":"%(json_host)", "request_proto":"%(proto)", "path":"%(json_uri)", "request_length":%(cl), "http_referer":"%(referer)", "http_user_agent":"%(uagent)"
log-req-encoder: format {"time":"${strftime:%%Y:%%m:%%d %%H:%%M:%%S}", "source":"uwsgi-req", ${msg}}
log-req-encoder: nl

0 comments on commit cd37436

Please sign in to comment.