-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
163 lines (130 loc) · 3.62 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"embed"
"encoding/base64"
"errors"
"fmt"
"html/template"
"net/http"
"os"
"runtime"
"time"
"github.com/google/uuid"
"github.com/jessevdk/go-flags"
cache "github.com/patrickmn/go-cache"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/webdevops/go-common/azuresdk/armclient"
"github.com/webdevops/go-common/azuresdk/prometheus/tracing"
"github.com/webdevops/go-common/prometheus/kusto"
"github.com/webdevops/azure-resourcegraph-exporter/config"
)
const (
Author = "webdevops.io"
UserAgent = "az-rg-exporter/"
)
var (
argparser *flags.Parser
Opts config.Opts
Config kusto.Config
AzureClient *armclient.ArmClient
metricCache *cache.Cache
//go:embed templates/*.html
templates embed.FS
// Git version information
gitCommit = "<unknown>"
gitTag = "<unknown>"
)
func main() {
initArgparser()
initLogger()
logger.Infof("starting azure-resourcegraph-exporter v%s (%s; %s; by %v)", gitTag, gitCommit, runtime.Version(), Author)
logger.Info(string(Opts.GetJson()))
initSystem()
initGlobalMetrics()
metricCache = cache.New(120*time.Second, 60*time.Second)
logger.Infof("loading config")
readConfig()
logger.Infof("init Azure")
initAzureConnection()
logger.Infof("starting http server on %s", Opts.Server.Bind)
startHttpServer()
}
// init argparser and parse/validate arguments
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 readConfig() {
Config = kusto.NewConfig(Opts.Config.Path)
if err := Config.Validate(); err != nil {
logger.Fatal(err)
}
}
func initAzureConnection() {
var err error
AzureClient, err = armclient.NewArmClientWithCloudName(*Opts.Azure.Environment, logger)
if err != nil {
logger.Fatal(err.Error())
}
AzureClient.SetUserAgent(UserAgent + gitTag)
}
// 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)
}
})
// report
tmpl := template.Must(template.ParseFS(templates, "templates/*.html"))
mux.HandleFunc("/query", func(w http.ResponseWriter, r *http.Request) {
cspNonce := base64.StdEncoding.EncodeToString([]byte(uuid.New().String()))
w.Header().Add("Content-Type", "text/html")
w.Header().Add("Referrer-Policy", "same-origin")
w.Header().Add("X-Frame-Options", "DENY")
w.Header().Add("X-XSS-Protection", "1; mode=block")
w.Header().Add("X-Content-Type-Options", "nosniff")
w.Header().Add("Content-Security-Policy",
fmt.Sprintf(
"default-src 'self'; script-src 'nonce-%[1]s'; style-src 'nonce-%[1]s'; img-src 'self' data:",
cspNonce,
),
)
templatePayload := struct {
Nonce string
}{
Nonce: cspNonce,
}
if err := tmpl.ExecuteTemplate(w, "query.html", templatePayload); err != nil {
logger.Error(err)
}
})
mux.Handle("/metrics", tracing.RegisterAzureMetricAutoClean(promhttp.Handler()))
mux.HandleFunc("/probe", handleProbeRequest)
srv := &http.Server{
Addr: Opts.Server.Bind,
Handler: mux,
ReadTimeout: Opts.Server.ReadTimeout,
WriteTimeout: Opts.Server.WriteTimeout,
}
logger.Fatal(srv.ListenAndServe())
}