|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.inlong.dataproxy.metrics.audit; |
| 19 | + |
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import org.apache.commons.lang.BooleanUtils; |
| 24 | +import org.apache.commons.lang.math.NumberUtils; |
| 25 | +import org.apache.commons.lang3.StringUtils; |
| 26 | +import org.apache.flume.Event; |
| 27 | +import org.apache.inlong.audit.AuditImp; |
| 28 | +import org.apache.inlong.audit.util.AuditConfig; |
| 29 | +import org.apache.inlong.dataproxy.config.ConfigManager; |
| 30 | +import org.apache.inlong.dataproxy.config.holder.CommonPropertiesHolder; |
| 31 | +import org.apache.inlong.dataproxy.consts.AttributeConstants; |
| 32 | +import org.apache.inlong.dataproxy.metrics.DataProxyMetricItem; |
| 33 | +import org.apache.inlong.dataproxy.utils.Constants; |
| 34 | + |
| 35 | +/** |
| 36 | + * |
| 37 | + * AuditUtils |
| 38 | + */ |
| 39 | +public class AuditUtils { |
| 40 | + |
| 41 | + public static final String AUDIT_KEY_FILE_PATH = "audit.filePath"; |
| 42 | + public static final String AUDIT_DEFAULT_FILE_PATH = "/data/inlong/audit/"; |
| 43 | + public static final String AUDIT_KEY_MAX_CACHE_ROWS = "audit.maxCacheRows"; |
| 44 | + public static final int AUDIT_DEFAULT_MAX_CACHE_ROWS = 2000000; |
| 45 | + public static final String AUDIT_KEY_PROXYS = "audit.proxys"; |
| 46 | + public static final String AUDIT_KEY_IS_AUDIT = "audit.isAudit"; |
| 47 | + |
| 48 | + public static final int AUDIT_ID_DATAPROXY_READ_SUCCESS = 5; |
| 49 | + public static final int AUDIT_ID_DATAPROXY_SEND_SUCCESS = 6; |
| 50 | + |
| 51 | + private static boolean IS_AUDIT = true; |
| 52 | + |
| 53 | + /** |
| 54 | + * initAudit |
| 55 | + */ |
| 56 | + public static void initAudit() { |
| 57 | + // IS_AUDIT |
| 58 | + IS_AUDIT = BooleanUtils.toBoolean(ConfigManager.getInstance().getCommonProperties().get(AUDIT_KEY_IS_AUDIT)); |
| 59 | + if (IS_AUDIT) { |
| 60 | + // AuditProxy |
| 61 | + String strIpPorts = ConfigManager.getInstance().getCommonProperties().get(AUDIT_KEY_PROXYS); |
| 62 | + HashSet<String> proxys = new HashSet<>(); |
| 63 | + if (!StringUtils.isBlank(strIpPorts)) { |
| 64 | + String[] ipPorts = strIpPorts.split("\\s+"); |
| 65 | + for (String ipPort : ipPorts) { |
| 66 | + proxys.add(ipPort); |
| 67 | + } |
| 68 | + } |
| 69 | + AuditImp.getInstance().setAuditProxy(proxys); |
| 70 | + // AuditConfig |
| 71 | + String filePath = ConfigManager.getInstance().getCommonProperties().getOrDefault(AUDIT_KEY_FILE_PATH, |
| 72 | + AUDIT_DEFAULT_FILE_PATH); |
| 73 | + int maxCacheRow = NumberUtils.toInt( |
| 74 | + ConfigManager.getInstance().getCommonProperties().get(AUDIT_KEY_MAX_CACHE_ROWS), |
| 75 | + AUDIT_DEFAULT_MAX_CACHE_ROWS); |
| 76 | + AuditConfig auditConfig = new AuditConfig(filePath, maxCacheRow); |
| 77 | + AuditImp.getInstance().setAuditConfig(auditConfig); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * add |
| 83 | + * |
| 84 | + * @param auditID |
| 85 | + * @param event |
| 86 | + */ |
| 87 | + public static void add(int auditID, Event event) { |
| 88 | + if (IS_AUDIT && event != null) { |
| 89 | + Map<String, String> headers = event.getHeaders(); |
| 90 | + String inlongGroupId = DataProxyMetricItem.getInlongGroupId(headers); |
| 91 | + String inlongStreamId = DataProxyMetricItem.getInlongStreamId(headers); |
| 92 | + long logTime = getLogTime(headers); |
| 93 | + AuditImp.getInstance().add(auditID, inlongGroupId, inlongStreamId, logTime, 1, event.getBody().length); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * getLogTime |
| 99 | + * |
| 100 | + * @param headers |
| 101 | + * @return |
| 102 | + */ |
| 103 | + public static long getLogTime(Map<String, String> headers) { |
| 104 | + String strLogTime = headers.get(Constants.HEADER_KEY_MSG_TIME); |
| 105 | + if (strLogTime == null) { |
| 106 | + strLogTime = headers.get(AttributeConstants.DATA_TIME); |
| 107 | + } |
| 108 | + if (strLogTime == null) { |
| 109 | + return System.currentTimeMillis(); |
| 110 | + } |
| 111 | + long logTime = NumberUtils.toLong(strLogTime, 0); |
| 112 | + if (logTime == 0) { |
| 113 | + logTime = System.currentTimeMillis(); |
| 114 | + } |
| 115 | + return logTime; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * getLogTime |
| 120 | + * |
| 121 | + * @param event |
| 122 | + * @return |
| 123 | + */ |
| 124 | + public static long getLogTime(Event event) { |
| 125 | + if (event != null) { |
| 126 | + Map<String, String> headers = event.getHeaders(); |
| 127 | + return getLogTime(headers); |
| 128 | + } |
| 129 | + return System.currentTimeMillis(); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * getAuditFormatTime |
| 134 | + * |
| 135 | + * @param msgTime |
| 136 | + * @return |
| 137 | + */ |
| 138 | + public static long getAuditFormatTime(long msgTime) { |
| 139 | + long auditFormatTime = msgTime - msgTime % CommonPropertiesHolder.getAuditFormatInterval(); |
| 140 | + return auditFormatTime; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * sendReport |
| 145 | + */ |
| 146 | + public static void sendReport() { |
| 147 | + AuditImp.getInstance().sendReport(); |
| 148 | + } |
| 149 | +} |
0 commit comments