You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by al...@apache.org on 2020/08/08 07:16:49 UTC

[skywalking-python] 01/01: Add thread propagation

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

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

commit bb9479f6158b91d69511cf95efc856df87624d67
Author: huawei <hu...@bit-s.cn>
AuthorDate: Sat Aug 8 15:12:43 2020 +0800

    Add thread propagation
---
 skywalking/decorators.py                   | 27 +++++++++++++++++++++++++++
 tests/plugin/sw_flask/services/consumer.py | 13 +++++--------
 2 files changed, 32 insertions(+), 8 deletions(-)

diff --git a/skywalking/decorators.py b/skywalking/decorators.py
index bc3c92d..686fa09 100644
--- a/skywalking/decorators.py
+++ b/skywalking/decorators.py
@@ -44,3 +44,30 @@ def trace(
         return wrapper
 
     return decorator
+
+
+def runnable(
+        op: str = None,
+        layer: Layer = Layer.Unknown,
+        component: Component = Component.Unknown,
+):
+    def decorator(func):
+        snapshot = get_context().capture()
+
+        @wraps(func)
+        def wrapper(*args, **kwargs):
+            _op = op or "Thread/"+func.__name__
+            context = get_context()
+            with context.new_local_span(op=_op) as span:
+                context.continued(snapshot)
+                span.layer = layer
+                span.component = component
+                try:
+                    func(*args, **kwargs)
+                except Exception:
+                    span.raised()
+                    raise
+
+        return wrapper
+
+    return decorator
\ No newline at end of file
diff --git a/tests/plugin/sw_flask/services/consumer.py b/tests/plugin/sw_flask/services/consumer.py
index af6d078..9ef590d 100644
--- a/tests/plugin/sw_flask/services/consumer.py
+++ b/tests/plugin/sw_flask/services/consumer.py
@@ -18,6 +18,7 @@
 import requests
 
 from skywalking import agent, config
+from skywalking.decorators import runnable
 
 if __name__ == '__main__':
     config.service_name = 'consumer'
@@ -34,17 +35,13 @@ if __name__ == '__main__':
         from skywalking.trace.context import get_context
         get_context().put_correlation("correlation", "correlation")
 
-        def post(snap):
-            with get_context().new_local_span("/test"):
-                get_context().continued(snap)
-                requests.post("http://provider:9091/users")
-
-        snapshot = get_context().capture()
+        @runnable(op="/test")
+        def post():
+            requests.post("http://provider:9091/users")
 
         from threading import Thread
-        t = Thread(target=post, args=(snapshot,))
+        t = Thread(target=post)
         t.start()
-        t.join()
 
         res = requests.post("http://provider:9091/users")