main.go 21 KB

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