-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor isEnabled in separate package configcheck
- Loading branch information
Showing
9 changed files
with
100 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2021-present Datadog, Inc. | ||
|
||
package configcheck | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/mohae/deepcopy" | ||
"go.opentelemetry.io/collector/confmap" | ||
|
||
"github.com/DataDog/datadog-agent/comp/core/config" | ||
coreconfig "github.com/DataDog/datadog-agent/pkg/config/setup" | ||
) | ||
|
||
// ReadConfigSection from a config.Component object. | ||
func ReadConfigSection(cfg config.Reader, section string) *confmap.Conf { | ||
// Viper doesn't work well when getting subsections, since it | ||
// ignores environment variables and nil-but-present sections. | ||
// To work around this, we do the following two steps: | ||
|
||
// Step one works around https://github.com/spf13/viper/issues/819 | ||
// If we only had the stuff below, the nil sections would be ignored. | ||
// We want to take into account nil-but-present sections. | ||
// | ||
// Furthermore, Viper returns an `interface{}` nil in the case where | ||
// `section` is present but empty: e.g. we want to read | ||
// "otlp_config.receiver", but we have | ||
// | ||
// otlp_config: | ||
// receiver: | ||
// | ||
// `GetStringMap` it will fail to cast `interface{}` nil to | ||
// `map[string]interface{}` nil; we use `Get` and cast manually. | ||
rawVal := cfg.Get(section) | ||
stringMap := map[string]interface{}{} | ||
if val, ok := rawVal.(map[string]interface{}); ok { | ||
// deep copy since `cfg.Get` returns a reference | ||
stringMap = deepcopy.Copy(val).(map[string]interface{}) | ||
} | ||
|
||
// Step two works around https://github.com/spf13/viper/issues/1012 | ||
// we check every key manually, and if it belongs to the OTLP receiver section, | ||
// we set it. We need to do this to account for environment variable values. | ||
prefix := section + "." | ||
for _, key := range cfg.AllKeysLowercased() { | ||
if strings.HasPrefix(key, prefix) && cfg.IsSet(key) { | ||
mapKey := strings.ReplaceAll(key[len(prefix):], ".", confmap.KeyDelimiter) | ||
// deep copy since `cfg.Get` returns a reference | ||
stringMap[mapKey] = deepcopy.Copy(cfg.Get(key)) | ||
} | ||
} | ||
return confmap.NewFromStringMap(stringMap) | ||
} | ||
|
||
// IsEnabled checks if OTLP pipeline is enabled in a given config. | ||
func IsEnabled(cfg config.Reader) bool { | ||
return hasSection(cfg, coreconfig.OTLPReceiverSubSectionKey) | ||
} | ||
|
||
// HasLogsSectionEnabled checks if OTLP logs are explicitly enabled in a given config. | ||
func HasLogsSectionEnabled(cfg config.Reader) bool { | ||
return hasSection(cfg, coreconfig.OTLPLogsEnabled) && cfg.GetBool(coreconfig.OTLPLogsEnabled) | ||
} | ||
|
||
func hasSection(cfg config.Reader, section string) bool { | ||
// HACK: We want to mark as enabled if the section is present, even if empty, so that we get errors | ||
// from unmarshaling/validation done by the Collector code. | ||
// | ||
// IsSet won't work here: it will return false if the section is present but empty. | ||
// To work around this, we check if the receiver key is present in the string map, which does the 'correct' thing. | ||
_, ok := ReadConfigSection(cfg, coreconfig.OTLPSection).ToStringMap()[section] | ||
return ok | ||
} | ||
|
||
// IsDisplayed checks if the OTLP section should be rendered in the Agent | ||
func IsDisplayed() bool { | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters