Преглед на файлове

update Prometheus exporter

ehlxr преди 2 години
родител
ревизия
148e3dab23
променени са 1 файла, в които са добавени 40 реда и са изтрити 32 реда
  1. 40 32
      all/prometheus/main.go

+ 40 - 32
all/prometheus/main.go

@@ -33,8 +33,8 @@ var (
 	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`
 	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`
 	videoDataQuerySQL       = `* | select appType, COUNT(*) as cnt, businessType, pageSource GROUP by appType, businessType, pageSource ORDER BY appType ASC, businessType LIMIT 1000`
 	videoDataQuerySQL       = `* | select appType, COUNT(*) as cnt, businessType, pageSource GROUP by appType, businessType, pageSource ORDER BY appType ASC, businessType LIMIT 1000`
 	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`
 	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`
-	shareDataQuerySQL       = `* and ( businessType = 'videoShareFriend' OR businessType = 'videoShareH5' OR businessType = 'videoPublish' ) | select appType, COUNT(DISTINCT mid) as cnt, businessType, pageSource GROUP by appType, pageSource, businessType ORDER BY appType ASC, cnt DESC LIMIT 1000 `
-	shareOperation1QuerySQL = `* and objectType: weapp_quitbutton OR objectType: weapp_share_navigator_moveVideo OR objectType: weapp_share_upload | select objectType, appType, COUNT(*) as cnt GROUP by objectType, appType ORDER BY appType asc, objectType asc `
+	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 `
+	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 `
 	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 `
 	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 `
 )
 )
 
 
@@ -73,20 +73,20 @@ func init() {
 	}
 	}
 }
 }
 
 
-func (re *CustomExport) Describe(ch chan<- *prometheus.Desc) {
-	ch <- re.AdInfoDesc
-	ch <- re.ApiInfoDesc
-	ch <- re.VideoPlayDesc
-	ch <- re.AdConfigDesc
-	ch <- re.AdInfo4Desc
-	ch <- re.ApiRtDesc
-	ch <- re.VideoDataDesc
-	ch <- re.DistributeApiDesc
-	ch <- re.ShareDataDesc
-	ch <- re.ShareOperationDesc
+func (c *CustomCollector) Describe(ch chan<- *prometheus.Desc) {
+	ch <- c.AdInfoDesc
+	ch <- c.ApiInfoDesc
+	ch <- c.VideoPlayDesc
+	ch <- c.AdConfigDesc
+	ch <- c.AdInfo4Desc
+	ch <- c.ApiRtDesc
+	ch <- c.VideoDataDesc
+	ch <- c.DistributeApiDesc
+	ch <- c.ShareDataDesc
+	ch <- c.ShareOperationDesc
 }
 }
 
 
-func (re *CustomExport) Collect(ch chan<- prometheus.Metric) {
+func (c *CustomCollector) Collect(ch chan<- prometheus.Metric) {
 	resp, err := longvideoReqeustLogStore.GetLogs("", time.Now().Unix()-60, time.Now().Unix(), apiQuerySQL, 0, 0, true)
 	resp, err := longvideoReqeustLogStore.GetLogs("", time.Now().Unix()-60, time.Now().Unix(), apiQuerySQL, 0, 0, true)
 	if err != nil || resp == nil {
 	if err != nil || resp == nil {
 		fmt.Printf("get api logs error %+v resp is %+v.", err, resp)
 		fmt.Printf("get api logs error %+v resp is %+v.", err, resp)
@@ -99,7 +99,7 @@ func (re *CustomExport) Collect(ch chan<- prometheus.Metric) {
 				k == "error" {
 				k == "error" {
 				v3, _ := strconv.ParseFloat(v2, 64)
 				v3, _ := strconv.ParseFloat(v2, 64)
 				ch <- prometheus.MustNewConstMetric(
 				ch <- prometheus.MustNewConstMetric(
-					re.ApiInfoDesc,
+					c.ApiInfoDesc,
 					prometheus.GaugeValue,
 					prometheus.GaugeValue,
 					v3,
 					v3,
 					v["appType"], v["requestUri"], k,
 					v["appType"], v["requestUri"], k,
@@ -125,7 +125,7 @@ func (re *CustomExport) Collect(ch chan<- prometheus.Metric) {
 				k == "adClick" {
 				k == "adClick" {
 				v3, _ := strconv.ParseFloat(v2, 64)
 				v3, _ := strconv.ParseFloat(v2, 64)
 				ch <- prometheus.MustNewConstMetric(
 				ch <- prometheus.MustNewConstMetric(
-					re.AdInfoDesc,
+					c.AdInfoDesc,
 					prometheus.GaugeValue,
 					prometheus.GaugeValue,
 					v3,
 					v3,
 					v["appType"], v["adGroup"], v["adType"], k,
 					v["appType"], v["adGroup"], v["adType"], k,
@@ -150,7 +150,7 @@ func (re *CustomExport) Collect(ch chan<- prometheus.Metric) {
 				k == "adClick" {
 				k == "adClick" {
 				v3, _ := strconv.ParseFloat(v2, 64)
 				v3, _ := strconv.ParseFloat(v2, 64)
 				ch <- prometheus.MustNewConstMetric(
 				ch <- prometheus.MustNewConstMetric(
-					re.AdInfo4Desc,
+					c.AdInfo4Desc,
 					prometheus.GaugeValue,
 					prometheus.GaugeValue,
 					v3,
 					v3,
 					v["appType"], v["adGroup"], v["adType"], k,
 					v["appType"], v["adGroup"], v["adType"], k,
@@ -173,7 +173,7 @@ func (re *CustomExport) Collect(ch chan<- prometheus.Metric) {
 				k == "noAds" {
 				k == "noAds" {
 				v3, _ := strconv.ParseFloat(v2, 64)
 				v3, _ := strconv.ParseFloat(v2, 64)
 				ch <- prometheus.MustNewConstMetric(
 				ch <- prometheus.MustNewConstMetric(
-					re.AdConfigDesc,
+					c.AdConfigDesc,
 					prometheus.GaugeValue,
 					prometheus.GaugeValue,
 					v3,
 					v3,
 					v["appType"], k,
 					v["appType"], k,
@@ -190,7 +190,7 @@ func (re *CustomExport) Collect(ch chan<- prometheus.Metric) {
 	// for _, v := range resp.Logs {
 	// for _, v := range resp.Logs {
 	// 	v2, _ := strconv.ParseFloat(v["plyaCount"], 64)
 	// 	v2, _ := strconv.ParseFloat(v["plyaCount"], 64)
 	// 	ch <- prometheus.MustNewConstMetric(
 	// 	ch <- prometheus.MustNewConstMetric(
-	// 		re.VideoPlayDesc,
+	// 		c.VideoPlayDesc,
 	// 		prometheus.GaugeValue,
 	// 		prometheus.GaugeValue,
 	// 		v2,
 	// 		v2,
 	// 		v["appType"], "videoPlayTotal", v["pageSource"],
 	// 		v["appType"], "videoPlayTotal", v["pageSource"],
@@ -205,7 +205,7 @@ func (re *CustomExport) Collect(ch chan<- prometheus.Metric) {
 	for _, v := range resp.Logs {
 	for _, v := range resp.Logs {
 		v2, _ := strconv.ParseFloat(v["cnt"], 64)
 		v2, _ := strconv.ParseFloat(v["cnt"], 64)
 		ch <- prometheus.MustNewConstMetric(
 		ch <- prometheus.MustNewConstMetric(
-			re.VideoPlayDesc,
+			c.VideoPlayDesc,
 			prometheus.GaugeValue,
 			prometheus.GaugeValue,
 			v2,
 			v2,
 			v["appType"], v["businessType"], v["pageSource"],
 			v["appType"], v["businessType"], v["pageSource"],
@@ -220,7 +220,7 @@ func (re *CustomExport) Collect(ch chan<- prometheus.Metric) {
 	for _, v := range resp.Logs {
 	for _, v := range resp.Logs {
 		v3, _ := strconv.ParseFloat(v["rt"], 64)
 		v3, _ := strconv.ParseFloat(v["rt"], 64)
 		ch <- prometheus.MustNewConstMetric(
 		ch <- prometheus.MustNewConstMetric(
-			re.ApiRtDesc,
+			c.ApiRtDesc,
 			prometheus.GaugeValue,
 			prometheus.GaugeValue,
 			v3,
 			v3,
 			v["appType"], v["requestUri"],
 			v["appType"], v["requestUri"],
@@ -235,7 +235,7 @@ func (re *CustomExport) Collect(ch chan<- prometheus.Metric) {
 	for _, v := range resp.Logs {
 	for _, v := range resp.Logs {
 		v3, _ := strconv.ParseFloat(v["cnt"], 64)
 		v3, _ := strconv.ParseFloat(v["cnt"], 64)
 		ch <- prometheus.MustNewConstMetric(
 		ch <- prometheus.MustNewConstMetric(
-			re.VideoDataDesc,
+			c.VideoDataDesc,
 			prometheus.GaugeValue,
 			prometheus.GaugeValue,
 			v3,
 			v3,
 			v["appType"], v["businessType"], v["pageSource"],
 			v["appType"], v["businessType"], v["pageSource"],
@@ -248,13 +248,13 @@ func (re *CustomExport) Collect(ch chan<- prometheus.Metric) {
 		return
 		return
 	}
 	}
 	for _, v := range resp.Logs {
 	for _, v := range resp.Logs {
-		fmt.Printf("get distribute api logs  %+v \n", v)
+		// fmt.Printf("get distribute api logs  %+v \n", v)
 		for k, v2 := range v {
 		for k, v2 := range v {
 			if k == "等于0" ||
 			if k == "等于0" ||
 				k == "小于4" {
 				k == "小于4" {
 				v3, _ := strconv.ParseFloat(v2, 64)
 				v3, _ := strconv.ParseFloat(v2, 64)
 				ch <- prometheus.MustNewConstMetric(
 				ch <- prometheus.MustNewConstMetric(
-					re.DistributeApiDesc,
+					c.DistributeApiDesc,
 					prometheus.GaugeValue,
 					prometheus.GaugeValue,
 					v3,
 					v3,
 					v["appType"], v["requestUri"], k,
 					v["appType"], v["requestUri"], k,
@@ -271,10 +271,18 @@ func (re *CustomExport) Collect(ch chan<- prometheus.Metric) {
 	for _, v := range resp.Logs {
 	for _, v := range resp.Logs {
 		v3, _ := strconv.ParseFloat(v["cnt"], 64)
 		v3, _ := strconv.ParseFloat(v["cnt"], 64)
 		ch <- prometheus.MustNewConstMetric(
 		ch <- prometheus.MustNewConstMetric(
-			re.ShareDataDesc,
+			c.ShareDataDesc,
 			prometheus.GaugeValue,
 			prometheus.GaugeValue,
 			v3,
 			v3,
-			v["appType"], v["businessType"], v["pageSource"],
+			v["appType"], v["businessType"], v["pageSource"], "user",
+		)
+
+		v3, _ = strconv.ParseFloat(v["cnt1"], 64)
+		ch <- prometheus.MustNewConstMetric(
+			c.ShareDataDesc,
+			prometheus.GaugeValue,
+			v3,
+			v["appType"], v["businessType"], v["pageSource"], "all",
 		)
 		)
 	}
 	}
 
 
@@ -286,7 +294,7 @@ func (re *CustomExport) Collect(ch chan<- prometheus.Metric) {
 	for _, v := range resp.Logs {
 	for _, v := range resp.Logs {
 		v3, _ := strconv.ParseFloat(v["cnt"], 64)
 		v3, _ := strconv.ParseFloat(v["cnt"], 64)
 		ch <- prometheus.MustNewConstMetric(
 		ch <- prometheus.MustNewConstMetric(
-			re.ShareOperationDesc,
+			c.ShareOperationDesc,
 			prometheus.GaugeValue,
 			prometheus.GaugeValue,
 			v3,
 			v3,
 			v["appType"], v["objectType"],
 			v["appType"], v["objectType"],
@@ -300,7 +308,7 @@ func (re *CustomExport) Collect(ch chan<- prometheus.Metric) {
 	for _, v := range resp.Logs {
 	for _, v := range resp.Logs {
 		v3, _ := strconv.ParseFloat(v["cnt"], 64)
 		v3, _ := strconv.ParseFloat(v["cnt"], 64)
 		ch <- prometheus.MustNewConstMetric(
 		ch <- prometheus.MustNewConstMetric(
-			re.ShareOperationDesc,
+			c.ShareOperationDesc,
 			prometheus.GaugeValue,
 			prometheus.GaugeValue,
 			v3,
 			v3,
 			v["appType"], v["type"],
 			v["appType"], v["type"],
@@ -308,7 +316,7 @@ func (re *CustomExport) Collect(ch chan<- prometheus.Metric) {
 	}
 	}
 }
 }
 
 
-type CustomExport struct {
+type CustomCollector struct {
 	AdInfoDesc  *prometheus.Desc
 	AdInfoDesc  *prometheus.Desc
 	ApiInfoDesc *prometheus.Desc
 	ApiInfoDesc *prometheus.Desc
 	// 视频播放
 	// 视频播放
@@ -330,7 +338,7 @@ type CustomExport struct {
 func main() {
 func main() {
 	// 注册一个采集器
 	// 注册一个采集器
 	reg := prometheus.NewPedanticRegistry()
 	reg := prometheus.NewPedanticRegistry()
-	reg.MustRegister(&CustomExport{
+	reg.MustRegister(&CustomCollector{
 		AdInfoDesc: prometheus.NewDesc(
 		AdInfoDesc: prometheus.NewDesc(
 			"ad_metrics_info",
 			"ad_metrics_info",
 			"Front report ad metrics info",
 			"Front report ad metrics info",
@@ -382,7 +390,7 @@ func main() {
 		ShareDataDesc: prometheus.NewDesc(
 		ShareDataDesc: prometheus.NewDesc(
 			"share_data_metrics",
 			"share_data_metrics",
 			"Share Data Metrics",
 			"Share Data Metrics",
-			[]string{"app_type", "_type", "source"},
+			[]string{"app_type", "_type", "source", "catg"},
 			prometheus.Labels{},
 			prometheus.Labels{},
 		),
 		),
 		ShareOperationDesc: prometheus.NewDesc(
 		ShareOperationDesc: prometheus.NewDesc(
@@ -398,7 +406,7 @@ func main() {
 	})
 	})
 	fmt.Println("Start server at :10000")
 	fmt.Println("Start server at :10000")
 	if err := http.ListenAndServe(":10000", nil); err != nil {
 	if err := http.ListenAndServe(":10000", nil); err != nil {
-		fmt.Printf("Error occur when start exporter server %v", err)
+		fmt.Printf("Error occur when start server %v", err)
 		os.Exit(1)
 		os.Exit(1)
 	}
 	}
 }
 }