0
0
Fork 0
mirror of https://github.com/alerta/alerta.git synced 2025-01-24 09:19:40 +00:00
alerta_alerta/alerta/utils/tracing.py
Asher Foa 4754c2c55e Fix flake W504 issues and ignore flake W503 (#1061)
* Ignore W503 since it contradicts W504

* Fix flake8 W504 - line break after binary operator
2019-08-11 01:18:26 +02:00

31 lines
769 B
Python

import uuid
from flask import Flask, g, request
class Tracing:
def __init__(self, app=None) -> None:
self.app = None
if app:
self.setup_tracing(app)
def setup_tracing(self, app: Flask) -> None:
app.before_request(self.get_request_id)
app.after_request(self.set_request_id)
@staticmethod
def get_request_id():
headers = request.headers
request_id = (
headers.get('X-Request-ID')
or headers.get('X-Amzn-Trace-Id')
or str(uuid.uuid4())
)
g.request_id = request_id
@staticmethod
def set_request_id(response):
if hasattr(g, 'request_id'):
response.headers['X-Request-ID'] = g.request_id
return response