add an API endpoint for testing external log aggregrator connectivity

see: #5164
This commit is contained in:
Ryan Petrello
2017-03-08 13:49:19 -05:00
parent 34990c304a
commit 8c1de7f109
6 changed files with 190 additions and 23 deletions

View File

@@ -5,6 +5,7 @@
import logging
import json
import requests
from requests.exceptions import RequestException
from copy import copy
# loggly
@@ -40,6 +41,10 @@ def unused_callback(sess, resp):
pass
class LoggingConnectivityException(Exception):
pass
class HTTPSNullHandler(logging.NullHandler):
"Placeholder null handler to allow loading without database access"
@@ -66,6 +71,31 @@ class BaseHTTPSHandler(logging.Handler):
kwargs[param] = getattr(settings, django_setting_name, None)
return cls(*args, **kwargs)
@classmethod
def perform_test(cls, settings):
"""
Tests logging connectivity for the current logging settings.
@raises LoggingConnectivityException
"""
handler = cls.from_django_settings(settings, async=True)
handler.enabled_flag = True
handler.setFormatter(LogstashFormatter(settings_module=settings))
logger = logging.getLogger(__file__)
fn, lno, func = logger.findCaller()
record = logger.makeRecord('awx', 10, fn, lno,
'Ansible Tower Connection Test', tuple(),
None, func)
futures = handler.emit(record)
for future in futures:
try:
resp = future.result()
if not resp.ok:
raise LoggingConnectivityException(
': '.join([str(resp.status_code), resp.reason or ''])
)
except RequestException as e:
raise LoggingConnectivityException(str(e))
def get_full_message(self, record):
if record.exc_info:
return '\n'.join(traceback.format_exception(*record.exc_info))