forked from thanos-io/thanos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.go
More file actions
154 lines (132 loc) · 4.83 KB
/
query.go
File metadata and controls
154 lines (132 loc) · 4.83 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package ui
import (
"html/template"
"net/http"
"path"
"sort"
"strings"
"time"
"github.com/go-kit/kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/prometheus/common/route"
"github.com/thanos-io/thanos/pkg/component"
extpromhttp "github.com/thanos-io/thanos/pkg/extprom/http"
"github.com/thanos-io/thanos/pkg/query"
v1 "github.com/thanos-io/thanos/pkg/query/api"
)
type Query struct {
*BaseUI
storeSet *query.StoreSet
externalPrefix, prefixHeader string
cwd string
birth time.Time
version v1.ThanosVersion
reg prometheus.Registerer
now func() model.Time
}
type thanosVersion struct {
Version string `json:"version"`
Revision string `json:"revision"`
Branch string `json:"branch"`
BuildUser string `json:"buildUser"`
BuildDate string `json:"buildDate"`
GoVersion string `json:"goVersion"`
}
func NewQueryUI(logger log.Logger, reg prometheus.Registerer, storeSet *query.StoreSet, externalPrefix, prefixHeader string, runtimeInfo func(log.Logger) v1.RuntimeInfo, buildInfo v1.ThanosVersion) *Query {
return &Query{
BaseUI: NewBaseUI(logger, "query_menu.html", queryTmplFuncs(), externalPrefix, prefixHeader, component.Query),
storeSet: storeSet,
externalPrefix: externalPrefix,
prefixHeader: prefixHeader,
cwd: runtimeInfo(logger).CWD,
birth: runtimeInfo(logger).StartTime,
version: buildInfo,
reg: reg,
now: model.Now,
}
}
func queryTmplFuncs() template.FuncMap {
return template.FuncMap{
"since": func(t time.Time) time.Duration {
return time.Since(t) / time.Millisecond * time.Millisecond
},
"formatTimestamp": func(timestamp int64) string {
return time.Unix(timestamp/1000, 0).Format(time.RFC3339)
},
"title": strings.Title,
}
}
// Register registers new GET routes for subpages and retirects from / to /graph.
func (q *Query) Register(r *route.Router, ins extpromhttp.InstrumentationMiddleware) {
instrf := func(name string, next func(w http.ResponseWriter, r *http.Request)) http.HandlerFunc {
return ins.NewHandler(name, http.HandlerFunc(next))
}
r.Get("/", instrf("root", q.root))
r.Get("/graph", instrf("graph", q.graph))
r.Get("/stores", instrf("stores", q.stores))
r.Get("/status", instrf("status", q.status))
r.Get("/static/*filepath", instrf("static", q.serveStaticAsset))
// Make sure that "<path-prefix>/new" is redirected to "<path-prefix>/new/" and
// not just the naked "/new/", which would be the default behavior of the router
// with the "RedirectTrailingSlash" option (https://godoc.org/github.com/julienschmidt/httprouter#Router.RedirectTrailingSlash),
// and which breaks users with a --web.route-prefix that deviates from the path derived
// from the external URL.
r.Get("/new", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, path.Join(GetWebPrefix(q.logger, q.externalPrefix, q.prefixHeader, r), "new")+"/", http.StatusFound)
})
r.Get("/new/*filepath", instrf("react-static", q.serveReactUI))
// TODO(bplotka): Consider adding more Thanos related data e.g:
// - What store nodes we see currently.
// - What sidecars we see currently.
}
// Root redirects "/" requests to "/graph", taking into account the path prefix value.
func (q *Query) root(w http.ResponseWriter, r *http.Request) {
prefix := GetWebPrefix(q.logger, q.externalPrefix, q.prefixHeader, r)
http.Redirect(w, r, path.Join("/", prefix, "/graph"), http.StatusFound)
}
func (q *Query) graph(w http.ResponseWriter, r *http.Request) {
prefix := GetWebPrefix(q.logger, q.externalPrefix, q.prefixHeader, r)
q.executeTemplate(w, "graph.html", prefix, nil)
}
func (q *Query) status(w http.ResponseWriter, r *http.Request) {
prefix := GetWebPrefix(q.logger, q.externalPrefix, q.prefixHeader, r)
q.executeTemplate(w, "status.html", prefix, struct {
Birth time.Time
CWD string
Version v1.ThanosVersion
}{
Birth: q.birth,
CWD: q.cwd,
Version: q.version,
})
}
func (q *Query) stores(w http.ResponseWriter, r *http.Request) {
prefix := GetWebPrefix(q.logger, q.externalPrefix, q.prefixHeader, r)
statuses := make(map[component.StoreAPI][]query.StoreStatus)
for _, status := range q.storeSet.GetStoreStatus() {
statuses[status.StoreType] = append(statuses[status.StoreType], status)
}
sources := make([]component.StoreAPI, 0, len(statuses))
for k := range statuses {
sources = append(sources, k)
}
sort.Slice(sources, func(i int, j int) bool {
if sources[i] == nil {
return false
}
if sources[j] == nil {
return true
}
return sources[i].String() < sources[j].String()
})
q.executeTemplate(w, "stores.html", prefix, struct {
Stores map[component.StoreAPI][]query.StoreStatus
Sources []component.StoreAPI
}{
Stores: statuses,
Sources: sources,
})
}