Skip to content

Commit

Permalink
[collector] support more powerful replace placeholder ^_^ and -_- (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsun28 committed Nov 6, 2022
1 parent 94edf56 commit 17807d8
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,11 @@ public void collect(CollectRep.MetricsData.Builder builder, long appId, String a
} catch (Exception e) {
log.warn("[snmp collect] error: {}", e.getMessage(), e);
builder.setCode(CollectRep.Code.FAIL);
builder.setMsg(e.getMessage());
if (e.getMessage() == null) {
builder.setMsg(e.toString());
} else {
builder.setMsg(e.getMessage());
}
}
}

Expand Down
126 changes: 87 additions & 39 deletions collector/src/main/java/com/usthe/collector/util/CollectUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@

package com.usthe.collector.util;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import com.usthe.common.entity.job.Configmap;
import com.usthe.common.util.CommonConstants;
Expand All @@ -46,9 +43,12 @@ public class CollectUtil {

private static final int DEFAULT_TIMEOUT = 60000;
private static final String SMILING_PLACEHOLDER = "^_^";
private static final String SMILING_PLACEHOLDER_REGEX = "\\^_\\^";
private static final String SMILING_PLACEHOLDER_REX = "\\^_\\^";
private static final String SMILING_PLACEHOLDER_REGEX = "(\\^_\\^)(\\w|-|$|\\.)+(\\^_\\^)";
private static final Pattern SMILING_PLACEHOLDER_REGEX_PATTERN = Pattern.compile(SMILING_PLACEHOLDER_REGEX);
private static final String CRYING_PLACEHOLDER = "-_-";
private static final String CRYING_PLACEHOLDER_REGEX = "-_-";
private static final String CRYING_PLACEHOLDER_REGEX = "(-_-)(\\w|-|$|\\.)+(-_-)";
private static final Pattern CRYING_PLACEHOLDER_REGEX_PATTERN = Pattern.compile(CRYING_PLACEHOLDER_REGEX);
private static final List<String> UNIT_SYMBOLS = Arrays.asList("G", "g", "M", "m", "K", "k", "B", "b", "%");

/**
Expand Down Expand Up @@ -196,15 +196,27 @@ public static JsonElement replaceCryPlaceholder(JsonElement jsonElement, Map<Str
if (element.isJsonPrimitive()) {
// Check if there are special characters Replace
String value = element.getAsString();
if (value.startsWith(CRYING_PLACEHOLDER) && value.endsWith(CRYING_PLACEHOLDER)) {
value = value.replaceAll(CRYING_PLACEHOLDER_REGEX, "");
Configmap param = configmap.get(value);
if (param != null) {
value = (String) param.getValue();
jsonObject.addProperty(entry.getKey(), value);
} else {
iterator.remove();
Matcher cryingMatcher = CRYING_PLACEHOLDER_REGEX_PATTERN.matcher(value);
if (cryingMatcher.matches()) {
cryingMatcher.reset();
while (cryingMatcher.find()) {
String group = cryingMatcher.group();
String replaceField = group.replaceAll(CRYING_PLACEHOLDER, "");
Configmap param = configmap.get(replaceField);
if (param != null) {
if (param.getValue() == null) {
if (group.length() == value.length()) {
value = null;
break;
} else {
value = value.replace(group, "");
}
} else {
value = value.replace(group, (String) param.getValue());
}
}
}
jsonObject.addProperty(entry.getKey(), value);
}
} else {
jsonObject.add(entry.getKey(), replaceCryPlaceholder(entry.getValue(), configmap));
Expand All @@ -219,15 +231,27 @@ public static JsonElement replaceCryPlaceholder(JsonElement jsonElement, Map<Str
if (element.isJsonPrimitive()) {
// Check if there are special characters Replace
String value = element.getAsString();
if (value.startsWith(CRYING_PLACEHOLDER) && value.endsWith(CRYING_PLACEHOLDER)) {
value = value.replaceAll(CRYING_PLACEHOLDER_REGEX, "");
Configmap param = configmap.get(value);
if (param != null) {
value = (String) param.getValue();
jsonArray.set(index, new JsonPrimitive(value));
} else {
iterator.remove();
Matcher cryingMatcher = CRYING_PLACEHOLDER_REGEX_PATTERN.matcher(value);
if (cryingMatcher.matches()) {
cryingMatcher.reset();
while (cryingMatcher.find()) {
String group = cryingMatcher.group();
String replaceField = group.replaceAll(CRYING_PLACEHOLDER, "");
Configmap param = configmap.get(replaceField);
if (param != null) {
if (param.getValue() == null) {
if (group.length() == value.length()) {
value = null;
break;
} else {
value = value.replace(group, "");
}
} else {
value = value.replace(group, (String) param.getValue());
}
}
}
jsonArray.set(index, value == null ? JsonNull.INSTANCE : new JsonPrimitive(value));
}
} else {
jsonArray.set(index, replaceCryPlaceholder(element, configmap));
Expand Down Expand Up @@ -257,7 +281,7 @@ public static JsonElement replaceSmilingPlaceholder(JsonElement jsonElement, Map
// Replace the attributes of the KEY-VALUE case such as http headers params
// 替换KEY-VALUE情况的属性 比如http headers params
if (key != null && key.startsWith(SMILING_PLACEHOLDER) && key.endsWith(SMILING_PLACEHOLDER)) {
key = key.replaceAll(SMILING_PLACEHOLDER_REGEX, "");
key = key.replaceAll(SMILING_PLACEHOLDER_REX, "");
Configmap param = configmap.get(key);
if (param != null && param.getType() == (byte) 3) {
String jsonValue = (String) param.getValue();
Expand All @@ -280,15 +304,27 @@ public static JsonElement replaceSmilingPlaceholder(JsonElement jsonElement, Map
// Check if there are special characters Replace
// 判断是否含有特殊字符 替换
String value = element.getAsString();
if (value.startsWith(SMILING_PLACEHOLDER) && value.endsWith(SMILING_PLACEHOLDER)) {
value = value.replaceAll(SMILING_PLACEHOLDER_REGEX, "");
Configmap param = configmap.get(value);
if (param != null) {
value = (String) param.getValue();
jsonObject.addProperty(entry.getKey(), value);
} else {
iterator.remove();
Matcher smilingMatcher = SMILING_PLACEHOLDER_REGEX_PATTERN.matcher(value);
if (smilingMatcher.matches()) {
smilingMatcher.reset();
while (smilingMatcher.find()) {
String group = smilingMatcher.group();
String replaceField = group.replaceAll(SMILING_PLACEHOLDER_REX, "");
Configmap param = configmap.get(replaceField);
if (param != null) {
if (param.getValue() == null) {
if (group.length() == value.length()) {
value = null;
break;
} else {
value = value.replace(group, "");
}
} else {
value = value.replace(group, (String) param.getValue());
}
}
}
jsonObject.addProperty(entry.getKey(), value);
}
} else {
jsonObject.add(entry.getKey(), replaceSmilingPlaceholder(entry.getValue(), configmap));
Expand All @@ -304,15 +340,27 @@ public static JsonElement replaceSmilingPlaceholder(JsonElement jsonElement, Map
// Check if there are special characters Replace
// 判断是否含有特殊字符 替换
String value = element.getAsString();
if (value.startsWith(SMILING_PLACEHOLDER) && value.endsWith(SMILING_PLACEHOLDER)) {
value = value.replaceAll(SMILING_PLACEHOLDER_REGEX, "");
Configmap param = configmap.get(value);
if (param != null) {
value = (String) param.getValue();
jsonArray.set(index, new JsonPrimitive(value));
} else {
iterator.remove();
Matcher smilingMatcher = SMILING_PLACEHOLDER_REGEX_PATTERN.matcher(value);
if (smilingMatcher.matches()) {
smilingMatcher.reset();
while (smilingMatcher.find()) {
String group = smilingMatcher.group();
String replaceField = group.replaceAll(SMILING_PLACEHOLDER_REX, "");
Configmap param = configmap.get(replaceField);
if (param != null) {
if (param.getValue() == null) {
if (group.length() == value.length()) {
value = null;
break;
} else {
value = value.replace(group, "");
}
} else {
value = value.replace(group, (String) param.getValue());
}
}
}
jsonArray.set(index, value == null ? JsonNull.INSTANCE : new JsonPrimitive(value));
}
} else {
jsonArray.set(index, replaceSmilingPlaceholder(element, configmap));
Expand Down

0 comments on commit 17807d8

Please sign in to comment.