forked from kiali/kiali
/
apps.go
38 lines (31 loc) · 1.04 KB
/
apps.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
package handlers
import (
"net/http"
"github.com/gorilla/mux"
"github.com/kiali/kiali/log"
"github.com/kiali/kiali/prometheus"
)
// AppMetrics is the API handler to fetch metrics to be displayed, related to an app-label grouping
func AppMetrics(w http.ResponseWriter, r *http.Request) {
getAppMetrics(w, r, prometheus.NewClient)
}
// getAppMetrics (mock-friendly version)
func getAppMetrics(w http.ResponseWriter, r *http.Request, promClientSupplier func() (*prometheus.Client, error)) {
vars := mux.Vars(r)
namespace := vars["namespace"]
app := vars["app"]
params := prometheus.MetricsQuery{Namespace: namespace, Apps: []string{app}}
err := extractMetricsQueryParams(r, ¶ms)
if err != nil {
RespondWithError(w, http.StatusBadRequest, err.Error())
return
}
prometheusClient, err := promClientSupplier()
if err != nil {
log.Error(err)
RespondWithError(w, http.StatusServiceUnavailable, "Prometheus client error: "+err.Error())
return
}
metrics := prometheusClient.GetMetrics(¶ms)
RespondWithJSON(w, http.StatusOK, metrics)
}