forked from webdevops/azure-metrics-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
probe_metrics_subscription.go
91 lines (77 loc) · 2.83 KB
/
probe_metrics_subscription.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
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 probeMetricsSubscriptionHandler(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.ProbeMetricsSubscriptionTimeoutDefault)
if err != nil {
contextLogger.Warn(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.NewRequestMetricSettingsForAzureResourceApi(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
}
if _, err = paramsGetListRequired(r.URL.Query(), "region"); 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("list:%x", sha1.Sum([]byte(r.URL.String()))) // #nosec G401
prober.EnableMetricsCache(metricsCache, cacheKey, settings.CacheDuration(startTime))
}
if !prober.FetchFromCache() {
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.RunOnSubscriptionScope()
} 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)
}