-
Notifications
You must be signed in to change notification settings - Fork 15
/
config.go
76 lines (63 loc) · 2.02 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
package main
import (
"flag"
"log"
"os"
"strings"
"time"
"github.com/jwkohnen/conntrack-stats-exporter/exporter"
)
type config struct {
addr string
path string
netns []string
prefix string
quiet bool
timeoutGathering time.Duration
timeoutShutdown time.Duration
timeoutHTTP time.Duration
fixMetricNames bool
logf func(string, ...any)
}
func configure() (config, []exporter.Option) {
// default values
c := config{
addr: ":9371",
path: "/metrics",
prefix: "conntrack_stats",
quiet: false,
timeoutGathering: time.Second * 5,
timeoutShutdown: time.Second * 3,
timeoutHTTP: time.Second * 10,
fixMetricNames: false,
logf: func(string, ...any) {},
}
var (
tmpNetns string
)
var fs = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
fs.StringVar(&c.path, "path", c.path, "metrics endpoint path")
fs.StringVar(&c.addr, "addr", c.addr, "TCP address to listen on")
fs.StringVar(&c.prefix, "prefix", c.prefix, "metrics prefix")
fs.BoolVar(&c.quiet, "quiet", c.quiet, "don't log anything")
fs.DurationVar(&c.timeoutGathering, "timeout-gathering", c.timeoutGathering, "timeout for gathering metrics")
fs.DurationVar(&c.timeoutShutdown, "timeout-shutdown", c.timeoutShutdown, "timeout for graceful shutdown")
fs.DurationVar(&c.timeoutHTTP, "timeout-http", c.timeoutHTTP, "timeout for HTTP requests")
fs.BoolVar(&c.fixMetricNames, "fix-metric-names", c.fixMetricNames, "fix historic metric name choices")
fs.StringVar(&tmpNetns, "netns", "", "List of netns names separated by comma")
_ = fs.Parse(os.Args[1:])
c.netns = strings.Split(tmpNetns, ",")
if !c.quiet {
c.logf = log.New(os.Stderr, "", 0).Printf
}
opts := []exporter.Option{
exporter.WithNetNs(c.netns),
exporter.WithTimeout(c.timeoutGathering),
exporter.WithPrefix(c.prefix),
exporter.WithErrorLogger(c.logf),
}
if c.fixMetricNames {
opts = append(opts, exporter.WithFixMetricNames())
}
return c, opts
}