sedi-relay/relay/http_debug.py

69 lines
1.9 KiB
Python
Raw Normal View History

2018-11-18 00:07:36 +00:00
import logging
import aiohttp
2018-11-18 22:03:53 +00:00
from collections import defaultdict
STATS = {
'requests': defaultdict(int),
'response_codes': defaultdict(int),
'response_codes_per_domain': defaultdict(lambda: defaultdict(int)),
'delivery_codes': defaultdict(int),
'delivery_codes_per_domain': defaultdict(lambda: defaultdict(int)),
'exceptions': defaultdict(int),
'exceptions_per_domain': defaultdict(lambda: defaultdict(int)),
'delivery_exceptions': defaultdict(int),
'delivery_exceptions_per_domain': defaultdict(lambda: defaultdict(int))
}
2018-11-18 00:07:36 +00:00
async def on_request_start(session, trace_config_ctx, params):
2018-11-18 22:03:53 +00:00
global STATS
2018-11-18 00:07:36 +00:00
logging.debug("HTTP START [%r], [%r]", session, params)
2018-11-18 22:03:53 +00:00
STATS['requests'][params.url.host] += 1
async def on_request_end(session, trace_config_ctx, params):
global STATS
logging.debug("HTTP END [%r], [%r]", session, params)
host = params.url.host
status = params.response.status
STATS['response_codes'][status] += 1
STATS['response_codes_per_domain'][host][status] += 1
if params.method == 'POST':
STATS['delivery_codes'][status] += 1
STATS['delivery_codes_per_domain'][host][status] += 1
async def on_request_exception(session, trace_config_ctx, params):
global STATS
logging.debug("HTTP EXCEPTION [%r], [%r]", session, params)
host = params.url.host
exception = repr(params.exception)
STATS['exceptions'][exception] += 1
STATS['exceptions_per_domain'][host][exception] += 1
if params.method == 'POST':
STATS['delivery_exceptions'][exception] += 1
STATS['delivery_exceptions_per_domain'][host][exception] += 1
2018-11-18 00:07:36 +00:00
def http_debug():
2022-05-06 07:04:51 +00:00
if logging.DEBUG >= logging.root.level:
return
2018-11-18 00:07:36 +00:00
trace_config = aiohttp.TraceConfig()
trace_config.on_request_start.append(on_request_start)
2018-11-18 22:03:53 +00:00
trace_config.on_request_end.append(on_request_end)
trace_config.on_request_exception.append(on_request_exception)
2022-05-06 07:04:51 +00:00
return [trace_config]