From e53d1d11a86da1c9a2523968c1812a2973df5a96 Mon Sep 17 00:00:00 2001 From: Michele Pugno Date: Fri, 7 Apr 2023 15:16:00 +0200 Subject: [PATCH 1/3] Accepts custom http headers from config file --- reframe/core/logging.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/reframe/core/logging.py b/reframe/core/logging.py index 6572657c62..7ee3add5be 100644 --- a/reframe/core/logging.py +++ b/reframe/core/logging.py @@ -473,6 +473,7 @@ def _create_httpjson_handler(site_config, config_prefix): extras = site_config.get(f'{config_prefix}/extras') ignore_keys = site_config.get(f'{config_prefix}/ignore_keys') json_formatter = site_config.get(f'{config_prefix}/json_formatter') + extra_headers = site_config.get(f'{config_prefix}/extra_headers') debug = site_config.get(f'{config_prefix}/debug') parsed_url = urllib.parse.urlparse(url) @@ -514,7 +515,7 @@ def _create_httpjson_handler(site_config, config_prefix): getlogger().warning('httpjson: running in debug mode; ' 'no data will be sent to the server') - return HTTPJSONHandler(url, extras, ignore_keys, json_formatter, debug) + return HTTPJSONHandler(url, extras, ignore_keys, json_formatter, extra_headers, debug) def _record_to_json(record, extras, ignore_keys): @@ -563,7 +564,8 @@ class HTTPJSONHandler(logging.Handler): } def __init__(self, url, extras=None, ignore_keys=None, - json_formatter=None, debug=False): + json_formatter=None, extra_headers=None, + debug=False): super().__init__() self._url = url self._extras = extras @@ -581,6 +583,10 @@ def __init__(self, url, extras=None, ignore_keys=None, "it must be 'json_formatter(record, extras, ignore_keys)'" ) + self._headers = {'Content-type': 'application/json', + 'Accept-Charset': 'UTF-8'} + if extra_headers: + self._headers.update(extra_headers) self._debug = debug def emit(self, record): @@ -604,8 +610,7 @@ def emit(self, record): try: requests.post( self._url, data=json_record, - headers={'Content-type': 'application/json', - 'Accept-Charset': 'UTF-8'} + headers=self._headers ) except requests.exceptions.RequestException as e: raise LoggingError('logging failed') from e From b83ea7108b3568b5029fae83b15f85291c53a749 Mon Sep 17 00:00:00 2001 From: Michele Pugno Date: Fri, 7 Apr 2023 15:58:08 +0200 Subject: [PATCH 2/3] updated config schema and docs --- docs/config_reference.rst | 10 ++++++++++ reframe/schemas/config.json | 2 ++ 2 files changed, 12 insertions(+) diff --git a/docs/config_reference.rst b/docs/config_reference.rst index e11af9806c..4172b16f3f 100644 --- a/docs/config_reference.rst +++ b/docs/config_reference.rst @@ -1345,6 +1345,15 @@ The additional properties for the ``httpjson`` handler are the following: These keys will be excluded from the log record that will be sent to the server. +.. py:attribute:: logging.handlers..httpjson..extra_headers + +.. py:attribute:: logging.handlers_perflog..httpjson..extra_headers + + :required: No + :default: ``{}`` + + A set of optional key/value pairs to be sent as HTTP message headers(e.g. API keys). + These may depend on the server configuration. The ``httpjson`` handler sends log messages in JSON format using an HTTP POST request to the specified URL. @@ -1361,6 +1370,7 @@ An example configuration of this handler for performance logging is shown here: 'data-version': '1.0' }, 'ignore_keys': ['check_perfvalues'] + 'extra_headers': {'Authorization': 'Token YOUR_API_TOKEN'} } diff --git a/reframe/schemas/config.json b/reframe/schemas/config.json index 2f3345555d..bee3a273bf 100644 --- a/reframe/schemas/config.json +++ b/reframe/schemas/config.json @@ -166,6 +166,7 @@ "items": {"type": "string"} }, "json_formatter": {}, + "extra_headers": {"type": "object"}, "debug": {"type": "boolean"} }, "required": ["url"] @@ -571,6 +572,7 @@ "logging/handlers*/httpjson_extras": {}, "logging/handlers*/httpjson_ignore_keys": [], "logging/handlers*/httpjson_json_formatter": null, + "logging/handlers*/httpjson_extra_headers": {}, "logging/handlers*/httpjson_debug": false, "logging/handlers*/stream_name": "stdout", "logging/handlers*/syslog_socktype": "udp", From dd29444f6701849e6cb1d300fb70a294d861d203 Mon Sep 17 00:00:00 2001 From: Vasileios Karakasis Date: Sat, 15 Apr 2023 01:41:36 +0300 Subject: [PATCH 3/3] Minor style changes --- docs/config_reference.rst | 24 ++++++++++++++---------- reframe/core/logging.py | 4 +++- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/docs/config_reference.rst b/docs/config_reference.rst index d046ca82af..a3f5e04749 100644 --- a/docs/config_reference.rst +++ b/docs/config_reference.rst @@ -1326,6 +1326,19 @@ The additional properties for the ``httpjson`` handler are the following: The URL to be used in the HTTP(S) request server. +.. py:attribute:: logging.handlers..httpjson..extra_headers + +.. py:attribute:: logging.handlers_perflog..httpjson..extra_headers + + :required: No + :default: ``{}`` + + A set of optional key/value pairs to be sent as HTTP message headers (e.g. API keys). + These may depend on the server configuration. + + .. versionadded:: 4.2 + + .. py:attribute:: logging.handlers..httpjson..extras .. py:attribute:: logging.handlers_perflog..httpjson..extras @@ -1345,15 +1358,6 @@ The additional properties for the ``httpjson`` handler are the following: These keys will be excluded from the log record that will be sent to the server. -.. py:attribute:: logging.handlers..httpjson..extra_headers - -.. py:attribute:: logging.handlers_perflog..httpjson..extra_headers - - :required: No - :default: ``{}`` - - A set of optional key/value pairs to be sent as HTTP message headers(e.g. API keys). - These may depend on the server configuration. The ``httpjson`` handler sends log messages in JSON format using an HTTP POST request to the specified URL. @@ -1365,12 +1369,12 @@ An example configuration of this handler for performance logging is shown here: 'type': 'httpjson', 'url': 'http://httpjson-server:12345/rfm', 'level': 'info', + 'extra_headers': {'Authorization': 'Token YOUR_API_TOKEN'}, 'extras': { 'facility': 'reframe', 'data-version': '1.0' }, 'ignore_keys': ['check_perfvalues'] - 'extra_headers': {'Authorization': 'Token YOUR_API_TOKEN'} } diff --git a/reframe/core/logging.py b/reframe/core/logging.py index 48b1a439a5..44d3d62f6e 100644 --- a/reframe/core/logging.py +++ b/reframe/core/logging.py @@ -515,7 +515,8 @@ def _create_httpjson_handler(site_config, config_prefix): getlogger().warning('httpjson: running in debug mode; ' 'no data will be sent to the server') - return HTTPJSONHandler(url, extras, ignore_keys, json_formatter, extra_headers, debug) + return HTTPJSONHandler(url, extras, ignore_keys, json_formatter, + extra_headers, debug) def _record_to_json(record, extras, ignore_keys): @@ -587,6 +588,7 @@ def __init__(self, url, extras=None, ignore_keys=None, 'Accept-Charset': 'UTF-8'} if extra_headers: self._headers.update(extra_headers) + self._debug = debug def emit(self, record):