1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

[PR #7517/8d886b42 backport][stable-7] Close elastic APM client to release connections (#7528)

Close elastic APM client to release connections (#7517)

* Close elastic APM client to release connections

* Changelog fragment

(cherry picked from commit 8d886b42ec)

Co-authored-by: Iuri de Silvio <iurisilvio@gmail.com>
This commit is contained in:
patchback[bot] 2023-11-15 22:28:02 +01:00 committed by GitHub
parent 1f94bd4a17
commit 9a6f7c5c3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 16 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- elastic callback plugin - close elastic client to not leak resources (https://github.com/ansible-collections/community.general/pull/7517).

View file

@ -84,6 +84,7 @@ import time
import uuid import uuid
from collections import OrderedDict from collections import OrderedDict
from contextlib import closing
from os.path import basename from os.path import basename
from ansible.errors import AnsibleError, AnsibleRuntimeError from ansible.errors import AnsibleError, AnsibleRuntimeError
@ -201,24 +202,25 @@ class ElasticSource(object):
apm_cli = self.init_apm_client(apm_server_url, apm_service_name, apm_verify_server_cert, apm_secret_token, apm_api_key) apm_cli = self.init_apm_client(apm_server_url, apm_service_name, apm_verify_server_cert, apm_secret_token, apm_api_key)
if apm_cli: if apm_cli:
instrument() # Only call this once, as early as possible. with closing(apm_cli):
if traceparent: instrument() # Only call this once, as early as possible.
parent = trace_parent_from_string(traceparent) if traceparent:
apm_cli.begin_transaction("Session", trace_parent=parent, start=parent_start_time) parent = trace_parent_from_string(traceparent)
else: apm_cli.begin_transaction("Session", trace_parent=parent, start=parent_start_time)
apm_cli.begin_transaction("Session", start=parent_start_time) else:
# Populate trace metadata attributes apm_cli.begin_transaction("Session", start=parent_start_time)
if self.ansible_version is not None: # Populate trace metadata attributes
label(ansible_version=self.ansible_version) if self.ansible_version is not None:
label(ansible_session=self.session, ansible_host_name=self.host, ansible_host_user=self.user) label(ansible_version=self.ansible_version)
if self.ip_address is not None: label(ansible_session=self.session, ansible_host_name=self.host, ansible_host_user=self.user)
label(ansible_host_ip=self.ip_address) if self.ip_address is not None:
label(ansible_host_ip=self.ip_address)
for task_data in tasks: for task_data in tasks:
for host_uuid, host_data in task_data.host_data.items(): for host_uuid, host_data in task_data.host_data.items():
self.create_span_data(apm_cli, task_data, host_data) self.create_span_data(apm_cli, task_data, host_data)
apm_cli.end_transaction(name=__name__, result=status, duration=end_time - parent_start_time) apm_cli.end_transaction(name=__name__, result=status, duration=end_time - parent_start_time)
def create_span_data(self, apm_cli, task_data, host_data): def create_span_data(self, apm_cli, task_data, host_data):
""" create the span with the given TaskData and HostData """ """ create the span with the given TaskData and HostData """