Skip to content

Commit

Permalink
[autodiscovery] Add ignore_autodiscovery_tags config parameter (#4521)
Browse files Browse the repository at this point in the history
In some cases, a check should not receive tags coming from the
autodiscovery listeners.
By default `ignore_autodiscovery_tags` is set to false which doesn't
change the behavior of the checks.
The first check that will use it is `kubernetes_state`.
  • Loading branch information
clamoriniere authored Nov 29, 2019
1 parent 4b6b2e5 commit eacb09d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 32 deletions.
29 changes: 18 additions & 11 deletions pkg/autodiscovery/configresolver/configresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,29 +116,36 @@ func Resolve(tpl integration.Config, svc listeners.Service) (integration.Config,
return resolvedConfig, errors.New("unable to resolve, service not ready")
}

tags, err := svc.GetTags()
if err != nil {
if err := SubstituteTemplateVariables(&resolvedConfig, templateVariables, svc); err != nil {
return resolvedConfig, err
}

err = SubstituteTemplateVariables(&resolvedConfig, templateVariables, svc)
if err != nil {
return resolvedConfig, err
}

err = SubstituteTemplateEnvVars(&resolvedConfig)
if err != nil {
if err := SubstituteTemplateEnvVars(&resolvedConfig); err != nil {
// We add the service name to the error here, since SubstituteTemplateEnvVars doesn't know about that
return resolvedConfig, fmt.Errorf("%s, skipping service %s", err, svc.GetEntity())
}

if !tpl.IgnoreAutodiscoveryTags {
if err := addServiceTags(&resolvedConfig, svc); err != nil {
return resolvedConfig, fmt.Errorf("unable to add tags for service '%s', err: %s", svc.GetEntity(), err)
}
}

return resolvedConfig, nil
}

func addServiceTags(resolvedConfig *integration.Config, svc listeners.Service) error {
tags, err := svc.GetTags()
if err != nil {
return err
}
for i := 0; i < len(resolvedConfig.Instances); i++ {
err = resolvedConfig.Instances[i].MergeAdditionalTags(tags)
if err != nil {
return resolvedConfig, err
return err
}
}
return resolvedConfig, nil
return nil
}

func getHost(tplVar []byte, svc listeners.Service) ([]byte, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/autodiscovery/configresolver/configresolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ func TestResolve(t *testing.T) {
ADIdentifiers: []string{"redis"},
Instances: []integration.Data{integration.Data("host: %%FOO%%")},
},
errorString: "yaml: found character that cannot start any token",
errorString: "unable to add tags for service 'a5901276aed1', err: yaml: found character that cannot start any token",
},
//// check overrides
{
Expand Down
27 changes: 14 additions & 13 deletions pkg/autodiscovery/integration/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,20 @@ const (

// Config is a generic container for configuration files
type Config struct {
Name string `json:"check_name"` // the name of the check
Instances []Data `json:"instances"` // array of Yaml configurations
InitConfig Data `json:"init_config"` // the init_config in Yaml (python check only)
MetricConfig Data `json:"metric_config"` // the metric config in Yaml (jmx check only)
LogsConfig Data `json:"logs"` // the logs config in Yaml (logs-agent only)
ADIdentifiers []string `json:"ad_identifiers"` // the list of AutoDiscovery identifiers (optional)
Provider string `json:"provider"` // the provider that issued the config
Entity string `json:"-"` // the entity ID (optional)
TaggerEntity string `json:"-"` // the tagger entity ID (optional)
ClusterCheck bool `json:"cluster_check"` // cluster-check configuration flag
NodeName string `json:"node_name"` // node name in case of an endpoint check backed by a pod
CreationTime CreationTime `json:"-"` // creation time of service
Source string `json:"source"` // the source of the configuration
Name string `json:"check_name"` // the name of the check
Instances []Data `json:"instances"` // array of Yaml configurations
InitConfig Data `json:"init_config"` // the init_config in Yaml (python check only)
MetricConfig Data `json:"metric_config"` // the metric config in Yaml (jmx check only)
LogsConfig Data `json:"logs"` // the logs config in Yaml (logs-agent only)
ADIdentifiers []string `json:"ad_identifiers"` // the list of AutoDiscovery identifiers (optional)
Provider string `json:"provider"` // the provider that issued the config
Entity string `json:"-"` // the entity ID (optional)
TaggerEntity string `json:"-"` // the tagger entity ID (optional)
ClusterCheck bool `json:"cluster_check"` // cluster-check configuration flag
NodeName string `json:"node_name"` // node name in case of an endpoint check backed by a pod
CreationTime CreationTime `json:"-"` // creation time of service
Source string `json:"source"` // the source of the configuration
IgnoreAutodiscoveryTags bool `json:"ignore_autodiscovery_tags"` // Use to ignore tags coming from autodiscovery
}

// CommonInstanceConfig holds the reserved fields for the yaml instance data
Expand Down
18 changes: 11 additions & 7 deletions pkg/autodiscovery/providers/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ import (
)

type configFormat struct {
ADIdentifiers []string `yaml:"ad_identifiers"`
ClusterCheck bool `yaml:"cluster_check"`
InitConfig interface{} `yaml:"init_config"`
MetricConfig interface{} `yaml:"jmx_metrics"`
LogsConfig interface{} `yaml:"logs"`
Instances []integration.RawMap
DockerImages []string `yaml:"docker_images"` // Only imported for deprecation warning
ADIdentifiers []string `yaml:"ad_identifiers"`
ClusterCheck bool `yaml:"cluster_check"`
InitConfig interface{} `yaml:"init_config"`
MetricConfig interface{} `yaml:"jmx_metrics"`
LogsConfig interface{} `yaml:"logs"`
Instances []integration.RawMap
DockerImages []string `yaml:"docker_images"` // Only imported for deprecation warning
IgnoreAutodiscoveryTags bool `yaml:"ignore_autodiscovery_tags"` // Use to ignore tags coming from autodiscovery
}

type configPkg struct {
Expand Down Expand Up @@ -303,6 +304,9 @@ func GetIntegrationConfigFromFile(name, fpath string) (integration.Config, error
// Copy cluster_check status
config.ClusterCheck = cf.ClusterCheck

// Copy ignore_autodiscovery_tags parameter
config.IgnoreAutodiscoveryTags = cf.IgnoreAutodiscoveryTags

// DockerImages entry was found: we ignore it if no ADIdentifiers has been found
if len(cf.DockerImages) > 0 && len(cf.ADIdentifiers) == 0 {
return config, errors.New("the 'docker_images' section is deprecated, please use 'ad_identifiers' instead")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Each section from every releasenote are combined when the
# CHANGELOG.rst is rendered. So the text needs to be worded so that
# it does not depend on any information only available in another
# section. This may mean repeating some details, but each section
# must be readable independently of the other.
#
# Each section note must be formatted as reStructuredText.
---
enhancements:
- |
Add `ignore_autodiscovery_tags` parameter config check.
In some cases, a check should not receive tags coming from the autodiscovery listeners.
By default `ignore_autodiscovery_tags` is set to false which doesn't change the behavior of the checks.
The first check that will use it is `kubernetes_state`.

0 comments on commit eacb09d

Please sign in to comment.