Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does python realize the filtering and alarm of the trace module of skywalking

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

python是怎么实现skywalking的trace模块过滤和报警,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

skywalking本身的报警功能,用起来视乎不是特别好用,目前想实现对skywalking的trace中的错误接口进行过滤并报警通知管理员和开发。所以自己就用python对skywalking做了二次数据清洗实现。项目方在了自己了github(https://github.com/shygit-dev/skywalking-cli-python)上了,有兴趣的同学可以做二次改造,共同学习。下面简单列出了代码内容:

sw-trace.py

#!/usr/bin/env python# _*_ coding: utf-8 _*_# Tile:# Author:shyimport requestsimport timeimport smtplibfrom email.mime.text import MIMETextimport redef interface_content_filter(trace_id): ''' 对详细日志内容(业务逻辑报错)进行过滤 :param trace_id: :return: 【1|0】 ''' url = "http://172.16.53.232:50001/query" params = { "trace_id": trace_id } detail_trace_id_log = requests.request(method="GET",url=url,params=params) detail_trace_id_log = detail_trace_id_log.text print(detail_trace_id_log) print(type(detail_trace_id_log)) with open("blackname_keyword_list","r",encoding="utf-8") as f: for line in f: print(line) result = re.search(line.strip(),detail_trace_id_log) print(result) if result != None: print("哥们匹配到日志黑名单关键字了:%s" % line) return 0 print("提示:%s不在关键字黑名单中" % trace_id) return 1def interface_filter(endpointName): """ 设置接口黑名单 :param endpointName: :return: 【1|0】 """ endpointName = re.sub("\(|\)",".",endpointName) with open("blackname_list","r",encoding="utf-8") as f: bn_list = f.read() match_result = re.search(endpointName.strip(),bn_list) if match_result == None: print("提示:接口不存在黑名单中") return 1 print("提示:接口在黑名单中") return 0def trace_erro_interface(start_time,end_time,sw_url,per_page_size,trace_detail_addr): """ skywalking trace功能对错误接口进行过滤,默认最大一次获取2000条数据,每分钟执行一次 :param start_time: :param end_time: :return: """ url = sw_url data = { "query": "query queryTraces($condition: TraceQueryCondition) {\n data: queryBasicTraces(condition: $condition) {\n traces {\n key: segmentId\n endpointNames\n duration\n start\n isError\n traceIds\n }\n total\n }}", "variables": { "condition": { "queryDuration": { "start": start_time, #"2021-12-07 1734" "end": end_time, "step": "MINUTE" }, "traceState": "ERROR", "paging": { "pageNum": 1, "pageSize": per_page_size, "needTotal": "true" }, "queryOrder": "BY_START_TIME" # "traceId": "b669d0069be84fce82261901de412e7c.430.16388637511348105" } } } result = requests.request(method="post",url=url,json=data) i = 0 # print(result.content) # print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(float("%s.%s" % (trace["start"][0:10],trace["start"][10:]))))) with open("mail.html","w",encoding="utf-8") as f: f.write('Title.t {border-right: 2px solid black;border-bottom: 2px solid black;}.t th,td {border-top: 2px solid black;border-left: 2px solid black;font-size: 10px;}最近15分钟统计:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report