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

[commands/check] Add --discovery-instances option to agent check command #10448

Merged
merged 6 commits into from
Jan 7, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
shorter name
  • Loading branch information
AlexandreYang committed Jan 6, 2022
commit 396d5e628b4ca0daba0f979ee22d35505e49c370
66 changes: 33 additions & 33 deletions cmd/agent/common/commands/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,31 @@ import (
)

var (
checkRate bool
checkTimes int
checkPause int
checkName string
checkDelay int
logLevel string
formatJSON bool
formatTable bool
breakPoint string
fullSketches bool
saveFlare bool
profileMemory bool
profileMemoryDir string
profileMemoryFrames string
profileMemoryGC string
profileMemoryCombine string
profileMemorySort string
profileMemoryLimit string
profileMemoryDiff string
profileMemoryFilters string
profileMemoryUnit string
profileMemoryVerbose string
discoveryTimeout uint
discoveryRetryInterval uint
discoveryInstancesCount uint
checkRate bool
checkTimes int
checkPause int
checkName string
checkDelay int
logLevel string
formatJSON bool
formatTable bool
breakPoint string
fullSketches bool
saveFlare bool
profileMemory bool
profileMemoryDir string
profileMemoryFrames string
profileMemoryGC string
profileMemoryCombine string
profileMemorySort string
profileMemoryLimit string
profileMemoryDiff string
profileMemoryFilters string
profileMemoryUnit string
profileMemoryVerbose string
discoveryTimeout uint
discoveryRetryInterval uint
discoveryInstances uint
)

func setupCmd(cmd *cobra.Command) {
Expand All @@ -82,7 +82,7 @@ func setupCmd(cmd *cobra.Command) {
cmd.Flags().BoolVarP(&saveFlare, "flare", "", false, "save check results to the log dir so it may be reported in a flare")
cmd.Flags().UintVarP(&discoveryTimeout, "discovery-timeout", "", 5, "max retry duration until Autodiscovery resolves the check template (in seconds)")
cmd.Flags().UintVarP(&discoveryRetryInterval, "discovery-retry-interval", "", 1, "duration between retries until Autodiscovery resolves the check template (in seconds)")
cmd.Flags().UintVarP(&discoveryInstancesCount, "discovery-instances-count", "", 1, "number of check instances to wait (retry/wait until the specified number of instances is not reached)")
cmd.Flags().UintVarP(&discoveryInstances, "discovery-instances", "", 1, "number of check instances to wait (retry/wait until the specified number of instances is not reached)")
AlexandreYang marked this conversation as resolved.
Show resolved Hide resolved
config.Datadog.BindPFlag("cmd.check.fullsketches", cmd.Flags().Lookup("full-sketches")) //nolint:errcheck

// Power user flags - mark as hidden
Expand Down Expand Up @@ -154,7 +154,7 @@ func Check(loggerName config.LoggerName, confFilePath *string, flagNoColor *bool
discoveryRetryInterval = discoveryTimeout
}

allConfigs := waitForConfigs(checkName, time.Duration(discoveryRetryInterval)*time.Second, time.Duration(discoveryTimeout)*time.Second, discoveryInstancesCount)
allConfigs := waitForConfigs(checkName, time.Duration(discoveryRetryInterval)*time.Second, time.Duration(discoveryTimeout)*time.Second, discoveryInstances)

// make sure the checks in cs are not JMX checks
for idx := range allConfigs {
Expand Down Expand Up @@ -723,25 +723,25 @@ func populateMemoryProfileConfig(initConfig map[string]interface{}) error {
return nil
}

// containsCheck returns true if the number of configs found for the check name is ast least the expected instances count
func containsCheck(checkName string, instancesCount uint, configs []integration.Config) bool {
// containsCheck returns true if the number of configs found for the check name is ast least the expected instances
func containsCheck(checkName string, instances uint, configs []integration.Config) bool {
AlexandreYang marked this conversation as resolved.
Show resolved Hide resolved
var matchedConfigsCount uint
for _, cfg := range configs {
if cfg.Name == checkName {
matchedConfigsCount++
}
}
return matchedConfigsCount >= instancesCount
return matchedConfigsCount >= instances
}

// waitForConfigs retries the collection of Autodiscovery configs until the check is found or the timeout is reached.
// Autodiscovery listeners run asynchronously, AC.GetAllConfigs() can fail at the beginning to resolve templated configs
// depending on non-deterministic factors (system load, network latency, active Autodiscovery listeners and their configurations).
// This function improves the resiliency of the check command.
AlexandreYang marked this conversation as resolved.
Show resolved Hide resolved
// Note: If the check corresponds to a non-template configuration it should be found on the first try and fast-returned.
func waitForConfigs(checkName string, retryInterval, timeout time.Duration, instancesCount uint) []integration.Config {
func waitForConfigs(checkName string, retryInterval, timeout time.Duration, instances uint) []integration.Config {
AlexandreYang marked this conversation as resolved.
Show resolved Hide resolved
allConfigs := common.AC.GetAllConfigs()
if containsCheck(checkName, instancesCount, allConfigs) {
if containsCheck(checkName, instances, allConfigs) {
return allConfigs
}

Expand All @@ -757,7 +757,7 @@ func waitForConfigs(checkName string, retryInterval, timeout time.Duration, inst
return allConfigs
case <-retryTicker.C:
allConfigs = common.AC.GetAllConfigs()
if containsCheck(checkName, instancesCount, allConfigs) {
if containsCheck(checkName, instances, allConfigs) {
return allConfigs
}
}
Expand Down