Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge configs from files and service discovery for the same check #3899

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 31 additions & 25 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1234,12 +1234,9 @@ def load_check_directory(agentConfig, hostname):
agentConfig['checksd_hostname'] = hostname
osname = get_os()

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

deprecated_checks.update(_deprecated_configs(agentConfig))

Expand All @@ -1254,31 +1251,40 @@ def load_check_directory(agentConfig, hostname):
if not conf_is_valid:
continue

if agentConfig.get(TRACE_CONFIG):
configs_and_sources[check_name] = (CONFIG_FROM_FILE, check_config)

# load the check
load_success, load_failure = load_check_from_places(check_config, check_name, checks_places, agentConfig)

initialized_checks.update(load_success)
init_failed_checks.update(load_failure)
configs_and_sources[check_name] = [ (CONFIG_FROM_FILE, check_config) ]

for check_name, service_disco_check_config in _service_disco_configs(agentConfig).iteritems():
# ignore this config from service disco if the check has been loaded through a file config
if check_name in initialized_checks or \
check_name in init_failed_checks or \
check_name in JMX_CHECKS:
continue

sd_init_config, sd_instances = service_disco_check_config[1]
if agentConfig.get(TRACE_CONFIG):
configs_and_sources[check_name] = (

# If a container or other discovered source wants to use the same check
# as defined in a file, append it to the instance list.
# The init_config will be from the first instance found, whether that's
# a file or the first container seen.
if configs_and_sources.get(check_name) is None:
configs_and_sources[check_name] = [ (
service_disco_check_config[0],
{'init_config': sd_init_config, 'instances': sd_instances})
{'init_config': sd_init_config, 'instances': sd_instances}
) ]
else:
configs_and_sources[check_name].append( (
service_disco_check_config[0],
{'init_config': sd_init_config, 'instances': sd_instances}) )

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

# load the check
# Merge the configs from multiple sources into the first element of each check_name list
for check_name, check_configs in configs_and_sources.iteritems():
if len(check_configs) > 1:
for config in check_configs[1:]:
configs_and_sources[check_name][0][1]['instances'].extend(config[1]['instances'])
log.warning('Different versions of `init_config` found for check %s. '
'Keeping the first one found.' % check_name)

# Load the checks
for check_name, checks in configs_and_sources.iteritems():
check_config = checks[0][1]
load_success, load_failure = load_check_from_places(check_config, check_name, checks_places, agentConfig)

initialized_checks.update(load_success)
Expand Down
17 changes: 9 additions & 8 deletions utils/configcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ def sd_configcheck(agentConfig):
# Then call load_check_directory here and pass the result to get_sd_configcheck
# to avoid circular imports
agentConfig[TRACE_CONFIG] = True
configs = {
# check_name: (config_source, config)
configs_and_sources = {
# check_name: [ (config_source, config), ... ]
}
print("\nLoading check configurations...\n\n")
configs = load_check_directory(agentConfig, get_hostname(agentConfig))
get_sd_configcheck(agentConfig, configs)
configs_and_sources = load_check_directory(agentConfig, get_hostname(agentConfig))
get_sd_configcheck(agentConfig, configs_and_sources)

def agent_container_inspect():
# Self inspection based on cgroups
Expand Down Expand Up @@ -87,13 +87,14 @@ def agent_container_inspect():
print "Could not inspect container: %s" % e


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

try:
print_containers()
Expand Down
12 changes: 6 additions & 6 deletions utils/service_discovery/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ def extract_agent_config(config):
agentConfig = {}

backend = config.get('Main', 'service_discovery_backend')
agentConfig['service_discovery'] = True
if backend in SD_BACKENDS:
agentConfig['service_discovery'] = True
else:
log.error("The backend {0} is not supported. "
"Service discovery won't be enabled.".format(backend))
agentConfig['service_discovery'] = False

conf_backend = None
if config.has_option('Main', 'sd_config_backend'):
conf_backend = config.get('Main', 'sd_config_backend')

if backend not in SD_BACKENDS:
log.error("The backend {0} is not supported. "
"Service discovery won't be enabled.".format(backend))
agentConfig['service_discovery'] = False

if conf_backend is None:
log.debug('No configuration backend provided for service discovery. '
'Only auto config templates will be used.')
Expand Down
5 changes: 2 additions & 3 deletions utils/service_discovery/sd_docker_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,9 @@ def get_configs(self):
else:
configs[check_name] = (source, (init_config, [instance]))
else:
conflict_init_msg = 'Different versions of `init_config` found for check {}. ' \
'Keeping the first one found.'
if configs[check_name][1][0] != init_config:
log.warning(conflict_init_msg.format(check_name))
log.warning('Different versions of `init_config` found for check %s. '
'Keeping the first one found.' % check_name)
if isinstance(instance, list):
for inst in instance:
configs[check_name][1][1].append(inst)
Expand Down