main.go 26 KB

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