app.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. from flask import Flask,Response
  2. from DBSession import session_maker
  3. from model import *
  4. from sqlalchemy.sql import func
  5. from utils import *
  6. import atexit
  7. from aliyunsdkcore.client import AcsClient
  8. from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
  9. from apscheduler.schedulers.background import BackgroundScheduler
  10. import json
  11. from model_longvideo import produce_video_task,produce_video_project
  12. from DBSession_longvideo import session_maker_longvideo
  13. from prometheus_client import Gauge,Counter, generate_latest
  14. from prometheus_client.core import CollectorRegistry
  15. from scheduler_jobs import interface_info_count
  16. from ex_response import ex_response
  17. import time
  18. import threading
  19. app = Flask(__name__)
  20. registry = CollectorRegistry(auto_describe=False)
  21. # # 定义后台执行调度器
  22. # scheduler = BackgroundScheduler()
  23. # scheduler.add_job(func=interface_info_count, trigger="interval", seconds=120)
  24. # scheduler.start()
  25. # atexit.register(lambda: scheduler.shutdown())
  26. client = AcsClient('LTAI4GBWbFvvXoXsSVBe1o9f', 'kRAikWitb4kDxaAyBqNrmLmllMEDO3', 'cn-hangzhou')
  27. healthcheck_status = Gauge("healthcheck_status", "ipaddress", ['instance_id','server_name', 'ipaddress'], registry=registry)
  28. url_http_avgtime = Gauge("url_http_times_avgs", "url of avgs", ['appType', 'url'], registry=registry)
  29. url_http_qps = Gauge("url_http_qps", "url of qps", ['appType','url'], registry=registry)
  30. url_http_expendtime_summary = Gauge("url_http_expendtime_summary", "expendtime summary", ['appType', 'url', 'duration'], registry=registry)
  31. probe_http_status_code = Gauge("http_status_code", 'h5',['server_name'], registry=registry)
  32. probe_http_total_time = Gauge("http_total_time", 'h5',['server_name'], registry=registry)
  33. probe_http_dns_time = Gauge("http_dns_time", 'h5',['server_name'], registry=registry)
  34. probe_http_connect_time = Gauge("http_connect_time", 'h5',['server_name'], registry=registry)
  35. probe_http_pretransfer_time = Gauge("http_pretransfer_time", 'h5',['server_name'], registry=registry)
  36. probe_http_first_byte_time = Gauge("http_first_byte_time", 'h5',['server_name'], registry=registry)
  37. slb_http_status_code = Gauge("slb_http_status_code", 'slb', ['server_name', 'status'], registry=registry)
  38. produce_video_task_count = Gauge("produce_video_task_count", 'success_status_count', ['status_count'], registry=registry)
  39. produce_video_task_rate = Gauge("produce_video_task_rate", 'produce_video_rate', ['produce_video_rate'], registry=registry)
  40. produce_video_tts_count = Gauge("tts_aliyun_azure", 'success', ['tts_channel'], registry=registry)
  41. logs_app_recommend_log_cnt_300 = Gauge("logs_app_recommend_log_null_cnt_300", "null cnt", ['cnt'], registry=registry)
  42. logs_app_recommend_log_cnt_all = Counter("logs_app_recommend_log_null_cnt_all", "all cnt", ['cnt'], registry=registry)
  43. @app.route('/update')
  44. def update():
  45. request = DescribeInstancesRequest()
  46. request.set_accept_format('json')
  47. request.set_PageSize(100)
  48. # request.set_InstanceNetworkType("vpc")
  49. request.set_Tags([
  50. {
  51. "Key": "ecs"
  52. }
  53. ])
  54. response = client.do_action_with_exception(request)
  55. instance_info = json.loads(response)
  56. intances_list_del()
  57. count = len(instance_info["Instances"]["Instance"])
  58. for i in range(len(instance_info["Instances"]["Instance"])):
  59. instance_id = instance_info["Instances"]["Instance"][i]["InstanceId"]
  60. if instance_info["Instances"]["Instance"][i]["InstanceNetworkType"] == "vpc":
  61. ipaddr = instance_info["Instances"]["Instance"][i]["VpcAttributes"]["PrivateIpAddress"]["IpAddress"][0]
  62. if instance_info["Instances"]["Instance"][i]["InstanceNetworkType"] == "classic":
  63. ipaddr = instance_info["Instances"]["Instance"][i]["InnerIpAddress"]["IpAddress"][0]
  64. server_name = instance_info["Instances"]["Instance"][i]["Tags"]["Tag"][0]["TagValue"]
  65. status = instance_info["Instances"]["Instance"][i]["Status"]
  66. instance_name = instance_info["Instances"]["Instance"][i]["HostName"]
  67. if status == "Running":
  68. status = 1
  69. instance_insert(instance_id, ipaddr, instance_name, server_name, status)
  70. return "更新完成"
  71. @app.route('/app/healthcheck/metrics')
  72. def app_healthcheck():
  73. threads = []
  74. with session_maker() as session:
  75. instance_infos = session.query(InstanceList).filter(InstanceList.server_name=="longvideoapi.prod").all()
  76. for index in range(len(instance_infos)):
  77. ipaddr = instance_infos[index].ipadd
  78. server_name = instance_infos[index].server_name
  79. http_code = healthcheck(ipaddr, server_name)
  80. instance_id = instance_infos[index].instance_id
  81. healthcheck_status.labels(instance_id, server_name, ipaddr).set(http_code)
  82. return Response(generate_latest(registry),mimetype="text/plain")
  83. @app.route('/app/qps/metrics')
  84. def qps_avgtime_count():
  85. threads = []
  86. with session_maker() as session:
  87. intface_infos = session.query(IntfaceList.interface_url).all()
  88. app_type = session.query(app_info.app_type).all()
  89. for i in range(len(intface_infos)):
  90. for index in range(len(app_type)):
  91. url = intface_infos[i].interface_url
  92. appType = app_type[index].app_type
  93. url_avgtime = count_avg_time(appType, url)
  94. url_qps = count_qps(appType, url)
  95. url_http_avgtime.labels(appType, url).set(url_avgtime)
  96. url_http_qps.labels(appType, url).set(url_qps)
  97. return Response(generate_latest(registry),mimetype="text/plain")
  98. @app.route('/h5/metrics')
  99. def h5_healthcheck():
  100. # curl_respon = ex_response(
  101. share_h5 = "share_h5"
  102. download_h5 = "download_h5"
  103. share_h5_url = "https://longvideoh5.piaoquantv.com/core/share?shareSource=customerMessage&fromAppType=0&qrAppType=0&versionCode=321&shareUid=12463024&shareMachineCode=weixin_openid_o0w175fPwp8yrtOGihYJhvnT9Ag4&h5WxrootPageSource=vlog-pages___category&videoId=2689415&isRecommendShare=1&h5ShareId=backend493cd67dd28f4ee395781d59881567211625976055926&shareDepth=0&state=#"
  104. download_h5_url = "https://longvideoh5.piaoquantv.com/dist_1_3_4/upload?accessToken=fe8914eb2e99d1fe8ddaa2f753f5ec613eb2dfbb&versionCode=323&galleryId=0&fileType=2&machineCode=weixin_openid_o0w175fPwp8yrtOGihYJhvnT9Ag4&platform=devtools&system=iOS%2010.0.1&appType=0&appId=wx89e7eb06478361d7&pageSource=vlog-pages%2Fwebview&loginUid=12463024&machineInfo=%7B%22sdkVersion%22%3A%222.4.1%22,%22brand%22%3A%22devtools%22,%22language%22%3A%22zh_CN%22,%22model%22%3A%22iPhone%20X%22,%22platform%22%3A%22devtools%22,%22system%22%3A%22iOS%2010.0.1%22,%22weChatVersion%22%3A%228.0.5%22,%22screenHeight%22%3A812,%22screenWidth%22%3A375,%22windowHeight%22%3A730,%22windowWidth%22%3A375,%22softVersion%22%3A%224.1.168%22%7D&wxHelpPagePath=%2Fpackage-my%2Fhelp-feedback%2Fhelp-feedback&transaction=2065ff98-6f27-4f09-c9eb-d366c99dd5d5&videoBarrageSwitch=true&addMusic=1&eventId=0&fromActivityId=0&sessionId=1626833289618-583a312d-81cd-62f9-cdd4-cf914c682d55&subSessionId=1626833289618-583a312d-81cd-62f9-cdd4-cf914c682d55&projectId=&entranceType=#wechat_redirec"
  105. shar_h5_curl_response = ex_response(share_h5_url)
  106. share_h5_url_info = shar_h5_curl_response.getinfo()
  107. download_h5_curl_response = ex_response(download_h5_url)
  108. download_h5_url_info = download_h5_curl_response.getinfo()
  109. probe_http_status_code.labels(share_h5).set(share_h5_url_info["http_code"])
  110. probe_http_status_code.labels(download_h5).set(download_h5_url_info["http_code"])
  111. probe_http_total_time.labels("share_h5").set(share_h5_url_info["total_time"]*1000)
  112. probe_http_total_time.labels("download_h5").set(download_h5_url_info["total_time"]*1000)
  113. probe_http_dns_time.labels("share_h5").set(share_h5_url_info["dns_time"]*1000)
  114. probe_http_dns_time.labels("download_h5").set(download_h5_url_info["dns_time"]*1000)
  115. probe_http_connect_time.labels("share_h5").set(share_h5_url_info["dns_time"]*1000)
  116. probe_http_connect_time.labels("download_h5").set(download_h5_url_info["dns_time"]*1000)
  117. probe_http_pretransfer_time.labels("share_h5").set(share_h5_url_info["pretransfer_time"]*1000)
  118. probe_http_pretransfer_time.labels("download_h5").set(download_h5_url_info["pretransfer_time"]*1000)
  119. probe_http_first_byte_time.labels("share_h5").set(share_h5_url_info["first_byte_time"]*1000)
  120. probe_http_first_byte_time.labels("download_h5").set(download_h5_url_info["first_byte_time"]*1000)
  121. return Response(generate_latest(registry), mimetype="text/plain")
  122. @app.route('/slbStatusCode/metrics')
  123. def slb_request_status_metric():
  124. svc_name = {'longvideoapi', 'clip', 'speed'}
  125. for name in svc_name:
  126. res = slb_status_code_count(name)
  127. if res:
  128. for i in range(len(res)):
  129. status = res[i]['status']
  130. cnt = float(res[i]['cnt'])
  131. slb_http_status_code.labels(name, status).set(cnt)
  132. return Response(generate_latest(registry), mimetype="text/plain")
  133. @app.route('/metrics')
  134. def all_metric():
  135. with session_maker_longvideo() as session:
  136. # video_progress_count = session.query(produce_video_task).filter(produce_video_task.task_status == 1).count()
  137. # video_success_count = session.query(produce_video_task).filter(produce_video_task.task_status == 2).count()
  138. # video_fail_count = session.query(produce_video_task).filter(produce_video_task.task_status == 3).count()
  139. # video_produce_speed = session.query(produce_video_task).filter(produce_video_task.submit_timestamp)
  140. # #视频合成成功率
  141. # produce_video_task_count.labels("video_sucess").set(video_success_count)
  142. # produce_video_task_count.labels("video_fail").set(video_fail_count)
  143. # produce_video_task_count.labels("video_progress").set(video_progress_count)
  144. # #合成速度
  145. # end_time = int(time.time())
  146. # start_time = end_time - (end_time - time.timezone) % 86400
  147. # res = session.query(produce_video_task).filter(produce_video_task.task_status==2).order_by(produce_video_task.id.desc()).first()
  148. # if res:
  149. # duration = res.duration
  150. # submit_timestamp = res.submit_timestamp
  151. # complete_timestamp = res.complete_timestamp
  152. # rate = (duration / (complete_timestamp - submit_timestamp) / 1000)
  153. # produce_video_task_rate.labels("produce_video_task_rate").set(round(rate, 3))
  154. # else:
  155. # produce_video_task_rate.labels("produce_video_task_rate").set(0)
  156. start_time = int(time.strftime("%Y%m%d%H%M", time.localtime())) * 100000000000
  157. end_time = (int(time.strftime("%Y%m%d%H%M", time.localtime())) + 5) * 100000000000
  158. sql = "select round(successCount/totalCount * 100,2) as 成功率 from (select count(*) as totalCount, sum(case when produce_status != 99 then 1 else 0 end) as successCount from produce_video_project where project_id > {} and project_id < {} and app_type not in (13,15)) t1".format(start_time, end_time)
  159. res = db_query(sql)
  160. if res[0] is not None:
  161. produce_video_task_rate.labels("produce_video_task_rate").set(res[0])
  162. else:
  163. produce_video_task_rate.labels("produce_video_task_rate").set(100)
  164. sql = "select v1 as 平均合成耗时,v2 as 平均视频时长, round(v2/v1,1) as 时长耗时比 from (select avg(produce_done_timestamp - submit_timestamp) as v1, avg(video_duration/1000) as v2 from produce_video_project where project_id > {} and project_id < {} and app_type not in (13,15) and produce_status in (5,6,7,8)) as t1".format(start_time, end_time)
  165. #tts
  166. res = db_query(sql)
  167. if res[2] is not None:
  168. produce_video_task_count.labels("video_progress").set(res[2])
  169. else:
  170. produce_video_task_count.labels("video_progress").set(0)
  171. res = logs_tts_count("aliyun",1)
  172. if res is not None:
  173. produce_video_tts_count.labels("aliyun_success").set(res[0]["count"])
  174. else:
  175. produce_video_tts_count.labels("aliyun_success").set(0)
  176. res = logs_tts_count("aliyun", 0)
  177. if res is not None:
  178. produce_video_tts_count.labels("aliyun_fail").set(res[0]["count"])
  179. else:
  180. produce_video_tts_count.labels("aliyun_fail").set(0)
  181. res = logs_tts_count("azure", 1)
  182. if res is not None:
  183. produce_video_tts_count.labels("azure_success").set(res[0]["count"])
  184. else:
  185. produce_video_tts_count.labels("azure_success").set(0)
  186. res = logs_tts_count("aliyun", 0)
  187. if res is not None:
  188. produce_video_tts_count.labels("azure_fail").set(res[0]["count"])
  189. else:
  190. produce_video_tts_count.labels("azure_fail").set(0)
  191. #接口qps与返回时长
  192. # with session_maker() as session:
  193. # intface_infos = session.query(IntfaceList).filter(IntfaceList.app_type == "1").all()
  194. # for i in range(len(intface_infos)):
  195. # url = intface_infos[i].interface_url
  196. # url_qps = intface_infos[i].qps
  197. # url_avg_time = intface_infos[i].avg_time
  198. # url_http_avgtime.labels(url).set(url_avg_time)
  199. # url_http_qps.labels(url).set(url_qps)
  200. #当日负载均衡http_code
  201. svc_name = {'longvideoapi', 'clip', 'speed', 'commonapi'}
  202. for name in svc_name:
  203. res = slb_status_code_count(name)
  204. if res:
  205. for i in range(len(res)):
  206. status = res[i]['status']
  207. cnt = float(res[i]['cnt'])
  208. print(cnt)
  209. slb_http_status_code.labels(name, status).set(cnt)
  210. #h5 healthcheck
  211. share_h5 = "share_h5"
  212. download_h5 = "download_h5"
  213. share_h5_url = "https://longvideoh5.piaoquantv.com/core/share?shareSource=customerMessage&fromAppType=0&qrAppType=0&versionCode=321&shareUid=12463024&shareMachineCode=weixin_openid_o0w175fPwp8yrtOGihYJhvnT9Ag4&h5WxrootPageSource=vlog-pages___category&videoId=2689415&isRecommendShare=1&h5ShareId=backend493cd67dd28f4ee395781d59881567211625976055926&shareDepth=0&state=#"
  214. download_h5_url = "https://longvideoh5.piaoquantv.com/dist_1_3_4/upload?accessToken=fe8914eb2e99d1fe8ddaa2f753f5ec613eb2dfbb&versionCode=323&galleryId=0&fileType=2&machineCode=weixin_openid_o0w175fPwp8yrtOGihYJhvnT9Ag4&platform=devtools&system=iOS%2010.0.1&appType=0&appId=wx89e7eb06478361d7&pageSource=vlog-pages%2Fwebview&loginUid=12463024&machineInfo=%7B%22sdkVersion%22%3A%222.4.1%22,%22brand%22%3A%22devtools%22,%22language%22%3A%22zh_CN%22,%22model%22%3A%22iPhone%20X%22,%22platform%22%3A%22devtools%22,%22system%22%3A%22iOS%2010.0.1%22,%22weChatVersion%22%3A%228.0.5%22,%22screenHeight%22%3A812,%22screenWidth%22%3A375,%22windowHeight%22%3A730,%22windowWidth%22%3A375,%22softVersion%22%3A%224.1.168%22%7D&wxHelpPagePath=%2Fpackage-my%2Fhelp-feedback%2Fhelp-feedback&transaction=2065ff98-6f27-4f09-c9eb-d366c99dd5d5&videoBarrageSwitch=true&addMusic=1&eventId=0&fromActivityId=0&sessionId=1626833289618-583a312d-81cd-62f9-cdd4-cf914c682d55&subSessionId=1626833289618-583a312d-81cd-62f9-cdd4-cf914c682d55&projectId=&entranceType=#wechat_redirec"
  215. shar_h5_curl_response = ex_response(share_h5_url)
  216. share_h5_url_info = shar_h5_curl_response.getinfo()
  217. download_h5_curl_response = ex_response(download_h5_url)
  218. download_h5_url_info = download_h5_curl_response.getinfo()
  219. probe_http_status_code.labels(share_h5).set(share_h5_url_info["http_code"])
  220. probe_http_status_code.labels(download_h5).set(download_h5_url_info["http_code"])
  221. probe_http_total_time.labels("share_h5").set(share_h5_url_info["total_time"]*1000)
  222. probe_http_total_time.labels("download_h5").set(download_h5_url_info["total_time"]*1000)
  223. probe_http_dns_time.labels("share_h5").set(share_h5_url_info["dns_time"]*1000)
  224. probe_http_dns_time.labels("download_h5").set(download_h5_url_info["dns_time"]*1000)
  225. probe_http_connect_time.labels("share_h5").set(share_h5_url_info["dns_time"]*1000)
  226. probe_http_connect_time.labels("download_h5").set(download_h5_url_info["dns_time"]*1000)
  227. probe_http_pretransfer_time.labels("share_h5").set(share_h5_url_info["pretransfer_time"]*1000)
  228. probe_http_pretransfer_time.labels("download_h5").set(download_h5_url_info["pretransfer_time"]*1000)
  229. probe_http_first_byte_time.labels("share_h5").set(share_h5_url_info["first_byte_time"]*1000)
  230. probe_http_first_byte_time.labels("download_h5").set(download_h5_url_info["first_byte_time"]*1000)
  231. #推荐业务
  232. res_null_cnt = count_recommend_null()
  233. logs_app_recommend_log_cnt_300.labels("recommend").set(res_null_cnt)
  234. # # logs_app_recommend_log_cnt_all.labels("recommend").inc(1)
  235. # logs_app_recommend_log_cnt_all.inc(res_null_cnt)
  236. return Response(generate_latest(registry), mimetype="text/plain")
  237. @app.route('/qps_avgtime/metrics')
  238. def qps_avgtime_metrics():
  239. app_type = ['1', '0', '4', '5', '6', '12', '13']
  240. for i in range(len(app_type)):
  241. appType = app_type[i]
  242. res = count_qps_avgtime(appType)
  243. for i in range(len(res.body)):
  244. url = res.body[i]["requestUri"]
  245. qps = int(res.body[i]["cnt"])
  246. avgtime = int(float(res.body[i]["avg_time"]))
  247. url_http_qps.labels(appType, url).set(qps)
  248. url_http_avgtime.labels(appType, url).set(avgtime)
  249. res = count_rt_less_time_count(appType, 0, 200)
  250. for i in range(len(res.body)):
  251. url = res.body[i]["requestUri"]
  252. count = res.body[i]["cnt"]
  253. url_http_expendtime_summary.labels(appType, url, "0-200").set(count)
  254. res = count_rt_less_time_count(appType, 200, 500)
  255. for i in range(len(res.body)):
  256. url = res.body[i]["requestUri"]
  257. count = res.body[i]["cnt"]
  258. url_http_expendtime_summary.labels(appType, url, "200-300").set(count)
  259. res = count_rt_less_time_count(appType, 500, 1000)
  260. for i in range(len(res.body)):
  261. url = res.body[i]["requestUri"]
  262. count = res.body[i]["cnt"]
  263. url_http_expendtime_summary.labels(appType, url, "500-1000").set(count)
  264. res = count_rt_less_time_count(appType, 1000, 10000)
  265. for i in range(len(res.body)):
  266. url = res.body[i]["requestUri"]
  267. count = res.body[i]["cnt"]
  268. url_http_expendtime_summary.labels(appType, url, ">1000").set(count)
  269. return Response(generate_latest(registry), mimetype="text/plain")
  270. if __name__ == '__main__':
  271. # app.run()
  272. app.run(host='192.168.201.1', port=9091)