Browse Source

feat:添加日志上报

zhaohaipeng 1 year ago
parent
commit
37f4bbe432

+ 70 - 5
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/filter/strategy/BlacklistContainer.java

@@ -1,6 +1,10 @@
 package com.tzld.piaoquan.recommend.server.service.filter.strategy;
 
-import com.alibaba.fastjson.JSON;
+import com.aliyun.openservices.aliyun.log.producer.LogProducer;
+import com.aliyun.openservices.aliyun.log.producer.Producer;
+import com.aliyun.openservices.aliyun.log.producer.ProducerConfig;
+import com.aliyun.openservices.aliyun.log.producer.ProjectConfig;
+import com.aliyun.openservices.log.common.LogItem;
 import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
@@ -15,6 +19,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
@@ -78,6 +83,21 @@ public class BlacklistContainer {
     @ApolloJsonValue("${content.security.filter.config:{}}")
     private Map<String, TagFilterConfig> tagFilterConfigMap;
 
+
+    @Value("${aliyun.log.blacklist.filter.log.project}")
+    private String project;
+    @Value("${aliyun.log.endpoint}")
+    private String endpoint;
+    @Value("${aliyun.log.accessKeyId}")
+    private String accessKeyId;
+    @Value("${aliyun.log.accessKeySecret}")
+    private String accessKeySecret;
+    @Value("${aliyun.log.blacklist.filter.log.store}")
+    private String logStore;
+    @Value("${spring.profiles.active}")
+    private String activeProfile;
+    private Producer producer;
+
     /**
      * 保存Tag标签与视频列表的映射
      * <br>
@@ -114,7 +134,10 @@ public class BlacklistContainer {
 
     @PostConstruct
     public void init() {
+        LOG.info("generalizationUserConditionConfig: {}", generalizationUserConditionConfig);
+        LOG.info("tagFilterConfigMap: {}", tagFilterConfigMap);
         refreshVideoTagCache();
+        initLogProducer();
     }
 
     @Scheduled(cron = "0 0/5 * * * ? ")
@@ -122,6 +145,14 @@ public class BlacklistContainer {
         refreshVideoTagCache();
     }
 
+    private void initLogProducer() {
+        LOG.info("BlacklistContainer.initLogProducer: project={}, endpoint={}, accessKeyId={}, accessKeySecret={}, logStore={}",
+                producer, endpoint, accessKeyId, accessKeySecret, logStore);
+        ProducerConfig producerConfig = new ProducerConfig();
+        producer = new LogProducer(producerConfig);
+        producer.putProjectConfig(new ProjectConfig(project, endpoint, accessKeyId, accessKeySecret));
+    }
+
     public void refreshVideoTagCache() {
         LOG.info("同步本地标签ID与视频列表的缓存任务开始");
         Map<Long, Set<Long>> tmpMap = new ConcurrentHashMap<>();
@@ -173,7 +204,7 @@ public class BlacklistContainer {
 
         return videoIds.stream().filter(videoId -> {
             if (videoTagAnyMatch(videoId, tagIdSet)) {
-                LOG.error("用户 {} 在因命中 {} 移除对应的视频ID {}: 请求参数为: hotSceneType={}, cityCode={}, clientIP={}",
+                LOG.info("用户 {} 在因命中 {} 移除对应的视频ID {}: 请求参数为: hotSceneType={}, cityCode={}, clientIP={}",
                         uid, userType, videoId, hotSceneType, cityCode, clientIP);
                 return false;
             }
@@ -184,12 +215,11 @@ public class BlacklistContainer {
     private String matchUserBlacklistTypeEnum(String uid, Long hotSceneType, String cityCode, String clientIP) {
         try {
             LOG.info("计算用户黑名单类型,判断参数: uid={}, hotSceneType={}, cityCode={}, clientIP={}", uid, hotSceneType, cityCode, clientIP);
-            LOG.info("generalizationUserConditionConfig: {}", generalizationUserConditionConfig);
-            LOG.info("tagFilterConfigMap: {}", tagFilterConfigMap);
             String key = this.calcUserRedisKey(uid);
             Map<String, String> uidBlacklistMap = blacklistCache.get(key);
             if (uidBlacklistMap.containsKey(uid)) {
                 String userType = uidBlacklistMap.get(uid);
+                this.filterLogUpload(uid, cityCode, hotSceneType, clientIP, userType, "UID");
                 LOG.info("用户 {} 在UID黑名单中命中 {}", uid, userType);
                 return userType;
             }
@@ -197,11 +227,14 @@ public class BlacklistContainer {
             Map<String, String> ipBlacklistMap = blacklistCache.get(IP_VISIO_BLACKLIST_HASH_KEY);
             if (ipBlacklistMap.containsKey(clientIP)) {
                 String userType = ipBlacklistMap.get(clientIP);
+                this.filterLogUpload(uid, cityCode, hotSceneType, clientIP, userType, "IP");
                 LOG.info("用户 {} 在IP黑名单中命中 {}, 参数为: clientIP为: {}", uid, userType, clientIP);
                 return userType;
             }
 
-            return this.matchGeneralizationUserType(uid, cityCode, hotSceneType);
+            String userType = this.matchGeneralizationUserType(uid, cityCode, hotSceneType);
+            this.filterLogUpload(uid, cityCode, hotSceneType, clientIP, userType, "RegionAndHotSceneType");
+            return userType;
         } catch (
                 Exception e) {
             LOG.error("blacklist filter isSafeVideoByUid error: ", e);
@@ -267,6 +300,38 @@ public class BlacklistContainer {
         return tagFilterConfig.getRecommendExcludeTag();
     }
 
+    private void filterLogUpload(String uid, String cityCode, Long hotSceneType, String clientIp, String fullUserType, String blacklistType) {
+        try {
+            String[] split = fullUserType.split(USER_TYPE_SUB_TYPE_CONNECTOR);
+            this.filterLogUpload(uid, cityCode, hotSceneType, clientIp, split[0], split[1], blacklistType);
+        } catch (
+                Exception e) {
+            LOG.error("filterLogUpload error: ", e);
+        }
+    }
+
+    private void filterLogUpload(String uid, String cityCode, Long hotSceneType, String clientIp, String userType, String userSubType, String blacklistType) {
+        try {
+            Map<String, String> logMap = new HashMap<>();
+            logMap.put("uid", uid);
+            logMap.put("cityCode", cityCode);
+            logMap.put("hotSceneType", hotSceneType.toString());
+            logMap.put("clientIp", clientIp);
+            logMap.put("userType", userType);
+            logMap.put("userSubType", userSubType);
+            logMap.put("env", activeProfile);
+            logMap.put("blacklistType", blacklistType);
+            logMap.put("uploadService", "recommend-server");
+
+            LogItem logItem = new LogItem();
+            logMap.forEach(logItem::PushBack);
+            producer.send(project, logStore, logItem);
+        } catch (
+                Exception e) {
+            LOG.error("blacklist filter upload log error: ", e);
+        }
+    }
+
     @Data
     private static class GeneralizationUserConfig {
         Set<Long> excludeHotSceneType;

+ 5 - 0
recommend-server-service/src/main/resources/application-dev.yml

@@ -114,6 +114,11 @@ aliyun:
     log:
       project: recommend-server-test
       logStore: timer
+  blacklist:
+    filter:
+      log:
+        project: wqsd-video-test
+        store: video_blacklist_security_filter_log
 
 logging:
   file:

+ 6 - 1
recommend-server-service/src/main/resources/application-pre.yml

@@ -112,4 +112,9 @@ aliyun:
   timer:
     log:
       project: recommend-server
-      logStore: timer
+      logStore: timer
+  blacklist:
+    filter:
+      log:
+        project: wqsd-video
+        store: video_blacklist_security_filter_log

+ 6 - 1
recommend-server-service/src/main/resources/application-prod.yml

@@ -112,4 +112,9 @@ aliyun:
   timer:
     log:
       project: recommend-server
-      logStore: timer
+      logStore: timer
+  blacklist:
+    filter:
+      log:
+        project: wqsd-video
+        store: video_blacklist_security_filter_log

+ 6 - 1
recommend-server-service/src/main/resources/application-test.yml

@@ -113,4 +113,9 @@ aliyun:
   timer:
     log:
       project: recommend-server-test
-      logStore: timer
+      logStore: timer
+  blacklist:
+    filter:
+      log:
+        project: wqsd-video-test
+        store: video_blacklist_security_filter_log