Skip to content

Commit

Permalink
fix typo: log4j2/config/metrics/mock (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
observeralone authored Jan 5, 2022
1 parent 6adee09 commit f2e68f7
Show file tree
Hide file tree
Showing 51 changed files with 329 additions and 253 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public class CompatibilityConversion {
ConfigConst.Namespace.FEIGN_CLIENT,
ConfigConst.Namespace.REST_TEMPLATE,
};
private static final Map<String, BiFunction<String, String, Conversion>> KEY_TO_NAMESPACE;
private static final Map<String, BiFunction<String, String, Conversion<?>>> KEY_TO_NAMESPACE;
private static final Set<String> METRIC_SKIP;
private static final Set<String> TRACING_SKIP;

static {
Map<String, BiFunction<String, String, Conversion>> map = new HashMap<>();
Map<String, BiFunction<String, String, Conversion<?>>> map = new HashMap<>();
map.put(ConfigConst.Observability.KEY_METRICS_ACCESS, SingleBuilder.observability(ConfigConst.Namespace.ACCESS));

map.put(ConfigConst.Observability.KEY_METRICS_REQUEST, MultipleBuilder.observability(Arrays.asList(REQUEST_NAMESPACE)));
Expand Down Expand Up @@ -76,7 +76,7 @@ public static Map<String, String> transform(Map<String, String> oldConfigs) {
Map<String, Object> changedKeys = new HashMap<>();
Map<String, String> newConfigs = new HashMap<>();
for (Map.Entry<String, String> entry : oldConfigs.entrySet()) {
Conversion conversion = transformConversion(entry.getKey());
Conversion<?> conversion = transformConversion(entry.getKey());
@SuppressWarnings("unchecked")
Object changed = conversion.transform(newConfigs, entry.getValue());
if (conversion.isChange()) {
Expand All @@ -95,7 +95,7 @@ public static Map<String, String> transform(Map<String, String> oldConfigs) {
return newConfigs;
}

private static Conversion transformConversion(String key) {
private static Conversion<?> transformConversion(String key) {
if (key.startsWith("observability.metrics.")) {
return metricConversion(key);
} else if (key.startsWith("observability.tracings.")) {
Expand All @@ -106,7 +106,7 @@ private static Conversion transformConversion(String key) {
return new FinalConversion(key, false);
}

private static Conversion metricConversion(String key) {
private static Conversion<?> metricConversion(String key) {
if (key.equals(ConfigConst.Observability.METRICS_ENABLED)) {
return new MultipleFinalConversion(Arrays.asList(new FinalConversion(ConfigConst.Observability.METRICS_ENABLED, true),
new FinalConversion(ConfigConst.Plugin.OBSERVABILITY_GLOBAL_METRIC_ENABLED, true)), true);
Expand All @@ -115,18 +115,18 @@ private static Conversion metricConversion(String key) {
}


private static Conversion tracingConversion(String key) {
private static Conversion<?> tracingConversion(String key) {
if (key.equals(ConfigConst.Observability.TRACE_ENABLED)) {
return new FinalConversion(ConfigConst.Plugin.OBSERVABILITY_GLOBAL_TRACING_ENABLED, true);
}
return conversion(key, TRACING_SKIP, ConfigConst.PluginID.TRACING);
}

private static Conversion forwardedHeadersConversion(String key) {
private static Conversion<?> forwardedHeadersConversion(String key) {
return new FinalConversion(key.replace(ConfigConst.GlobalCanaryLabels.SERVICE_HEADERS + ".", ProgressFields.EASEAGENT_PROGRESS_FORWARDED_HEADERS_CONFIG + "."), true);
}

private static Conversion conversion(String key, Set<String> skipSet, String pluginId) {
private static Conversion<?> conversion(String key, Set<String> skipSet, String pluginId) {
String[] keys = ConfigConst.split(key);
if (keys.length < 4) {
return new FinalConversion(key, false);
Expand All @@ -135,7 +135,7 @@ private static Conversion conversion(String key, Set<String> skipSet, String plu
if (skipSet.contains(key2)) {
return new FinalConversion(key, false);
}
BiFunction<String, String, Conversion> builder = KEY_TO_NAMESPACE.get(key2);
BiFunction<String, String, Conversion<?>> builder = KEY_TO_NAMESPACE.get(key2);
if (builder == null) {
builder = SingleBuilder.observability(key2);
}
Expand Down Expand Up @@ -253,7 +253,7 @@ public boolean isChange() {
}
}

static class SingleBuilder implements BiFunction<String, String, Conversion> {
static class SingleBuilder implements BiFunction<String, String, Conversion<?>> {
private final String domain;
private final String namespace;

Expand All @@ -272,7 +272,7 @@ static SingleBuilder observability(String namespace) {
}
}

static class MultipleBuilder implements BiFunction<String, String, Conversion> {
static class MultipleBuilder implements BiFunction<String, String, Conversion<?>> {
private final String domain;
private final List<String> namespaces;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private static HashMap<String, String> extractPropsMap(InputStream in) throws IO
return map;
}

private static void validConfigs(Configs configs) {
public static void validConfigs(Configs configs) {
//validate serviceName and systemName
ValidateUtils.validate(configs, ConfigConst.SERVICE_NAME, HasText);
ValidateUtils.validate(configs, ConfigConst.SYSTEM_NAME, HasText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public static <R> void bindProp(String name, Config configs, BiFunction<Config,
}

public static Map<String, String> json2KVMap(String json) throws IOException {
ObjectMapper JSON = new ObjectMapper();
JsonNode node = JSON.readTree(json);
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(json);
List<Map.Entry<String, String>> list = extractKVs(null, node);
return list.stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
Expand Down Expand Up @@ -101,17 +101,19 @@ public static boolean isGlobal(String namespace) {
}

public static boolean isPluginConfig(String key) {
if (key.startsWith(PLUGIN_PREFIX)) {
return true;
boolean r = false;
if (key != null && key.startsWith(PLUGIN_PREFIX)) {
r = true;
}
return false;
return r;
}

public static boolean isPluginConfig(String key, String domain, String namespace, String id) {
if (key.startsWith(ConfigConst.join(PLUGIN, domain, namespace, id))) {
return true;
boolean r = false;
if (key != null && key.startsWith(ConfigConst.join(PLUGIN, domain, namespace, id))) {
r = true;
}
return false;
return r;
}

public static PluginProperty pluginProperty(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,29 @@ public void shutdown() {
shutdownRunnable.run();
}

private synchronized void onChange(Map<String, String> sources) {
protected synchronized void onChange(Map<String, String> sources) {
Set<Key> sourceKeys = keys(sources.keySet());
Map<String, String> newSources = buildNewSources(sourceKeys, sources);
for (Key sourceKey : sourceKeys) {
pluginSourceConfigs.put(sourceKey, PluginSourceConfig.build(sourceKey.getDomain(), sourceKey.getNamespace(), sourceKey.getId(), newSources));
}
Set<Key> changeKeys = buildChangeKeys(sourceKeys);

for (Key changeKey : changeKeys) {
final PluginConfig oldConfig = pluginConfigs.remove(changeKey);
final PluginConfig newConfig = getConfig(changeKey.getDomain(), changeKey.getNamespace(), changeKey.id, oldConfig);
if (oldConfig == null) {
continue;
}
try {
oldConfig.foreachConfigChangeListener(listener -> listener.onChange(oldConfig, newConfig));
} catch (Exception e) {
LOGGER.warn("change config<{}> fail: {}", changeKey.toString(), e.getMessage());
}
}
}

private Map<String, String> buildNewSources(Set<Key> sourceKeys, Map<String, String> sources) {
Map<String, String> newSources = new HashMap<>();
for (Key sourceKey : sourceKeys) {
PluginSourceConfig pluginSourceConfig = pluginSourceConfigs.get(sourceKey);
Expand All @@ -113,32 +134,23 @@ private synchronized void onChange(Map<String, String> sources) {
newSources.putAll(pluginSourceConfig.getSource());
}
newSources.putAll(sources);
for (Key sourceKey : sourceKeys) {
pluginSourceConfigs.put(sourceKey, PluginSourceConfig.build(sourceKey.getDomain(), sourceKey.getNamespace(), sourceKey.getId(), newSources));
}
return newSources;
}

private Set<Key> buildChangeKeys(Set<Key> sourceKeys) {
Set<Key> changeKeys = new HashSet<>(sourceKeys);
for (Key key : sourceKeys) {
if (!ConfigUtils.isGlobal(key.getNamespace())) {
continue;
}
for (Key oldKey : pluginConfigs.keySet()) {
if (key.id.equals(oldKey.id)) {
changeKeys.add(oldKey);
if (!key.id.equals(oldKey.id)) {
continue;
}
changeKeys.add(oldKey);
}
}
for (Key changeKey : changeKeys) {
final PluginConfig oldConfig = pluginConfigs.remove(changeKey);
final PluginConfig newConfig = getConfig(changeKey.getDomain(), changeKey.getNamespace(), changeKey.id, oldConfig);
if (oldConfig == null) {
continue;
}
try {
oldConfig.foreachConfigChangeListener(listener -> listener.onChange(oldConfig, newConfig));
} catch (Exception e) {
LOGGER.warn("change config<{}> fail: {}", changeKey.toString(), e.getMessage());
}
}
return changeKeys;
}

class Key {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public ValidException(String message) {
}

public interface Validator {
void validate(String name, String value) throws ValidException;
void validate(String name, String value);
}

public static void validate(Configs configs, String name, Validator... vs) throws ValidException {
public static void validate(Configs configs, String name, Validator... vs){
String value = configs.getString(name);
for (Validator one : vs) {
one.validate(name, value);
Expand All @@ -51,7 +51,7 @@ public static void validate(Configs configs, String name, Validator... vs) throw

public static final Validator NumberInt = (name, value) -> {
try {
int rst = Integer.parseInt(value.trim());
Integer.parseInt(value.trim());
} catch (Exception e) {
throw new ValidException(String.format("Property[%s] has no integer value", name));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,60 +32,60 @@ public class CompatibilityConversionTest {
@Test
public void transform() {
Map<String, String> newMap = CompatibilityConversion.transform(Collections.singletonMap(ConfigConst.Observability.METRICS_ENABLED, "false"));
assertEquals(newMap.size(), 2);
assertEquals(2, newMap.size());
assertTrue(newMap.containsKey(ConfigConst.Plugin.OBSERVABILITY_GLOBAL_METRIC_ENABLED));
assertTrue(newMap.containsKey(ConfigConst.Observability.METRICS_ENABLED));
newMap = CompatibilityConversion.transform(Collections.singletonMap(ConfigConst.Observability.TRACE_ENABLED, "false"));
assertEquals(newMap.size(), 1);
assertEquals(1, newMap.size());
assertTrue(newMap.containsKey(ConfigConst.Plugin.OBSERVABILITY_GLOBAL_TRACING_ENABLED));
newMap = CompatibilityConversion.transform(Collections.singletonMap("globalCanaryHeaders.serviceHeaders.mesh-app-backend.0", "X-canary"));
assertEquals(newMap.size(), 1);
assertEquals(1, newMap.size());
assertTrue(newMap.containsKey(ProgressFields.EASEAGENT_PROGRESS_FORWARDED_HEADERS_CONFIG + ".mesh-app-backend.0"));

newMap = CompatibilityConversion.transform(Collections.singletonMap("observability.outputServer.bootstrapServer", "tstatssta"));
assertEquals(newMap.size(), 1);
assertEquals(1, newMap.size());
assertTrue(newMap.containsKey("observability.outputServer.bootstrapServer"));


newMap = CompatibilityConversion.transform(Collections.singletonMap("observability.metrics.access.enabled", "true"));
assertEquals(newMap.size(), 1);
assertEquals(newMap.get("plugin.observability.access.metric.enabled"), "true");
assertEquals(1, newMap.size());
assertEquals("true", newMap.get("plugin.observability.access.metric.enabled"));


newMap = CompatibilityConversion.transform(Collections.singletonMap("observability.metrics.access.interval", "30"));
assertEquals(newMap.size(), 1);
assertEquals(newMap.get("plugin.observability.access.metric.interval"), "30");
assertEquals(1, newMap.size());
assertEquals("30", newMap.get("plugin.observability.access.metric.interval"));

newMap = CompatibilityConversion.transform(Collections.singletonMap("observability.tracings.remoteInvoke.enabled", "true"));
assertEquals(newMap.size(), 1);
assertEquals(newMap.get("plugin.observability.webclient.tracing.enabled"), "true");
assertEquals(1, newMap.size());
assertEquals("true", newMap.get("plugin.observability.webclient.tracing.enabled"));

newMap = CompatibilityConversion.transform(Collections.singletonMap("observability.tracings.request.enabled", "true"));
assertEquals(newMap.size(), REQUEST_NAMESPACE.length);
assertEquals(REQUEST_NAMESPACE.length, newMap.size());
for (String s : REQUEST_NAMESPACE) {
assertEquals(newMap.get("plugin.observability." + s + ".tracing.enabled"), "true");
assertEquals("true", newMap.get("plugin.observability." + s + ".tracing.enabled"));
}

newMap = CompatibilityConversion.transform(Collections.singletonMap("observability.metrics.jvmGc.enabled", "true"));
assertEquals(newMap.size(), 1);
assertEquals(newMap.get("observability.metrics.jvmGc.enabled"), "true");
assertEquals(1, newMap.size());
assertEquals("true", newMap.get("observability.metrics.jvmGc.enabled"));


newMap = CompatibilityConversion.transform(Collections.singletonMap("observability.tracings.sampledByQPS", "100"));
assertEquals(newMap.size(), 1);
assertEquals(newMap.get("observability.tracings.sampledByQPS"), "100");
assertEquals(1, newMap.size());
assertEquals("100", newMap.get("observability.tracings.sampledByQPS"));


newMap = CompatibilityConversion.transform(Collections.singletonMap("observability.tracings.output.enabled", "true"));
assertEquals(newMap.size(), 1);
assertEquals(newMap.get("observability.tracings.output.enabled"), "true");
assertEquals(1, newMap.size());
assertEquals("true", newMap.get("observability.tracings.output.enabled"));

newMap = CompatibilityConversion.transform(Collections.singletonMap("observability.metrics.jdbcConnection.interval", "30"));
assertEquals(newMap.size(), 1);
assertEquals(newMap.get("plugin.observability.jdbcConnection.metric.interval"), "30");
assertEquals(1, newMap.size());
assertEquals("30", newMap.get("plugin.observability.jdbcConnection.metric.interval"));

newMap = CompatibilityConversion.transform(Collections.singletonMap("observability.metrics.aaaaaaaaaa.interval", "30"));
assertEquals(newMap.size(), 1);
assertEquals(newMap.get("plugin.observability.aaaaaaaaaa.metric.interval"), "30");
assertEquals(1, newMap.size());
assertEquals("30", newMap.get("plugin.observability.aaaaaaaaaa.metric.interval"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ public void isPluginConfig() {
@Test
public void pluginProperty() {
PluginProperty pluginProperty = ConfigUtils.pluginProperty("plugin.observability.kafka.self.enabled");
Assert.assertEquals(pluginProperty.getDomain(), "observability");
Assert.assertEquals(pluginProperty.getNamespace(), "kafka");
Assert.assertEquals(pluginProperty.getId(), "self");
Assert.assertEquals(pluginProperty.getProperty(), "enabled");
Assert.assertEquals("observability", pluginProperty.getDomain());
Assert.assertEquals("kafka", pluginProperty.getNamespace());
Assert.assertEquals("self", pluginProperty.getId());
Assert.assertEquals("enabled", pluginProperty.getProperty());
pluginProperty = ConfigUtils.pluginProperty("plugin.observability.kafka.self.tcp.enabled");
Assert.assertEquals(pluginProperty.getDomain(), "observability");
Assert.assertEquals(pluginProperty.getNamespace(), "kafka");
Assert.assertEquals(pluginProperty.getId(), "self");
Assert.assertEquals(pluginProperty.getProperty(), "tcp.enabled");
Assert.assertEquals("observability", pluginProperty.getDomain());
Assert.assertEquals("kafka", pluginProperty.getNamespace());
Assert.assertEquals("self", pluginProperty.getId());
Assert.assertEquals("tcp.enabled", pluginProperty.getProperty());
try {
ConfigUtils.pluginProperty("plugin.observability.kafka.self");
assertTrue("must be error", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ public void test_check_old() {
List<ChangeItem> rst = addListener(configs);
configs.updateConfigs(Collections.singletonMap("hello", "test"));
ChangeItem first = rst.get(0);
Assert.assertEquals(first.getFullName(), "hello");
Assert.assertEquals(first.getName(), "hello");
Assert.assertEquals(first.getOldValue(), "world");
Assert.assertEquals(first.getNewValue(), "test");
Assert.assertEquals("hello", first.getFullName());
Assert.assertEquals("hello", first.getName());
Assert.assertEquals("world", first.getOldValue());
Assert.assertEquals("test", first.getNewValue());
}


Expand All @@ -78,10 +78,10 @@ public void test_check_new() {
List<ChangeItem> rst = addListener(configs);
configs.updateConfigs(Collections.singletonMap("name", "666"));
ChangeItem first = rst.get(0);
Assert.assertEquals(first.getFullName(), "name");
Assert.assertEquals(first.getName(), "name");
Assert.assertEquals("name", first.getFullName());
Assert.assertEquals("name", first.getName());
Assert.assertNull(first.getOldValue());
Assert.assertEquals(first.getNewValue(), "666");
Assert.assertEquals("666", first.getNewValue());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class IPluginConfigConstTest {
@Test
public void testExtractHeaderName() {
String prefix = "globalCanaryHeaders.serviceHeaders";
assertEquals(prefix, ConfigConst.GlobalCanaryLabels.SERVICE_HEADERS);
assertEquals(ConfigConst.GlobalCanaryLabels.SERVICE_HEADERS, prefix);
assertEquals("hello", ConfigConst.GlobalCanaryLabels.extractHeaderName(prefix + ".test-aaa.0.hello"));
assertEquals("world", ConfigConst.GlobalCanaryLabels.extractHeaderName(prefix + ".test-aaa.1.world"));
assertNull(ConfigConst.GlobalCanaryLabels.extractHeaderName(prefix + ".test-bbb"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ PluginConfigManager build(Map<String, String> source) {
}

@Test
public void builder() {
public void testBuild() {
build();
assertTrue(true);
}

@Test
Expand Down Expand Up @@ -141,7 +142,4 @@ public void getConfig1() throws InterruptedException {
configs.updateConfigs(Collections.singletonMap(String.format("ssss.%s.global.%s.enabled", DOMAIN, TEST_AAA_ID), "false"));
}

@Test
public void shutdown() {
}
}
Loading

0 comments on commit f2e68f7

Please sign in to comment.