-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathConfig.java
More file actions
243 lines (205 loc) · 8.38 KB
/
Copy pathConfig.java
File metadata and controls
243 lines (205 loc) · 8.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package io.arex.inst.runtime.config;
import io.arex.agent.bootstrap.constants.ConfigConstants;
import io.arex.agent.bootstrap.util.ConcurrentHashSet;
import io.arex.agent.bootstrap.util.StringUtil;
import io.arex.inst.runtime.context.RecordLimiter;
import io.arex.inst.runtime.listener.EventProcessor;
import io.arex.inst.runtime.model.DynamicClassEntity;
import io.arex.inst.runtime.model.RecordRuleEntity;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import static io.arex.agent.bootstrap.constants.ConfigConstants.STORAGE_MODE;
import static io.arex.agent.bootstrap.constants.ConfigConstants.STORAGE_SERVICE_MODE;
public class Config {
private static Config INSTANCE = null;
static void update(boolean enableDebug, String serviceName, List<DynamicClassEntity> dynamicClassList,
Map<String, String> properties, Set<String> excludeServiceOperations,
int dubboStreamReplayThreshold, int recordRate,
List<RecordRuleEntity> recordRuleList, boolean existUrlParamRule, boolean existBodyParamRule) {
INSTANCE = new Config(enableDebug, serviceName, dynamicClassList, properties,
excludeServiceOperations, dubboStreamReplayThreshold, recordRate,
recordRuleList, existUrlParamRule, existBodyParamRule);
}
public static Config get() {
return INSTANCE;
}
private final boolean enableDebug;
private final String serviceName;
private final List<DynamicClassEntity> dynamicClassList;
private Map<String, DynamicClassEntity> dynamicClassSignatureMap;
private String[] dynamicAbstractClassList;
private final Map<String, String> properties;
private final Set<String> excludeServiceOperations;
private final int dubboStreamReplayThreshold;
private final int recordRate;
private final String recordVersion;
private final Set<String> includeServiceOperations;
private final String[] coveragePackages;
private final List<RecordRuleEntity> recordRuleList;
private final boolean existUrlParamRule;
private final boolean existBodyParamRule;
Config(boolean enableDebug, String serviceName, List<DynamicClassEntity> dynamicClassList,
Map<String, String> properties,
Set<String> excludeServiceOperations, int dubboStreamReplayThreshold, int recordRate,
List<RecordRuleEntity> recordRuleList, boolean existUrlParamRule, boolean existBodyParamRule) {
this.enableDebug = enableDebug;
this.serviceName = serviceName;
this.dynamicClassList = dynamicClassList;
this.properties = properties;
this.excludeServiceOperations = buildExcludeServiceOperations(excludeServiceOperations);
this.dubboStreamReplayThreshold = dubboStreamReplayThreshold;
this.recordRate = recordRate;
this.recordVersion = properties.get("arex.agent.version");
this.includeServiceOperations = StringUtil.splitToSet(properties.get("includeServiceOperations"), ',');
this.coveragePackages = StringUtil.split(properties.get(ConfigConstants.COVERAGE_PACKAGES), ',');
this.recordRuleList = recordRuleList;
this.existUrlParamRule = existUrlParamRule;
this.existBodyParamRule = existBodyParamRule;
buildDynamicClassInfo();
}
private Set<String> buildExcludeServiceOperations(Set<String> excludeServiceOperations) {
if (excludeServiceOperations == null) {
return Collections.emptySet();
}
Set<String> excludeServiceOperationSet = new ConcurrentHashSet<>();
excludeServiceOperationSet.addAll(excludeServiceOperations);
return excludeServiceOperationSet;
}
private void buildDynamicClassInfo() {
if (dynamicClassList == null) {
this.dynamicClassSignatureMap = Collections.emptyMap();
this.dynamicAbstractClassList = StringUtil.EMPTY_STRING_ARRAY;
return;
}
Map<String, DynamicClassEntity> map = new HashMap<>(dynamicClassList.size());
ArrayList<String> list = new ArrayList<>(dynamicClassList.size());
for (DynamicClassEntity entity : dynamicClassList) {
map.putIfAbsent(entity.getSignature(), entity);
if (entity.isAbstractClass()) {
list.add(entity.removedAbstractClassPrefix());
}
}
this.dynamicClassSignatureMap = map;
this.dynamicAbstractClassList = list.toArray(StringUtil.EMPTY_STRING_ARRAY);
}
public String[] getCoveragePackages() {
return coveragePackages;
}
public String getRecordVersion() {
return recordVersion;
}
public DynamicClassEntity getDynamicEntity(String methodSignature) {
return dynamicClassSignatureMap.get(methodSignature);
}
public Map<String, DynamicClassEntity> getDynamicClassSignatureMap() {
return dynamicClassSignatureMap;
}
public String[] getDynamicAbstractClassList() {
return dynamicAbstractClassList;
}
public boolean isEnableDebug() {
return this.enableDebug;
}
public List<DynamicClassEntity> getDynamicClassList() {
return this.dynamicClassList;
}
public Set<String> excludeServiceOperations() {
return this.excludeServiceOperations;
}
public String getServiceName() {
return this.serviceName;
}
public String getString(String name) {
return getString(name, null);
}
public String getString(String name, String defaultValue) {
return getRawProperty(name, defaultValue);
}
public boolean getBoolean(String name, boolean defaultValue) {
return safeGetTypedProperty(name, Boolean::parseBoolean, defaultValue);
}
public int getInt(String name, int defaultValue) {
return safeGetTypedProperty(name, Integer::parseInt, defaultValue);
}
public long getLong(String name, long defaultValue) {
return safeGetTypedProperty(name, Long::parseLong, defaultValue);
}
public double getDouble(String name, double defaultValue) {
return safeGetTypedProperty(name, Double::parseDouble, defaultValue);
}
private <T> T safeGetTypedProperty(String name, Function<String, T> parser, T defaultValue) {
try {
T value = getTypedProperty(name, parser);
return value == null ? defaultValue : value;
} catch (RuntimeException t) {
return defaultValue;
}
}
private <T> T getTypedProperty(String name, Function<String, T> parser) {
String value = getRawProperty(name, null);
if (value == null || value.trim().isEmpty()) {
return null;
}
return parser.apply(value);
}
private String getRawProperty(String name, String defaultValue) {
return this.properties.getOrDefault(name, defaultValue);
}
public int getDubboStreamReplayThreshold() {
return dubboStreamReplayThreshold;
}
public int getRecordRate() {
return recordRate;
}
public Set<String> getIncludeServiceOperations() {
return includeServiceOperations;
}
public boolean isLocalStorage() {
return STORAGE_MODE.equalsIgnoreCase(getString(STORAGE_SERVICE_MODE));
}
public List<RecordRuleEntity> getRecordRuleList() {
return recordRuleList;
}
public boolean isExistUrlParamRule() {
return existUrlParamRule;
}
public boolean isExistBodyParamRule() {
return existBodyParamRule;
}
/**
* Conditions for determining invalid recording configuration:<br/>
* 1. rate <= 0 <br/>
* 2. not in working time <br/>
* 3. exceed rate limit <br/>
* 4. local IP match target IP <br/>
* 5. record disabled by config
*
* @return true: invalid, false: valid
*/
public boolean invalidRecord(String path) {
if (!EventProcessor.dependencyInitComplete()) {
return true;
}
if (isLocalStorage()) {
return false;
}
if (getRecordRate() <= 0) {
return true;
}
if (!getBoolean(ConfigConstants.DURING_WORK, false)) {
return true;
}
if (!getBoolean(ConfigConstants.AGENT_ENABLED, false)) {
return true;
}
if (getBoolean(ConfigConstants.DISABLE_RECORD, false)) {
return true;
}
return !RecordLimiter.acquire(path);
}
}