-
Notifications
You must be signed in to change notification settings - Fork 26
/
probe_metrics_resource.go
104 lines (90 loc) · 3.15 KB
/
probe_metrics_resource.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
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 probeMetricsResourceHandler(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.ProbeMetricsResourceTimeoutDefault)
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.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
}
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("resource:%x", sha1.Sum([]byte(r.URL.String()))) // #nosec G401
prober.EnableMetricsCache(metricsCache, cacheKey, settings.CacheDuration(startTime))
}
if resourceList, err := paramsGetListRequired(r.URL.Query(), "target"); err == nil {
targetList := []metrics.MetricProbeTarget{}
for _, resourceId := range resourceList {
targetList = append(
targetList,
metrics.MetricProbeTarget{
ResourceId: resourceId,
Metrics: settings.Metrics,
Aggregations: settings.Aggregations,
},
)
}
prober.AddTarget(targetList...)
} else {
contextLogger.Errorln(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
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.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)
}