main.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "os"
  6. "strconv"
  7. "time"
  8. "github.com/prometheus/client_golang/prometheus"
  9. "github.com/prometheus/client_golang/prometheus/promhttp"
  10. sls "github.com/aliyun/aliyun-log-go-sdk"
  11. )
  12. var (
  13. AccessKeyID = "LTAIWYUujJAm7CbH"
  14. AccessKeySecret = "RfSjdiWwED1sGFlsjXv0DlfTnZTG1P"
  15. Endpoint = "cn-hangzhou.log.aliyuncs.com"
  16. adActionLogStore *sls.LogStore
  17. adOwnActionLogStore *sls.LogStore
  18. adOwnConvLogStore *sls.LogStore
  19. longvideoReqeustLogStore *sls.LogStore
  20. videoPlayStore *sls.LogStore
  21. videoActionStore *sls.LogStore
  22. simpleeventStore *sls.LogStore
  23. userShareStore *sls.LogStore
  24. adRequestStore *sls.LogStore
  25. adReportQuerySQL = `* | SELECT appType, json_extract_scalar(extParams, '$.eventInfos.ab_test002') AS adGroup, CASE WHEN adType = 1 THEN 'BANNER广告' WHEN adType = 2 THEN '视频广告' WHEN adType = 3 THEN '贴片广告' WHEN adType = 4 THEN '插屏广告' WHEN adType = 5 THEN '开屏广告' WHEN adType = 6 THEN '激励广告' ELSE ' ' END AS adType, COUNT( DISTINCT CASE WHEN businessType = 'adLoaded' THEN machineCode END ) AS adLoaded, COUNT( DISTINCT CASE WHEN businessType = 'adRequest' THEN machineCode END ) AS adRequest, COUNT( DISTINCT CASE WHEN businessType = 'adView' THEN machineCode END ) AS adView, COUNT( DISTINCT CASE WHEN businessType = 'adClick' THEN machineCode END ) AS adClick, COUNT( DISTINCT CASE WHEN businessType = 'adCloseBtnTap' THEN machineCode END ) AS adCloseBtnTap, COUNT( DISTINCT CASE WHEN businessType = 'adCloseGestureTap' THEN machineCode END ) AS adCloseGestureTap, COUNT( DISTINCT CASE WHEN businessType = 'adCloseMaskTap' THEN machineCode END ) AS adCloseMaskTap, COUNT( DISTINCT CASE WHEN businessType = 'adLoadError' THEN machineCode END ) AS adLoadError, COUNT( DISTINCT CASE WHEN businessType = 'adConfigRequest' THEN machineCode END ) AS adConfigRequest, COUNT( DISTINCT CASE WHEN businessType = 'adConfigLoaded' THEN machineCode END ) AS adConfigLoaded GROUP BY adGroup, adType, appType HAVING adGroup IS NOT NULL AND adType != ' ' ORDER BY appType ASC LIMIT 1000`
  26. adOwnWecahtReportQuerySQL = `* and ownAdSystemType:weixin| SELECT appType, json_extract_scalar(extParams, '$.eventInfos.ab_test002') AS adGroup, CASE WHEN adType = 1 THEN 'BANNER广告' WHEN adType = 2 THEN '视频广告' WHEN adType = 3 THEN '贴片广告' WHEN adType = 4 THEN '插屏广告' WHEN adType = 5 THEN '开屏广告' WHEN adType = 6 THEN '激励广告' ELSE ' ' END AS adType, COUNT( DISTINCT CASE WHEN businessType = 'adLoaded' THEN machineCode END ) AS adLoaded, COUNT( DISTINCT CASE WHEN businessType = 'adRequest' THEN machineCode END ) AS adRequest, COUNT( DISTINCT CASE WHEN businessType = 'adView' THEN machineCode END ) AS adView, COUNT( DISTINCT CASE WHEN businessType = 'adClick' THEN machineCode END ) AS adClick, COUNT( DISTINCT CASE WHEN businessType = 'adCloseBtnTap' THEN machineCode END ) AS adCloseBtnTap, COUNT( DISTINCT CASE WHEN businessType = 'adCloseGestureTap' THEN machineCode END ) AS adCloseGestureTap, COUNT( DISTINCT CASE WHEN businessType = 'adCloseMaskTap' THEN machineCode END ) AS adCloseMaskTap, COUNT( DISTINCT CASE WHEN businessType = 'adLoadError' THEN machineCode END ) AS adLoadError, COUNT( DISTINCT CASE WHEN businessType = 'adConfigRequest' THEN machineCode END ) AS adConfigRequest, COUNT( DISTINCT CASE WHEN businessType = 'adConfigLoaded' THEN machineCode END ) AS adConfigLoaded GROUP BY adGroup, adType, appType HAVING adGroup IS NOT NULL AND adType != ' ' ORDER BY appType ASC LIMIT 1000`
  27. adOwnReportQuerySQL = `* and ownAdSystemType:own| SELECT appType, json_extract_scalar(extParams, '$.eventInfos.ab_test002') AS adGroup, ownAdPositionId, COUNT( DISTINCT CASE WHEN businessType = 'adLoaded' THEN machineCode END ) AS adLoaded, COUNT( DISTINCT CASE WHEN businessType = 'adRequest' THEN machineCode END ) AS adRequest, COUNT( DISTINCT CASE WHEN businessType = 'adView' THEN machineCode END ) AS adView, COUNT( DISTINCT CASE WHEN businessType = 'adClick' THEN machineCode END ) AS adClick, COUNT( DISTINCT CASE WHEN businessType = 'adH5View' THEN machineCode END ) AS adH5View,COUNT( DISTINCT CASE WHEN businessType = 'adH5Load' THEN machineCode END ) AS adH5Load, COUNT( DISTINCT CASE WHEN businessType = 'adCloseBtnTap' THEN machineCode END ) AS adCloseBtnTap, COUNT( DISTINCT CASE WHEN businessType = 'adCloseGestureTap' THEN machineCode END ) AS adCloseGestureTap, COUNT( DISTINCT CASE WHEN businessType = 'adCloseMaskTap' THEN machineCode END ) AS adCloseMaskTap, COUNT( DISTINCT CASE WHEN businessType = 'adLoadError' THEN machineCode END ) AS adLoadError GROUP BY adGroup, ownAdPositionId, appType HAVING adGroup IS NOT NULL AND ownAdPositionId IS NOT NULL ORDER BY appType ASC LIMIT 1000`
  28. adOwnConvReportQuerySQL = `* | SELECT appId, eventType ,count(*) as conv group by appId, eventType order by conv desc LIMIT 1000`
  29. adReportQuerySQL4 = `* | SELECT appType, json_extract_scalar(extParams, '$.eventInfos.ab_test004') AS adGroup, CASE WHEN adType = 1 THEN 'BANNER广告' WHEN adType = 2 THEN '视频广告' WHEN adType = 3 THEN '贴片广告' WHEN adType = 4 THEN '插屏广告' WHEN adType = 5 THEN '开屏广告' WHEN adType = 6 THEN '激励广告' ELSE ' ' END AS adType, COUNT( DISTINCT CASE WHEN businessType = 'adLoaded' THEN machineCode END ) AS adLoaded, COUNT( DISTINCT CASE WHEN businessType = 'adRequest' THEN machineCode END ) AS adRequest, COUNT( DISTINCT CASE WHEN businessType = 'adView' THEN machineCode END ) AS adView, COUNT( DISTINCT CASE WHEN businessType = 'adClick' THEN machineCode END ) AS adClick, COUNT( DISTINCT CASE WHEN businessType = 'adShowError' THEN machineCode END ) AS adShowError, COUNT( DISTINCT CASE WHEN businessType = 'adLoadError' THEN machineCode END ) AS adLoadError, COUNT( DISTINCT CASE WHEN businessType = 'adConfigRequest' THEN machineCode END ) AS adConfigRequest, COUNT( DISTINCT CASE WHEN businessType = 'adConfigLoaded' THEN machineCode END ) AS adConfigLoaded GROUP BY adGroup, adType, appType HAVING adGroup IS NOT NULL AND adType != ' ' ORDER BY appType ASC LIMIT 1000`
  30. apiQuerySQL = `* and requestUri: "/longvideoapi/video/distribute/category/videoList/v2" or requestUri: "/longvideoapi/video/v2/detail" or requestUri: "/longvideoapi/video/v2/sharePageDetail" or requestUri: "/longvideoapi/video/recommend/sharePage/list" | SELECT requestUri, json_extract_scalar(params, '$.appType') as appType, COUNT(*) as total, count( case when resultCode = 0 THEN 1 else null end ) as success, count( case when resultCode != 0 THEN 1 else null end ) as error GROUP by appType, requestUri order by requestUri asc limit 1000`
  31. videoPlayQuerySQL = `* | select appType, COUNT(*) as cnt, businessType, pageSource GROUP by appType, businessType, pageSource ORDER BY appType ASC, cnt DESC LIMIT 1000`
  32. // videoPlayCountQuerySQL = `* AND businessType : videoPlay | select appType, COUNT(*) as plyaCount, pageSource GROUP BY appType, pageSource ORDER BY appType ASC`
  33. // adConfigQuerySQL = `* and requestUri: /longvideoapi/ad/position/info | select json_extract_scalar(params, '$.appType') as appType,count(*) as total, count(case when resultCode = 0 then 1 else null end) as success,count(case when resultCode != 0 then 1 else null end) as error,count(case when resultSize > 0 then 1 else null end) as hasAds,count(case when resultSize <= 0 then 1 else null end) as noAds group by appType`
  34. adConfigQuerySQL = `* and url : "/ad/position/info" | SELECT count(*) as total, count( case when json_extract_scalar(responseBody, '$.code') = '0' then 1 else null end ) as success, count( case when json_extract_scalar(responseBody, '$.code') != '0' then 1 else null end ) as error, count( case when json_array_length(json_extract(responseBody, '$.data')) <= 0 then 1 else null end ) as noAds, count( case when json_array_length(json_extract(responseBody, '$.data')) > 0 then 1 else null end ) as hasAds, json_extract(requestBody, '$.baseInfo.appType') as appType group by appType `
  35. apiRtQuerySQL = `* and requestUri: "/longvideoapi/video/distribute/category/videoList/v2" or requestUri: "/longvideoapi/video/v2/detail" or requestUri: "/longvideoapi/video/v2/sharePageDetail" or requestUri: "/longvideoapi/video/recommend/sharePage/list" | select "requestUri", "params.appType" as "appType" , avg("expendTime") as "rt" from log group by "requestUri", "appType" order by "appType" asc limit 1000`
  36. videoDataQuerySQL = `* | select appType, COUNT(*) as cnt, businessType, pageSource GROUP by appType, businessType, pageSource ORDER BY appType ASC, businessType LIMIT 1000`
  37. distributeApiQuerySQL = ` * and (requestUri: "/longvideoapi/video/distribute/category/videoList/v2" or requestUri: "/longvideoapi/video/recommend/sharePage/list") and not resultSize: 4 | SELECT json_extract_scalar(params, '$.appType') as appType, requestUri, COUNT( DISTINCT CASE WHEN resultSize = 0 THEN json_extract_scalar(params, '$.machineCode') END ) AS "等于0", COUNT( DISTINCT CASE WHEN (resultSize < 4 and resultSize >0) THEN json_extract_scalar(params, '$.machineCode') END ) AS "小于4" GROUP BY requestUri, appType ORDER BY appType ASC LIMIT 1000`
  38. shareDataQuerySQL = `* and ( businessType = 'videoShareFriend' OR businessType = 'videoShareH5' OR businessType = 'videoPublish' ) | select appType, COUNT(DISTINCT mid) as cnt, COUNT(*) as cnt1, businessType, pageSource GROUP by appType, pageSource, businessType ORDER BY appType ASC, cnt DESC LIMIT 1000 `
  39. shareOperation1QuerySQL = `* and objectType: weapp_quitbutton OR objectType: weapp_share_navigator_moveVideo OR objectType: weapp_share_upload OR ( objectType: weapp_share_shareFriendWindow AND businessType: buttonClick ) | select objectType, appType, COUNT(*) as cnt GROUP by objectType, appType ORDER BY appType asc, objectType asc `
  40. shareOperation2QuerySQL = `* and pageSource: "user-videos-share" | select COUNT(*) as cnt, ( CASE WHEN groupShare IS NULL THEN 'shareFriend' WHEN groupShare = 1 THEN 'shareGroup' ELSE ' ' END ) as type, appType GROUP BY type, appType ORDER BY appType asc, type asc `
  41. )
  42. func init() {
  43. client := sls.CreateNormalInterface(Endpoint, AccessKeyID, AccessKeySecret, "")
  44. var err error
  45. longvideoReqeustLogStore, err = client.GetLogStore("wqsd-longvideoapi", "request-log")
  46. if err != nil {
  47. panic(err)
  48. }
  49. adActionLogStore, err = client.GetLogStore("wqsd-longvideo-frontend-log-prd", "ad-action-log")
  50. if err != nil {
  51. panic(err)
  52. }
  53. adOwnActionLogStore, err = client.GetLogStore("video-upload-data-prod", "own-ad-action-log")
  54. if err != nil {
  55. panic(err)
  56. }
  57. adOwnConvLogStore, err = client.GetLogStore("video-upload-data-prod", "ad_own_open_conv")
  58. if err != nil {
  59. panic(err)
  60. }
  61. videoActionStore, err = client.GetLogStore("wqsd-video", "video-action-log")
  62. if err != nil {
  63. panic(err)
  64. }
  65. videoPlayStore, err = client.GetLogStore("wqsd-video", "video-play-log")
  66. if err != nil {
  67. panic(err)
  68. }
  69. simpleeventStore, err = client.GetLogStore("wqsd-longvideo-frontend-log-prd", "simpleevent-log")
  70. if err != nil {
  71. panic(err)
  72. }
  73. userShareStore, err = client.GetLogStore("wqsd-user", "user-share-log")
  74. if err != nil {
  75. panic(err)
  76. }
  77. adRequestStore, err = client.GetLogStore("ms-ad", "request-log")
  78. if err != nil {
  79. panic(err)
  80. }
  81. }
  82. func (c *CustomCollector) Describe(ch chan<- *prometheus.Desc) {
  83. ch <- c.AdInfoDesc
  84. ch <- c.AdOwnWechatInfoDesc
  85. ch <- c.AdOwnInfoDesc
  86. ch <- c.AdOwnConvDesc
  87. ch <- c.ApiInfoDesc
  88. ch <- c.VideoPlayDesc
  89. ch <- c.AdConfigDesc
  90. ch <- c.AdInfo4Desc
  91. ch <- c.ApiRtDesc
  92. ch <- c.VideoDataDesc
  93. ch <- c.DistributeApiDesc
  94. ch <- c.ShareDataDesc
  95. ch <- c.ShareOperationDesc
  96. }
  97. func (c *CustomCollector) Collect(ch chan<- prometheus.Metric) {
  98. resp, err := longvideoReqeustLogStore.GetLogs("", time.Now().Unix()-60, time.Now().Unix(), apiQuerySQL, 0, 0, true)
  99. if err != nil || resp == nil {
  100. log.Printf("get api logs error %+v resp is %+v.", err, resp)
  101. return
  102. }
  103. for _, v := range resp.Logs {
  104. for k, v2 := range v {
  105. if k == "total" ||
  106. k == "success" ||
  107. k == "error" {
  108. v3, _ := strconv.ParseFloat(v2, 64)
  109. ch <- prometheus.MustNewConstMetric(
  110. c.ApiInfoDesc,
  111. prometheus.GaugeValue,
  112. v3,
  113. v["appType"], v["requestUri"], k,
  114. )
  115. }
  116. }
  117. }
  118. resp, err = adActionLogStore.GetLogs("", time.Now().Unix()-60, time.Now().Unix(), adReportQuerySQL, 0, 0, true)
  119. if err != nil || resp == nil {
  120. log.Printf("get ad report logs ab_test002 error %+v resp is %+v.", err, resp)
  121. return
  122. }
  123. for _, v := range resp.Logs {
  124. for k, v2 := range v {
  125. if k == "adRequest" ||
  126. k == "adShowError" ||
  127. k == "adLoadError" ||
  128. k == "adConfigRequest" ||
  129. k == "adConfigLoaded" ||
  130. k == "adLoaded" ||
  131. k == "adView" ||
  132. k == "adCloseBtnTap" ||
  133. k == "adCloseGestureTap" ||
  134. k == "adCloseMaskTap" ||
  135. k == "adClick" {
  136. v3, _ := strconv.ParseFloat(v2, 64)
  137. ch <- prometheus.MustNewConstMetric(
  138. c.AdInfoDesc,
  139. prometheus.GaugeValue,
  140. v3,
  141. v["appType"], v["adGroup"], v["adType"], k,
  142. )
  143. }
  144. }
  145. }
  146. resp, err = adActionLogStore.GetLogs("", time.Now().Unix()-60, time.Now().Unix(), adReportQuerySQL4, 0, 0, true)
  147. if err != nil || resp == nil {
  148. log.Printf("get ad report logs ab_test004 error %+v resp is %+v.", err, resp)
  149. return
  150. }
  151. for _, v := range resp.Logs {
  152. for k, v2 := range v {
  153. if k == "adRequest" ||
  154. k == "adShowError" ||
  155. k == "adLoadError" ||
  156. k == "adConfigRequest" ||
  157. k == "adConfigLoaded" ||
  158. k == "adLoaded" ||
  159. k == "adView" ||
  160. k == "adCloseBtnTap" ||
  161. k == "adCloseGestureTap" ||
  162. k == "adCloseMaskTap" ||
  163. k == "adClick" {
  164. v3, _ := strconv.ParseFloat(v2, 64)
  165. ch <- prometheus.MustNewConstMetric(
  166. c.AdInfo4Desc,
  167. prometheus.GaugeValue,
  168. v3,
  169. v["appType"], v["adGroup"], v["adType"], k,
  170. )
  171. }
  172. }
  173. }
  174. resp, err = adOwnActionLogStore.GetLogs("", time.Now().Unix()-60, time.Now().Unix(), adOwnWecahtReportQuerySQL, 0, 0, true)
  175. if err != nil || resp == nil {
  176. log.Printf("get own ad report logs ab_test002 error %+v resp is %+v.", err, resp)
  177. return
  178. }
  179. for _, v := range resp.Logs {
  180. for k, v2 := range v {
  181. if k == "adRequest" ||
  182. k == "adShowError" ||
  183. k == "adLoadError" ||
  184. k == "adConfigRequest" ||
  185. k == "adConfigLoaded" ||
  186. k == "adLoaded" ||
  187. k == "adView" ||
  188. k == "adCloseBtnTap" ||
  189. k == "adCloseGestureTap" ||
  190. k == "adCloseMaskTap" ||
  191. k == "adClick" {
  192. v3, _ := strconv.ParseFloat(v2, 64)
  193. ch <- prometheus.MustNewConstMetric(
  194. c.AdOwnWechatInfoDesc,
  195. prometheus.GaugeValue,
  196. v3,
  197. v["appType"], v["adGroup"], v["adType"], k,
  198. )
  199. }
  200. }
  201. }
  202. resp, err = adOwnActionLogStore.GetLogs("", time.Now().Unix()-60, time.Now().Unix(), adOwnReportQuerySQL, 0, 0, true)
  203. if err != nil || resp == nil {
  204. log.Printf("get own ad report logs ab_test002 error %+v resp is %+v.", err, resp)
  205. return
  206. }
  207. for _, v := range resp.Logs {
  208. for k, v2 := range v {
  209. if k == "adRequest" ||
  210. k == "adLoadError" ||
  211. k == "adConfigRequest" ||
  212. k == "adConfigLoaded" ||
  213. k == "adLoaded" ||
  214. k == "adView" ||
  215. k == "adH5View" ||
  216. k == "adH5Load" ||
  217. k == "adCloseBtnTap" ||
  218. k == "adCloseGestureTap" ||
  219. k == "adCloseMaskTap" ||
  220. k == "adClick" {
  221. v3, _ := strconv.ParseFloat(v2, 64)
  222. ch <- prometheus.MustNewConstMetric(
  223. c.AdOwnInfoDesc,
  224. prometheus.GaugeValue,
  225. v3,
  226. v["appType"], v["adGroup"], v["ownAdPositionId"], k,
  227. )
  228. }
  229. }
  230. }
  231. resp, err = adOwnConvLogStore.GetLogs("", time.Now().Unix()-60, time.Now().Unix(), adOwnConvReportQuerySQL, 0, 0, true)
  232. if err != nil || resp == nil {
  233. log.Printf("get own ad report logs error %+v resp is %+v.", err, resp)
  234. return
  235. }
  236. for _, v := range resp.Logs {
  237. for k, v2 := range v {
  238. if k == "conv" {
  239. v3, _ := strconv.ParseFloat(v2, 64)
  240. ch <- prometheus.MustNewConstMetric(
  241. c.AdOwnConvDesc,
  242. prometheus.GaugeValue,
  243. v3,
  244. v["appId"], v["eventType"], k,
  245. )
  246. }
  247. }
  248. }
  249. resp, err = adRequestStore.GetLogs("", time.Now().Unix()-60, time.Now().Unix(), adConfigQuerySQL, 0, 0, true)
  250. if err != nil || resp == nil {
  251. log.Printf("get ad config logs error %+v resp is %+v.", err, resp)
  252. return
  253. }
  254. for _, v := range resp.Logs {
  255. for k, v2 := range v {
  256. if k == "total" ||
  257. k == "success" ||
  258. k == "error" ||
  259. k == "hasAds" ||
  260. k == "noAds" {
  261. v3, _ := strconv.ParseFloat(v2, 64)
  262. ch <- prometheus.MustNewConstMetric(
  263. c.AdConfigDesc,
  264. prometheus.GaugeValue,
  265. v3,
  266. v["appType"], k,
  267. )
  268. }
  269. }
  270. }
  271. // resp, err = videoActionStore.GetLogs("", time.Now().Unix()-60, time.Now().Unix(), videoPlayCountQuerySQL, 0, 0, true)
  272. // if err != nil || resp == nil {
  273. // log.Printf("get video plyaCount logs error %+v resp is %+v.", err, resp)
  274. // return
  275. // }
  276. // for _, v := range resp.Logs {
  277. // v2, _ := strconv.ParseFloat(v["plyaCount"], 64)
  278. // ch <- prometheus.MustNewConstMetric(
  279. // c.VideoPlayDesc,
  280. // prometheus.GaugeValue,
  281. // v2,
  282. // v["appType"], "videoPlayTotal", v["pageSource"],
  283. // )
  284. // }
  285. resp, err = videoPlayStore.GetLogs("", time.Now().Unix()-60, time.Now().Unix(), videoPlayQuerySQL, 0, 0, true)
  286. if err != nil || resp == nil {
  287. log.Printf("get video play logs error %+v resp is %+v.", err, resp)
  288. return
  289. }
  290. for _, v := range resp.Logs {
  291. v2, _ := strconv.ParseFloat(v["cnt"], 64)
  292. ch <- prometheus.MustNewConstMetric(
  293. c.VideoPlayDesc,
  294. prometheus.GaugeValue,
  295. v2,
  296. v["appType"], v["businessType"], v["pageSource"],
  297. )
  298. }
  299. resp, err = longvideoReqeustLogStore.GetLogs("", time.Now().Unix()-60, time.Now().Unix(), apiRtQuerySQL, 0, 0, true)
  300. if err != nil || resp == nil {
  301. log.Printf("get api rt logs error %+v resp is %+v.", err, resp)
  302. return
  303. }
  304. for _, v := range resp.Logs {
  305. v3, _ := strconv.ParseFloat(v["rt"], 64)
  306. ch <- prometheus.MustNewConstMetric(
  307. c.ApiRtDesc,
  308. prometheus.GaugeValue,
  309. v3,
  310. v["appType"], v["requestUri"],
  311. )
  312. }
  313. resp, err = videoActionStore.GetLogs("", time.Now().Unix()-60, time.Now().Unix(), videoDataQuerySQL, 0, 0, true)
  314. if err != nil || resp == nil {
  315. log.Printf("get video data logs error %+v resp is %+v.", err, resp)
  316. return
  317. }
  318. for _, v := range resp.Logs {
  319. v3, _ := strconv.ParseFloat(v["cnt"], 64)
  320. ch <- prometheus.MustNewConstMetric(
  321. c.VideoDataDesc,
  322. prometheus.GaugeValue,
  323. v3,
  324. v["appType"], v["businessType"], v["pageSource"],
  325. )
  326. }
  327. resp, err = longvideoReqeustLogStore.GetLogs("", time.Now().Unix()-60, time.Now().Unix(), distributeApiQuerySQL, 0, 0, true)
  328. if err != nil || resp == nil {
  329. log.Printf("get distribute api logs error %+v resp is %+v.", err, resp)
  330. return
  331. }
  332. for _, v := range resp.Logs {
  333. // log.Printf("get distribute api logs %+v \n", v)
  334. for k, v2 := range v {
  335. if k == "等于0" ||
  336. k == "小于4" {
  337. v3, _ := strconv.ParseFloat(v2, 64)
  338. ch <- prometheus.MustNewConstMetric(
  339. c.DistributeApiDesc,
  340. prometheus.GaugeValue,
  341. v3,
  342. v["appType"], v["requestUri"], k,
  343. )
  344. }
  345. }
  346. }
  347. resp, err = videoActionStore.GetLogs("", time.Now().Unix()-60, time.Now().Unix(), shareDataQuerySQL, 0, 0, true)
  348. if err != nil || resp == nil {
  349. log.Printf("get share data logs error %+v resp is %+v.", err, resp)
  350. return
  351. }
  352. for _, v := range resp.Logs {
  353. v3, _ := strconv.ParseFloat(v["cnt"], 64)
  354. ch <- prometheus.MustNewConstMetric(
  355. c.ShareDataDesc,
  356. prometheus.GaugeValue,
  357. v3,
  358. v["appType"], v["businessType"], v["pageSource"], "user",
  359. )
  360. v3, _ = strconv.ParseFloat(v["cnt1"], 64)
  361. ch <- prometheus.MustNewConstMetric(
  362. c.ShareDataDesc,
  363. prometheus.GaugeValue,
  364. v3,
  365. v["appType"], v["businessType"], v["pageSource"], "all",
  366. )
  367. }
  368. resp, err = simpleeventStore.GetLogs("", time.Now().Unix()-60, time.Now().Unix(), shareOperation1QuerySQL, 0, 0, true)
  369. if err != nil || resp == nil {
  370. log.Printf("get share operation1 logs error %+v resp is %+v.", err, resp)
  371. return
  372. }
  373. for _, v := range resp.Logs {
  374. v3, _ := strconv.ParseFloat(v["cnt"], 64)
  375. ch <- prometheus.MustNewConstMetric(
  376. c.ShareOperationDesc,
  377. prometheus.GaugeValue,
  378. v3,
  379. v["appType"], v["objectType"],
  380. )
  381. }
  382. resp, err = userShareStore.GetLogs("", time.Now().Unix()-60, time.Now().Unix(), shareOperation2QuerySQL, 0, 0, true)
  383. if err != nil || resp == nil {
  384. log.Printf("get share operation2 logs error %+v resp is %+v.", err, resp)
  385. return
  386. }
  387. for _, v := range resp.Logs {
  388. v3, _ := strconv.ParseFloat(v["cnt"], 64)
  389. ch <- prometheus.MustNewConstMetric(
  390. c.ShareOperationDesc,
  391. prometheus.GaugeValue,
  392. v3,
  393. v["appType"], v["type"],
  394. )
  395. }
  396. }
  397. type CustomCollector struct {
  398. AdInfoDesc *prometheus.Desc
  399. AdOwnWechatInfoDesc *prometheus.Desc
  400. AdOwnInfoDesc *prometheus.Desc
  401. AdOwnConvDesc *prometheus.Desc
  402. ApiInfoDesc *prometheus.Desc
  403. AdConfigDesc *prometheus.Desc
  404. AdInfo4Desc *prometheus.Desc
  405. // 视频播放
  406. VideoPlayDesc *prometheus.Desc
  407. // 接口响应时间
  408. ApiRtDesc *prometheus.Desc
  409. // 播放数据
  410. VideoDataDesc *prometheus.Desc
  411. // 分发服务
  412. DistributeApiDesc *prometheus.Desc
  413. // 分享卡片数据
  414. ShareDataDesc *prometheus.Desc
  415. // 分享卡片操作
  416. ShareOperationDesc *prometheus.Desc
  417. }
  418. func main() {
  419. // 注册一个采集器
  420. reg := prometheus.NewPedanticRegistry()
  421. reg.MustRegister(&CustomCollector{
  422. AdInfoDesc: prometheus.NewDesc(
  423. "ad_metrics_info",
  424. "Front report ad metrics info",
  425. []string{"app_type", "ad_group", "ad_type", "_type"},
  426. prometheus.Labels{},
  427. ),
  428. AdOwnWechatInfoDesc: prometheus.NewDesc(
  429. "ad_own_wechat_metrics_info",
  430. "Front report ad own metrics info",
  431. []string{"app_type", "ad_group", "ad_type", "_type"},
  432. prometheus.Labels{},
  433. ),
  434. AdOwnInfoDesc: prometheus.NewDesc(
  435. "ad_own_metrics_info",
  436. "Front report ad own metrics info",
  437. []string{"app_type", "ad_group", "own_ad_positionId", "_type"},
  438. prometheus.Labels{},
  439. ),
  440. AdOwnConvDesc: prometheus.NewDesc(
  441. "ad_own_conv_metrics_info",
  442. "Front report ad own metrics info",
  443. []string{"app_id", "event_type", "_type"},
  444. prometheus.Labels{},
  445. ),
  446. ApiInfoDesc: prometheus.NewDesc(
  447. "api_metrics_info",
  448. "Core API Metrics Info",
  449. []string{"app_type", "url", "_type"},
  450. prometheus.Labels{},
  451. ),
  452. VideoPlayDesc: prometheus.NewDesc(
  453. "video_play_metrics_info",
  454. "Video Play Metrics Info",
  455. []string{"app_type", "_type", "source"},
  456. prometheus.Labels{},
  457. ),
  458. AdConfigDesc: prometheus.NewDesc(
  459. "ad_config_metrics_info",
  460. "Ad system deal request metrics info",
  461. []string{"app_type", "_type"},
  462. prometheus.Labels{},
  463. ),
  464. AdInfo4Desc: prometheus.NewDesc(
  465. "ad_metrics_info_004",
  466. "Front report ad metrics info ab_test004",
  467. []string{"app_type", "ad_group", "ad_type", "_type"},
  468. prometheus.Labels{},
  469. ),
  470. ApiRtDesc: prometheus.NewDesc(
  471. "api_metrics_rt",
  472. "Core API Metrics Response Time",
  473. []string{"app_type", "url"},
  474. prometheus.Labels{},
  475. ),
  476. VideoDataDesc: prometheus.NewDesc(
  477. "video_data_metrics",
  478. "Video Data Metrics",
  479. []string{"app_type", "_type", "source"},
  480. prometheus.Labels{},
  481. ),
  482. DistributeApiDesc: prometheus.NewDesc(
  483. "distribute_api_metrics",
  484. "Distribute Api Metrics",
  485. []string{"app_type", "url", "_type"},
  486. prometheus.Labels{},
  487. ),
  488. ShareDataDesc: prometheus.NewDesc(
  489. "share_data_metrics",
  490. "Share Data Metrics",
  491. []string{"app_type", "_type", "source", "catg"},
  492. prometheus.Labels{},
  493. ),
  494. ShareOperationDesc: prometheus.NewDesc(
  495. "share_operation_metrics",
  496. "Share Operation Metrics",
  497. []string{"app_type", "_type"},
  498. prometheus.Labels{},
  499. ),
  500. })
  501. http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
  502. promhttp.HandlerFor(prometheus.Gatherers{reg}, promhttp.HandlerOpts{}).ServeHTTP(w, r)
  503. })
  504. log.Println("pq Collector Start server at :10000")
  505. if err := http.ListenAndServe(":10000", nil); err != nil {
  506. log.Printf("Error occur when start server %v", err)
  507. os.Exit(1)
  508. }
  509. }