Skip to content

Commit 499c761

Browse files
committed
refactor: simplify setting default config
1 parent 219d1b0 commit 499c761

File tree

1 file changed

+45
-38
lines changed

1 file changed

+45
-38
lines changed

sourceplusplus/SourcePlusPlus.py

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,22 @@
1616

1717
class SourcePlusPlus(object):
1818

19-
def get_config_value(self, env, default, true_default):
20-
env_value = os.getenv(env)
21-
if env_value is not None:
22-
return env_value
23-
elif default is not None:
24-
return default
25-
else:
26-
return true_default
19+
@staticmethod
20+
def __set_config_default(config_dict, config_name, env, default):
21+
config_path = config_name.split(".")
22+
tmp_config = config_dict
23+
for i in range(len(config_path)):
24+
if i == len(config_path) - 1 and tmp_config.get(config_path[i]) is None:
25+
if default is bool:
26+
tmp_config[config_path[i]] = str(os.getenv(env, default)).lower() == "true"
27+
else:
28+
tmp_config[config_path[i]] = os.getenv(env, default)
29+
30+
if tmp_config.get(config_path[i]) is None:
31+
tmp_config[config_path[i]] = {}
32+
tmp_config = tmp_config[config_path[i]]
33+
else:
34+
tmp_config = tmp_config[config_path[i]]
2735

2836
def __init__(self, args: dict = None):
2937
if args is None:
@@ -33,41 +41,40 @@ def __init__(self, args: dict = None):
3341
if os.path.exists(probe_config_file):
3442
probe_config = yaml.full_load(open(probe_config_file, "r"))
3543

36-
# ensure probe_config has required keys
37-
if probe_config.get("spp") is None:
38-
probe_config["spp"] = {}
39-
if probe_config.get("skywalking") is None:
40-
probe_config["skywalking"] = {}
41-
if probe_config["skywalking"].get("collector") is None:
42-
probe_config["skywalking"]["collector"] = {}
43-
if probe_config["skywalking"].get("agent") is None:
44-
probe_config["skywalking"]["agent"] = {}
45-
46-
# set default values
47-
probe_config["spp"]["probe_id"] = self.get_config_value(
48-
"SPP_PROBE_ID", probe_config["spp"].get("probe_id"), str(uuid.uuid4())
44+
# set spp default values
45+
self.__set_config_default(
46+
probe_config, "spp.probe_id", "SPP_PROBE_ID",
47+
str(uuid.uuid4())
4948
)
50-
probe_config["spp"]["platform_host"] = self.get_config_value(
51-
"SPP_PLATFORM_HOST", probe_config["spp"].get("platform_host"), "localhost"
49+
self.__set_config_default(
50+
probe_config, "spp.platform_host", "SPP_PLATFORM_HOST",
51+
"localhost"
5252
)
53-
probe_config["spp"]["platform_port"] = self.get_config_value(
54-
"SPP_PLATFORM_PORT", probe_config["spp"].get("platform_port"), 12800
53+
self.__set_config_default(
54+
probe_config, "spp.platform_port", "SPP_PLATFORM_PORT",
55+
12800
5556
)
56-
probe_config["spp"]["verify_host"] = str(self.get_config_value(
57-
"SPP_TLS_VERIFY_HOST", probe_config["spp"].get("verify_host"), True
58-
)).lower() == "true"
59-
probe_config["spp"]["ssl_enabled"] = str(self.get_config_value(
60-
"SPP_HTTP_SSL_ENABLED", probe_config["spp"].get("ssl_enabled"), True
61-
)).lower() == "true"
62-
probe_config["skywalking"]["agent"]["service_name"] = self.get_config_value(
63-
"SPP_SERVICE_NAME", probe_config["skywalking"]["agent"].get("service_name"), "spp"
57+
self.__set_config_default(
58+
probe_config, "spp.verify_host", "SPP_TLS_VERIFY_HOST",
59+
True
60+
)
61+
self.__set_config_default(
62+
probe_config, "spp.ssl_enabled", "SPP_HTTP_SSL_ENABLED",
63+
True
64+
)
65+
self.__set_config_default(
66+
probe_config, "skywalking.agent.service_name", "SPP_SERVICE_NAME",
67+
"spp"
6468
)
6569

66-
skywalking_port = self.get_config_value("SPP_OAP_PORT", 11800, 11800)
67-
probe_config["skywalking"]["collector"]["backend_service"] = self.get_config_value(
68-
"SPP_SKYWALKING_BACKEND_SERVICE",
69-
probe_config["skywalking"]["collector"].get("backend_service"),
70-
probe_config["spp"]["platform_host"] + ":" + str(skywalking_port)
70+
# set sw default values
71+
self.__set_config_default(
72+
probe_config, "skywalking.collector.backend_service", "SPP_SKYWALKING_BACKEND_SERVICE",
73+
probe_config["spp"]["platform_host"] + ":11800"
74+
)
75+
self.__set_config_default(
76+
probe_config, "skywalking.plugin.toolkit.log.transmit_formatted", "SPP_SKYWALKING_LOG_TRANSMIT_FORMATTED",
77+
True
7178
)
7279

7380
for key, val in args.items():

0 commit comments

Comments
 (0)