From 0f80c4f14ee952c5c43d225a2fbd90129c6b4bb3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl>
Date: Wed, 20 May 2020 17:45:55 +0200
Subject: [PATCH] fix utils

---
 coolamqp/attaches/declarer.py  | 23 +++++++++++++++++------
 coolamqp/attaches/publisher.py |  4 ++--
 coolamqp/attaches/utils.py     |  3 +--
 3 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/coolamqp/attaches/declarer.py b/coolamqp/attaches/declarer.py
index 1e0cbdb..ebc579d 100644
--- a/coolamqp/attaches/declarer.py
+++ b/coolamqp/attaches/declarer.py
@@ -45,14 +45,23 @@ class Operation(object):
 
     def span_exception(self, exception):
         if self.parent_span is not None:
-            if self.processing_span is not None:
+            if self.enqueued_span is None:
                 from opentracing import tags, logs
-                self.processing_span.set_tag(tags.ERROR, True)
-                self.processing_span.log_kv({logs.EVENT: tags.ERROR,
+                self.enqueued_span.set_tag(tags.ERROR, True)
+                self.enqueued_span.log_kv({logs.EVENT: tags.ERROR,
                                              logs.ERROR_KIND: exception,
                                              logs.ERROR_OBJECT: exception})
-                self.processing_span.finish()
-                self.processing_span = None
+                self.enqueued_span.finish()
+                self.enqueued_span = None
+
+                if self.processing_span is not None:
+                    from opentracing import tags, logs
+                    self.processing_span.set_tag(tags.ERROR, True)
+                    self.processing_span.log_kv({logs.EVENT: tags.ERROR,
+                                                 logs.ERROR_KIND: exception,
+                                                 logs.ERROR_OBJECT: exception})
+                    self.processing_span.finish()
+                    self.processing_span = None
             if self.enqueued_span is not None:
                 self.enqueued_span.finish()
                 self.enqueued_span = None
@@ -73,10 +82,12 @@ class Operation(object):
             self.processing_span = self.declarer.cluster.tracer.start_span('Declaring',
                                                                            child_of=self.parent_span,
                                                                            references=follows_from(self.enqueued_span))
+            self.enqueued_span = None
 
     def span_finished(self):
-        if self.parent_span is None:
+        if self.processing_span is None:
             self.processing_span.finish()
+            self.processing_span = None
 
     def span_begin(self):
         if self.enqueued_span is not None:
diff --git a/coolamqp/attaches/publisher.py b/coolamqp/attaches/publisher.py
index 77cc6b9..09dd41c 100644
--- a/coolamqp/attaches/publisher.py
+++ b/coolamqp/attaches/publisher.py
@@ -212,8 +212,8 @@ class Publisher(Channeler, Synchronized):
 
             if not fut.set_running_or_notify_cancel():
                 if span_enqueued is not None:
-                    import opentracing
-                    span_enqueued.log_kv({opentracing.logs.EVENT: 'Cancelled'})
+                    from opentracing import logs
+                    span_enqueued.log_kv({logs.EVENT: 'Cancelled'})
                     span_enqueued.finish()
                     parent_span.finish()
                 continue  # cancelled
diff --git a/coolamqp/attaches/utils.py b/coolamqp/attaches/utils.py
index 5fe5686..e9a693c 100644
--- a/coolamqp/attaches/utils.py
+++ b/coolamqp/attaches/utils.py
@@ -135,8 +135,7 @@ class AtomicTagger(object):
                 i = len(self.tags) - 1  # start index
 
                 while i > 0:  # this will terminate at i=0
-                    if self.tags[i][
-                        0] > tag:  # this means we should insert it here...
+                    if self.tags[i][0] > tag:  # this means we should insert it here...
                         break
                     i -= 1  # previousl index
 
-- 
GitLab