-
Notifications
You must be signed in to change notification settings - Fork 460
/
http_admin.go
64 lines (52 loc) · 1.93 KB
/
http_admin.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
// SPDX-License-Identifier: AGPL-3.0-only
// Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/pkg/distributor/http_admin.go
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: The Cortex Authors.
package distributor
import (
_ "embed" // Used to embed html template
"encoding/json"
"fmt"
"html/template"
"net/http"
"sort"
"strings"
"time"
"github.com/grafana/mimir/pkg/util"
)
//go:embed ingester_stats.gohtml
var ingesterStatsPageHTML string
var ingesterStatsPageTemplate = template.Must(template.New("webpage").Parse(ingesterStatsPageHTML))
type ingesterStatsPageContents struct {
Now time.Time `json:"now"`
Stats []UserIDStats `json:"stats"`
ReplicationFactor int `json:"replicationFactor"`
}
type userStatsByTimeseries []UserIDStats
func (s userStatsByTimeseries) Len() int { return len(s) }
func (s userStatsByTimeseries) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s userStatsByTimeseries) Less(i, j int) bool {
return s[i].NumSeries > s[j].NumSeries ||
(s[i].NumSeries == s[j].NumSeries && s[i].UserID < s[j].UserID)
}
// AllUserStatsHandler shows stats for all users.
func (d *Distributor) AllUserStatsHandler(w http.ResponseWriter, r *http.Request) {
stats, err := d.AllUserStats(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
sort.Sort(userStatsByTimeseries(stats))
if encodings, found := r.Header["Accept"]; found &&
len(encodings) > 0 && strings.Contains(encodings[0], "json") {
if err := json.NewEncoder(w).Encode(stats); err != nil {
http.Error(w, fmt.Sprintf("Error marshalling response: %v", err), http.StatusInternalServerError)
}
return
}
util.RenderHTTPResponse(w, ingesterStatsPageContents{
Now: time.Now(),
Stats: stats,
ReplicationFactor: d.ingestersRing.ReplicationFactor(),
}, ingesterStatsPageTemplate, r)
}