You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ke...@apache.org on 2020/11/24 06:29:19 UTC
[skywalking-python] branch master updated: [Enhancement] Async task
context support (#88)
This is an automated email from the ASF dual-hosted git repository.
kezhenxu94 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 28cfe9b [Enhancement] Async task context support (#88)
28cfe9b is described below
commit 28cfe9b9d76ff078a69b94cf004cfa15ebafa161
Author: Tomasz Pytel <to...@gmail.com>
AuthorDate: Tue Nov 24 03:29:12 2020 -0300
[Enhancement] Async task context support (#88)
---
skywalking/agent/protocol/grpc.py | 3 ++-
skywalking/trace/context.py | 45 ++++++++++++++++++++++++++++++---------
2 files changed, 37 insertions(+), 11 deletions(-)
diff --git a/skywalking/agent/protocol/grpc.py b/skywalking/agent/protocol/grpc.py
index ddee75a..0a7da16 100644
--- a/skywalking/agent/protocol/grpc.py
+++ b/skywalking/agent/protocol/grpc.py
@@ -15,6 +15,7 @@
# limitations under the License.
#
+import logging
from skywalking.loggings import logger
import traceback
from queue import Queue
@@ -62,7 +63,7 @@ class GrpcProtocol(Protocol):
return self.state == grpc.ChannelConnectivity.READY
def on_error(self):
- traceback.print_exc()
+ traceback.print_exc() if logger.isEnabledFor(logging.DEBUG) else None
self.channel.unsubscribe(self._cb)
self.channel.subscribe(self._cb, try_to_connect=True)
diff --git a/skywalking/trace/context.py b/skywalking/trace/context.py
index 61b0635..9f73bf7 100644
--- a/skywalking/trace/context.py
+++ b/skywalking/trace/context.py
@@ -15,7 +15,6 @@
# limitations under the License.
#
-import threading
from typing import List
from skywalking import agent, config
@@ -111,12 +110,13 @@ class SpanContext(object):
self.spans.append(span)
def stop(self, span: Span) -> bool:
- assert span is self.spans[-1]
+ idx = self.spans.index(span) # span SHOULD always be at end but in async-world it MAY not be, so don't crash
- span.finish(self.segment) and self.spans.pop()
+ if span.finish(self.segment):
+ del self.spans[idx]
if len(self.spans) == 0:
- _thread_local.context = None
+ _local().context = None
agent.archive(self.segment)
return len(self.spans) == 0
@@ -212,13 +212,38 @@ class NoopContext(SpanContext):
self._correlation.update(snapshot.correlation)
-_thread_local = threading.local()
-_thread_local.context = None
+try: # attempt to use async-local instead of thread-local context
+ import contextvars
+
+ __local = contextvars.ContextVar('local')
+
+ class AsyncLocal:
+ pass
+
+ def _local():
+ try:
+ return __local.get()
+
+ except LookupError:
+ local = AsyncLocal()
+ __local.set(local)
+
+ return local
+
+except ImportError:
+ import threading
+
+ __local = threading.local()
+
+ def _local():
+ return __local
def get_context() -> SpanContext:
- if not hasattr(_thread_local, 'context'):
- _thread_local.context = None
- _thread_local.context = _thread_local.context or (SpanContext() if agent.connected() else NoopContext())
+ local = _local()
+ context = getattr(local, 'context', False)
+
+ if not context:
+ context = local.context = (SpanContext() if agent.connected() else NoopContext())
- return _thread_local.context
+ return context