From 5732023aa2935bf483a62542dc6725527edec0fa Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Tue, 18 Oct 2022 08:16:11 +0100 Subject: [PATCH] [opentelemetry][callback] support opentelemetry-api 1.13 (#5342) * [opentelemetry][callback] support opentelemetry-api 1.13 * [opentelemetry][callback] changelog fragment * Update changelogs/fragments/5342-opentelemetry_bug_fix_opentelemetry-api-1.13.yml Co-authored-by: Felix Fontein * [opentelemetry-callback] refactor time_ns in a function * fix linting * change branch outside of the function Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> * [opentelemetry]: remove options from suggestion * Apply suggestions from code review Co-authored-by: Felix Fontein Co-authored-by: Felix Fontein Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> --- ...lemetry_bug_fix_opentelemetry-api-1.13.yml | 2 ++ plugins/callback/opentelemetry.py | 31 +++++++++++++------ 2 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 changelogs/fragments/5342-opentelemetry_bug_fix_opentelemetry-api-1.13.yml diff --git a/changelogs/fragments/5342-opentelemetry_bug_fix_opentelemetry-api-1.13.yml b/changelogs/fragments/5342-opentelemetry_bug_fix_opentelemetry-api-1.13.yml new file mode 100644 index 0000000000..e5fa7958ac --- /dev/null +++ b/changelogs/fragments/5342-opentelemetry_bug_fix_opentelemetry-api-1.13.yml @@ -0,0 +1,2 @@ +bugfixes: + - opentelemetry callback plugin - support opentelemetry-api 1.13.0 that removed support for ``_time_ns`` (https://github.com/ansible-collections/community.general/pull/5342). diff --git a/plugins/callback/opentelemetry.py b/plugins/callback/opentelemetry.py index e3584a7ae2..e00e1d71ad 100644 --- a/plugins/callback/opentelemetry.py +++ b/plugins/callback/opentelemetry.py @@ -121,13 +121,32 @@ try: from opentelemetry.sdk.trace.export import ( BatchSpanProcessor ) - from opentelemetry.util._time import _time_ns + + # Support for opentelemetry-api <= 1.12 + try: + from opentelemetry.util._time import _time_ns + except ImportError as imp_exc: + OTEL_LIBRARY_TIME_NS_ERROR = imp_exc + else: + OTEL_LIBRARY_TIME_NS_ERROR = None + except ImportError as imp_exc: OTEL_LIBRARY_IMPORT_ERROR = imp_exc + OTEL_LIBRARY_TIME_NS_ERROR = imp_exc else: OTEL_LIBRARY_IMPORT_ERROR = None +if sys.version_info >= (3, 7): + time_ns = time.time_ns +elif not OTEL_LIBRARY_TIME_NS_ERROR: + time_ns = _time_ns +else: + def time_ns(): + # Support versions older than 3.7 with opentelemetry-api > 1.12 + return int(time.time() * 1e9) + + class TaskData: """ Data about an individual task. @@ -139,10 +158,7 @@ class TaskData: self.path = path self.play = play self.host_data = OrderedDict() - if sys.version_info >= (3, 7): - self.start = time.time_ns() - else: - self.start = _time_ns() + self.start = time_ns() self.action = action self.args = args self.dump = None @@ -168,10 +184,7 @@ class HostData: self.name = name self.status = status self.result = result - if sys.version_info >= (3, 7): - self.finish = time.time_ns() - else: - self.finish = _time_ns() + self.finish = time_ns() class OpenTelemetrySource(object):