-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
95 lines (82 loc) · 2.38 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
package main
import (
"context"
"flag"
"github.com/mattdurham/aix_agent/pkg/common"
"github.com/mattdurham/aix_agent/pkg/config"
"github.com/mattdurham/aix_agent/pkg/metrics"
"github.com/mattdurham/aix_agent/pkg/scrape"
write2 "github.com/mattdurham/aix_agent/pkg/write"
"net/http"
"os"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var aixUp = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "aix_agent_up",
Help: "Defines if aix is up and running",
}, []string{"up"})
func main() {
var configFile string
flag.StringVar(&configFile, "config.file", "", "the location of the config file")
flag.Parse()
if configFile == "" {
panic("config file not defined")
}
configBytes, err := os.ReadFile(configFile)
if err != nil {
panic(err)
}
cfg, err := config.ConvertConfig(configBytes)
if err != nil {
panic(err)
}
w := log.NewSyncWriter(os.Stdout)
logger := log.NewLogfmtLogger(w)
var option level.Option
switch cfg.LogLevel {
case "debug":
option = level.AllowDebug()
case "info":
option = level.AllowInfo()
case "error":
option = level.AllowError()
case "warn":
option = level.AllowWarn()
default:
option = level.AllowError()
}
logger = level.NewFilter(logger, option)
logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
r := mux.NewRouter()
agentMetricsReg := prometheus.NewRegistry()
agentMetricsReg.MustRegister(aixUp)
statsReg := prometheus.NewRegistry()
c := metrics.NewCollector(logger)
statsReg.MustRegister(c)
statsReg.MustRegister(aixUp)
r.Handle("/agent/metrics", promhttp.HandlerFor(agentMetricsReg, promhttp.HandlerOpts{}))
r.Handle("/metrics", promhttp.HandlerFor(statsReg, promhttp.HandlerOpts{}))
http.Handle("/", r)
// setup mem chan
sendTo := make(chan []*common.StoredMetric)
// setup scraper
scraper := scrape.NewScraper(logger, sendTo, "localhost:8000")
// setup remote write
write := write2.NewRemote(logger, cfg, sendTo)
ctx := context.Background()
go write.Run(ctx)
go scraper.Run("http://localhost:8000/metrics", 10*time.Second, ctx)
srv := &http.Server{
Handler: r,
Addr: "127.0.0.1:8000",
ReadTimeout: 30 * time.Second,
}
aixUp.WithLabelValues("true").Inc()
level.Info(logger).Log("msg", "serving")
srv.ListenAndServe()
}