Skip to content

Commit cc422fc

Browse files
authored
Merge pull request #2854 from Lewih/develop
[feat] Allow custom HTTP headers with the `httpjson` log handler
2 parents 06dd845 + 2f6187a commit cc422fc

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

docs/config_reference.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,19 @@ The additional properties for the ``httpjson`` handler are the following:
13261326
The URL to be used in the HTTP(S) request server.
13271327

13281328

1329+
.. py:attribute:: logging.handlers..httpjson..extra_headers
1330+
1331+
.. py:attribute:: logging.handlers_perflog..httpjson..extra_headers
1332+
1333+
:required: No
1334+
:default: ``{}``
1335+
1336+
A set of optional key/value pairs to be sent as HTTP message headers (e.g. API keys).
1337+
These may depend on the server configuration.
1338+
1339+
.. versionadded:: 4.2
1340+
1341+
13291342
.. py:attribute:: logging.handlers..httpjson..extras
13301343
13311344
.. py:attribute:: logging.handlers_perflog..httpjson..extras
@@ -1356,6 +1369,7 @@ An example configuration of this handler for performance logging is shown here:
13561369
'type': 'httpjson',
13571370
'url': 'http://httpjson-server:12345/rfm',
13581371
'level': 'info',
1372+
'extra_headers': {'Authorization': 'Token YOUR_API_TOKEN'},
13591373
'extras': {
13601374
'facility': 'reframe',
13611375
'data-version': '1.0'

reframe/core/logging.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ def _create_httpjson_handler(site_config, config_prefix):
473473
extras = site_config.get(f'{config_prefix}/extras')
474474
ignore_keys = site_config.get(f'{config_prefix}/ignore_keys')
475475
json_formatter = site_config.get(f'{config_prefix}/json_formatter')
476+
extra_headers = site_config.get(f'{config_prefix}/extra_headers')
476477
debug = site_config.get(f'{config_prefix}/debug')
477478

478479
parsed_url = urllib.parse.urlparse(url)
@@ -514,7 +515,8 @@ def _create_httpjson_handler(site_config, config_prefix):
514515
getlogger().warning('httpjson: running in debug mode; '
515516
'no data will be sent to the server')
516517

517-
return HTTPJSONHandler(url, extras, ignore_keys, json_formatter, debug)
518+
return HTTPJSONHandler(url, extras, ignore_keys, json_formatter,
519+
extra_headers, debug)
518520

519521

520522
def _record_to_json(record, extras, ignore_keys):
@@ -563,7 +565,8 @@ class HTTPJSONHandler(logging.Handler):
563565
}
564566

565567
def __init__(self, url, extras=None, ignore_keys=None,
566-
json_formatter=None, debug=False):
568+
json_formatter=None, extra_headers=None,
569+
debug=False):
567570
super().__init__()
568571
self._url = url
569572
self._extras = extras
@@ -581,6 +584,11 @@ def __init__(self, url, extras=None, ignore_keys=None,
581584
"it must be 'json_formatter(record, extras, ignore_keys)'"
582585
)
583586

587+
self._headers = {'Content-type': 'application/json',
588+
'Accept-Charset': 'UTF-8'}
589+
if extra_headers:
590+
self._headers.update(extra_headers)
591+
584592
self._debug = debug
585593

586594
def emit(self, record):
@@ -604,8 +612,7 @@ def emit(self, record):
604612
try:
605613
requests.post(
606614
self._url, data=json_record,
607-
headers={'Content-type': 'application/json',
608-
'Accept-Charset': 'UTF-8'}
615+
headers=self._headers
609616
)
610617
except requests.exceptions.RequestException as e:
611618
raise LoggingError('logging failed') from e

reframe/schemas/config.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
"items": {"type": "string"}
167167
},
168168
"json_formatter": {},
169+
"extra_headers": {"type": "object"},
169170
"debug": {"type": "boolean"}
170171
},
171172
"required": ["url"]
@@ -571,6 +572,7 @@
571572
"logging/handlers*/httpjson_extras": {},
572573
"logging/handlers*/httpjson_ignore_keys": [],
573574
"logging/handlers*/httpjson_json_formatter": null,
575+
"logging/handlers*/httpjson_extra_headers": {},
574576
"logging/handlers*/httpjson_debug": false,
575577
"logging/handlers*/stream_name": "stdout",
576578
"logging/handlers*/syslog_socktype": "udp",

0 commit comments

Comments
 (0)