exporter-wx.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. from flask import Flask,Response
  2. from DBSession import session_maker
  3. from model import InstanceList
  4. from utils import *
  5. import atexit
  6. from aliyunsdkcore.client import AcsClient
  7. from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
  8. from apscheduler.schedulers.background import BackgroundScheduler
  9. import json
  10. from model_longvideo import produce_video_task
  11. from DBSession_longvideo import session_maker_longvideo
  12. from prometheus_client import Gauge,Counter, generate_latest,start_http_server
  13. from prometheus_client.core import CollectorRegistry
  14. from scheduler_jobs import interface_info_count
  15. from ex_response import ex_response
  16. import time
  17. import threading
  18. app = Flask(__name__)
  19. registry = CollectorRegistry(auto_describe=False)
  20. # 定义后台执行调度器
  21. scheduler = BackgroundScheduler()
  22. scheduler.add_job(func=interface_info_count, trigger="interval", seconds=60)
  23. scheduler.start()
  24. atexit.register(lambda: scheduler.shutdown())
  25. client = AcsClient('LTAI4GBWbFvvXoXsSVBe1o9f', 'kRAikWitb4kDxaAyBqNrmLmllMEDO3', 'cn-hangzhou')
  26. healthcheck_status = Gauge("healthcheck_status", "ipaddress", ['instance_id','server_name', 'ipaddress'], registry=registry)
  27. url_http_avgtime = Gauge("url_http_times_avgs", "url of avgs", ['url'], registry=registry)
  28. url_http_qps = Gauge("url_http_qps", "url of qps", ['url'], registry=registry)
  29. probe_http_status_code = Gauge("http_status_code", 'h5',['server_name'], registry=registry)
  30. probe_http_total_time = Gauge("http_total_time", 'h5',['server_name'], registry=registry)
  31. probe_http_dns_time = Gauge("http_dns_time", 'h5',['server_name'], registry=registry)
  32. probe_http_connect_time = Gauge("http_connect_time", 'h5',['server_name'], registry=registry)
  33. probe_http_pretransfer_time = Gauge("http_pretransfer_time", 'h5',['server_name'], registry=registry)
  34. probe_http_first_byte_time = Gauge("http_first_byte_time", 'h5',['server_name'], registry=registry)
  35. slb_http_status_code = Gauge("slb_http_status_code", 'slb', ['server_name', 'status'], registry=registry)
  36. produce_video_task_success_count = Gauge("produce_video_task_success_count", 'success_status_count', ['success_status_count'], registry=registry)
  37. produce_video_task_fail_count = Gauge("produce_video_task_fail_count", 'fail_status_count', ['fail_status_count'], registry=registry)
  38. produce_video_task_progress_count = Gauge("produce_video_task_progress_count", 'progress_status_count', ['progress_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. def healthcheck():
  42. with session_maker() as session:
  43. instance_infos = session.query(InstanceList).filter(InstanceList.server_name=="longvideoapi.prod").all()
  44. for index in range(len(instance_infos)):
  45. ipaddr = instance_infos[index].ipadd
  46. server_name = instance_infos[index].server_name
  47. http_code = healthcheck(ipaddr, server_name)
  48. instance_id = instance_infos[index].instance_id
  49. healthcheck_status.labels(instance_id, server_name, ipaddr).set(http_code)
  50. def interface_qps():
  51. with session_maker() as session:
  52. intface_infos = session.query(IntfaceList).filter(IntfaceList.app_type == "1").all()
  53. for i in range(len(intface_infos)):
  54. url = intface_infos[i].interface_url
  55. url_qps = intface_infos[i].qps
  56. url_avg_time = intface_infos[i].avg_time
  57. url_http_avgtime.labels(url).set(url_avg_time)
  58. url_http_qps.labels(url).set(url_qps)
  59. def h5_healthcheck():
  60. share_h5 = "share_h5"
  61. download_h5 = "download_h5"
  62. 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=#"
  63. 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"
  64. shar_h5_curl_response = ex_response(share_h5_url)
  65. share_h5_url_info = shar_h5_curl_response.getinfo()
  66. download_h5_curl_response = ex_response(download_h5_url)
  67. download_h5_url_info = download_h5_curl_response.getinfo()
  68. probe_http_status_code.labels(share_h5).set(share_h5_url_info["http_code"])
  69. probe_http_status_code.labels(download_h5).set(download_h5_url_info["http_code"])
  70. probe_http_total_time.labels("share_h5").set(share_h5_url_info["total_time"]*1000)
  71. probe_http_total_time.labels("download_h5").set(download_h5_url_info["total_time"]*1000)
  72. probe_http_dns_time.labels("share_h5").set(share_h5_url_info["dns_time"]*1000)
  73. probe_http_dns_time.labels("download_h5").set(download_h5_url_info["dns_time"]*1000)
  74. probe_http_connect_time.labels("share_h5").set(share_h5_url_info["dns_time"]*1000)
  75. probe_http_connect_time.labels("download_h5").set(download_h5_url_info["dns_time"]*1000)
  76. probe_http_pretransfer_time.labels("share_h5").set(share_h5_url_info["pretransfer_time"]*1000)
  77. probe_http_pretransfer_time.labels("download_h5").set(download_h5_url_info["pretransfer_time"]*1000)
  78. probe_http_first_byte_time.labels("share_h5").set(share_h5_url_info["first_byte_time"]*1000)
  79. probe_http_first_byte_time.labels("download_h5").set(download_h5_url_info["first_byte_time"]*1000)
  80. def produce_video_task_status_count():
  81. with session_maker_longvideo() as session:
  82. video_progress_count = session.query(produce_video_task).filter(produce_video_task.task_status == 1).count()
  83. video_success_count = session.query(produce_video_task).filter(produce_video_task.task_status == 2).count()
  84. video_fail_count = session.query(produce_video_task).filter(produce_video_task.task_status == 3).count()
  85. video_produce_speed = session.query(produce_video_task).filter(produce_video_task.submit_timestamp)
  86. #视频合成成功率
  87. produce_video_task_success_count.labels("video_sucess_count").set(video_success_count)
  88. produce_video_task_fail_count.labels("video_fail_count").set(video_fail_count)
  89. produce_video_task_progress_count.labels("video_progress_count").set(video_progress_count)
  90. #合成速度
  91. end_time = int(time.time())
  92. start_time = end_time - (end_time - time.timezone) % 86400
  93. res = session.query(produce_video_task).filter(produce_video_task.task_status==2).order_by(produce_video_task.id.desc()).first()
  94. if res:
  95. duration = res.duration
  96. submit_timestamp = res.submit_timestamp
  97. complete_timestamp = res.complete_timestamp
  98. rate = (duration / (complete_timestamp - submit_timestamp) / 1000)
  99. produce_video_task_rate.labels("produce_video_task_rate").set(round(rate, 3))
  100. else:
  101. produce_video_task_rate.labels("produce_video_task_rate").set(0)
  102. #tts
  103. res = logs_tts_count("aliyun",1)
  104. if res[0]["count"]:
  105. produce_video_tts_count.labels("aliyun_success").set(res[0]["count"])
  106. res = logs_tts_count("aliyun", 0)
  107. if res[0]["count"]:
  108. produce_video_tts_count.labels("aliyun_fail").set(res[0]["count"])
  109. res = logs_tts_count("azure", 1)
  110. if res[0]["count"]:
  111. produce_video_tts_count.labels("azure_success").set(res[0]["count"])
  112. res = logs_tts_count("aliyun", 0)
  113. if res[0]["count"]:
  114. produce_video_tts_count.labels("azure_fail").set(res[0]["count"])
  115. def count_metric(url):
  116. avgs = count_avg_time(url)
  117. qps = count_qps(url)
  118. # url_http_times_avgs.labels(url, avgs)
  119. url_http_qps.labels(url).set(qps)
  120. def count_threads(url):
  121. while True:
  122. t = threading.Thread(target=count_metric,args=(url,))
  123. t.setDaemon(True)
  124. t.start()
  125. time.sleep(10)
  126. if __name__ == '__main__':
  127. start_http_server(9091)
  128. thread_num = 10
  129. threads = []
  130. for i in range(thread_num):
  131. t = threading.Thread(target=count_threads,args=(url,))
  132. threads.append(t)
  133. for thread in threads:
  134. thread.setDaemon(True)
  135. thread.start()
  136. thread.join()