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

Error out on incomplete config sections #589

Merged
merged 7 commits into from
Aug 29, 2024
Merged
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
45 changes: 25 additions & 20 deletions config/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,11 @@ func Read(logger *util.Logger, filename string) (Config, error) {

sections := configFile.Sections()
for _, section := range sections {
sectionName := section.Name()
if sectionName == "pganalyze" || sectionName == ini.DefaultSection {
// we already handled the pganalyze section above, and we don't use the default section
continue
}
config := &ServerConfig{}
*config = *defaultConfig

Expand All @@ -760,7 +765,20 @@ func Read(logger *util.Logger, filename string) (Config, error) {
if err != nil {
return conf, err
}
config.SectionName = section.Name()

if config.DbURL != "" {
_, err := url.Parse(config.DbURL)
if err != nil {
logger.PrintError("Could not parse db_url in section %s; check URL format and note that any special characters must be percent-encoded", config.SectionName)
}
}

if config.GetDbName() == "" {
logger.PrintError("No connection info found for section %s; see https://pganalyze.com/docs/collector/settings", sectionName)
continue
}

config.SectionName = sectionName
config.SystemID, config.SystemType, config.SystemScope, config.SystemIDFallback, config.SystemTypeFallback, config.SystemScopeFallback = identifySystem(*config)

config.Identifier = ServerIdentifier{
Expand All @@ -771,32 +789,19 @@ func Read(logger *util.Logger, filename string) (Config, error) {
SystemScope: config.SystemScope,
}

if config.GetDbName() != "" {
// Ensure we have no duplicate identifiers within one collector
skip := false
for _, server := range conf.Servers {
if config.Identifier == server.Identifier {
skip = true
}
}
if skip {
// Ensure we have no duplicate identifiers within one collector
for _, server := range conf.Servers {
if config.Identifier == server.Identifier {
logger.PrintError("Skipping config section %s, detected as duplicate. Note: To monitor multiple databases on the same server, db_name accepts a comma-separated list.", config.SectionName)
} else {
conf.Servers = append(conf.Servers, *config)
continue
}
}

if config.DbURL != "" {
_, err := url.Parse(config.DbURL)
if err != nil {
prefixedLogger := logger.WithPrefix(config.SectionName)
prefixedLogger.PrintError("Could not parse db_url; check URL format and note that any special characters must be percent-encoded")
}
}
conf.Servers = append(conf.Servers, *config)
}

if len(conf.Servers) == 0 {
return conf, fmt.Errorf("Configuration file is empty, please edit %s and reload the collector", filename)
return conf, fmt.Errorf("Configuration contains no valid servers, please edit %s and reload the collector", filename)
}
} else {
if util.IsHeroku() {
Expand Down
Loading