/
config.go
81 lines (65 loc) · 2.64 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package config
import (
"errors"
"fmt"
"strings"
"time"
"github.com/namsral/flag"
)
type Config struct {
Timeout time.Duration
Port uint
Verbose bool
InsecureSkipVerify bool
EventStoreURL string
EventStoreUser string
EventStorePassword string
EnableParkedMessagesStats bool
Streams []string
StreamsSeparator string
EnableTCPConnectionStats bool
}
func Load(args []string, suppressOutput bool) (*Config, error) {
config := &Config{}
fs := flag.NewFlagSet("flagset", flag.ContinueOnError)
fs.String(flag.DefaultConfigFlagname, "", "Path to config file")
fs.StringVar(&config.EventStoreURL, "eventstore-url", "http://localhost:2113", "EventStore URL")
fs.StringVar(&config.EventStoreUser, "eventstore-user", "", "EventStore User")
fs.StringVar(&config.EventStorePassword, "eventstore-password", "", "EventStore Password")
fs.UintVar(&config.Port, "port", 9448, "Port to expose scraping endpoint on")
fs.DurationVar(&config.Timeout, "timeout", time.Second*10, "Timeout for the scrape operation")
fs.BoolVar(&config.Verbose, "verbose", false, "Enable verbose logging")
fs.BoolVar(&config.InsecureSkipVerify, "insecure-skip-verify", false, "Skip TLS certificatte verification for EventStore HTTP client")
fs.BoolVar(&config.EnableParkedMessagesStats, "enable-parked-messages-stats", false, "Enable parked messages stats scraping")
streamsString := fs.String("streams", "", "List of streams to get metrics for")
fs.StringVar(&config.StreamsSeparator, "streams-separator", ",", "Separator for streams list (default: ',')")
fs.BoolVar(&config.EnableTCPConnectionStats, "enable-tcp-connection-stats", false, "Enable TCP connection stats scraping")
if suppressOutput {
fs.Usage = func() {}
}
err := fs.Parse(args)
if err != nil {
return nil, err
}
config.Streams = parseStreamList(streamsString, config.StreamsSeparator)
err = config.validate()
if err != nil {
return nil, err
}
return config, nil
}
func (config *Config) validate() error {
if (config.EventStoreUser != "" && config.EventStorePassword == "") || (config.EventStoreUser == "" && config.EventStorePassword != "") {
return errors.New("EventStore user and password should both be specified, or should both be empty")
}
if len(config.StreamsSeparator) != 1 {
return fmt.Errorf("streams separator should be a single character, got %s", config.StreamsSeparator)
}
return nil
}
func parseStreamList(streamsString *string, streamsSeparator string) []string {
if streamsString == nil || *streamsString == "" {
return []string{}
}
return strings.Split(*streamsString, streamsSeparator)
}