From 967607939b68e56e768673730869bea13e21b5ef Mon Sep 17 00:00:00 2001 From: Chirica Gheorghe Date: Wed, 20 Oct 2021 23:55:29 +0300 Subject: [PATCH 1/2] Add connection_class as input param --- elasticecslogging/handlers.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/elasticecslogging/handlers.py b/elasticecslogging/handlers.py index 0e2f653..f044c59 100644 --- a/elasticecslogging/handlers.py +++ b/elasticecslogging/handlers.py @@ -82,6 +82,7 @@ class IndexNameFrequency(Enum): __DEFAULT_ADDITIONAL_FIELDS_IN_ENV = {} __DEFAULT_ES_INDEX_NAME = 'python_logger' __DEFAULT_RAISE_ON_EXCEPTION = False + __DEFAULT_CONNECTION_CLASS = RequestsHttpConnection __LOGGING_FILTER_FIELDS = ['msecs', 'relativeCreated', @@ -158,7 +159,9 @@ def __init__(self, index_name_frequency=__DEFAULT_INDEX_FREQUENCY, es_additional_fields=__DEFAULT_ADDITIONAL_FIELDS, es_additional_fields_in_env=__DEFAULT_ADDITIONAL_FIELDS_IN_ENV, - raise_on_indexing_exceptions=__DEFAULT_RAISE_ON_EXCEPTION): + raise_on_indexing_exceptions=__DEFAULT_RAISE_ON_EXCEPTION, + connection_class=__DEFAULT_CONNECTION_CLASS + ): """ Handler constructor :param hosts: The list of hosts that elasticsearch clients will connect. The list can be provided @@ -200,6 +203,7 @@ def __init__(self, no value for the field. :param raise_on_indexing_exceptions: A boolean, True only for debugging purposes to raise exceptions caused when + :param connection_class: A class, used by default to make http connections :return: A ready to be used ElasticECSHandler. """ logging.Handler.__init__(self) @@ -226,6 +230,8 @@ def __init__(self, self.es_additional_fields = copy.deepcopy(es_additional_fields.copy()) self.es_additional_fields.setdefault('ecs', {})['version'] = ElasticECSHandler.__ECS_VERSION + self.connection_class = connection_class + agent_dict = self.es_additional_fields.setdefault('agent', {}) agent_dict['ephemeral_id'] = uuid.uuid4() agent_dict['type'] = ElasticECSHandler.__AGENT_TYPE @@ -261,7 +267,7 @@ def __get_es_client(self): self._client = Elasticsearch(hosts=self.hosts, use_ssl=self.use_ssl, verify_certs=self.verify_certs, - connection_class=RequestsHttpConnection, + connection_class=self.connection_class, serializer=self.serializer) return self._client @@ -271,7 +277,7 @@ def __get_es_client(self): http_auth=self.auth_details, use_ssl=self.use_ssl, verify_certs=self.verify_certs, - connection_class=RequestsHttpConnection, + connection_class=self.connection_class, serializer=self.serializer) return self._client @@ -282,7 +288,7 @@ def __get_es_client(self): return Elasticsearch(hosts=self.hosts, use_ssl=self.use_ssl, verify_certs=self.verify_certs, - connection_class=RequestsHttpConnection, + connection_class=self.connection_class, http_auth=HTTPKerberosAuth(mutual_authentication=DISABLED), serializer=self.serializer) @@ -296,7 +302,7 @@ def __get_es_client(self): http_auth=awsauth, use_ssl=self.use_ssl, verify_certs=True, - connection_class=RequestsHttpConnection, + connection_class=self.connection_class, serializer=self.serializer ) return self._client From 1c38699c441e1c5e116f200f3c30d1b75d0c29e5 Mon Sep 17 00:00:00 2001 From: Chirica Gheorghe Date: Sat, 23 Oct 2021 20:49:59 +0300 Subject: [PATCH 2/2] Fix exc_info --- elasticecslogging/handlers.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/elasticecslogging/handlers.py b/elasticecslogging/handlers.py index f044c59..1f86c54 100644 --- a/elasticecslogging/handlers.py +++ b/elasticecslogging/handlers.py @@ -444,10 +444,12 @@ def _log_record_to_ecs_fields(self, log_record): exc_info = log_record_dict.pop('exc_info') if exc_info: exc_type, exc_value, traceback_object = exc_info + name = exc_type.__name__ if exc_type else None + es_record['error'] = { - 'code': exc_type.__name__, + 'code': name, 'id': uuid.uuid4(), - 'type': exc_type.__name__, + 'type': name, 'message': str(exc_value), 'stack_trace': "".join(traceback.format_exception(exc_type, exc_value, traceback_object)) }