forked from webdevops/azure-metrics-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
probe_metrics_resourcegraph.go
103 lines (87 loc) · 3.19 KB
/
probe_metrics_resourcegraph.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
package main
import (
"context"
"crypto/sha1" // #nosec G505
"fmt"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/webdevops/azure-metrics-exporter/config"
"github.com/webdevops/azure-metrics-exporter/metrics"
)
func probeMetricsResourceGraphHandler(w http.ResponseWriter, r *http.Request) {
var err error
var timeoutSeconds float64
startTime := time.Now()
contextLogger := buildContextLoggerFromRequest(r)
registry := prometheus.NewRegistry()
// If a timeout is configured via the Prometheus header, add it to the request.
timeoutSeconds, err = getPrometheusTimeout(r, config.ProbeMetricsResourceGraphTimeoutDefault)
if err != nil {
contextLogger.Warnln(err)
http.Error(w, fmt.Sprintf("failed to parse timeout from Prometheus header: %s", err), http.StatusBadRequest)
return
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeoutSeconds*float64(time.Second)))
defer cancel()
r = r.WithContext(ctx)
var settings metrics.RequestMetricSettings
if settings, err = metrics.NewRequestMetricSettings(r, opts); err != nil {
contextLogger.Warnln(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if _, err = paramsGetListRequired(r.URL.Query(), "subscription"); err != nil {
contextLogger.Warnln(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
resourceType, err := paramsGetRequired(r.URL.Query(), "resourceType")
if err != nil {
contextLogger.Warnln(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
prober := metrics.NewMetricProber(ctx, contextLogger, w, &settings, opts)
prober.SetUserAgent(UserAgent + gitTag)
prober.SetAzureClient(AzureClient)
prober.SetAzureResourceTagManager(AzureResourceTagManager)
prober.SetPrometheusRegistry(registry)
if settings.Cache != nil {
cacheKey := fmt.Sprintf("scrape:%x", sha1.Sum([]byte(r.URL.String()))) // #nosec G401
prober.EnableMetricsCache(metricsCache, cacheKey, settings.CacheDuration(startTime))
}
if opts.Azure.ServiceDiscovery.CacheDuration.Seconds() > 0 {
prober.EnableServiceDiscoveryCache(azureCache, opts.Azure.ServiceDiscovery.CacheDuration)
}
if !prober.FetchFromCache() {
err := prober.ServiceDiscovery.FindResourceGraph(ctx, settings.Subscriptions, resourceType, settings.Filter)
if err != nil {
contextLogger.Errorln(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
prober.RegisterSubscriptionCollectFinishCallback(func(subscriptionId string) {
// global stats counter
prometheusCollectTime.With(prometheus.Labels{
"subscriptionID": subscriptionId,
"handler": config.ProbeMetricsListUrl,
"filter": settings.Filter,
}).Observe(time.Since(startTime).Seconds())
})
prober.Run()
} else {
w.Header().Add("X-metrics-cached", "true")
for _, subscriptionId := range settings.Subscriptions {
prometheusMetricRequests.With(prometheus.Labels{
"subscriptionID": subscriptionId,
"handler": config.ProbeMetricsListUrl,
"filter": settings.Filter,
"result": "cached",
}).Inc()
}
}
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}