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/12/09 03:50:12 UTC

[skywalking-python] branch master updated: make @trace decorator support async functions (#92)

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 3890372  make @trace decorator support async functions (#92)
3890372 is described below

commit 3890372ed76c004ad15f479df9677bd6320a6210
Author: taskmgr <ta...@users.noreply.github.com>
AuthorDate: Wed Dec 9 11:50:04 2020 +0800

    make @trace decorator support async functions (#92)
    
    Co-authored-by: Zhenxu Ke <ke...@apache.org>
---
 skywalking/decorators.py | 46 +++++++++++++++++++++++++++++++---------------
 1 file changed, 31 insertions(+), 15 deletions(-)

diff --git a/skywalking/decorators.py b/skywalking/decorators.py
index a48e057..444c7a5 100644
--- a/skywalking/decorators.py
+++ b/skywalking/decorators.py
@@ -17,6 +17,7 @@
 
 from functools import wraps
 from typing import List
+import inspect
 
 from skywalking import Layer, Component
 from skywalking.trace.context import get_context
@@ -30,22 +31,37 @@ def trace(
         tags: List[Tag] = None,
 ):
     def decorator(func):
-        @wraps(func)
-        def wrapper(*args, **kwargs):
-            _op = op or func.__name__
-            context = get_context()
-            with context.new_local_span(op=_op) as span:
-                span.layer = layer
-                span.component = component
-                [span.tag(tag) for tag in tags or []]
-                try:
-                    result = func(*args, **kwargs)
-                    return result
-                except Exception:
-                    span.raised()
-                    raise
+        _op = op or func.__name__
+        context = get_context()
+        if inspect.iscoroutinefunction(func):
+            @wraps(func)
+            async def wrapper(*args, **kwargs):
+                with context.new_local_span(op=_op) as span:
+                    span.layer = layer
+                    span.component = component
+                    [span.tag(tag) for tag in tags or []]
+                    try:
+                        result = func(*args, **kwargs)
+                        return await result
+                    except Exception:
+                        span.raised()
+                        raise
+            return wrapper
 
-        return wrapper
+        else:
+            @wraps(func)
+            def wrapper(*args, **kwargs):
+                with context.new_local_span(op=_op) as span:
+                    span.layer = layer
+                    span.component = component
+                    [span.tag(tag) for tag in tags or []]
+                    try:
+                        result = func(*args, **kwargs)
+                        return result
+                    except Exception:
+                        span.raised()
+                        raise
+            return wrapper
 
     return decorator