-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
146 lines (119 loc) · 3.53 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"errors"
"fmt"
"net/http"
"os"
"runtime"
flags "github.com/jessevdk/go-flags"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/webdevops/go-common/prometheus/collector"
"go.uber.org/zap"
"github.com/webdevops/go-common/azuresdk/armclient"
"github.com/webdevops/go-common/azuresdk/prometheus/tracing"
"github.com/webdevops/azure-keyvault-exporter/config"
)
const (
Author = "webdevops.io"
UserAgent = "azure-keyvault-exporter/"
)
var (
argparser *flags.Parser
Opts config.Opts
AzureClient *armclient.ArmClient
AzureSubscriptionsIterator *armclient.SubscriptionsIterator
AzureResourceTagManager *armclient.ResourceTagManager
// Git version information
gitCommit = "<unknown>"
gitTag = "<unknown>"
// cache config
cacheTag = "v1"
)
func main() {
initArgparser()
initLogger()
logger.Infof("starting azure-keyvault-exporter v%s (%s; %s; by %v)", gitTag, gitCommit, runtime.Version(), Author)
logger.Info(string(Opts.GetJson()))
initSystem()
logger.Infof("init Azure connection")
initAzureConnection()
logger.Infof("starting metrics collection")
initMetricCollector()
logger.Infof("Starting http server on %s", Opts.Server.Bind)
startHttpServer()
}
func initArgparser() {
argparser = flags.NewParser(&Opts, flags.Default)
_, err := argparser.Parse()
// check if there is an parse error
if err != nil {
var flagsErr *flags.Error
if ok := errors.As(err, &flagsErr); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
fmt.Println()
argparser.WriteHelp(os.Stdout)
os.Exit(1)
}
}
}
func initAzureConnection() {
var err error
AzureClient, err = armclient.NewArmClientWithCloudName(*Opts.Azure.Environment, logger)
if err != nil {
logger.Fatal(err.Error())
}
AzureClient.SetUserAgent(UserAgent + gitTag)
// limit subscriptions (if filter is set)
if len(Opts.Azure.Subscription) >= 1 {
AzureClient.SetSubscriptionID(Opts.Azure.Subscription...)
}
// init subscription iterator
AzureSubscriptionsIterator = armclient.NewSubscriptionIterator(AzureClient)
// init resource tag manager
AzureResourceTagManager, err = AzureClient.TagManager.ParseTagConfig(Opts.Azure.ResourceTags)
if err != nil {
logger.Fatalf(`unable to parse resourceTag configuration "%s": %v"`, Opts.Azure.ResourceTags, err.Error())
}
}
func initMetricCollector() {
collectorName := "keyvault"
if Opts.Scrape.Time.Seconds() > 0 {
c := collector.New(collectorName, &MetricsCollectorKeyvault{}, logger)
c.SetScapeTime(Opts.Scrape.Time)
c.SetConcurrency(Opts.Scrape.Concurrency)
c.SetCache(
Opts.GetCachePath(collectorName+".json"),
collector.BuildCacheTag(cacheTag, Opts.Azure, Opts.KeyVault),
)
if err := c.Start(); err != nil {
logger.Fatal(err.Error())
}
} else {
logger.With(zap.String("collector", collectorName)).Info("collector disabled")
}
}
// start and handle prometheus handler
func startHttpServer() {
mux := http.NewServeMux()
// healthz
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
if _, err := fmt.Fprint(w, "Ok"); err != nil {
logger.Error(err)
}
})
// readyz
mux.HandleFunc("/readyz", func(w http.ResponseWriter, r *http.Request) {
if _, err := fmt.Fprint(w, "Ok"); err != nil {
logger.Error(err)
}
})
mux.Handle("/metrics", tracing.RegisterAzureMetricAutoClean(promhttp.Handler()))
srv := &http.Server{
Addr: Opts.Server.Bind,
Handler: mux,
ReadTimeout: Opts.Server.ReadTimeout,
WriteTimeout: Opts.Server.WriteTimeout,
}
logger.Fatal(srv.ListenAndServe())
}