Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/config_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1356,6 +1369,7 @@ 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'
Expand Down
15 changes: 11 additions & 4 deletions reframe/core/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -514,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, debug)
return HTTPJSONHandler(url, extras, ignore_keys, json_formatter,
extra_headers, debug)


def _record_to_json(record, extras, ignore_keys):
Expand Down Expand Up @@ -563,7 +565,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
Expand All @@ -581,6 +584,11 @@ 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):
Expand All @@ -604,8 +612,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
Expand Down
2 changes: 2 additions & 0 deletions reframe/schemas/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
"items": {"type": "string"}
},
"json_formatter": {},
"extra_headers": {"type": "object"},
"debug": {"type": "boolean"}
},
"required": ["url"]
Expand Down Expand Up @@ -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",
Expand Down