-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
40 lines (33 loc) · 1.12 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import structlog
from pymongo import MongoClient
from pymongo.errors import ConnectionFailure
from json import dumps
from flask import request
log = structlog.get_logger()
def http_healthcheck_handler(mongo_host, mongo_port, version):
postdb = MongoClient(mongo_host, int(mongo_port),
serverSelectionTimeoutMS=2000)
try:
postdb.admin.command('ismaster')
except ConnectionFailure:
postdb_status = 0
else:
postdb_status = 1
status = postdb_status
healthcheck = {
'status': status,
'dependent_services': {
'postdb': postdb_status
},
'version': version
}
return dumps(healthcheck)
def log_event(event_type, name, message, params={}):
request_id = request.headers['Request-Id'] \
if 'Request-Id' in request.headers else None
if event_type == 'info':
log.info(name, service='post', request_id=request_id,
message=message, params=params)
elif event_type == 'error':
log.error(name, service='post', request_id=request_id,
message=message, params=params)