This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
147 lines (108 loc) · 3.37 KB
/
main.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import json
import logging
import os
from urllib.parse import urlparse
import boto3
import requests
import structlog
from redis import Redis as RealRedis
class AWS:
def ssm_get_parameter(self, *args, **kwargs):
ssm = boto3.client("ssm")
return ssm.get_parameter(*args, **kwargs)
def cloudwatch_put_metric_data(self, *args, **kwargs):
cw = boto3.client("cloudwatch")
return cw.put_metric_data(*args, **kwargs)
class Redis:
def __init__(self):
self.redis = None
def setup(self, *args, **kwargs):
if self.redis is None:
self.redis = RealRedis(*args, **kwargs)
return self
def llen(self, queue):
return self.redis.llen(queue)
def copy_event_to_message(_logger, _log_method, event_dict):
event_dict["message"] = event_dict["event"]
return event_dict
def get_logger(log_level_str):
processors = [
structlog.processors.add_log_level,
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
structlog.processors.TimeStamper(),
structlog.processors.CallsiteParameterAdder(),
copy_event_to_message,
structlog.processors.JSONRenderer(),
]
log_level = {
"critical": logging.CRITICAL,
"error": logging.ERROR,
"warning": logging.WARNING,
"info": logging.INFO,
"debug": logging.DEBUG,
}.get(log_level_str.lower())
structlog.configure(
processors=processors,
wrapper_class=structlog.make_filtering_bound_logger(log_level),
)
return structlog.get_logger()
def handler(event, context):
env = os.environ.get("ENV", "prod")
queue = os.environ.get("QUEUE", "restyled:agent:webhooks")
log_level = os.environ.get("LOG_LEVEL", "info")
logger = get_logger(log_level)
aws = AWS()
redis = Redis()
try:
return handler_(aws, redis, env, queue, logger)
except:
logger.error("Exception", exc_info=True)
return {"ok": False, "env": env, "queue": queue}
def handler_(aws, redis, env, queue, logger):
dimensions = [
{
"Name": "Environment",
"Value": env
},
{
"Name": "QueueName",
"Value": queue
},
]
url = get_redis_url(aws, env)
if url.scheme == "rediss":
redis.setup(
host=url.hostname,
port=url.port,
password=url.password,
ssl=True,
ssl_cert_reqs=None,
)
else:
redis.setup(host=url.hostname, port=url.port, password=url.password)
depth = redis.llen(queue)
logger.info(
"Putting depth metric",
app="record-metrics",
env=env,
queue=queue,
depth=depth,
)
aws.cloudwatch_put_metric_data(
Namespace="Restyled",
MetricData=[{
"MetricName": "QueueDepth",
"Value": depth,
"Unit": "Count",
"Dimensions": dimensions,
}],
)
return {"ok": True, "env": env, "queue": queue, "depth": depth}
def get_redis_url(aws, env):
token_parameter_name = "/restyled/%s/redis-url" % env
token_parameter = aws.ssm_get_parameter(Name=token_parameter_name)
return urlparse(token_parameter["Parameter"]["Value"])
if __name__ == "__main__":
result = handler(None, None)
print(json.dumps(result, indent=2))