圆月山庄资源网 Design By www.vgjia.com
对于分布式追踪,主要有以下的几个概念:
- 追踪 Trace:就是由分布的微服务协作所支撑的一个事务。一个追踪,包含为该事务提供服务的各个服务请求。
- 跨度 Span:Span是事务中的一个工作流,一个Span包含了时间戳,日志和标签信息。Span之间包含父子关系,或者主从(Followup)关系。
- 跨度上下文 Span Context:跨度上下文是支撑分布式追踪的关键,它可以在调用的服务之间传递,上下文的内容包括诸如:从一个服务传递到另一个服务的时间,追踪的ID,Span的ID还有其它需要从上游服务传递到下游服务的信息。
我实现了一种简单的调用追踪。
import uuid import os import time l = [] class Recorder(object): def __init__(self,servername,root_span = None): if root_span == None: self.__span = Span(servername) else: self.__span = Span(servername,root_span) #上下文管理器 def __enter__(self): return self.__span # 退出方法中,用来实现善后处理工作 def __exit__(self, exc_type, exc_val, exc_tb): self.__span.record() self.__span.record_save(self.__span.span) class Span(object): def __init__(self,servername,root_span = None): self.servername = servername self.span = self.newspan() if root_span != None: root_span.dic['child_span'] = self.span self.span["root_span_flag"] = False def newspan(self): self.dic = { "spanid": uuid.uuid4().int, "servername": self.servername, "location": "", "ip": "", "durationtime": 0, "starttime": time.time(), "endtime":0, "tag": "", "log": "", "root_span_flag":True, "child_span": "" } return self.dic def record_save(self, span): currenttracer = {"id": span["spanid"], "data": span} print(currenttracer) l.append(currenttracer) def record(self): self.span["servername"] = self.servername self.span["location"] = os.getcwd() + "." + self.servername self.span["endtime"] = time.time() self.span["durationtime"] = self.span["endtime"] - self.span["starttime"] def setspantag(self,tag): self.span["tag"] = tag def setspanlog(self,log): self.span["log"] = log # 连续调用 with Recorder('server1') as span: time.sleep(1) span.setspantag("test") #调用server1方法 print("server1") with Recorder('server2',span) as span1: time.sleep(2) # 调用server2方法 print("server2") with Recorder('server3',span1) as span2: time.sleep(0.5) # 调用server3方法 print("server3") #单独调用记录 with Recorder('server4') as span_test1: time.sleep(1.5) print("server4") with Recorder('server5') as span_test2: time.sleep(2) print("server5")
运行结果:
server1 server2 server3 {'id': 224716339449765695394515303164364012192, 'data': {'spanid': 224716339449765695394515303164364012192, 'servername': 'server3', 'location': 'D:\\python_protest\\protest\\trace\\trace_main.server3', 'ip': '', 'durationtime': 0.5004403591156006, 'starttime': 1598947338.0551107, 'endtime': 1598947338.555551, 'tag': '', 'log': '', 'root_span_flag': False, 'child_span': None}} {'id': 254736847532758359233387151739984206570, 'data': {'spanid': 254736847532758359233387151739984206570, 'servername': 'server2', 'location': 'D:\\python_protest\\protest\\trace\\trace_main.server2', 'ip': '', 'durationtime': 2.501264810562134, 'starttime': 1598947336.0542862, 'endtime': 1598947338.555551, 'tag': '', 'log': '', 'root_span_flag': False, 'child_span': {'spanid': 224716339449765695394515303164364012192, 'servername': 'server3', 'location': 'D:\\python_protest\\protest\\trace\\trace_main.server3', 'ip': '', 'durationtime': 0.5004403591156006, 'starttime': 1598947338.0551107, 'endtime': 1598947338.555551, 'tag': '', 'log': '', 'root_span_flag': False, 'child_span': None}}} {'id': 91028031631192607088457781914309166266, 'data': {'spanid': 91028031631192607088457781914309166266, 'servername': 'server1', 'location': 'D:\\python_protest\\protest\\trace\\trace_main.server1', 'ip': '', 'durationtime': 3.5021069049835205, 'starttime': 1598947335.0534441, 'endtime': 1598947338.555551, 'tag': 'test', 'log': '', 'root_span_flag': True, 'child_span': {'spanid': 254736847532758359233387151739984206570, 'servername': 'server2', 'location': 'D:\\python_protest\\protest\\trace\\trace_main.server2', 'ip': '', 'durationtime': 2.501264810562134, 'starttime': 1598947336.0542862, 'endtime': 1598947338.555551, 'tag': '', 'log': '', 'root_span_flag': False, 'child_span': {'spanid': 224716339449765695394515303164364012192, 'servername': 'server3', 'location': 'D:\\python_protest\\protest\\trace\\trace_main.server3', 'ip': '', 'durationtime': 0.5004403591156006, 'starttime': 1598947338.0551107, 'endtime': 1598947338.555551, 'tag': '', 'log': '', 'root_span_flag': False, 'child_span': None}}}} server4 {'id': 103171729522922437998918618387133480096, 'data': {'spanid': 103171729522922437998918618387133480096, 'servername': 'server4', 'location': 'D:\\python_protest\\protest\\trace\\trace_main.server4', 'ip': '', 'durationtime': 1.5001769065856934, 'starttime': 1598947338.555551, 'endtime': 1598947340.055728, 'tag': '', 'log': '', 'root_span_flag': True, 'child_span': None}} server5 {'id': 320091321623887285825256878422834254741, 'data': {'spanid': 320091321623887285825256878422834254741, 'servername': 'server5', 'location': 'D:\\python_protest\\protest\\trace\\trace_main.server5', 'ip': '', 'durationtime': 2.0005736351013184, 'starttime': 1598947340.055728, 'endtime': 1598947342.0563016, 'tag': '', 'log': '', 'root_span_flag': True, 'child_span': None}}
关于下一步,会使用redis存储其结果,并进行相应的输出分析。
修过不能追踪同级调用的问题
import uuid import os import time l = [] class Recorder(object): def __init__(self,servername,root_span = None): if root_span == None: self.__span = Span(servername) else: self.__span = Span(servername,root_span) #上下文管理器 def __enter__(self): return self.__span # 退出方法中,用来实现善后处理工作 def __exit__(self, exc_type, exc_val, exc_tb): self.__span.record() self.__span.record_save(self.__span.span) class Span(object): def __init__(self,servername,root_span = None): self.servername = servername self.span = self.newspan() if root_span != None: root_span.dic['child_span'].append(self.span) self.span["root_span_flag"] = False def newspan(self): self.dic = { "spanid": uuid.uuid4().int, "servername": self.servername, "location": "", "ip": "", "durationtime": 0, "starttime": time.time(), "endtime":0, "tag": "", "log": "", "root_span_flag":True, "child_span": [] } return self.dic def record_save(self, span): currenttracer = {"id": span["spanid"], "data": span} print(currenttracer) l.append(currenttracer) def record(self): self.span["servername"] = self.servername self.span["location"] = os.getcwd() + "." + self.servername self.span["endtime"] = time.time() self.span["durationtime"] = self.span["endtime"] - self.span["starttime"] def setspantag(self,tag): self.span["tag"] = tag def setspanlog(self,log): self.span["log"] = log # 连续调用 with Recorder('server1') as span: time.sleep(1) span.setspantag("test") #调用server1方法 print("server1") with Recorder('server2',span) as span1: time.sleep(2) # 调用server2方法 print("server2") with Recorder('server3',span1) as span2: time.sleep(0.5) # 调用server3方法 print("server3") # 并级连续调用 # with Recorder('server1') as span1: # time.sleep(1) # span1.setspantag("test") # #调用server1方法 # print("server1") # with Recorder('server2',span1) as span2: # time.sleep(2) # # 调用server2方法 # print("server2") # with Recorder('server3',span1) as span3: # time.sleep(0.5) # # 调用server3方法 # print("server3") # #单独调用记录 # with Recorder('server4') as span_test1: # time.sleep(1.5) # print("server4") # # with Recorder('server5') as span_test2: # time.sleep(2) # print("server5")
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
标签:
Python,函数,调用追踪
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
更新日志
2024年11月01日
2024年11月01日
- 《暗喻幻想》顺风耳作用介绍
- 崔健1985-梦中的倾诉[再版][WAV+CUE]
- 黄子馨《追星Xin的恋人们2》HQ头版限量编号[WAV+CUE]
- 孟庭苇《情人的眼泪》开盘母带[低速原抓WAV+CUE]
- 孙露《谁为我停留HQCD》[低速原抓WAV+CUE][1.1G]
- 孙悦《时光音乐会》纯银CD[低速原抓WAV+CUE][1.1G]
- 任然《渐晚》[FLAC/分轨][72.32MB]
- 英雄联盟新英雄安蓓萨上线了吗 新英雄安蓓萨技能介绍
- 魔兽世界奥杜尔竞速赛什么时候开启 奥杜尔竞速赛开启时间介绍
- 无畏契约CGRS准星代码多少 CGRS准星代码分享一览
- 张靓颖.2012-倾听【少城时代】【WAV+CUE】
- 游鸿明.1999-五月的雪【大宇国际】【WAV+CUE】
- 曹方.2005-遇见我【钛友文化】【WAV+CUE】
- Unity6引擎上线:稳定性提升、CPU性能最高提升4倍
- 人皇Sky今日举行婚礼!电竞传奇步入新篇章