-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck-webapp.py
50 lines (39 loc) · 1.27 KB
/
check-webapp.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
41
42
43
44
45
46
47
48
49
import nest_asyncio
import configparser
import traceback
from datadog import initialize, statsd
from datadog.api import Event
nest_asyncio.apply()
import requests
config = configparser.ConfigParser()
config.read('config.ini')
# Initialize the Datadog client
initialize(
api_key=config['DEFAULT'].get('datadog-api-key', ''),
app_key=config['DEFAULT'].get('datadog-app-key', '')
)
def handle_ex(e):
# Send an event to Datadog
error_text = str(e) if isinstance(e, str) else traceback.format_exc()
# Increment error counter
statsd.increment('webapp.health_check.errors', tags=['service:webapp'])
# Send detailed event
Event.create(
title='Webapp Health Check Failed',
text=error_text,
alert_type='error',
tags=['service:webapp', 'monitor:health_check']
)
try:
# try to load the url
url = f"http://{config['DEFAULT']['ngrok-subdomain']}.ngrok.io/health"
response = requests.get(url)
if response.status_code == 200:
# Track successful health checks
statsd.increment('webapp.health_check.success', tags=['service:webapp'])
exit(0)
else:
raise Exception(f"Error! The server returned a {response.status_code} status code.")
except Exception as e:
handle_ex(e)
raise