Skip to content
Merged
Show file tree
Hide file tree
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
64 changes: 64 additions & 0 deletions internal/collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,70 @@ matched_metric_mem{label_name="label2"} 200
# TYPE connections gauge
connections{type="tcp"} 150
connections{type="udp"} 25
`,
},
{
name: "value parsing failure",
config: &config.Config{
Metrics: []config.Metric{
{
Name: "parse_fail_metric",
Help: "metric that fails to parse.",
Type: "gauge",
Command: "echo 'not_number'",
},
},
},
executor: &mockExecutor{
output: "not_number",
},
expectedMetric: ``,
},
{
name: "field index out of range",
config: &config.Config{
Metrics: []config.Metric{
{
Name: "index_metric",
Help: "metric where field index is out of range.",
Type: "gauge",
Command: "echo 'one_value'",
Field: 1,
},
},
},
executor: &mockExecutor{
output: "one_value",
},
expectedMetric: ``,
},
{
name: "sub-metric with no matching pattern",
config: &config.Config{
Metrics: []config.Metric{
{
Name: "filter_metric",
Help: "metric with a sub-metric that filters lines.",
Command: "echo -e 'matched_line 100\nunmatched_line 200'",
SubMetrics: []config.SubMetric{
{
Name: "filtered_sub",
Help: "created for matched lines.",
Type: "gauge",
Field: 1,
Match: "^matched_line",
},
},
},
},
},
executor: &mockExecutor{
output: "matched_line 100\nunmatched_line 200",
},
expectedMetric: `
# HELP filter_metric_filtered_sub created for matched lines.
# TYPE filter_metric_filtered_sub gauge
filter_metric_filtered_sub 100
`,
},
}
Expand Down
8 changes: 4 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ type Logging struct {
}

type Global struct {
Timeout time.Duration `yaml:"timeout"`
CacheTTL time.Duration `yaml:"cache_ttl"`
MaxConcurrent int `yaml:"max_concurrent"`
CommandBlacklist []string `yaml:"command_blacklist"`
Timeout time.Duration `yaml:"timeout,omitempty"`
CacheTTL time.Duration `yaml:"cache_ttl,omitempty"`
MaxConcurrent int `yaml:"max_concurrent,omitempty"`
CommandBlacklist []string `yaml:"command_blacklist,omitempty"`
}

type Metric struct {
Expand Down
15 changes: 15 additions & 0 deletions internal/config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"os"
"time"

"gopkg.in/yaml.v3"
)
Expand All @@ -28,6 +29,18 @@ func GetPath() string {
return "configs/config.yaml"
}

func (c *Config) applyDefaults() {
if c.Global.Timeout == 0 {
c.Global.Timeout = 30 * time.Second
}
if c.Global.CacheTTL == 0 {
c.Global.CacheTTL = 3 * time.Second
}
if c.Global.MaxConcurrent == 0 {
c.Global.MaxConcurrent = 10
}
}

// Load reads and parses a YAML configuration file into Config struct.
//
// Returns an error if the file can`t be read or if the YAML is invalid. Panics if cfg is nil.
Expand All @@ -44,6 +57,8 @@ func Load(path string, cfg *Config) error {
return fmt.Errorf("failed to parse YAML from file %s: %w", path, err)
}

cfg.applyDefaults()

err = cfg.Validate()
if err != nil {
return fmt.Errorf("configuration is invalid: %s", err)
Expand Down
Loading