diff --git a/hyperglass/api/routes.py b/hyperglass/api/routes.py index fd240a7..8acb5a8 100644 --- a/hyperglass/api/routes.py +++ b/hyperglass/api/routes.py @@ -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 diff --git a/hyperglass/configuration/__init__.py b/hyperglass/configuration/__init__.py index c265196..de2553e 100644 --- a/hyperglass/configuration/__init__.py +++ b/hyperglass/configuration/__init__.py @@ -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 diff --git a/hyperglass/configuration/models/logging.py b/hyperglass/configuration/models/logging.py index 2dcd726..2be6d92 100644 --- a/hyperglass/configuration/models/logging.py +++ b/hyperglass/configuration/models/logging.py @@ -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 diff --git a/hyperglass/log.py b/hyperglass/log.py index f885edc..40da227 100644 --- a/hyperglass/log.py +++ b/hyperglass/log.py @@ -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