Commit 46da2a27 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Added gunicorn even parser to logstash handler.

parent aea048cb
Pipeline #68513 failed with stages
in 14 minutes and 19 seconds
......@@ -20,3 +20,5 @@ target/
.vscode/
vscode/
nomad.yaml
gunicorn.log.conf
gunicorn.conf
\ No newline at end of file
......@@ -165,6 +165,20 @@ class LogstashHandler(logstash.TCPLogstashHandler):
return False
_gunicorn_pattern_parts = [
r'(?P<host>\S+)', # host %h
r'\S+', # indent %l (unused)
r'(?P<user>\S+)', # user %u
r'\[(?P<time>.+)\]', # time %t
r'"(?P<request>.+)"', # request "%r"
r'(?P<status>[0-9]+)', # status %>s
r'(?P<size>\S+)', # size %b (careful, can be '-')
r'"(?P<referer>.*)"', # referer "%{Referer}i"
r'"(?P<agent>.*)"', # user agent "%{User-agent}i"
]
_gunicorn_pattern = re.compile(r'\s+'.join(_gunicorn_pattern_parts) + r'\s*\Z')
class LogstashFormatter(logstash.formatter.LogstashFormatterBase):
def format(self, record):
......@@ -178,7 +192,6 @@ class LogstashFormatter(logstash.formatter.LogstashFormatterBase):
'@timestamp': self.format_timestamp(record.created),
'@version': '1',
'event': structlog['event'],
'message': structlog['event'],
'host': self.host,
'path': record.pathname,
'tags': self.tags,
......@@ -208,6 +221,27 @@ class LogstashFormatter(logstash.formatter.LogstashFormatterBase):
structlog['nomad.release'] = config.release
message.update(structlog)
# Handle gunicorn access events
if record.name == 'gunicorn.access':
gunicorn_message = structlog['event']
gunicorn_record = _gunicorn_pattern.match(gunicorn_message).groupdict()
if gunicorn_record['user'] == '-':
gunicorn_record['user'] = None
gunicorn_record['status'] = int(gunicorn_record['status'])
if gunicorn_record['size'] == '-':
gunicorn_record['size'] = 0
else:
gunicorn_record['size'] = int(gunicorn_record['size'])
if gunicorn_record['referer'] == '-':
gunicorn_record['referer'] = None
message.update({'gunicorn.%s' % key: value for key, value in gunicorn_record.items()})
message['event'] = gunicorn_record['request']
# Add extra fields
message.update(self.get_extra_fields(record))
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment