Skip to content

Add connection_class as input param #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 15 additions & 7 deletions elasticecslogging/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -438,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))
}
Expand Down