This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
handler.go
118 lines (93 loc) · 3.05 KB
/
handler.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
package metrics
import (
"net/http"
"github.com/go-chi/chi"
"github.com/servicemeshinterface/smi-metrics/pkg/mesh"
metrics "github.com/servicemeshinterface/smi-sdk-go/pkg/apis/metrics/v1alpha1"
log "github.com/sirupsen/logrus"
"github.com/unrolled/render"
)
// Handler provides the routes required to serve TrafficMetrics
type Handler struct {
Mesh mesh.Mesh
render *render.Render
}
// NewHandler returns a handler that has been initialized to defaults.
func NewHandler(meshInstance mesh.Mesh) (*Handler, error) {
return &Handler{
Mesh: meshInstance,
render: render.New(),
}, nil
}
// Resources returns all the supported resource types for this server
func (h *Handler) resources(w http.ResponseWriter, r *http.Request) {
lst, err := h.Mesh.GetSupportedResources(r.Context())
if err != nil {
h.jsonResponse(w, http.StatusBadRequest, err.Error())
return
}
h.jsonResponse(w, http.StatusOK, lst)
}
// List returns a list of TrafficMetrics for a specific resource type
func (h *Handler) list(w http.ResponseWriter, r *http.Request) {
kind := r.Context().Value(detailsKey).(*mesh.ResourceDetails).Kind
interval := r.Context().Value(intervalKey).(*metrics.Interval)
namespace := chi.URLParam(r, "namespace")
query := mesh.Query{
Name: "",
Namespace: namespace,
Kind: kind,
}
resourceMetrics, err := h.Mesh.GetResourceMetrics(r.Context(), query, interval)
if err != nil {
h.jsonResponse(w, http.StatusInternalServerError, err.Error())
return
}
h.jsonResponse(w, http.StatusOK, resourceMetrics)
}
// Get a set of metrics for a specific resource
func (h *Handler) get(w http.ResponseWriter, r *http.Request) {
interval := r.Context().Value(intervalKey).(*metrics.Interval)
kind := r.Context().Value(detailsKey).(*mesh.ResourceDetails).Kind
namespace := chi.URLParam(r, "namespace")
name := chi.URLParam(r, "name")
query := mesh.Query{
Name: name,
Namespace: namespace,
Kind: kind,
}
resourceMetrics, err := h.Mesh.GetResourceMetrics(r.Context(), query, interval)
if err != nil {
h.jsonResponse(w, http.StatusInternalServerError, err.Error())
return
}
if len(resourceMetrics.Items) != 1 {
for _, x := range resourceMetrics.Items {
log.Info(x.Resource)
}
log.Errorf("Wrong number of items: %d", len(resourceMetrics.Items))
h.jsonResponse(w, http.StatusInternalServerError, mesh.ErrorResponse{
Error: "unable to lookup metrics",
})
return
}
h.jsonResponse(w, http.StatusOK, resourceMetrics.Items[0])
}
func (h *Handler) edges(w http.ResponseWriter, r *http.Request) {
interval := r.Context().Value(intervalKey).(*metrics.Interval)
details := r.Context().Value(detailsKey).(*mesh.ResourceDetails)
kind := details.Kind
namespace := chi.URLParam(r, "namespace")
name := chi.URLParam(r, "name")
query := mesh.Query{
Name: name,
Namespace: namespace,
Kind: kind,
}
edgeMetrics, err := h.Mesh.GetEdgeMetrics(r.Context(), query, interval, details)
if err != nil {
h.jsonResponse(w, http.StatusInternalServerError, err.Error())
return
}
h.jsonResponse(w, http.StatusOK, edgeMetrics)
}