/
metadata_handler.go
51 lines (43 loc) · 1.4 KB
/
metadata_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
package querier
import (
"net/http"
"github.com/cortexproject/cortex/pkg/util"
)
type metricMetadata struct {
Type string `json:"type"`
Help string `json:"help"`
Unit string `json:"unit"`
}
const (
statusSuccess = "success"
statusError = "error"
)
type metadataResult struct {
Status string `json:"status"`
Data map[string][]metricMetadata `json:"data,omitempty"`
Error string `json:"error,omitempty"`
}
// MetadataHandler returns metric metadata held by Cortex for a given tenant.
// It is kept and returned as a set.
func MetadataHandler(d Distributor) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
resp, err := d.MetricsMetadata(r.Context())
if err != nil {
w.WriteHeader(http.StatusBadRequest)
util.WriteJSONResponse(w, metadataResult{Status: statusError, Error: err.Error()})
return
}
// Put all the elements of the pseudo-set into a map of slices for marshalling.
metrics := map[string][]metricMetadata{}
for _, m := range resp {
ms, ok := metrics[m.Metric]
if !ok {
// Most metrics will only hold 1 copy of the same metadata.
ms = make([]metricMetadata, 0, 1)
metrics[m.Metric] = ms
}
metrics[m.Metric] = append(ms, metricMetadata{Type: string(m.Type), Help: m.Help, Unit: m.Unit})
}
util.WriteJSONResponse(w, metadataResult{Status: statusSuccess, Data: metrics})
})
}