-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start preparing application for taking yaml config
- Loading branch information
Showing
13 changed files
with
433 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package config | ||
|
||
import ( | ||
"log/slog" | ||
"os" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
const ( | ||
defaultConfigLocation = "config.yml" | ||
defaultPort = 9198 | ||
defaultLogLevel = "info" | ||
defaultLogstashURL = "http://localhost:9600" | ||
) | ||
|
||
var ( | ||
ExporterConfigLocation = getEnvWithDefault("EXPORTER_CONFIG_LOCATION", defaultConfigLocation) | ||
) | ||
|
||
// LogstashServer represents individual Logstash server configuration | ||
type LogstashServer struct { | ||
URL string `yaml:"url"` | ||
} | ||
|
||
// LogstashConfig holds the configuration for all Logstash servers | ||
type LogstashConfig struct { | ||
Servers []LogstashServer `yaml:"servers"` | ||
} | ||
|
||
// ServerConfig represents the server configuration | ||
type ServerConfig struct { | ||
// Host is the host the exporter will listen on. | ||
// Defaults to an empty string, which will listen on all interfaces | ||
// Can be overridden by setting the HOST environment variable | ||
// For windows, use "localhost", because an empty string will not work | ||
// with the default windows firewall configuration. | ||
// Alternatively you can change the firewall configuration to allow | ||
// connections to the port from all interfaces. | ||
Host string `yaml:"host"` | ||
Port int `yaml:"port"` | ||
} | ||
|
||
// LoggingConfig represents the logging configuration | ||
type LoggingConfig struct { | ||
Level string `yaml:"level"` | ||
} | ||
|
||
// Config represents the overall configuration loaded from the YAML file | ||
type Config struct { | ||
Logstash LogstashConfig `yaml:"logstash"` | ||
Server ServerConfig `yaml:"server"` | ||
Logging LoggingConfig `yaml:"logging"` | ||
} | ||
|
||
// loadConfig loads the configuration from the YAML file. | ||
func loadConfig(location string) (*Config, error) { | ||
data, err := os.ReadFile(location) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var config Config | ||
err = yaml.Unmarshal(data, &config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &config, nil | ||
} | ||
|
||
// mergeWithDefault merges the loaded configuration with the default configuration values. | ||
func mergeWithDefault(config *Config) *Config { | ||
if config == nil { | ||
config = &Config{} | ||
} | ||
|
||
if config.Server.Port == 0 { | ||
slog.Debug("using default port", "port", defaultPort) | ||
config.Server.Port = defaultPort | ||
} | ||
|
||
if config.Logging.Level == "" { | ||
slog.Debug("using default log level", "level", defaultLogLevel) | ||
config.Logging.Level = defaultLogLevel | ||
} | ||
|
||
if len(config.Logstash.Servers) == 0 { | ||
slog.Debug("using default logstash server", "url", defaultLogstashURL) | ||
config.Logstash.Servers = append(config.Logstash.Servers, LogstashServer{ | ||
URL: defaultLogstashURL, | ||
}) | ||
} | ||
|
||
return config | ||
} | ||
|
||
// GetConfig combines loadConfig and mergeWithDefault to get the final configuration. | ||
func GetConfig(location string) (*Config, error) { | ||
config, err := loadConfig(location) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
mergedConfig := mergeWithDefault(config) | ||
return mergedConfig, nil | ||
} | ||
|
||
func (cfg *Config) GetLogstashUrls() []string { | ||
urls := make([]string, len(cfg.Logstash.Servers)) | ||
for i, server := range cfg.Logstash.Servers { | ||
urls[i] = server.URL | ||
} | ||
return urls | ||
} |
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,173 @@ | ||
package config | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestLoadConfig(t *testing.T) { | ||
t.Parallel() | ||
t.Run("loads valid config", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
location := "../fixtures/valid_config.yml" | ||
config, err := loadConfig(location) | ||
|
||
if err != nil { | ||
t.Fatalf("got an error %v", err) | ||
} | ||
if config == nil { | ||
t.Fatal("expected config to be non-nil") | ||
} | ||
if config.Logstash.Servers[0].URL != "http://localhost:9601" { | ||
t.Errorf("expected URL to be %v, got %v", "http://localhost:9601", config.Logstash.Servers[0].URL) | ||
} | ||
}) | ||
|
||
t.Run("returns error for non-existent file", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
location := "../fixtures/non_existent.yml" | ||
config, err := loadConfig(location) | ||
|
||
if err == nil { | ||
t.Fatal("expected error, got none") | ||
} | ||
if config != nil { | ||
t.Fatal("expected config to be nil") | ||
} | ||
}) | ||
|
||
t.Run("returns error for invalid config", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
location := "../fixtures/invalid_config.toml" | ||
config, err := loadConfig(location) | ||
|
||
if err == nil { | ||
t.Fatal("expected error, got none") | ||
} | ||
|
||
if config != nil { | ||
t.Fatal("expected config to be nil") | ||
} | ||
}) | ||
} | ||
|
||
func TestMergeWithDefault(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("merge with empty config", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
config := &Config{} | ||
mergedConfig := mergeWithDefault(config) | ||
|
||
if mergedConfig.Server.Port != defaultPort { | ||
t.Errorf("expected port to be %v, got %v", defaultPort, mergedConfig.Server.Port) | ||
} | ||
if mergedConfig.Logging.Level != defaultLogLevel { | ||
t.Errorf("expected level to be %v, got %v", defaultLogLevel, mergedConfig.Logging.Level) | ||
} | ||
if mergedConfig.Logstash.Servers[0].URL != defaultLogstashURL { | ||
t.Errorf("expected URL to be %v, got %v", defaultLogstashURL, mergedConfig.Logstash.Servers[0].URL) | ||
} | ||
}) | ||
|
||
t.Run("merge with nil config", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
mergedConfig := mergeWithDefault(nil) | ||
|
||
if mergedConfig.Server.Port != defaultPort { | ||
t.Errorf("expected port to be %v, got %v", defaultPort, mergedConfig.Server.Port) | ||
} | ||
if mergedConfig.Logging.Level != defaultLogLevel { | ||
t.Errorf("expected level to be %v, got %v", defaultLogLevel, mergedConfig.Logging.Level) | ||
} | ||
if mergedConfig.Logstash.Servers[0].URL != defaultLogstashURL { | ||
t.Errorf("expected URL to be %v, got %v", defaultLogstashURL, mergedConfig.Logstash.Servers[0].URL) | ||
} | ||
}) | ||
|
||
t.Run("merge with non-empty config", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
config := &Config{ | ||
Server: ServerConfig{ | ||
Port: 1234, | ||
}, | ||
Logging: LoggingConfig{ | ||
Level: "debug", | ||
}, | ||
Logstash: LogstashConfig{ | ||
Servers: []LogstashServer{ | ||
{URL: "http://localhost:9601"}, | ||
{URL: "http://localhost:9602"}, | ||
}, | ||
}, | ||
} | ||
|
||
mergedConfig := mergeWithDefault(config) | ||
|
||
if mergedConfig.Server.Port != 1234 { | ||
t.Errorf("expected port to be %v, got %v", 1234, mergedConfig.Server.Port) | ||
} | ||
|
||
if mergedConfig.Logging.Level != "debug" { | ||
t.Errorf("expected level to be %v, got %v", "debug", mergedConfig.Logging.Level) | ||
} | ||
|
||
if mergedConfig.Logstash.Servers[0].URL != "http://localhost:9601" { | ||
t.Errorf("expected URL to be %v, got %v", "http://localhost:9601", mergedConfig.Logstash.Servers[0].URL) | ||
} | ||
|
||
if mergedConfig.Logstash.Servers[1].URL != "http://localhost:9602" { | ||
t.Errorf("expected URL to be %v, got %v", "http://localhost:9602", mergedConfig.Logstash.Servers[1].URL) | ||
} | ||
}) | ||
} | ||
|
||
func TestGetConfig(t *testing.T) { | ||
t.Run("returns valid config", func(t *testing.T) { | ||
|
||
location := "../fixtures/valid_config.yml" | ||
config, err := GetConfig(location) | ||
|
||
if err != nil { | ||
t.Fatalf("got an error %v", err) | ||
} | ||
if config == nil { | ||
t.Fatal("expected config to be non-nil") | ||
} | ||
}) | ||
|
||
t.Run("returns error for invalid config", func(t *testing.T) { | ||
location := "../fixtures/invalid_config.yml" | ||
config, err := GetConfig(location) | ||
|
||
if err == nil { | ||
t.Fatal("expected error, got none") | ||
} | ||
if config != nil { | ||
t.Fatal("expected config to be nil") | ||
} | ||
}) | ||
} | ||
|
||
func TestGetLogstashUrls(t *testing.T) { | ||
t.Run("gets Logstash URLs correctly", func(t *testing.T) { | ||
config := &Config{ | ||
Logstash: LogstashConfig{ | ||
Servers: []LogstashServer{{URL: "http://localhost:9601"}}, | ||
}, | ||
} | ||
|
||
urls := config.GetLogstashUrls() | ||
expectedUrls := []string{"http://localhost:9601"} | ||
|
||
if !reflect.DeepEqual(urls, expectedUrls) { | ||
t.Errorf("expected urls to be %v, got %v", expectedUrls, urls) | ||
} | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,2 @@ | ||
[config] | ||
theConfigShouldBe = 'yaml' |
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,8 @@ | ||
logstash: | ||
servers: | ||
- url: "http://localhost:9601" | ||
server: | ||
host: "127.0.0.1" | ||
port: 9200 | ||
logging: | ||
level: "debug" |
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
Oops, something went wrong.