This is an automated email from the ASF dual-hosted git repository.

yihaochen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-python.git


The following commit(s) were added to refs/heads/master by this push:
     new 788182f  Fix unexpected 'No active span' IllegalStateError (#311)
788182f is described below

commit 788182f61e5934647ac5754f0e2af80e1a25461d
Author: ZEAL <zea...@gmail.com>
AuthorDate: Tue Jun 13 14:00:46 2023 +0800

    Fix unexpected 'No active span' IllegalStateError (#311)
    
    Fix unexpected 'No active span' IllegalStateError (#311)
---
 CHANGELOG.md                    |  3 +++
 skywalking/log/sw_logging.py    |  7 +++++--
 skywalking/plugins/sw_loguru.py |  7 +++++--
 skywalking/trace/context.py     | 14 ++++++++------
 4 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index ea12c56..4f39774 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,9 @@
 
 ### 1.1.0
 
+- Fixes:
+  - Fix unexpected 'No active span' IllegalStateError (#311)
+ 
 ### 1.0.1
 
 - Feature:
diff --git a/skywalking/log/sw_logging.py b/skywalking/log/sw_logging.py
index 1c88aea..801befe 100644
--- a/skywalking/log/sw_logging.py
+++ b/skywalking/log/sw_logging.py
@@ -83,8 +83,11 @@ def install():
             # exceptions before any span is even created, just ignore these 
fields and
             # avoid appending 'no active span' traceback that could be 
confusing.
             # Or simply the log is generated outside any span context.
-            active_span_id = context.active_span.sid
-            primary_endpoint_name = context.primary_endpoint.get_name()
+            # But actually NO need to raise and catch IllegalStateError here.
+            active_span = context.active_span
+            if active_span is not None:
+                active_span_id = active_span.sid
+                primary_endpoint_name = context.primary_endpoint.get_name() if 
context.primary_endpoint else ''
         except IllegalStateError:
             pass
 
diff --git a/skywalking/plugins/sw_loguru.py b/skywalking/plugins/sw_loguru.py
index 8528b17..72d0857 100644
--- a/skywalking/plugins/sw_loguru.py
+++ b/skywalking/plugins/sw_loguru.py
@@ -79,8 +79,11 @@ def install():
             # exceptions before any span is even created, just ignore these 
fields and
             # avoid appending 'no active span' traceback that could be 
confusing.
             # Or simply the log is generated outside any span context.
-            active_span_id = context.active_span.sid
-            primary_endpoint_name = context.primary_endpoint.get_name()
+            # But actually NO need to raise and catch IllegalStateError here.
+            active_span = context.active_span
+            if active_span is not None:
+                active_span_id = active_span.sid
+                primary_endpoint_name = context.primary_endpoint.get_name() if 
context.primary_endpoint else ''
         except IllegalStateError:
             pass
 
diff --git a/skywalking/trace/context.py b/skywalking/trace/context.py
index 4b9e796..cf79d0d 100644
--- a/skywalking/trace/context.py
+++ b/skywalking/trace/context.py
@@ -227,16 +227,18 @@ class SpanContext:
         return False
 
     @staticmethod
-    def peek() -> Optional[Span]:
+    def peek(raise_if_none: bool = False) -> Optional[Span]:
         spans = _spans()
-        return spans[-1] if spans else None
+        if not spans:
+            if raise_if_none:
+                raise IllegalStateError('No active span')
+            else:
+                return None
+        return spans[-1]
 
     @property
     def active_span(self):
-        active_span = self.peek()
-        if not active_span:
-            raise IllegalStateError('No active span')
-        return active_span
+        return self.peek(raise_if_none=False)
 
     def get_correlation(self, key):
         if key in self._correlation:

Reply via email to