forked from mirrors/thatmattlove-hyperglass
fix http logging bug
This commit is contained in:
parent
306b3cc140
commit
99879c468e
4 changed files with 14 additions and 4 deletions
|
|
@ -36,7 +36,7 @@ async def query(query_data: Query, request: Request):
|
|||
if ip_address(request.client.host).is_loopback:
|
||||
network_info = {"prefix": None, "asn": None}
|
||||
else:
|
||||
network_info = get_network_info("199.34.92.64")
|
||||
network_info = get_network_info(request.client.host)
|
||||
|
||||
network_info = {
|
||||
"prefix": str(network_info["prefix"]),
|
||||
|
|
@ -63,6 +63,7 @@ async def query(query_data: Query, request: Request):
|
|||
"network": network_info,
|
||||
},
|
||||
params.logging.http,
|
||||
log,
|
||||
)
|
||||
|
||||
# Initialize cache
|
||||
|
|
|
|||
|
|
@ -157,6 +157,9 @@ if params.logging.syslog is not None and params.logging.syslog.enable:
|
|||
syslog_port=params.logging.syslog.port,
|
||||
)
|
||||
|
||||
if params.logging.http is not None and params.logging.http.enable:
|
||||
log.debug("HTTP logging is enabled")
|
||||
|
||||
# Perform post-config initialization string formatting or other
|
||||
# functions that require access to other config levels. E.g.,
|
||||
# something in 'params.web.text' needs to be formatted with a value
|
||||
|
|
|
|||
|
|
@ -57,6 +57,7 @@ class Http(HyperglassModelExtra):
|
|||
authentication: Optional[HttpAuth]
|
||||
headers: Dict[StrictStr, Union[StrictStr, StrictInt, StrictBool, None]] = {}
|
||||
params: Dict[StrictStr, Union[StrictStr, StrictInt, StrictBool, None]] = {}
|
||||
key: Optional[StrictStr]
|
||||
verify_ssl: StrictBool = True
|
||||
timeout: Union[StrictFloat, StrictInt] = 5.0
|
||||
|
||||
|
|
|
|||
|
|
@ -102,21 +102,26 @@ def enable_syslog_logging(logger, syslog_host, syslog_port):
|
|||
return True
|
||||
|
||||
|
||||
async def query_hook(query, http_logging):
|
||||
async def query_hook(query, http_logging, log):
|
||||
"""Log a query to an http server."""
|
||||
import httpx
|
||||
|
||||
from hyperglass.util import parse_exception
|
||||
|
||||
if http_logging.key is not None:
|
||||
query = {http_logging.key: query}
|
||||
|
||||
log.debug("Sending query data to webhook:\n{}", query)
|
||||
|
||||
async with httpx.AsyncClient(**http_logging.decoded()) as client:
|
||||
try:
|
||||
response = await client.post(str(http_logging.host), json=query)
|
||||
|
||||
if response.status_code not in range(200, 300):
|
||||
print(f"{response.status_code}: {response.text}", file=sys.stderr)
|
||||
log.error(f"{response.status_code} error: {response.text}")
|
||||
|
||||
except httpx.HTTPError as err:
|
||||
parsed = parse_exception(err)
|
||||
print(parsed, file=sys.stderr)
|
||||
log.error(parsed)
|
||||
|
||||
return True
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue