Browse Source

优化日志

jiantaoli 4 years ago
parent
commit
2206d45d03
4 changed files with 74 additions and 72 deletions
  1. 55 47
      ads_handler.go
  2. 10 10
      adslib/addata/custom.go
  3. 5 5
      adslib/addata/xiaomi.go
  4. 4 10
      adslib/device/device.go

+ 55 - 47
ads_handler.go

@@ -66,6 +66,11 @@ func adsHandler(c *gin.Context) {
66
 	request.Parse(c)
66
 	request.Parse(c)
67
 
67
 
68
 	log.Printf("%+v", request)
68
 	log.Printf("%+v", request)
69
+
70
+	// 放这里主要为了拿到reqeust id做日志记录
71
+	dspInfo := utils.DspParam{}
72
+	dspInfo.Init()
73
+
69
 	advertiser := "xiaomi"
74
 	advertiser := "xiaomi"
70
 	uaClient := request.UaClient
75
 	uaClient := request.UaClient
71
 	// 获取ua
76
 	// 获取ua
@@ -144,7 +149,7 @@ func adsHandler(c *gin.Context) {
144
 
149
 
145
 	// 定制city code
150
 	// 定制city code
146
 	cityCode, err := city.GetCityCode(ipInfo.City)
151
 	cityCode, err := city.GetCityCode(ipInfo.City)
147
-	log.Infof("got city code: %d", cityCode)
152
+	log.WithField("request_id", dspInfo.RequestId).Infof("got city code: %d", cityCode)
148
 	if err != nil {
153
 	if err != nil {
149
 		c.String(404, "get city code failed: %s", err)
154
 		c.String(404, "get city code failed: %s", err)
150
 		return
155
 		return
@@ -164,12 +169,12 @@ func adsHandler(c *gin.Context) {
164
 		// 频率控制
169
 		// 频率控制
165
 		freqControlConf, err := redis_data.GetFreqCrontolConf(request.ReqSource)
170
 		freqControlConf, err := redis_data.GetFreqCrontolConf(request.ReqSource)
166
 		if err != nil {
171
 		if err != nil {
167
-			log.Errorf("get freq control conf failed: %s", err)
172
+			log.WithField("request_id", dspInfo.RequestId).Errorf("get freq control conf failed: %s", err)
168
 			c.String(404, "get freq control conf failed: %s", err)
173
 			c.String(404, "get freq control conf failed: %s", err)
169
 			return
174
 			return
170
 		}
175
 		}
171
 
176
 
172
-		log.Infof("freq control conf: %+v", freqControlConf)
177
+		log.WithField("request_id", dspInfo.RequestId).Infof("freq control conf: %+v", freqControlConf)
173
 		hour, _ := strconv.Atoi(time.Now().Format("01"))
178
 		hour, _ := strconv.Atoi(time.Now().Format("01"))
174
 		tmpControlInterval, ok := freqControlConf.GetControlTime(hour)
179
 		tmpControlInterval, ok := freqControlConf.GetControlTime(hour)
175
 		if ok {
180
 		if ok {
@@ -187,7 +192,7 @@ func adsHandler(c *gin.Context) {
187
 	needControl := false
192
 	needControl := false
188
 	if request.ReqSource != "wzb_h5" && lastReqTime != 0 && time.Now().Unix()-lastReqTime < int64(freqControlInterval) {
193
 	if request.ReqSource != "wzb_h5" && lastReqTime != 0 && time.Now().Unix()-lastReqTime < int64(freqControlInterval) {
189
 		// 需要凭空
194
 		// 需要凭空
190
-		log.Infof("need control: %d", lastReqTime)
195
+		log.WithField("request_id", dspInfo.RequestId).Infof("need control: %d", lastReqTime)
191
 		needControl = true
196
 		needControl = true
192
 		device.SetAdsTagLog(advertiser, request.ReqSource, "DS_FREQ_0", cityCode)
197
 		device.SetAdsTagLog(advertiser, request.ReqSource, "DS_FREQ_0", cityCode)
193
 	} else {
198
 	} else {
@@ -197,7 +202,7 @@ func adsHandler(c *gin.Context) {
197
 	//### 检查是否是ip黑名单
202
 	//### 检查是否是ip黑名单
198
 	isIpBlack, err := ads_checker.CheckBlackIp(ip)
203
 	isIpBlack, err := ads_checker.CheckBlackIp(ip)
199
 	if err != nil {
204
 	if err != nil {
200
-		log.Errorf("check black ip failed: %s", err)
205
+		log.WithField("request_id", dspInfo.RequestId).Errorf("check black ip failed: %s", err)
201
 		c.String(404, "check black ip failed: %s", err)
206
 		c.String(404, "check black ip failed: %s", err)
202
 		return
207
 		return
203
 	}
208
 	}
@@ -209,7 +214,7 @@ func adsHandler(c *gin.Context) {
209
 	// 获取渠道的黑白性
214
 	// 获取渠道的黑白性
210
 	reqChannelFlag, err := redis_data.GetChannelFlag(request.ReqSource, "ads_req")
215
 	reqChannelFlag, err := redis_data.GetChannelFlag(request.ReqSource, "ads_req")
211
 	if err != nil {
216
 	if err != nil {
212
-		log.Errorf("get req channel flag failed: %s", err)
217
+		log.WithField("request_id", dspInfo.RequestId).Errorf("get req channel flag failed: %s", err)
213
 		c.String(404, "get req channel flag failed: %s", err)
218
 		c.String(404, "get req channel flag failed: %s", err)
214
 		return
219
 		return
215
 	}
220
 	}
@@ -241,12 +246,12 @@ func adsHandler(c *gin.Context) {
241
 		randomNum := 3
246
 		randomNum := 3
242
 		deviceConf, realRedisKey, err := device.GetMiDeviceConf(ip, randomNum, specialDeviceNum)
247
 		deviceConf, realRedisKey, err := device.GetMiDeviceConf(ip, randomNum, specialDeviceNum)
243
 		if err != nil {
248
 		if err != nil {
244
-			log.Errorf("get mi device conf failed: %s", err)
249
+			log.WithField("request_id", dspInfo.RequestId).Errorf("get mi device conf failed: %s", err)
245
 			c.String(404, "get mi device conf failed: %s", err)
250
 			c.String(404, "get mi device conf failed: %s", err)
246
 			return
251
 			return
247
 		}
252
 		}
248
 		if deviceConf != nil {
253
 		if deviceConf != nil {
249
-			log.Infof("use cache imei: %+v", deviceConf)
254
+			log.WithField("request_id", dspInfo.RequestId).Infof("use cache imei: %+v", deviceConf)
250
 			device.SetAdsTagLog(advertiser, request.ReqSource, "DS_CACHE", cityCode) // 通过缓存请求
255
 			device.SetAdsTagLog(advertiser, request.ReqSource, "DS_CACHE", cityCode) // 通过缓存请求
251
 			imei = deviceConf.Imei
256
 			imei = deviceConf.Imei
252
 			uaClient = deviceConf.Ua
257
 			uaClient = deviceConf.Ua
@@ -266,7 +271,7 @@ func adsHandler(c *gin.Context) {
266
 				fakeImei := ""
271
 				fakeImei := ""
267
 				fakeDeviceConf, leftCnt, err := device.GetDailyFakeDeviceConfByCityCode(cityCode)
272
 				fakeDeviceConf, leftCnt, err := device.GetDailyFakeDeviceConfByCityCode(cityCode)
268
 				if err != nil {
273
 				if err != nil {
269
-					log.Errorf("get device conf failed: %s", err)
274
+					log.WithField("request_id", dspInfo.RequestId).Errorf("get device conf failed: %s", err)
270
 					c.String(404, "get device conf failed: %s", err)
275
 					c.String(404, "get device conf failed: %s", err)
271
 					return
276
 					return
272
 				}
277
 				}
@@ -278,7 +283,7 @@ func adsHandler(c *gin.Context) {
278
 					device.SetAdsTagLog(advertiser, request.ReqSource, "DS_REPL", cityCode)
283
 					device.SetAdsTagLog(advertiser, request.ReqSource, "DS_REPL", cityCode)
279
 					err := device.SetMiDeviceConf(realRedisKey, *fakeDeviceConf)
284
 					err := device.SetMiDeviceConf(realRedisKey, *fakeDeviceConf)
280
 					if err != nil {
285
 					if err != nil {
281
-						log.Errorf("set mi device conf failed: %s", err)
286
+						log.WithField("request_id", dspInfo.RequestId).Errorf("set mi device conf failed: %s", err)
282
 					}
287
 					}
283
 					imei = fakeDeviceConf.Imei
288
 					imei = fakeDeviceConf.Imei
284
 					uaClient = fakeDeviceConf.Ua
289
 					uaClient = fakeDeviceConf.Ua
@@ -325,34 +330,37 @@ func adsHandler(c *gin.Context) {
325
 		// 解码ua_client
330
 		// 解码ua_client
326
 		uaClient, _ = url.QueryUnescape(uaClient)
331
 		uaClient, _ = url.QueryUnescape(uaClient)
327
 	} else if request.IsMiDevice() {
332
 	} else if request.IsMiDevice() {
328
-		log.Info("real mi")
333
+		log.WithField("request_id", dspInfo.RequestId).Info("real mi")
329
 		device.SetAdsTagLog(advertiser, request.ReqSource, "DS_MI_REAL", cityCode) // 是小米设备
334
 		device.SetAdsTagLog(advertiser, request.ReqSource, "DS_MI_REAL", cityCode) // 是小米设备
330
 		userFlag = 3
335
 		userFlag = 3
331
 		sendPhoneType = 1
336
 		sendPhoneType = 1
332
 	}
337
 	}
333
 
338
 
334
 	// 检查下,替换后的设备是否是黑名单, 再次检查, 防止放入cache后, 新增了黑名单
339
 	// 检查下,替换后的设备是否是黑名单, 再次检查, 防止放入cache后, 新增了黑名单
335
-	isBlackImei, err := device.CheckIsBlackImei(imei, false)
340
+	checkImei := imei
341
+	// 替换过的imei从redis取的, 都是md5后的, 不需要再md5
342
+	if replaceFlag != 1 {
343
+		checkImei = utils.Md5(imei)
344
+	}
345
+	isBlackImei, err := device.CheckIsBlackImei(checkImei)
336
 	if err != nil {
346
 	if err != nil {
337
-		log.Errorf("get device conf failed: %s", err)
347
+		log.WithField("request_id", dspInfo.RequestId).Errorf("get device conf failed: %s", err)
338
 		c.String(404, "get device conf failed: %s", err)
348
 		c.String(404, "get device conf failed: %s", err)
339
 		return
349
 		return
340
 	}
350
 	}
341
 
351
 
342
-	log.Tracef("replace flag: %d", replaceFlag)
352
+	log.WithField("request_id", dspInfo.RequestId).Tracef("replace flag: %d", replaceFlag)
343
 	if isBlackImei {
353
 	if isBlackImei {
344
 		if replaceFlag == 1 {
354
 		if replaceFlag == 1 {
345
-			log.Trace("replace")
355
+			log.WithField("request_id", dspInfo.RequestId).Trace("replace")
346
 			device.SetAdsTagLog(advertiser, request.ReqSource, "DS_BLACK_IMEI_REPLACE", cityCode)
356
 			device.SetAdsTagLog(advertiser, request.ReqSource, "DS_BLACK_IMEI_REPLACE", cityCode)
347
 		} else {
357
 		} else {
348
-			log.Trace("not replace")
358
+			log.WithField("request_id", dspInfo.RequestId).Trace("not replace")
349
 			device.SetAdsTagLog(advertiser, request.ReqSource, "DS_BLACK_IMEI", cityCode)
359
 			device.SetAdsTagLog(advertiser, request.ReqSource, "DS_BLACK_IMEI", cityCode)
350
 		}
360
 		}
351
 	}
361
 	}
352
 
362
 
353
 	// 组装公共的dsp_info
363
 	// 组装公共的dsp_info
354
-	dspInfo := utils.DspParam{}
355
-	dspInfo.Init()
356
 	dspInfo.DspCityCode = cityCode
364
 	dspInfo.DspCityCode = cityCode
357
 	dspInfo.Imei = imei
365
 	dspInfo.Imei = imei
358
 	dspInfo.OriginImei = originImei
366
 	dspInfo.OriginImei = originImei
@@ -379,12 +387,12 @@ func adsHandler(c *gin.Context) {
379
 	dspInfo.RealReqSource = request.ReqSource
387
 	dspInfo.RealReqSource = request.ReqSource
380
 	dspInfo.SendPhoneType = sendPhoneType
388
 	dspInfo.SendPhoneType = sendPhoneType
381
 	md5Imei := imei
389
 	md5Imei := imei
382
-	if dspInfo.ReplaceFlag == 0 {
383
-		md5Imei = utils.Md5(imei)
390
+	if dspInfo.OriginImei != "" {
391
+		md5Imei = utils.Md5(dspInfo.OriginImei)
384
 	}
392
 	}
385
 
393
 
386
 	dspInfo.RealMd5Imei = md5Imei
394
 	dspInfo.RealMd5Imei = md5Imei
387
-	log.Infof("dsp: %+v", dspInfo)
395
+	log.WithField("request_id", dspInfo.RequestId).Infof("dsp: %+v", dspInfo)
388
 	//ads_item = None
396
 	//ads_item = None
389
 	//xiaomi_response = []
397
 	//xiaomi_response = []
390
 	hour, _ := strconv.Atoi(time.Now().Format("01"))
398
 	hour, _ := strconv.Atoi(time.Now().Format("01"))
@@ -397,7 +405,7 @@ func adsHandler(c *gin.Context) {
397
 		}
405
 		}
398
 	}
406
 	}
399
 
407
 
400
-	log.Infof("can request: %t", canRequest)
408
+	log.WithField("request_id", dspInfo.RequestId).Infof("can request: %t", canRequest)
401
 
409
 
402
 	var adData *addata.AdData
410
 	var adData *addata.AdData
403
 
411
 
@@ -405,11 +413,11 @@ func adsHandler(c *gin.Context) {
405
 	if canRequest && !isBlackImei && reqChannelFlag.ChannelFlag == 1 && flowFlag == 1 && !isIpBlack && advertiser == "xiaomi" && request.ReqSource != "" && !needControl && (replaceFlag == 1 || request.IsMiDevice()) {
413
 	if canRequest && !isBlackImei && reqChannelFlag.ChannelFlag == 1 && flowFlag == 1 && !isIpBlack && advertiser == "xiaomi" && request.ReqSource != "" && !needControl && (replaceFlag == 1 || request.IsMiDevice()) {
406
 		adData, err = addata.GetAdsInfos(&dspInfo, advertiser)
414
 		adData, err = addata.GetAdsInfos(&dspInfo, advertiser)
407
 		if err != nil {
415
 		if err != nil {
408
-			log.Errorf("get mi ad data failed: %s", err)
416
+			log.WithField("request_id", dspInfo.RequestId).Errorf("get mi ad data failed: %s", err)
409
 			c.String(404, "get mi ad data failed: %s", err)
417
 			c.String(404, "get mi ad data failed: %s", err)
410
 			return
418
 			return
411
 		}
419
 		}
412
-		log.Infof("get mi ads data: %+v", adData)
420
+		log.WithField("request_id", dspInfo.RequestId).Infof("get mi ads data: %+v", adData)
413
 	}
421
 	}
414
 
422
 
415
 	canMixFlag := 1
423
 	canMixFlag := 1
@@ -417,7 +425,7 @@ func adsHandler(c *gin.Context) {
417
 	response := Response{}
425
 	response := Response{}
418
 	response.Msg = "ok"
426
 	response.Msg = "ok"
419
 	if adData != nil && len(adData.TargetAddition) > 1 {
427
 	if adData != nil && len(adData.TargetAddition) > 1 {
420
-		log.Infof("add target js order")
428
+		log.WithField("request_id", dspInfo.RequestId).Infof("add target js order")
421
 		xiaomiResponseFlag = 1
429
 		xiaomiResponseFlag = 1
422
 		realTarget := ""
430
 		realTarget := ""
423
 		if adData.Target != "" {
431
 		if adData.Target != "" {
@@ -432,11 +440,11 @@ func adsHandler(c *gin.Context) {
432
 		adData.Target = realTarget
440
 		adData.Target = realTarget
433
 		adData.JsOrderId, err = redis_data.GetMinScriptOrderByAdv(advertiser)
441
 		adData.JsOrderId, err = redis_data.GetMinScriptOrderByAdv(advertiser)
434
 		if err != nil {
442
 		if err != nil {
435
-			log.Errorf("get min script order by adv failed: %s", err)
443
+			log.WithField("request_id", dspInfo.RequestId).Errorf("get min script order by adv failed: %s", err)
436
 			c.String(404, "get min script order by adv failed: %s", err)
444
 			c.String(404, "get min script order by adv failed: %s", err)
437
 			return
445
 			return
438
 		}
446
 		}
439
-		log.Infof("got min script order: %d", adData.JsOrderId)
447
+		log.WithField("request_id", dspInfo.RequestId).Infof("got min script order: %d", adData.JsOrderId)
440
 		adData.UserAgent = uaClient
448
 		adData.UserAgent = uaClient
441
 
449
 
442
 		if dspInfo.ReqSource == "kuxin" && adData.Target != "" {
450
 		if dspInfo.ReqSource == "kuxin" && adData.Target != "" {
@@ -452,11 +460,11 @@ func adsHandler(c *gin.Context) {
452
 		// // 判断这个渠道是否要去融合和融合的流量占比
460
 		// // 判断这个渠道是否要去融合和融合的流量占比
453
 		// mixChannelFlag, err := redis_data.GetChannelFlag(dspInfo.ReqSource, "ads_mix")
461
 		// mixChannelFlag, err := redis_data.GetChannelFlag(dspInfo.ReqSource, "ads_mix")
454
 		// if err != nil {
462
 		// if err != nil {
455
-		// 	log.Errorf("get device conf failed: %s", err)
463
+		// 	log.WithField("request_id", dspInfo.RequestId).Errorf("get device conf failed: %s", err)
456
 		// 	c.String(404, "get device conf failed: %s", err)
464
 		// 	c.String(404, "get device conf failed: %s", err)
457
 		// 	return
465
 		// 	return
458
 		// }
466
 		// }
459
-		flowRandomNum = rand.Intn(100)
467
+		// flowRandomNum = rand.Intn(100)
460
 		//isOverFlow := false
468
 		//isOverFlow := false
461
 		//if flowRandomNum > mixChannelFlag.Weigth {
469
 		//if flowRandomNum > mixChannelFlag.Weigth {
462
 		//	isOverFlow = true
470
 		//	isOverFlow = true
@@ -481,25 +489,25 @@ func adsHandler(c *gin.Context) {
481
 				serverActionResponse["server_video_timer"] = 1
489
 				serverActionResponse["server_video_timer"] = 1
482
 			}
490
 			}
483
 		}
491
 		}
484
-		log.Infof("server action response: %+v", serverActionResponse)
492
+		log.WithField("request_id", dspInfo.RequestId).Infof("server action response: %+v", serverActionResponse)
485
 		// 增加跟随订单
493
 		// 增加跟随订单
486
 		if len(adData.TargetAddition) == 2 && serverActionResponse["server_video_finish"] == 1 && (dspInfo.ReqSource == "kuxin" || dspInfo.ReqSource == "zhiku") {
494
 		if len(adData.TargetAddition) == 2 && serverActionResponse["server_video_finish"] == 1 && (dspInfo.ReqSource == "kuxin" || dspInfo.ReqSource == "zhiku") {
487
-			log.Infof("add more order")
495
+			log.WithField("request_id", dspInfo.RequestId).Infof("add more order")
488
 			adData, err = addata.CombineOrderBy(adData, &dspInfo)
496
 			adData, err = addata.CombineOrderBy(adData, &dspInfo)
489
 			if err != nil {
497
 			if err != nil {
490
-				log.Errorf("combine order failed: %s", err)
498
+				log.WithField("request_id", dspInfo.RequestId).Errorf("combine order failed: %s", err)
491
 				c.String(404, "combine order failed: %s", err.Error())
499
 				c.String(404, "combine order failed: %s", err.Error())
492
 				return
500
 				return
493
 			}
501
 			}
494
-			log.Infof("get custom order: %+v", adData)
502
+			log.WithField("request_id", dspInfo.RequestId).Infof("get custom order: %+v", adData)
495
 		}
503
 		}
496
 
504
 
497
 		//# 检查最后是否有click
505
 		//# 检查最后是否有click
498
-		log.Infof("check ads item new: %+v", adData)
506
+		log.WithField("request_id", dspInfo.RequestId).Infof("check ads item new: %+v", adData)
499
 
507
 
500
 		for _, targetAddition := range adData.TargetAddition {
508
 		for _, targetAddition := range adData.TargetAddition {
501
 			if targetAddition.Type == "CLICK" || targetAddition.Type == "VIDEO_TIMER" {
509
 			if targetAddition.Type == "CLICK" || targetAddition.Type == "VIDEO_TIMER" {
502
-				log.Infof("can't mix, targetAddition: %+v", targetAddition)
510
+				log.WithField("request_id", dspInfo.RequestId).Infof("can't mix, targetAddition: %+v", targetAddition)
503
 				canMixFlag = 0
511
 				canMixFlag = 0
504
 				break
512
 				break
505
 			}
513
 			}
@@ -507,7 +515,7 @@ func adsHandler(c *gin.Context) {
507
 
515
 
508
 		if request.NewAdsFlag == 0 {
516
 		if request.NewAdsFlag == 0 {
509
 			response.AdData = *adData
517
 			response.AdData = *adData
510
-			log.Infof("update response: +%v", response)
518
+			log.WithField("request_id", dspInfo.RequestId).Infof("update response: +%v", response)
511
 		} else {
519
 		} else {
512
 			jsonBytes, err := json.Marshal(*adData)
520
 			jsonBytes, err := json.Marshal(*adData)
513
 			if err != nil {
521
 			if err != nil {
@@ -519,7 +527,7 @@ func adsHandler(c *gin.Context) {
519
 		}
527
 		}
520
 	}
528
 	}
521
 
529
 
522
-	log.Infof("can mix: %d, xiaomi rsp flag: %d", canMixFlag, xiaomiResponseFlag)
530
+	log.WithField("request_id", dspInfo.RequestId).Infof("can mix: %d, xiaomi rsp flag: %d", canMixFlag, xiaomiResponseFlag)
523
 	if canMixFlag == 1 {
531
 	if canMixFlag == 1 {
524
 		response.Result = 2
532
 		response.Result = 2
525
 		response.Msg = "no ads"
533
 		response.Msg = "no ads"
@@ -536,7 +544,7 @@ func adsHandler(c *gin.Context) {
536
 				c.String(404, "get custom ads info failed: %s", err.Error())
544
 				c.String(404, "get custom ads info failed: %s", err.Error())
537
 				return
545
 				return
538
 			}
546
 			}
539
-			log.Infof("dingtou: %+v", customAdData)
547
+			log.WithField("request_id", dspInfo.RequestId).Infof("dingtou: %+v", customAdData)
540
 
548
 
541
 			// 定投没有就跑其他的
549
 			// 定投没有就跑其他的
542
 			if customAdData == nil {
550
 			if customAdData == nil {
@@ -554,7 +562,7 @@ func adsHandler(c *gin.Context) {
554
 			if customAdData != nil && (shortMessage == "ads_vast_response" || shortMessage == "ads_tt_thirds") {
562
 			if customAdData != nil && (shortMessage == "ads_vast_response" || shortMessage == "ads_tt_thirds") {
555
 				adDataBytes, err := json.Marshal(*customAdData)
563
 				adDataBytes, err := json.Marshal(*customAdData)
556
 				if err != nil {
564
 				if err != nil {
557
-					log.Errorf("marshal adData failed: %s\n", err)
565
+					log.WithField("request_id", dspInfo.RequestId).Errorf("marshal adData failed: %s\n", err)
558
 				}
566
 				}
559
 				grayLogData := struct {
567
 				grayLogData := struct {
560
 					Ip           string
568
 					Ip           string
@@ -606,7 +614,7 @@ func adsHandler(c *gin.Context) {
606
 
614
 
607
 					adDataBytes, err := json.Marshal(lastAdData)
615
 					adDataBytes, err := json.Marshal(lastAdData)
608
 					if err != nil {
616
 					if err != nil {
609
-						log.Errorf("marshal adData failed: %s", err)
617
+						log.WithField("request_id", dspInfo.RequestId).Errorf("marshal adData failed: %s", err)
610
 					}
618
 					}
611
 					grayLogData := struct {
619
 					grayLogData := struct {
612
 						ShortMessage string `json:"short_message"`
620
 						ShortMessage string `json:"short_message"`
@@ -617,7 +625,7 @@ func adsHandler(c *gin.Context) {
617
 					}
625
 					}
618
 
626
 
619
 					graylog.Log(grayLogData)
627
 					graylog.Log(grayLogData)
620
-					log.Infof("replace addata: %+v", lastAdData)
628
+					log.WithField("request_id", dspInfo.RequestId).Infof("replace addata: %+v", lastAdData)
621
 					customAdData = &lastAdData
629
 					customAdData = &lastAdData
622
 				}
630
 				}
623
 			}
631
 			}
@@ -627,19 +635,19 @@ func adsHandler(c *gin.Context) {
627
 					response.Result = 0
635
 					response.Result = 0
628
 					response.Msg = "ok"
636
 					response.Msg = "ok"
629
 
637
 
630
-					log.Infof("rsp1: %+v", customAdData)
638
+					log.WithField("request_id", dspInfo.RequestId).Infof("rsp1: %+v", customAdData)
631
 					jsonBytes, err := json.Marshal(customAdData)
639
 					jsonBytes, err := json.Marshal(customAdData)
632
 					if err != nil {
640
 					if err != nil {
633
 						c.String(404, "marsha custom addata failed: %s", err.Error())
641
 						c.String(404, "marsha custom addata failed: %s", err.Error())
634
 						return
642
 						return
635
 					}
643
 					}
636
-					log.Infof("rsp1: %s", jsonBytes)
644
+					log.WithField("request_id", dspInfo.RequestId).Infof("rsp1: %s", jsonBytes)
637
 					encryptData, _ := encrypt.Encrypt(jsonBytes, []byte(adslib.GetConf().SecretKey))
645
 					encryptData, _ := encrypt.Encrypt(jsonBytes, []byte(adslib.GetConf().SecretKey))
638
 					response.AdsResult = encryptData
646
 					response.AdsResult = encryptData
639
 
647
 
640
-					log.Infof("encrypt: %s", encryptData)
648
+					log.WithField("request_id", dspInfo.RequestId).Infof("encrypt: %s", encryptData)
641
 					s, err := encrypt.Decrypt(encryptData, []byte(adslib.GetConf().SecretKey))
649
 					s, err := encrypt.Decrypt(encryptData, []byte(adslib.GetConf().SecretKey))
642
-					log.Info(s, err)
650
+					log.WithField("request_id", dspInfo.RequestId).Info(s, err)
643
 
651
 
644
 					graylog.ReportGrayLog(request, dspInfo, 0, 0)
652
 					graylog.ReportGrayLog(request, dspInfo, 0, 0)
645
 					rspBytes, err := json.Marshal(response)
653
 					rspBytes, err := json.Marshal(response)
@@ -647,7 +655,7 @@ func adsHandler(c *gin.Context) {
647
 						c.String(404, "marshal Response failed: %s", err.Error())
655
 						c.String(404, "marshal Response failed: %s", err.Error())
648
 						return
656
 						return
649
 					}
657
 					}
650
-					log.Infof("rsp1: %s", rspBytes)
658
+					log.WithField("request_id", dspInfo.RequestId).Infof("rsp1: %s", rspBytes)
651
 					c.String(200, string(rspBytes))
659
 					c.String(200, string(rspBytes))
652
 					return
660
 					return
653
 				} else {
661
 				} else {
@@ -676,6 +684,6 @@ func adsHandler(c *gin.Context) {
676
 		graylog.ReportGrayLog(request, dspInfo, 0, 0)
684
 		graylog.ReportGrayLog(request, dspInfo, 0, 0)
677
 	}
685
 	}
678
 	device.SetAdsTagLog(advertiser, dspInfo.ReqSource, "DS_REQ", cityCode) // 所有请求
686
 	device.SetAdsTagLog(advertiser, dspInfo.ReqSource, "DS_REQ", cityCode) // 所有请求
679
-	log.Infof("rsp3: %s", rspBytes)
687
+	log.WithField("request_id", dspInfo.RequestId).Infof("rsp3: %s", rspBytes)
680
 	c.String(200, string(rspBytes))
688
 	c.String(200, string(rspBytes))
681
 }
689
 }

+ 10 - 10
adslib/addata/custom.go

@@ -147,14 +147,14 @@ func getNeedDispatchCount(adData *redis_data.AdOrderInfo) (int, error) {
147
 func GetCustomAdsInfos(dsp *utils.DspParam, orderType int, fixFlag int, xiaomiHasFlag int) (*AdData, error) {
147
 func GetCustomAdsInfos(dsp *utils.DspParam, orderType int, fixFlag int, xiaomiHasFlag int) (*AdData, error) {
148
 	order, err := GetOneAds(dsp, orderType, fixFlag)
148
 	order, err := GetOneAds(dsp, orderType, fixFlag)
149
 	if err != nil {
149
 	if err != nil {
150
-		log.Errorf("get one ads failed: %s", err)
150
+		log.WithField("request_id", dsp.RequestId).Errorf("get one ads failed: %s", err)
151
 		return nil, err
151
 		return nil, err
152
 	}
152
 	}
153
 	if order == nil {
153
 	if order == nil {
154
 		return nil, nil
154
 		return nil, nil
155
 	}
155
 	}
156
 
156
 
157
-	log.Infof("begin got custom ad info, xiaomi has flag: %d", xiaomiHasFlag)
157
+	log.WithField("request_id", dsp.RequestId).Infof("begin got custom ad info, xiaomi has flag: %d", xiaomiHasFlag)
158
 	if xiaomiHasFlag == 1 {
158
 	if xiaomiHasFlag == 1 {
159
 		if strings.Index(order.Title, "_ios") > 0 {
159
 		if strings.Index(order.Title, "_ios") > 0 {
160
 			return nil, nil
160
 			return nil, nil
@@ -181,7 +181,7 @@ func GetCustomAdsInfos(dsp *utils.DspParam, orderType int, fixFlag int, xiaomiHa
181
 
181
 
182
 	redis_data.SetPlanDispatchCount(order.OrderID, "show", needDispatchCnt, curTime)
182
 	redis_data.SetPlanDispatchCount(order.OrderID, "show", needDispatchCnt, curTime)
183
 
183
 
184
-	log.Infof("all count: %d, over kpi: %d, show kpi: %d", needDispatchCnt, finishShowCnt, order.ShowKpi)
184
+	log.WithField("request_id", dsp.RequestId).Infof("all count: %d, over kpi: %d, show kpi: %d", needDispatchCnt, finishShowCnt, order.ShowKpi)
185
 	// 计算曲线比例
185
 	// 计算曲线比例
186
 	rate := float32(order.ShowKpi) / float32(needDispatchCnt)
186
 	rate := float32(order.ShowKpi) / float32(needDispatchCnt)
187
 
187
 
@@ -195,7 +195,7 @@ func GetCustomAdsInfos(dsp *utils.DspParam, orderType int, fixFlag int, xiaomiHa
195
 	if err != nil {
195
 	if err != nil {
196
 		return nil, err
196
 		return nil, err
197
 	}
197
 	}
198
-	log.Infof("line value: %d, rate: %f", lineValue, rate)
198
+	log.WithField("request_id", dsp.RequestId).Infof("line value: %d, rate: %f", lineValue, rate)
199
 
199
 
200
 	// 当前分钟需要放出去的数量
200
 	// 当前分钟需要放出去的数量
201
 	curMinuteNeedDispatchCnt := int(float32(lineValue) * rate)
201
 	curMinuteNeedDispatchCnt := int(float32(lineValue) * rate)
@@ -205,11 +205,11 @@ func GetCustomAdsInfos(dsp *utils.DspParam, orderType int, fixFlag int, xiaomiHa
205
 
205
 
206
 	redis_data.SetOrderPlanDispatchCount(order.OrderID, "show", curMinuteNeedDispatchCnt, curTime)
206
 	redis_data.SetOrderPlanDispatchCount(order.OrderID, "show", curMinuteNeedDispatchCnt, curTime)
207
 
207
 
208
-	log.Infof("tt thirds: %+v", order)
208
+	log.WithField("request_id", dsp.RequestId).Infof("tt thirds: %+v", order)
209
 	// 获取当前分钟已经完成的下发
209
 	// 获取当前分钟已经完成的下发
210
 	curMinuteFinishedDispatchCnt, err := redis_data.GetPreMinuteFinishedDispatchCount(order.OrderID, "show", curTime)
210
 	curMinuteFinishedDispatchCnt, err := redis_data.GetPreMinuteFinishedDispatchCount(order.OrderID, "show", curTime)
211
 
211
 
212
-	log.Infof("o value: %d, w value: %d", curMinuteFinishedDispatchCnt, curMinuteNeedDispatchCnt)
212
+	log.WithField("request_id", dsp.RequestId).Infof("o value: %d, w value: %d", curMinuteFinishedDispatchCnt, curMinuteNeedDispatchCnt)
213
 	if curMinuteFinishedDispatchCnt < curMinuteNeedDispatchCnt {
213
 	if curMinuteFinishedDispatchCnt < curMinuteNeedDispatchCnt {
214
 		data := AdData{
214
 		data := AdData{
215
 			Duration:  5,
215
 			Duration:  5,
@@ -235,7 +235,7 @@ func GetCustomAdsInfos(dsp *utils.DspParam, orderType int, fixFlag int, xiaomiHa
235
 		if strings.Index(order.Title, "_ios") != -1 {
235
 		if strings.Index(order.Title, "_ios") != -1 {
236
 			iosImei, iosUa, err := redis_data.GetIosUaImei(dsp.Ip)
236
 			iosImei, iosUa, err := redis_data.GetIosUaImei(dsp.Ip)
237
 			if err != nil {
237
 			if err != nil {
238
-				log.Errorf("get ios ua imei failed: %s", err)
238
+				log.WithField("request_id", dsp.RequestId).Errorf("get ios ua imei failed: %s", err)
239
 				return nil, err
239
 				return nil, err
240
 			}
240
 			}
241
 			if iosUa != "" {
241
 			if iosUa != "" {
@@ -270,7 +270,7 @@ func GetCustomAdsInfos(dsp *utils.DspParam, orderType int, fixFlag int, xiaomiHa
270
 			if r < 40 && xiaomiHasFlag == 0 {
270
 			if r < 40 && xiaomiHasFlag == 0 {
271
 				iosImei, iosUa, err := redis_data.GetIosUaImei(dsp.Ip)
271
 				iosImei, iosUa, err := redis_data.GetIosUaImei(dsp.Ip)
272
 				if err != nil {
272
 				if err != nil {
273
-					log.Errorf("get ios ua imei failed: %s", err)
273
+					log.WithField("request_id", dsp.RequestId).Errorf("get ios ua imei failed: %s", err)
274
 					return nil, err
274
 					return nil, err
275
 				}
275
 				}
276
 				if iosUa != "" {
276
 				if iosUa != "" {
@@ -320,7 +320,7 @@ func GetCustomAdsInfos(dsp *utils.DspParam, orderType int, fixFlag int, xiaomiHa
320
 
320
 
321
 		r := rand.Intn(1000)
321
 		r := rand.Intn(1000)
322
 		clickRate := int(float32(order.ClickKpi) / float32(order.ShowKpi) * 1000)
322
 		clickRate := int(float32(order.ClickKpi) / float32(order.ShowKpi) * 1000)
323
-		log.Infof("rand: %d, rate: %d", r, clickRate)
323
+		log.WithField("request_id", dsp.RequestId).Infof("rand: %d, rate: %d", r, clickRate)
324
 		if r < clickRate {
324
 		if r < clickRate {
325
 			//下发点击
325
 			//下发点击
326
 			addi = genMonitorAction("CLICK", order.Title, dsp.ReqSource, clickUrl)
326
 			addi = genMonitorAction("CLICK", order.Title, dsp.ReqSource, clickUrl)
@@ -328,7 +328,7 @@ func GetCustomAdsInfos(dsp *utils.DspParam, orderType int, fixFlag int, xiaomiHa
328
 			md5Skip := utils.Md5(targetUrl)
328
 			md5Skip := utils.Md5(targetUrl)
329
 			_, err = redis_data.IncrFinishedDispatchCount(order.OrderID, "click", 1, curTime)
329
 			_, err = redis_data.IncrFinishedDispatchCount(order.OrderID, "click", 1, curTime)
330
 			if err != nil {
330
 			if err != nil {
331
-				log.Errorf("incr finished dispatch count failed: %s", err)
331
+				log.WithField("request_id", dsp.RequestId).Errorf("incr finished dispatch count failed: %s", err)
332
 			}
332
 			}
333
 			realTarget := adslib.GetConf().Host + fmt.Sprintf("?action=LOADING&req_source=%s&advertiser=%s&skip=%s&skip_other=%s",
333
 			realTarget := adslib.GetConf().Host + fmt.Sprintf("?action=LOADING&req_source=%s&advertiser=%s&skip=%s&skip_other=%s",
334
 				dsp.ReqSource, order.Title, "", md5Skip)
334
 				dsp.ReqSource, order.Title, "", md5Skip)

+ 5 - 5
adslib/addata/xiaomi.go

@@ -190,7 +190,7 @@ func GetAdsInfos(dsp *utils.DspParam, advertiser string) (*AdData, error) {
190
 		target := html.UnescapeString(target)
190
 		target := html.UnescapeString(target)
191
 		targetParseUrl, err := url.Parse(target)
191
 		targetParseUrl, err := url.Parse(target)
192
 		if err != nil {
192
 		if err != nil {
193
-			log.Errorf("parse url failed, url: %s, err: %s", target, err)
193
+			log.WithField("request_id", dsp.RequestId).Errorf("parse url failed, url: %s, err: %s", target, err)
194
 			continue
194
 			continue
195
 		}
195
 		}
196
 
196
 
@@ -315,11 +315,11 @@ func CombineLastAdsInfos(dsp *utils.DspParam, advertiser string, gotoUrls []stri
315
 
315
 
316
 	clickChannelFlag, err := redis_data.GetChannelFlag(dsp.ReqSource, "ads_click")
316
 	clickChannelFlag, err := redis_data.GetChannelFlag(dsp.ReqSource, "ads_click")
317
 	if err != nil {
317
 	if err != nil {
318
-		log.Errorf("get ads_click channel flag failed: %s", err)
318
+		log.WithField("request_id", dsp.RequestId).Errorf("get ads_click channel flag failed: %s", err)
319
 		return nil, err
319
 		return nil, err
320
 	}
320
 	}
321
 
321
 
322
-	log.Tracef("click channel flag: %+v\n", clickChannelFlag)
322
+	log.WithField("request_id", dsp.RequestId).Tracef("click channel flag: %+v\n", clickChannelFlag)
323
 
323
 
324
 	clickRandom := rand.Intn(100)
324
 	clickRandom := rand.Intn(100)
325
 	needClick := false
325
 	needClick := false
@@ -461,7 +461,7 @@ func CombineLastAdsInfos(dsp *utils.DspParam, advertiser string, gotoUrls []stri
461
 
461
 
462
 		canReport, err := redis_data.GetDeviceIpReport(dsp.Imei, dsp.Ip)
462
 		canReport, err := redis_data.GetDeviceIpReport(dsp.Imei, dsp.Ip)
463
 		if err != nil {
463
 		if err != nil {
464
-			log.Errorf("get device ip report failed: %s", err)
464
+			log.WithField("request_id", dsp.RequestId).Errorf("get device ip report failed: %s", err)
465
 			return nil, err
465
 			return nil, err
466
 		}
466
 		}
467
 
467
 
@@ -520,7 +520,7 @@ func genAdsAction(urls []string, advertiser string,
520
 		lastUrls = append(urls, reportUrl)
520
 		lastUrls = append(urls, reportUrl)
521
 	}
521
 	}
522
 
522
 
523
-	log.Tracef("action: %s, all send: %d,  all show: %d, control: +%v %+v %t\n", action, allSendNum, allShowNum, flowControlConf, lastUrls, needControl)
523
+	log.WithField("request_id", dsp.RequestId).Tracef("action: %s, all send: %d,  all show: %d, control: +%v %+v %t\n", action, allSendNum, allShowNum, flowControlConf, lastUrls, needControl)
524
 
524
 
525
 	if len(lastUrls) == 0 {
525
 	if len(lastUrls) == 0 {
526
 		duration = 0
526
 		duration = 0

+ 4 - 10
adslib/device/device.go

@@ -7,7 +7,6 @@ import (
7
 	log "github.com/sirupsen/logrus"
7
 	log "github.com/sirupsen/logrus"
8
 	"math/rand"
8
 	"math/rand"
9
 	"miads/adslib/ads_redis"
9
 	"miads/adslib/ads_redis"
10
-	"miads/adslib/utils"
11
 	"strconv"
10
 	"strconv"
12
 	"strings"
11
 	"strings"
13
 	"time"
12
 	"time"
@@ -151,7 +150,7 @@ func GetDailyFakeDeviceConfByCityCode(cityCode int) (*DeviceConf, int, error) {
151
 			continue
150
 			continue
152
 		}
151
 		}
153
 
152
 
154
-		isBlackItem, err := CheckIsBlackImei(d.Imei, false)
153
+		isBlackItem, err := CheckIsBlackImei(d.Imei)
155
 		if err != nil {
154
 		if err != nil {
156
 			continue
155
 			continue
157
 		}
156
 		}
@@ -182,17 +181,12 @@ func GetDailyFakeDeviceConfByCityCode(cityCode int) (*DeviceConf, int, error) {
182
 }
181
 }
183
 
182
 
184
 // 检查是否是黑名单
183
 // 检查是否是黑名单
185
-func CheckIsBlackImei(imei string, needMd5 bool) (bool, error) {
184
+func CheckIsBlackImei(imei string) (bool, error) {
186
 	conn := ads_redis.RedisConn.Get()
185
 	conn := ads_redis.RedisConn.Get()
187
 	defer conn.Close()
186
 	defer conn.Close()
188
 
187
 
189
-	checkImei := imei
190
-	if needMd5 {
191
-		checkImei = utils.Md5(imei)
192
-	}
193
-
194
-	redisKey := "ads_black_device_" + checkImei[0:2]
195
-	isBlackImei, err := redis.Bool(conn.Do("SISMEMBER", redisKey, checkImei))
188
+	redisKey := "ads_black_device_" + imei[0:2]
189
+	isBlackImei, err := redis.Bool(conn.Do("SISMEMBER", redisKey, imei))
196
 	if err != nil {
190
 	if err != nil {
197
 		return false, err
191
 		return false, err
198
 	}
192
 	}