-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
113 lines (98 loc) · 2.79 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
107
108
109
110
111
112
113
// Copyright 2020-2023 gtp_exporter authors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
//go:build linux
// +build linux
// Command gtp_exporter implements a Prometheus exporter for Linux kernel GTP.
package main
import (
"fmt"
"net/http"
"os"
"time"
"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/version"
)
// Variables for metadata.
var (
// Version to be recognized in GoReleaser.
Version = "unset"
// Revision(short commit hash) to be recognized in GoReleaser.
Revision = "unset"
)
type gtplogger struct {
logger log.Logger
}
func (l gtplogger) Println(v ...interface{}) {
_ = level.Error(l.logger).Log("msg", fmt.Sprint(v...))
}
func init() {
prometheus.MustRegister(version.NewCollector("gtp_exporter"))
}
func main() {
var (
listenAddress = kingpin.Flag(
"web.listen-address",
"Address on which to expose metrics and web interface.",
).Default(":9721").String()
metricsPath = kingpin.Flag(
"web.telemetry-path",
"Path under which to expose metrics.",
).Default("/metrics").String()
)
promlogConfig := &promlog.Config{}
flag.AddFlags(kingpin.CommandLine, promlogConfig)
kingpin.Version(version.Print("gtp_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger := promlog.New(promlogConfig)
_ = level.Info(logger).Log("msg", "Starting gtp_exporter", "version", version.Info())
_ = level.Info(logger).Log("msg", "Build context", "build_context", version.BuildContext())
prometheus.MustRegister(NewExporter(logger))
http.Handle(
*metricsPath,
promhttp.InstrumentMetricHandler(
prometheus.DefaultRegisterer,
promhttp.HandlerFor(
prometheus.DefaultGatherer,
promhttp.HandlerOpts{ErrorLog: >plogger{logger: logger}},
),
),
)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write(
[]byte(
`<html>
<head><title>GTP Exporter</title></head>
<body>
<h1>GTP Exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>`,
),
)
if err != nil {
_ = level.Error(logger).Log(
"msg:", "failed to handle http writer",
"err:", err,
)
}
})
_ = level.Info(logger).Log("msg", "Listening on address", "address", *listenAddress)
s := &http.Server{
Addr: *listenAddress,
Handler: nil,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
if err := s.ListenAndServe(); err != nil {
_ = level.Error(logger).Log("msg", "Error starting HTTP server", "err", err)
os.Exit(1)
}
}