main.go 26 KB

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