|
|
@@ -1,7 +1,6 @@
|
|
|
package com.usthe.collector.dispatch.timer;
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
-import com.google.gson.GsonBuilder;
|
|
|
import com.google.gson.JsonArray;
|
|
|
import com.google.gson.JsonElement;
|
|
|
import com.google.gson.JsonObject;
|
|
|
@@ -13,6 +12,7 @@ import com.usthe.common.entity.job.Job;
|
|
|
import com.usthe.common.entity.job.Metrics;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
@@ -24,8 +24,9 @@ import java.util.stream.Collectors;
|
|
|
*/
|
|
|
public class WheelTimerTask implements TimerTask {
|
|
|
|
|
|
- private Job job;
|
|
|
- private MetricsTaskDispatch metricsTaskDispatch;
|
|
|
+ private final Job job;
|
|
|
+ private final MetricsTaskDispatch metricsTaskDispatch;
|
|
|
+ private static final Gson GSON = new Gson();
|
|
|
|
|
|
public WheelTimerTask(Job job) {
|
|
|
this.metricsTaskDispatch = SpringContextHolder.getBean(MetricsTaskDispatch.class);
|
|
|
@@ -35,63 +36,77 @@ public class WheelTimerTask implements TimerTask {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 初始化job 将监控实际参数值对采集字段进行替换
|
|
|
+ * 初始化job填充信息
|
|
|
* @param job job
|
|
|
*/
|
|
|
private void initJobMetrics(Job job) {
|
|
|
+ // 将监控实际参数值对采集字段进行替换
|
|
|
List<Configmap> config = job.getConfigmap();
|
|
|
Map<String, Configmap> configmap = config.stream().collect(Collectors.toMap(Configmap::getKey, item -> item));
|
|
|
List<Metrics> metrics = job.getMetrics();
|
|
|
-
|
|
|
- Gson gson = new Gson();
|
|
|
List<Metrics> metricsTmp = new ArrayList<>(metrics.size());
|
|
|
for (Metrics metric : metrics) {
|
|
|
- JsonElement jsonElement = gson.toJsonTree(metric);
|
|
|
+ JsonElement jsonElement = GSON.toJsonTree(metric);
|
|
|
jsonElement = replaceSpecialValue(jsonElement, configmap);
|
|
|
- metric = gson.fromJson(jsonElement, Metrics.class);
|
|
|
+ metric = GSON.fromJson(jsonElement, Metrics.class);
|
|
|
metricsTmp.add(metric);
|
|
|
}
|
|
|
job.setMetrics(metricsTmp);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * json参数替换
|
|
|
+ * @param jsonElement json
|
|
|
+ * @param configmap 参数map
|
|
|
+ * @return json
|
|
|
+ */
|
|
|
private JsonElement replaceSpecialValue(JsonElement jsonElement, Map<String, Configmap> configmap) {
|
|
|
if (jsonElement.isJsonObject()) {
|
|
|
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
|
|
- jsonObject.entrySet().forEach(entry -> {
|
|
|
+ Iterator<Map.Entry<String, JsonElement>> iterator = jsonObject.entrySet().iterator();
|
|
|
+ while (iterator.hasNext()) {
|
|
|
+ Map.Entry<String, JsonElement> entry = iterator.next();
|
|
|
JsonElement element = entry.getValue();
|
|
|
if (element.isJsonPrimitive()) {
|
|
|
// 判断是否含有特殊字符 替换
|
|
|
String value = element.getAsString();
|
|
|
- if (value.startsWith("^_^")) {
|
|
|
+ if (value.startsWith("^_^") && value.endsWith("^_^")) {
|
|
|
value = value.replaceAll("\\^_\\^", "");
|
|
|
Configmap param = configmap.get(value);
|
|
|
if (param != null) {
|
|
|
value = (String) param.getValue();
|
|
|
jsonObject.addProperty(entry.getKey(), value);
|
|
|
+ } else {
|
|
|
+ iterator.remove();
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
jsonObject.add(entry.getKey(), replaceSpecialValue(entry.getValue(), configmap));
|
|
|
}
|
|
|
- });
|
|
|
+ }
|
|
|
} else if (jsonElement.isJsonArray()) {
|
|
|
JsonArray jsonArray = jsonElement.getAsJsonArray();
|
|
|
- for (int i = 0; i < jsonArray.size(); i++) {
|
|
|
- JsonElement element = jsonArray.get(i);
|
|
|
+ Iterator<JsonElement> iterator = jsonArray.iterator();
|
|
|
+ int index = 0;
|
|
|
+ while (iterator.hasNext()) {
|
|
|
+ JsonElement element = iterator.next();
|
|
|
if (element.isJsonPrimitive()) {
|
|
|
// 判断是否含有特殊字符 替换
|
|
|
String value = element.getAsString();
|
|
|
- if (value.startsWith("^_^")) {
|
|
|
+ if (value.startsWith("^_^") && value.endsWith("^_^")) {
|
|
|
value = value.replaceAll("\\^_\\^", "");
|
|
|
Configmap param = configmap.get(value);
|
|
|
if (param != null) {
|
|
|
value = (String) param.getValue();
|
|
|
- jsonArray.set(i, new JsonPrimitive(value));
|
|
|
+ jsonArray.set(index, new JsonPrimitive(value));
|
|
|
+ } else {
|
|
|
+ iterator.remove();
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
- jsonArray.set(i, replaceSpecialValue(element, configmap));
|
|
|
+ jsonArray.set(index, replaceSpecialValue(element, configmap));
|
|
|
}
|
|
|
+ index++;
|
|
|
}
|
|
|
}
|
|
|
return jsonElement;
|