generated from jkroepke/repository-template-go
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
158 lines (129 loc) · 4.46 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
package main
import (
"context"
"fmt"
stdlog "log"
"net/http"
"os"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription"
"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/jkroepke/azure-monitor-exporter/pkg/cache"
"github.com/jkroepke/azure-monitor-exporter/pkg/probe"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
versionCollector "github.com/prometheus/client_golang/prometheus/collectors/version"
_ "github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
)
func main() {
reg := prometheus.NewRegistry()
webConfig := webflag.AddFlags(kingpin.CommandLine, ":8080")
kingpin.Version(version.Print("azure-monitor-exporter"))
promlogConfig := &promlog.Config{}
flag.AddFlags(kingpin.CommandLine, promlogConfig)
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger := promlog.New(promlogConfig)
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
_ = level.Error(logger).Log("msg", "Error obtain azure credentials", "err", err)
os.Exit(1)
}
subscriptions, err := discoverSubscriptions(cred)
if err != nil {
_ = level.Error(logger).Log("msg", "Error obtain azure credentials", "err", err)
os.Exit(1)
}
_ = level.Info(logger).Log("msg", "discovered subscriptions", "subscriptions", strings.Join(subscriptions, ","))
// Add go runtime metrics and process collectors.
reg.MustRegister(
collectors.NewGoCollector(),
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
versionCollector.NewCollector("azure_monitor_exporter"),
)
queryCache := cache.NewCache[probe.Resources]()
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{
Registry: reg,
ErrorLog: stdlog.New(log.NewStdlibAdapter(logger), "ERROR: ", stdlog.LstdFlags),
}))
http.HandleFunc("/probe", func(w http.ResponseWriter, r *http.Request) {
probeCollector, err := probe.New(r, logger, cred, subscriptions, queryCache)
if err != nil {
_ = level.Error(logger).Log("msg", "Error creating probe", "err", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
registry := prometheus.NewRegistry()
registry.MustRegister(probeCollector)
promhttp.HandlerFor(registry, promhttp.HandlerOpts{
Registry: reg,
ErrorLog: stdlog.New(log.NewStdlibAdapter(logger), "ERROR: ", stdlog.LstdFlags),
}).ServeHTTP(w, r)
})
landingPage, err := web.NewLandingPage(web.LandingConfig{
Name: "azure-monitor-exporter",
Description: "Prometheus Exporter for Azure Monitor",
Version: version.Info(),
Form: web.LandingForm{
Action: "/probe",
Inputs: []web.LandingFormInput{
{
Label: "Resource Graph Query",
Type: "text",
Name: "query",
Placeholder: "resources | where type == 'microsoft.compute/virtualmachines'",
},
},
},
Links: []web.LandingLinks{
{
Address: "/metrics",
Text: "Metrics",
},
},
})
if err != nil {
_ = level.Error(logger).Log("err", err)
os.Exit(1)
}
http.Handle("/", landingPage)
srv := &http.Server{
ReadHeaderTimeout: time.Second * 3,
ErrorLog: stdlog.New(log.NewStdlibAdapter(logger), "ERROR: ", stdlog.LstdFlags),
}
if err := web.ListenAndServe(srv, webConfig, logger); err != nil {
_ = level.Error(logger).Log("msg", "Error starting HTTP server", "err", err)
os.Exit(1)
}
}
func discoverSubscriptions(cred azcore.TokenCredential) ([]string, error) {
subscriptionClient, err := armsubscription.NewSubscriptionsClient(cred, nil)
if err != nil {
return nil, fmt.Errorf("failed to create subscription client: %w", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
subscriptions := make([]string, 0)
pager := subscriptionClient.NewListPager(nil)
for pager.More() {
page, err := pager.NextPage(ctx)
if err != nil {
return nil, fmt.Errorf("failed to advance page: %w", err)
}
for _, v := range page.Value {
subscriptions = append(subscriptions, *v.SubscriptionID)
}
}
return subscriptions, nil
}