Python函数调用追踪实现代码


Posted in Python onNovember 27, 2020

对于分布式追踪,主要有以下的几个概念:

  • 追踪 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 相关文章推荐
Python中使用第三方库xlutils来追加写入Excel文件示例
Apr 05 Python
Python使用functools模块中的partial函数生成偏函数
Jul 02 Python
利用Python脚本生成sitemap.xml的实现方法
Jan 31 Python
django创建自定义模板处理器的实例详解
Aug 14 Python
Python cookbook(数据结构与算法)从任意长度的可迭代对象中分解元素操作示例
Feb 13 Python
解决pycharm无法调用pip安装的包问题
May 18 Python
Python判断两个文件是否相同与两个文本进行相同项筛选的方法
Mar 01 Python
用Python配平化学方程式的方法
Jul 20 Python
Python调用C语言的实现
Jul 26 Python
深入了解python中元类的相关知识
Aug 29 Python
15行Python代码实现免费发送手机短信推送消息功能
Feb 27 Python
python从PDF中提取数据的示例
Oct 30 Python
Numpy中np.max的用法及np.maximum区别
Nov 27 #Python
python 实现aes256加密
Nov 27 #Python
pandas使用函数批量处理数据(map、apply、applymap)
Nov 27 #Python
python3.7中安装paddleocr及paddlepaddle包的多种方法
Nov 27 #Python
Python基于template实现字符串替换
Nov 27 #Python
使用Python画了一棵圣诞树的实例代码
Nov 27 #Python
python 用opencv实现霍夫线变换
Nov 27 #Python
You might like
批量去除PHP文件中bom的PHP代码
2012/03/13 PHP
php轻量级的性能分析工具xhprof的安装使用
2015/08/12 PHP
PHP APP微信提现接口代码
2018/09/30 PHP
阿里对象存储OSS在laravel框架中的使用方法
2019/10/13 PHP
Thinkphp集成抖音SDK的实现方法
2020/04/28 PHP
经常用到的JavasScript事件的翻译
2007/04/09 Javascript
javascript在myeclipse中报错的解决方法
2013/10/29 Javascript
javascipt:filter过滤介绍及使用
2014/09/10 Javascript
javascript中attachEvent用法实例分析
2015/05/14 Javascript
javascript实现的多个层切换效果通用函数实例
2015/07/06 Javascript
Vue的移动端多图上传插件vue-easy-uploader的示例代码
2017/11/27 Javascript
vue.js使用v-model指令实现的数据双向绑定功能示例
2018/05/22 Javascript
Vue filter格式化时间戳时间成标准日期格式的方法
2018/09/16 Javascript
javascript触发模拟鼠标点击事件
2019/06/26 Javascript
微信小程序实现电影App导航和轮播
2020/11/30 Javascript
详解uniapp的全局变量实现方式
2021/01/11 Javascript
用python实现批量重命名文件的代码
2012/05/25 Python
老生常谈Python进阶之装饰器
2017/05/11 Python
Python栈算法的实现与简单应用示例
2017/11/01 Python
基于python 凸包问题的解决
2020/04/16 Python
解决PyCharm无法使用lxml库的问题(图解)
2020/12/22 Python
发现两个有趣的CSS3动画效果
2013/08/14 HTML / CSS
HTML5 Canvas实现玫瑰曲线和心形图案的代码实例
2014/04/10 HTML / CSS
HTML5如何实现元素拖拽
2016/03/11 HTML / CSS
伦敦高达60%折扣的钻石珠宝商:Purely Diamonds
2018/06/24 全球购物
海量信息软件测试笔试题
2015/08/08 面试题
医务人员自我评价
2014/01/26 职场文书
消防安全承诺书
2014/05/22 职场文书
学校群众路线专项整治方案
2014/10/31 职场文书
二手车交易协议书标准版
2014/11/16 职场文书
2015年路政工作总结
2015/05/22 职场文书
婚庆主持词大全
2015/06/30 职场文书
2015年教师节主持词
2015/07/03 职场文书
在 Golang 中实现 Cache::remember 方法详解
2021/03/30 Python
解决Golang中ResponseWriter的一个坑
2021/04/27 Golang
Django数据库(SQlite)基本入门使用教程
2022/07/07 Python