-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
206 lines (167 loc) · 5.02 KB
/
handlers.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"context"
"embed"
"html/template"
"net/http"
"path"
"sort"
"strconv"
"strings"
"sync"
"github.com/grafana/dskit/kv/memberlist"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
//go:embed index.gohtml
var indexPageHTML string
func newIndexPageContent() *IndexPageContent {
return &IndexPageContent{}
}
type indexPageContents struct {
LinkGroups []IndexPageLinkGroup
}
// IndexPageContent is a map of sections to path -> description.
type IndexPageContent struct {
mu sync.Mutex
elements []IndexPageLinkGroup
}
type IndexPageLinkGroup struct {
weight int
Desc string
Links []IndexPageLink
}
type IndexPageLink struct {
Desc string
Path string
}
// List of weights to order link groups in the same order as weights are ordered here.
const (
metricsWeight = iota
hostWeight
hostFactWeight
defaultWeight
ringWeight
memberlistWeight
)
func (pc *IndexPageContent) AddLinks(weight int, groupDesc string, links []IndexPageLink) {
pc.mu.Lock()
defer pc.mu.Unlock()
pc.elements = append(pc.elements, IndexPageLinkGroup{weight: weight, Desc: groupDesc, Links: links})
}
func (pc *IndexPageContent) GetContent() []IndexPageLinkGroup {
pc.mu.Lock()
els := append([]IndexPageLinkGroup(nil), pc.elements...)
pc.mu.Unlock()
sort.Slice(els, func(i, j int) bool {
if els[i].weight != els[j].weight {
return els[i].weight < els[j].weight
}
return els[i].Desc < els[j].Desc
})
return els
}
//go:embed static
var staticFiles embed.FS
func indexHandler(httpPathPrefix string, content *IndexPageContent) http.HandlerFunc {
templ := template.New("main")
templ.Funcs(map[string]interface{}{
"AddPathPrefix": func(link string) string {
return path.Join(httpPathPrefix, link)
},
})
template.Must(templ.Parse(indexPageHTML))
return func(w http.ResponseWriter, _ *http.Request) {
err := templ.Execute(w, indexPageContents{LinkGroups: content.GetContent()})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
//go:embed memberlist_status.gohtml
var memberlistStatusPageHTML string
func memberlistStatusHandler(httpPathPrefix string, kvs *memberlist.KVInitService) http.Handler {
templ := template.New("memberlist_status")
templ.Funcs(map[string]interface{}{
"AddPathPrefix": func(link string) string { return path.Join(httpPathPrefix, link) },
"StringsJoin": strings.Join,
})
template.Must(templ.Parse(memberlistStatusPageHTML))
return memberlist.NewHTTPStatusHandler(kvs, templ)
}
func hostFactHandler(w http.ResponseWriter, r *http.Request, collector HostFactCollector) {
ctx, cancel := context.WithCancel(r.Context())
defer cancel()
r = r.WithContext(ctx)
registry := prometheus.NewRegistry()
collector.Client.SetHostsFactsRegistry(registry)
// Get Prometheus timeout header
collector.PrometheusTimeout, _ = strconv.ParseFloat(r.Header.Get("X-Prometheus-Scrape-Timeout-Seconds"), 64)
expiredCacheParam := r.URL.Query().Get("expired-cache")
if expiredCacheParam != "" {
if !collector.CacheConfig.Enabled {
http.Error(w, "cache not enabled", http.StatusBadRequest)
return
}
var err error
collector.UseExpiredCache, err = strconv.ParseBool(expiredCacheParam)
if err != nil {
http.Error(w, "expired-cache should be a boolean", http.StatusBadRequest)
return
}
}
cacheParam := r.URL.Query().Get("cache")
if cacheParam != "" {
if !collector.CacheConfig.Enabled {
http.Error(w, "cache not enabled", http.StatusBadRequest)
return
}
var err error
collector.UseCache, err = strconv.ParseBool(cacheParam)
if err != nil {
http.Error(w, "cache should be a boolean", http.StatusBadRequest)
return
}
}
registry.MustRegister(collector)
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
func hostHandler(w http.ResponseWriter, r *http.Request, collector HostCollector) {
ctx, cancel := context.WithCancel(r.Context())
defer cancel()
r = r.WithContext(ctx)
registry := prometheus.NewRegistry()
collector.Client.SetHostsRegistry(registry)
// Get Prometheus timeout header
collector.PrometheusTimeout, _ = strconv.ParseFloat(r.Header.Get("X-Prometheus-Scrape-Timeout-Seconds"), 64)
expiredCacheParam := r.URL.Query().Get("expired-cache")
if expiredCacheParam != "" {
if !collector.CacheConfig.Enabled {
http.Error(w, "cache not enabled", http.StatusBadRequest)
return
}
var err error
collector.UseExpiredCache, err = strconv.ParseBool(expiredCacheParam)
if err != nil {
http.Error(w, "expired-cache should be a boolean", http.StatusBadRequest)
return
}
}
cacheParam := r.URL.Query().Get("cache")
if cacheParam != "" {
if !collector.CacheConfig.Enabled {
http.Error(w, "cache not enabled", http.StatusBadRequest)
return
}
var err error
collector.UseCache, err = strconv.ParseBool(cacheParam)
if err != nil {
http.Error(w, "cache should be a boolean", http.StatusBadRequest)
return
}
}
registry.MustRegister(collector)
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}