未验证 提交 1c26c13e 编写于 作者: H huawei 提交者: GitHub

[Core][Feature] Add cross thread propagation (#62)

上级 4f7b2510
......@@ -86,7 +86,7 @@ with context.new_local_span(op='https://github.com/apache') as span:
from time import sleep
from skywalking import Component
from skywalking.decorators import trace
from skywalking.decorators import trace, runnable
from skywalking.trace.context import SpanContext, get_context
@trace() # the operation name is the method name('some_other_method') by default
......@@ -99,6 +99,15 @@ def some_method():
some_other_method()
@runnable() # cross thread propagation
def some_method():
some_other_method()
from threading import Thread
t = Thread(target=some_method)
t.start()
context: SpanContext = get_context()
with context.new_entry_span(op=str('https://github.com/apache/skywalking')) as span:
span.component = Component.Flask
......
......@@ -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
......@@ -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")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册