Skip to content

Commit

Permalink
Merge pull request #44 from digineo/cleanup
Browse files Browse the repository at this point in the history
cleanup: remove Gopkg configuration
  • Loading branch information
czerwonk authored Mar 19, 2021
2 parents f0600b0 + 5a0d23c commit b75d8cc
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 234 deletions.
186 changes: 0 additions & 186 deletions Gopkg.lock

This file was deleted.

34 changes: 0 additions & 34 deletions Gopkg.toml

This file was deleted.

11 changes: 7 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package config

import (
"fmt"
"io"
"time"

yaml "gopkg.in/yaml.v2"
)

// Config represents configuration for the exporter
// Config represents configuration for the exporter.
type Config struct {
Targets []string `yaml:"targets"`

Expand All @@ -34,9 +35,10 @@ func (d *duration) UnmarshalYAML(unmashal func(interface{}) error) error {
}
dur, err := time.ParseDuration(s)
if err != nil {
return err
return fmt.Errorf("failed to decode duration: %w", err)
}
*d = duration(dur)

return nil
}

Expand All @@ -50,12 +52,13 @@ func (d *duration) Set(dur time.Duration) {
*d = duration(dur)
}

// FromYAML reads YAML from reader and unmarshals it to Config
// FromYAML reads YAML from reader and unmarshals it to Config.
func FromYAML(r io.Reader) (*Config, error) {
c := &Config{}
err := yaml.NewDecoder(r).Decode(c)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to decode YAML: %w", err)
}

return c, nil
}
3 changes: 2 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

func TestParseConfig(t *testing.T) {
t.Parallel()

f, err := os.Open("testdata/config_test.yml")
if err != nil {
t.Error("failed to open file", err)
Expand Down Expand Up @@ -52,5 +54,4 @@ func TestParseConfig(t *testing.T) {
if expected := 120; c.Ping.Size != uint16(expected) {
t.Errorf("expected ping.payload-size to be %d, got %d", expected, c.Ping.Size)
}

}
17 changes: 11 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func main() {
kingpin.FatalUsage("ping.history-size must be greater than 0")
}

if cfg.Ping.Size < 0 || cfg.Ping.Size > 65500 {
if cfg.Ping.Size > 65500 {
kingpin.FatalUsage("ping.size must be between 0 and 65500")
}

Expand Down Expand Up @@ -112,18 +112,18 @@ func startMonitor(cfg *config.Config) (*mon.Monitor, error) {
resolver := setupResolver(cfg)
var bind4, bind6 string
if ln, err := net.Listen("tcp4", "127.0.0.1:0"); err == nil {
//ipv4 enabled
// ipv4 enabled
ln.Close()
bind4 = "0.0.0.0"
}
if ln, err := net.Listen("tcp6", "[::1]:0"); err == nil {
//ipv6 enabled
// ipv6 enabled
ln.Close()
bind6 = "::"
}
pinger, err := ping.New(bind4, bind6)
if err != nil {
return nil, err
return nil, fmt.Errorf("cannot start monitoring: %w", err)
}

if pinger.PayloadSize() != cfg.Ping.Size {
Expand Down Expand Up @@ -188,7 +188,8 @@ func startServer(monitor *mon.Monitor) {
reg.MustRegister(&pingCollector{monitor: monitor})
h := promhttp.HandlerFor(reg, promhttp.HandlerOpts{
ErrorLog: log.NewErrorLogger(),
ErrorHandling: promhttp.ContinueOnError})
ErrorHandling: promhttp.ContinueOnError,
})
http.Handle(*metricsPath, h)

log.Infof("Listening for %s on %s", *metricsPath, *listenAddress)
Expand All @@ -199,19 +200,21 @@ func loadConfig() (*config.Config, error) {
if *configFile == "" {
cfg := config.Config{}
addFlagToConfig(&cfg)

return &cfg, nil
}

f, err := os.Open(*configFile)
if err != nil {
return nil, err
return nil, fmt.Errorf("cannot load config file: %w", err)
}
defer f.Close()

cfg, err := config.FromYAML(f)
if err == nil {
addFlagToConfig(cfg)
}

return cfg, err
}

Expand All @@ -225,8 +228,10 @@ func setupResolver(cfg *config.Config) *net.Resolver {
}
dialer := func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{}

return d.DialContext(ctx, "udp", cfg.DNS.Nameserver)
}

return &net.Resolver{PreferGo: true, Dial: dialer}
}

Expand Down
Loading

0 comments on commit b75d8cc

Please sign in to comment.