forked from cortexproject/cortex
/
http_admin.go
97 lines (85 loc) · 2.55 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
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
package distributor
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
"sort"
"strings"
"time"
"github.com/cortexproject/cortex/pkg/util"
)
const tpl = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cortex Ingester Stats</title>
</head>
<body>
<h1>Cortex Ingester Stats</h1>
<p>Current time: {{ .Now }}</p>
<p><b>NB stats do not account for replication factor, which is currently set to {{ .ReplicationFactor }}</b></p>
<form action="" method="POST">
<input type="hidden" name="csrf_token" value="$__CSRF_TOKEN_PLACEHOLDER__">
<table border="1">
<thead>
<tr>
<th>User</th>
<th># Series</th>
<th>Total Ingest Rate</th>
<th>API Ingest Rate</th>
<th>Rule Ingest Rate</th>
</tr>
</thead>
<tbody>
{{ range .Stats }}
<tr>
<td>{{ .UserID }}</td>
<td align='right'>{{ .UserStats.NumSeries }}</td>
<td align='right'>{{ printf "%.2f" .UserStats.IngestionRate }}</td>
<td align='right'>{{ printf "%.2f" .UserStats.APIIngestionRate }}</td>
<td align='right'>{{ printf "%.2f" .UserStats.RuleIngestionRate }}</td>
</tr>
{{ end }}
</tbody>
</table>
</form>
</body>
</html>`
var tmpl *template.Template
func init() {
tmpl = template.Must(template.New("webpage").Parse(tpl))
}
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, struct {
Now time.Time `json:"now"`
Stats []UserIDStats `json:"stats"`
ReplicationFactor int `json:"replicationFactor"`
}{
Now: time.Now(),
Stats: stats,
ReplicationFactor: d.ingestersRing.ReplicationFactor(),
}, tmpl, r)
}