-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
103 lines (85 loc) · 3.09 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rumblefrog/go-a2s"
"github.com/armsnyder/a2s-exporter/internal/collector"
)
// buildVersion variable is set at build time.
var buildVersion = "development"
func main() {
// Flags.
address := flag.String("address", envOrDefault("A2S_EXPORTER_QUERY_ADDRESS", ""), "Address of the A2S query server as host:port (This is a separate port from the main server port).")
port := flag.Int("port", envOrDefaultInt("A2S_EXPORTER_PORT", 9841), "Port for the metrics exporter.")
path := flag.String("path", envOrDefault("A2S_EXPORTER_PATH", "/metrics"), "Path for the metrics exporter.")
namespace := flag.String("namespace", envOrDefault("A2S_EXPORTER_NAMESPACE", "a2s"), "Namespace prefix for all exported a2s metrics.")
excludePlayerMetrics := flag.Bool("exclude-player-metrics", envOrDefaultBool("A2S_EXPORTER_EXCLUDE_PLAYER_METRICS", false), "If true, exclude all `player_*` metrics. This option may be necessary for some servers.")
a2sOnlyMetrics := flag.Bool("a2s-only-metrics", envOrDefaultBool("A2S_EXPORTER_A2S_ONLY_METRICS", false), "If true, excludes Go runtime and promhttp metrics.")
maxPacketSize := flag.Int("max-packet-size", envOrDefaultInt("A2S_EXPORTER_MAX_PACKET_SIZE", 1400), "Advanced option to set a non-standard max packet size of the A2S query server.")
help := flag.Bool("h", false, "Show help.")
version := flag.Bool("version", false, "Show build version.")
flag.Parse()
// Show version.
if *version || flag.Arg(0) == "version" {
fmt.Println(buildVersion)
os.Exit(0)
}
// Show help.
if *help || flag.NArg() > 0 {
flag.Usage()
os.Exit(1)
}
// Check required arguments.
if *address == "" {
fmt.Println("address argument is required")
flag.Usage()
os.Exit(1)
}
// Set up prometheus metrics registry.
var registry *prometheus.Registry
if *a2sOnlyMetrics {
registry = prometheus.NewRegistry()
} else {
registry = prometheus.DefaultRegisterer.(*prometheus.Registry)
}
// Register A2S metrics.
clientOptions := []func(*a2s.Client) error{
a2s.SetMaxPacketSize(uint32(*maxPacketSize)),
}
registry.MustRegister(collector.New(*namespace, *address, *excludePlayerMetrics, clientOptions...))
// Set up http handler.
handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
if !*a2sOnlyMetrics {
handler = promhttp.InstrumentMetricHandler(registry, handler)
}
http.Handle(*path, handler)
// Run http server.
fmt.Printf("Serving metrics at http://127.0.0.1:%d%s\n", *port, *path)
fmt.Println(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
os.Exit(1)
}
func envOrDefault(key, def string) string {
if v, ok := os.LookupEnv(key); ok {
return v
}
return def
}
func envOrDefaultInt(key string, def int) int {
if v, ok := os.LookupEnv(key); ok {
v2, _ := strconv.Atoi(v)
return v2
}
return def
}
func envOrDefaultBool(key string, def bool) bool {
if v, ok := os.LookupEnv(key); ok {
return !strings.EqualFold(v, "false") && !strings.EqualFold(v, "0")
}
return def
}