一、代码

import os
import random
import logging import sentry_sdk
from sentry_sdk.tracing import Transaction
from sentry_sdk.integrations.logging import LoggingIntegration
from sentry_sdk.integrations.threading import ThreadingIntegration
from sentry_sdk.integrations.argv import ArgvIntegration
from sentry_sdk.integrations.excepthook import ExcepthookIntegration
from sentry_sdk.integrations.dedupe import DedupeIntegration
from sentry_sdk.integrations.atexit import AtexitIntegration sentry_logging = LoggingIntegration(
level=logging.INFO, # Capture info and above as breadcrumbs
event_level=logging.ERROR, # Send errors as events
)
# 初始化操作===================================================================================
sentry_sdk_url = "http://758ff93e86034309877973d6fea02272@localhost:9000/2"
sentry_sdk.init(
dsn=sentry_sdk_url,
environment='dev',
integrations=[
AtexitIntegration(),
DedupeIntegration(),
ExcepthookIntegration(),
# ModulesIntegration(),
# StdlibIntegration(),
ThreadingIntegration(),
ArgvIntegration(),
sentry_logging,
],
# default_integrations=False,
# auto_enabling_integrations=False,
traces_sample_rate=1.0,
# #
# # 个人身份信息
# send_default_pii=True,
# release='1.1.0',
) def get_trace_headers(name: str = None) -> dict:
t = sentry_sdk.Hub.current.scope.transaction
if not t:
t = sentry_sdk.start_transaction()
t.name = "workers-pipeline" if not name else name
t.sampled = True
# log.debug(f"Start new sentry trace transaction.")
print("Start new sentry trace transaction.")
headers = dict(sentry_sdk.Hub.current.iter_trace_propagation_headers(t))
# log.debug(f"Sentry-trace: {headers}")
print(f"Sentry-trace: {headers}")
return headers def set_trace_scope_tags(**kwargs):
"""设置追踪标签"""
if kwargs:
# noinspection PyBroadException
try:
nsrsbh = kwargs.get("nsrsbh", "")
nsrmc = kwargs.get("nsrmc", "") if not nsrmc:
nsrmc = nsrsbh
with sentry_sdk.configure_scope() as scope:
scope.set_user({"id": nsrsbh, "username": nsrmc})
for k, v in kwargs.items():
scope.set_tag(k, v)
sentry_sdk.set_tag(k, v)
except Exception: # pylint: disable=broad-except
return # 1、生成跟踪对象=================================================================================================
trace = get_trace_headers()
transaction = Transaction.continue_from_headers(trace)
trace_id = transaction.trace_id
task_id = random.randint(10000, 99999)
print(task_id)
print(trace_id)
# 2、设置全局参数=========================================================================================== d = {
"version": "1.0.0",
"name": "start pipe",
"host_ip": "localhost",
"port": "8000",
"pid": os.getpid(),
"task_id": task_id,
"trace_id": trace_id,
}
set_trace_scope_tags(**d)
# 3、开始收集
with sentry_sdk.start_transaction(transaction) as t:
t.name = "start task"
t.op = "start workers"
t.description = "start"
t.sampled = True
t.set_tag("task_id", task_id)
t.set_tag("trace_id", trace_id)
try:
# 任务
print(1)
except:
t.set_status("failure")
t.set_status("ok")
# 子记录 child============================================================================
trace_headers = dict(sentry_sdk.Hub.current.iter_trace_propagation_headers(transaction))
transaction = Transaction.continue_from_headers(trace_headers)
with sentry_sdk.start_transaction(transaction) as t:
t.name = "run task"
t.op = "run workers"
t.description = "run"
t.sampled = True
t.set_tag("task_id", task_id)
t.set_tag("trace_id", trace_id)
t.set_status("failure")
t.set_status("ok")
# 第二个记录 和 第一个记录平行关系,只需要绑定 trace_id====================================================================
with sentry_sdk.start_transaction(name="task parallel", op="parallel workers", trace_id=trace_id, sampled=True) as t:
t.name = "task parallel"
t.op = "workers"
t.set_tag("task_id", task_id)
try:
print(1) # 任务
except:
t.set_status("failure")
t.set_status("ok")
# 子记录2=================================================
trace_headers = dict(sentry_sdk.Hub.current.iter_trace_propagation_headers(t))
transaction = Transaction.continue_from_headers(trace_headers)
with sentry_sdk.start_transaction(transaction) as child_t:
child_t.name = "task child parallel"
child_t.op = "child workers"
child_t.set_tag("task_id", task_id)
try:
print(1)
with child_t.start_child(op="child", description="child1") as c:
c.set_tag("task_id", task_id)
with c.start_child(op="child-child", description="child2") as cc:
cc.set_tag("task_id", task_id)
except:
child_t.set_status("failure")
child_t.set_status("ok")

二、结果



最新文章

  1. 导出数据到Excel --使用ExcelReport有感
  2. vs code插件记录
  3. VSS每次打开都需要服务器账号和密码的解决方法
  4. debuggee python
  5. [Effective JavaScript 笔记]第43条:使用Object的直接实例构造轻量级的字典
  6. cmd界面的编码如何改为utf8
  7. 王立平--EditPlus激活码
  8. 小白鼓捣GIT的心得
  9. 浅尝key-value数据库(三)——MongoDB的分布式
  10. Linux下给mysql创建用户分配权限
  11. [CQOI2009]dance跳舞
  12. Oracle R12 多组织访问的控制 - MOAC(Multi-Org Access Control)
  13. pymongo的操作
  14. Django View 进阶
  15. 最邻近规则分类(K-Nearest Neighbor)KNN算法
  16. bower配置私服nexus3
  17. Android App组件之Fragment说明和示例
  18. asp.net正则匹配嵌套Html标签
  19. WPF SourceInitialized 事件
  20. 双系统IOS\windows7 换成Windows10后果

热门文章

  1. Jmeter 之连接数据库
  2. js 非空判断
  3. Python实验报告(第8章)
  4. Python二维码扫描
  5. VMware Workstation Pro 16安装CentOS7超详细图文步骤
  6. python之路24之 面向对象动静态方法、继承、派生
  7. [Untiy]贪吃蛇大作战(五)——游戏主界面
  8. AIGC 很火,想微调个自己的模型试试看?(不是卖课的)
  9. cordova完整版本创建、修改自定义插件重新调试步骤带截图
  10. 华为云服务器8000通道映射到本地,本地浏览器访问jupyter