aliyun-log-exporter.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import prometheus_client
  2. from prometheus_client import Gauge,start_http_server,Counter
  3. import time
  4. import threading
  5. from io import BytesIO
  6. from aliyun.log.logclient import LogClient
  7. from aliyun.log.getlogsrequest import GetLogsRequest
  8. #
  9. # url_http_code = Counter("url_http_code", "request http_code of the host",['code','url'])
  10. # url_http_request_time = Counter("url_http_request_time", "request http_request_time of the host",['le','url'])
  11. # http_request_total = Counter("http_request_total", "request request total of the host",['url'])
  12. url_http_times_avgs = Gauge("url_http_times_avgs","url of avgs",['appType', 'url'])
  13. url_http_qps = Gauge("url_http_qps","url of qps",['url'])
  14. endpoint = 'cn-hangzhou.log.aliyuncs.com'
  15. accessKeyId = 'LTAIWYUujJAm7CbH'
  16. accessKey = 'RfSjdiWwED1sGFlsjXv0DlfTnZTG1P'
  17. client = LogClient(endpoint, accessKeyId, accessKey)
  18. start_time = int(time.time())
  19. end_time = start_time - 60
  20. def count_qps(url):
  21. logstore = 'request-log'
  22. project = 'wqsd-longvideoapi'
  23. topic = ""
  24. req = GetLogsRequest(project, logstore, end_time, start_time, topic,
  25. " params.appType=15 and requestUri:'{}' |select requestUri,count(1) as cnt, date_format(logTimestamp/1000,'%Y-%m-%d %H:%i:%S') as date group by requestUri, date order by cnt desc limit 1".format(url)
  26. , 30, 0, False)
  27. try:
  28. res = client.get_logs(req)
  29. except Exception as e:
  30. print(e)
  31. qps = 0
  32. else:
  33. if res.body:
  34. qps = res.body[0]["cnt"]
  35. else:
  36. qps = 0
  37. return qps
  38. def count_metric(url):
  39. qps = count_qps(url)
  40. # url_http_times_avgs.labels(url).set(avgs)
  41. url_http_qps.labels(url).set(qps)
  42. def count_threads(url):
  43. while True:
  44. t = threading.Thread(target=count_metric,args=(url,))
  45. t.setDaemon(True)
  46. t.start()
  47. time.sleep(60)
  48. if __name__ == '__main__':
  49. start_http_server(9092)
  50. requestUri_list = [
  51. 'longvideoapi/sys/config',
  52. 'longvideoapi/video/played',
  53. 'longvideoapi/video/shared/weixin/friend',
  54. 'longvideoapi/user/login/v2',
  55. 'longvideoapi/user/info/getUserInfoExt',
  56. 'longvideoapi/user/token/check',
  57. 'longvideoapi/user/token/refresh',
  58. 'longvideoapi/user/info/homepageHead',
  59. 'longvideoapi/user/share/homepage/circle',
  60. 'longvideoapi/user/idols',
  61. 'longvideoapi/user/fans',
  62. 'longvideoapi/user/recommend/list/v4',
  63. 'longvideoapi/user/outside/subscribeBatch',
  64. 'longvideoapi/user/info/getBaseUserInfo',
  65. 'longvideoapi/user/info/getTargetBaseUserInfo',
  66. 'longvideoapi/user/getWxUserInfoByCode',
  67. 'longvideoapi/user/follow',
  68. 'longvideoapi/user/unfollow',
  69. 'longvideoapi/video/favorite',
  70. 'longvideoapi/video/unfavorite',
  71. 'longvideoapi/video/shared/weixin/circle',
  72. 'longvideoapi/videoCollection/loadUserVideoCollections',
  73. 'longvideoapi/video/v2/sharePage/queryVideosByCollectionId',
  74. 'longvideoapi/video/v2/detailPage/queryVideosByCollectionId',
  75. 'longvideoapi/videoCollection/loadUserVideoCollectionsForShareOrDetailPage',
  76. 'longvideoapi/video/distribute/getAppInfo',
  77. 'longvideoapi/video/distribute/category/videoList/v2',
  78. 'longvideoapi/video/v2/followed',
  79. 'longvideoapi/video/v2/loadHotVideosByCollectionId',
  80. 'longvideoapi/video/v2/loadLatelyVideosByCollectionId',
  81. 'longvideoapi/video/v2/detail',
  82. 'longvideoapi/video/v2/sharePageDetail',
  83. 'longvideoapi/video/send',
  84. 'longvideoapi/video/getCoverImagePaths',
  85. 'longvideoapi/video/updateVideo',
  86. 'longvideoapi/video/deleteVideo',
  87. 'longvideoapi/video/barrage/send',
  88. 'longvideoapi/video/barrage/switch',
  89. 'longvideoapi/video/videoActionReport',
  90. 'longvideoapi/video/barrage/v2/list',
  91. 'longvideoapi/video/shared/h5"',
  92. 'longvideoapi/user/share/report',
  93. 'longvideoapi/oss/signature',
  94. 'longvideoapi/oss/completeUpload',
  95. 'longvideoapi/oss/producevideo/getStsToken',
  96. 'longvideoapi/operate/message/list',
  97. 'longvideoapi/video/message/list',
  98. 'longvideoapi/sys/message/list',
  99. 'longvideoapi/user/station/message/statistics/info',
  100. 'longvideoapi/user/station/message/statistics/read',
  101. 'longvideoapi/user/station/message/totalCount/v2',
  102. 'longvideoapi/user/station/message/totalRead',
  103. 'longvideoapi/user/station/notice/list',
  104. 'longvideoapi/user/station/message/share/list',
  105. 'longvideoapi/user/station/message/prise/list',
  106. 'longvideoapi/user/station/message/favorite/list',
  107. 'longvideoapi/user/station/message/comment/list',
  108. 'longvideoapi/user/station/message/barrage/list',
  109. 'longvideoapi/operation/message/list',
  110. 'longvideoapi/search/userandvideo/list',
  111. 'longvideoapi/search/tips',
  112. 'longvideoapi/search/user/list',
  113. 'longvideoapi/search/video/list',
  114. 'longvideoapi/search/hot/words',
  115. 'longvideoapi/search/hot/videos',
  116. 'longvideoapi/video/distribute/search/recommendVideoes/v2',
  117. 'longvideoapi/comment/top/save',
  118. 'longvideoapi/comment/top/page',
  119. 'longvideoapi/comment/praise',
  120. 'longvideoapi/comment/second/save',
  121. 'longvideoapi/comment/second/page',
  122. 'longvideoapi/comment/delete',
  123. 'longvideoapi/comment/updelete',
  124. 'longvideoapi/comment/hots',
  125. 'longvideoapi/bank/withdraw/getBankWithdrawConfig',
  126. 'longvideoapi/bank/withdraw/getBankWithdrawRecordList',
  127. 'longvideoapi/bank/withdraw/wxSamllapp/requestWithdraw',
  128. 'longvideoapi/purchase/video/list',
  129. 'longvideoapi/profits/myself/head',
  130. 'longvideoapi/profits/myself/list',
  131. 'longvideoapi/profits/myself/recordList/v2',
  132. 'longvideoapi/profits/reward/recordListAccordingToUser',
  133. 'longvideoapi/profits/reward/recordListAccordingToVideo',
  134. 'longvideoapi/video/reward/getVideoRewardHeadInfo',
  135. 'longvideoapi/video/reward/getVideoRewardRecordList',
  136. 'longvideoapi/video/reward/getVideoRewardConfig',
  137. 'longvideoapi/video/reward/getVideoRewardConfig/v2',
  138. 'longvideoapi/order/updateClientPayStatus',
  139. 'longvideoapi/order/purchase',
  140. 'longvideoapi/profits/videoPay/head',
  141. 'longvideoapi/profits/videoPay/list',
  142. 'longvideoapi/video/getPayedVideoPath',
  143. 'longvideoapi/order/reward',
  144. 'longvideoapi/video/reward/getVideoDetailRewardInfo',
  145. 'longvideoapi/video/recommend/detailPage/list',
  146. 'longvideoapi/video/recommend/sharePage/list',
  147. 'longvideoapi/measure/report/view',
  148. 'longvideoapi/video/validatePwd"',
  149. 'longvideoapi/video/validatePwdAuth"',
  150. 'longvideoapi/video/v2/getCutVideo',
  151. 'longvideoapi/frontConfig/getWxFrontConfig',
  152. 'longvideoapi/frontConfig/getVideoPlayConfig',
  153. 'longvideoapi/frontConfig/getUserFrontConfig',
  154. 'longvideoapi/rhythmapp/homepage/getAllVideos',
  155. 'longvideoapi/ad/position/info'
  156. ]
  157. threads = []
  158. for url in requestUri_list:
  159. t = threading.Thread(target=count_threads,args=(url,))
  160. threads.append(t)
  161. for thread in threads:
  162. thread.setDaemon(True)
  163. thread.start()
  164. thread.join()