From f7b139375205a341407cdae63275bb3c25529d15 Mon Sep 17 00:00:00 2001
From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com>
Date: Sun, 19 May 2024 21:10:17 +0200
Subject: [PATCH] [PR #8367/03240ad7 backport][stable-7] fix(opentelemetry):
 close span even if no logs are sent (#8388)

fix(opentelemetry): close span even if no logs are sent (#8367)

* fix(opentelemetry): close span even if no logs are sent

* changelog

(cherry picked from commit 03240ad7dc57aefc14b1ceba3e08abec0c62500b)

Co-authored-by: Victor Martinez <victormartinezrubio@gmail.com>
---
 changelogs/fragments/8367-fix-close-span-if-no-logs.yaml | 2 ++
 plugins/callback/opentelemetry.py                        | 3 ++-
 2 files changed, 4 insertions(+), 1 deletion(-)
 create mode 100644 changelogs/fragments/8367-fix-close-span-if-no-logs.yaml

diff --git a/changelogs/fragments/8367-fix-close-span-if-no-logs.yaml b/changelogs/fragments/8367-fix-close-span-if-no-logs.yaml
new file mode 100644
index 0000000000..e0a90be311
--- /dev/null
+++ b/changelogs/fragments/8367-fix-close-span-if-no-logs.yaml
@@ -0,0 +1,2 @@
+bugfixes:
+  - "opentelemetry callback plugin - close spans always (https://github.com/ansible-collections/community.general/pull/8367)."
diff --git a/plugins/callback/opentelemetry.py b/plugins/callback/opentelemetry.py
index 492e420716..bd34a364c1 100644
--- a/plugins/callback/opentelemetry.py
+++ b/plugins/callback/opentelemetry.py
@@ -350,7 +350,8 @@ class OpenTelemetrySource(object):
         if not disable_logs:
             # This will avoid populating span attributes to the logs
             span.add_event(task_data.dump, attributes={} if disable_attributes_in_logs else attributes)
-            span.end(end_time=host_data.finish)
+        # Close span always
+        span.end(end_time=host_data.finish)
 
     def set_span_attributes(self, span, attributes):
         """ update the span attributes with the given attributes if not None """