Skip to content

Commit 151aec3

Browse files
committed
Merge configs from files and service discovery for the same check
1 parent 2ce604e commit 151aec3

File tree

2 files changed

+40
-33
lines changed

2 files changed

+40
-33
lines changed

config.py

+31-25
Original file line numberDiff line numberDiff line change
@@ -1234,12 +1234,9 @@ def load_check_directory(agentConfig, hostname):
12341234
agentConfig['checksd_hostname'] = hostname
12351235
osname = get_os()
12361236

1237-
# the TRACE_CONFIG flag is used by the configcheck to trace config object loading and
1238-
# where they come from (service discovery, auto config or config file)
1239-
if agentConfig.get(TRACE_CONFIG):
1240-
configs_and_sources = {
1241-
# check_name: (config_source, config)
1242-
}
1237+
configs_and_sources = {
1238+
# check_name: [ (config_source, config), ... ]
1239+
}
12431240

12441241
deprecated_checks.update(_deprecated_configs(agentConfig))
12451242

@@ -1254,31 +1251,40 @@ def load_check_directory(agentConfig, hostname):
12541251
if not conf_is_valid:
12551252
continue
12561253

1257-
if agentConfig.get(TRACE_CONFIG):
1258-
configs_and_sources[check_name] = (CONFIG_FROM_FILE, check_config)
1259-
1260-
# load the check
1261-
load_success, load_failure = load_check_from_places(check_config, check_name, checks_places, agentConfig)
1262-
1263-
initialized_checks.update(load_success)
1264-
init_failed_checks.update(load_failure)
1254+
configs_and_sources[check_name] = [ (CONFIG_FROM_FILE, check_config) ]
12651255

12661256
for check_name, service_disco_check_config in _service_disco_configs(agentConfig).iteritems():
1267-
# ignore this config from service disco if the check has been loaded through a file config
1268-
if check_name in initialized_checks or \
1269-
check_name in init_failed_checks or \
1270-
check_name in JMX_CHECKS:
1271-
continue
1272-
12731257
sd_init_config, sd_instances = service_disco_check_config[1]
1274-
if agentConfig.get(TRACE_CONFIG):
1275-
configs_and_sources[check_name] = (
1258+
1259+
# If a container or other discovered source wants to use the same check
1260+
# as defined in a file, append it to the instance list.
1261+
# The init_config will be from the first instance found, whether that's
1262+
# a file or the first container seen.
1263+
if configs_and_sources.get(check_name) is None:
1264+
configs_and_sources[check_name] = [ (
12761265
service_disco_check_config[0],
1277-
{'init_config': sd_init_config, 'instances': sd_instances})
1266+
{'init_config': sd_init_config, 'instances': sd_instances}
1267+
) ]
1268+
else:
1269+
configs_and_sources[check_name].append( (
1270+
service_disco_check_config[0],
1271+
{'init_config': sd_init_config, 'instances': sd_instances}) )
12781272

1279-
check_config = {'init_config': sd_init_config, 'instances': sd_instances}
1273+
# If called from utils/configcheck.py, return the list of checks that were found
1274+
if agentConfig.get(TRACE_CONFIG):
1275+
return configs_and_sources
12801276

1281-
# load the check
1277+
# Merge the configs from multiple sources into the first element of each check_name list
1278+
for check_name, check_configs in configs_and_sources.iteritems():
1279+
if len(check_configs) > 1:
1280+
for config in check_configs[1:]:
1281+
configs_and_sources[check_name][0][1]['instances'].extend(config[1]['instances'])
1282+
log.warning('Different versions of `init_config` found for check %s. '
1283+
'Keeping the first one found.' % check_name)
1284+
1285+
# Load the checks
1286+
for check_name, checks in configs_and_sources.iteritems():
1287+
check_config = checks[0][1]
12821288
load_success, load_failure = load_check_from_places(check_config, check_name, checks_places, agentConfig)
12831289

12841290
initialized_checks.update(load_success)

utils/configcheck.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ def sd_configcheck(agentConfig):
5151
# Then call load_check_directory here and pass the result to get_sd_configcheck
5252
# to avoid circular imports
5353
agentConfig[TRACE_CONFIG] = True
54-
configs = {
55-
# check_name: (config_source, config)
54+
configs_and_sources = {
55+
# check_name: [ (config_source, config), ... ]
5656
}
5757
print("\nLoading check configurations...\n\n")
58-
configs = load_check_directory(agentConfig, get_hostname(agentConfig))
59-
get_sd_configcheck(agentConfig, configs)
58+
configs_and_sources = load_check_directory(agentConfig, get_hostname(agentConfig))
59+
get_sd_configcheck(agentConfig, configs_and_sources)
6060

6161
def agent_container_inspect():
6262
# Self inspection based on cgroups
@@ -87,13 +87,14 @@ def agent_container_inspect():
8787
print "Could not inspect container: %s" % e
8888

8989

90-
def get_sd_configcheck(agentConfig, configs):
90+
def get_sd_configcheck(agentConfig, configs_and_sources):
9191
"""Trace how the configuration objects are loaded and from where.
9292
Also print containers detected by the agent and templates from the config store."""
9393
print("\nSource of the configuration objects built by the agent:\n")
94-
for check_name, config in configs.iteritems():
95-
print('Check "%s":\n source --> %s\n config --> %s\n' %
96-
(check_name, config[0], json.dumps(config[1], indent=2)))
94+
for check_name, configs in configs_and_sources:
95+
for config in configs:
96+
print('Check "%s":\n source --> %s\n config --> %s\n' %
97+
(check_name, config[0], json.dumps(config[1], indent=2)))
9798

9899
try:
99100
print_containers()

0 commit comments

Comments
 (0)