Skip to content

Commit

Permalink
further optimize conf.settings access when logging is enabled
Browse files Browse the repository at this point in the history
the callback receiver is still fairly slow when logging is enabled due
to constant setting lookups; this speeds things up considerably

related: ansible#5618
  • Loading branch information
ryanpetrello committed Jan 22, 2020
1 parent 046518a commit aeb7a63
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
12 changes: 9 additions & 3 deletions awx/conf/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Python
from collections import namedtuple
import contextlib
import logging
import re
Expand Down Expand Up @@ -136,6 +135,15 @@ def filter_sensitive(registry, key, value):
return value


class TransientSetting(object):

__slots__ = ('pk', 'value')

def __init__(self, pk, value):
self.pk = pk
self.value = value


class EncryptedCacheProxy(object):

def __init__(self, cache, registry, encrypter=None, decrypter=None):
Expand Down Expand Up @@ -186,8 +194,6 @@ def set_many(self, data, **kwargs):
self.set(key, value, log=False, **kwargs)

def _handle_encryption(self, method, key, value):
TransientSetting = namedtuple('TransientSetting', ['pk', 'value'])

if value is not empty and self.registry.is_setting_encrypted(key):
# If the setting exists in the database, we'll use its primary key
# as part of the AES key when encrypting/decrypting
Expand Down
28 changes: 13 additions & 15 deletions awx/main/utils/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ def serialize(cls, message):

class LogstashFormatter(LogstashFormatterBase):

def __init__(self, *args, **kwargs):
self.cluster_host_id = settings.CLUSTER_HOST_ID
self.tower_uuid = None
uuid = (
getattr(settings, 'LOG_AGGREGATOR_TOWER_UUID', None) or
getattr(settings, 'INSTALL_UUID', None)
)
if uuid:
self.tower_uuid = uuid
super(LogstashFormatter, self).__init__(*args, **kwargs)

def reformat_data_for_log(self, raw_data, kind=None):
'''
Process dictionaries from various contexts (job events, activity stream
Expand Down Expand Up @@ -231,21 +242,8 @@ def get_extra_fields(self, record):
log_kind = record.name[len('awx.analytics.'):]
fields = self.reformat_data_for_log(fields, kind=log_kind)
# General AWX metadata
for log_name, setting_name in [
('type', 'LOG_AGGREGATOR_TYPE'),
('cluster_host_id', 'CLUSTER_HOST_ID'),
('tower_uuid', 'LOG_AGGREGATOR_TOWER_UUID')]:
if hasattr(settings, setting_name):
fields[log_name] = getattr(settings, setting_name, None)
elif log_name == 'type':
fields[log_name] = 'other'

uuid = (
getattr(settings, 'LOG_AGGREGATOR_TOWER_UUID', None) or
getattr(settings, 'INSTALL_UUID', None)
)
if uuid:
fields['tower_uuid'] = uuid
fields['cluster_host_id'] = self.cluster_host_id
fields['tower_uuid'] = self.tower_uuid
return fields

def format(self, record):
Expand Down
19 changes: 18 additions & 1 deletion awx/main/utils/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Python
import logging
import json
import os
import requests
import time
import threading
Expand All @@ -18,6 +19,7 @@

# requests futures, a dependency used by these handlers
from requests_futures.sessions import FuturesSession
import cachetools

# AWX
from awx.main.utils.formatters import LogstashFormatter
Expand Down Expand Up @@ -273,6 +275,16 @@ def __init__(self, *args, **kwargs):
}


TTLCache = cachetools.TTLCache

if 'py.test' in os.environ.get('_'):
# don't cache settings in unit tests
class TTLCache(TTLCache):

def __getitem__(self, item):
raise KeyError()


class AWXProxyHandler(logging.Handler):
'''
Handler specific to the AWX external logging feature
Expand Down Expand Up @@ -316,6 +328,7 @@ def serialize(cls, message):
def get_handler_class(self, protocol):
return HANDLER_MAPPING.get(protocol, AWXNullHandler)

@cachetools.cached(cache=TTLCache(maxsize=1, ttl=3), key=lambda *args, **kw: 'get_handler')
def get_handler(self, custom_settings=None, force_create=False):
new_kwargs = {}
use_settings = custom_settings or settings
Expand All @@ -342,10 +355,14 @@ def get_handler(self, custom_settings=None, force_create=False):
self._handler.setFormatter(self.formatter)
return self._handler

@cachetools.cached(cache=TTLCache(maxsize=1, ttl=3), key=lambda *args, **kw: 'should_audit')
def should_audit(self):
return settings.LOG_AGGREGATOR_AUDIT

def emit(self, record):
if AWXProxyHandler.thread_local.enabled:
actual_handler = self.get_handler()
if settings.LOG_AGGREGATOR_AUDIT:
if self.should_audit():
self.auditor.setLevel(settings.LOG_AGGREGATOR_LEVEL)
self.auditor.emit(record)
return actual_handler.emit(record)
Expand Down

0 comments on commit aeb7a63

Please sign in to comment.