-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
97 lines (81 loc) · 2.17 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
package main
import (
"context"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"runtime"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"github.com/sylr/prometheus-azure-exporter/pkg/config"
"github.com/sylr/prometheus-azure-exporter/pkg/metrics"
"github.com/sylr/prometheus-azure-exporter/pkg/tools"
)
var (
version = "dev"
goVersion = runtime.Version()
)
var (
azureExporterBuildInfo = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "azure_exporter",
Subsystem: "",
Name: "build_info",
Help: "Prometheus azure exporter build info",
},
[]string{"version", "goversion"},
)
)
func init() {
// Log as JSON instead of the default ASCII formatter.
log.SetFormatter(&log.TextFormatter{
DisableColors: true,
DisableSorting: false,
SortingFunc: tools.SortLogKeys,
})
// Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example
log.SetOutput(os.Stdout)
// Only log the info severity or above.
log.SetLevel(log.InfoLevel)
// Register build info
prometheus.MustRegister(azureExporterBuildInfo)
}
// main
func main() {
// looping for --version in args
for _, val := range os.Args {
if val == "--version" {
fmt.Printf("prometheus-azure-exporter version %s\n", version)
os.Exit(0)
} else if val == "--" {
break
}
}
// Configuration
err := setConfig()
if err != nil {
os.Exit(1)
}
go watchConfigFile()
// Log options
log.Debugf("Options: %+v", config.CurrentConfig)
log.Infof("Version: %s", version)
// Set build info
azureExporterBuildInfo.WithLabelValues(version, goVersion).Set(1)
// Configure metrics update interval
metrics.SetDefaultUpdateMetricsInterval(config.CurrentConfig.UpdateInterval)
// Update metrics process
ctx := context.Background()
go metrics.UpdateMetrics(ctx)
// Prometheus http endpoint
listeningAddress := fmt.Sprintf("%s:%d", config.CurrentConfig.ListeningAddress, config.CurrentConfig.ListeningPort)
http.Handle("/metrics", promhttp.Handler())
err = http.ListenAndServe(listeningAddress, nil)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
}