-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
106 lines (81 loc) · 3.47 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"flag"
"net"
"strconv"
"github.com/rocketmq-exporter-go/exporter"
"github.com/apache/rocketmq-client-go/v2/rlog"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
// _ "net/http/pprof"
)
func toFlagInt(name string, help string, value string) *int {
flag.CommandLine.String(name, value, help) // hack around flag.Parse and glog.init flags
return kingpin.Flag(name, help).Default(value).Int()
}
func toFlagString(name string, help string, value string) *string {
flag.CommandLine.String(name, value, help) // hack around flag.Parse and glog.init flags
return kingpin.Flag(name, help).Default(value).String()
}
func toFlagStrings(name string, help string, value string) *[]string {
flag.CommandLine.String(name, value, help) // hack around flag.Parse and glog.init flags
return kingpin.Flag(name, help).Default(value).Strings()
}
func toFlagBool(name string, help string, value bool, valueString string) *bool {
flag.CommandLine.Bool(name, value, help) // hack around flag.Parse and glog.init flags
return kingpin.Flag(name, help).Default(valueString).Bool()
}
func toFlagIntVar(name string, help string, value int, valueString string, target *int) {
flag.CommandLine.Int(name, value, help) // hack around flag.Parse and glog.init flags
kingpin.Flag(name, help).Default(valueString).IntVar(target)
}
func main() {
var (
listenAddress = toFlagString("web.listen-address", "Address to listen on for web interface and telemetry.", ":9999")
metricsPath = toFlagString("web.telemetry-path", "Path under which to expose metrics.", "/metrics")
logFileEnable = toFlagBool("log.file.enable", "Log write to file enable.", false, "false")
logFilePath = toFlagString("log.file.path", "Path of log file.", "./logs/go.log")
useDomain = toFlagBool("rocketmq.nameserver.use.domian", "Address of rocketmq nameserver whether use domian.", false, "false")
nameServerIpAddress = toFlagStrings("rocketmq.nameserver", "Address (host:port) of rocketmq nameserver.", "127.0.0.1:9876")
nameServerDomainAddress = toFlagString("rocketmq.nameserver.domain", "Address (domain:port) of rocketmq nameserver.", "rocketmq.namesvr:9876")
nameServerPort = toFlagInt("rocketmq.nameserver.port", "Port of rocketmq nameserver.", "9876")
opts = &exporter.RocketmqOptions{}
)
toFlagIntVar("workers", "Number of workers", 100, "100", &opts.Workers)
kingpin.Version(version.Print("rocketmq_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
if *logFileEnable {
rlog.SetOutputPath(*logFilePath)
}
if *useDomain {
var ips, err = LookupIP(*nameServerDomainAddress, *nameServerPort)
rlog.Fatal("rocketmq.nameserver.domain parse err", map[string]interface{}{
"domain": *nameServerDomainAddress,
"err": err,
})
opts.NameServerAddress = ips
} else {
opts.NameServerAddress = *nameServerIpAddress
}
rlog.Info("Fly ", map[string]interface{}{
"listenAddress": *listenAddress,
"metricsPath": *metricsPath,
"opts": *opts,
})
exporter.Fly(*listenAddress, *metricsPath, opts)
}
// Because rocketmq client requires ip address rather than domain address
// LookupIP looks up host
// It returns a slice of that host's IPv4 addresses.
func LookupIP(host string, port int) ([]string, error) {
ips, err := net.LookupIP(host)
if err != nil {
return nil, err
}
var ipList = make([]string, len(ips))
for index, ip := range ips {
ipList[index] = ip.To4().String() + ":" + strconv.Itoa(port)
}
return ipList, nil
}