Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom status page templates #144

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 10 additions & 148 deletions kv/memberlist/kv_init_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package memberlist

import (
"context"
_ "embed"
"encoding/json"
"fmt"
"html/template"
Expand Down Expand Up @@ -157,7 +158,7 @@ func (kvs *KVInitService) ServeHTTP(w http.ResponseWriter, req *http.Request) {

sent, received := kv.getSentAndReceivedMessages()

v := pageData{
v := statusPageData{
Now: time.Now(),
Memberlist: kv.memberlist,
SortedMembers: members,
Expand All @@ -183,7 +184,7 @@ func (kvs *KVInitService) ServeHTTP(w http.ResponseWriter, req *http.Request) {
return
}

err := pageTemplate.Execute(w, v)
err := defaultPageTemplate.Execute(w, v)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
Expand Down Expand Up @@ -276,7 +277,9 @@ func downloadKey(w http.ResponseWriter, kv *KV, store map[string]valueDesc, key
_, _ = w.Write(encoded)
}

type pageData struct {
// statusPageData is the root element of the status page served through HTTP.
// Depending on the accepted content-type, it will be either marshaled as JSON or provided to the rendered HTML template.
type statusPageData struct {
Now time.Time
Memberlist *memberlist.Memberlist
SortedMembers []*memberlist.Node
Expand All @@ -285,149 +288,8 @@ type pageData struct {
ReceivedMessages []message
}

var pageTemplate = template.Must(template.New("webpage").Funcs(template.FuncMap{
//go:embed status.gohtml
var defaultPageContent string
var defaultPageTemplate = template.Must(template.New("webpage").Funcs(template.FuncMap{
"StringsJoin": strings.Join,
}).Parse(pageContent))

const pageContent = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Memberlist Status</title>
</head>
<body>
<h1>Memberlist Status</h1>
<p>Current time: {{ .Now }}</p>

<ul>
<li>Health Score: {{ .Memberlist.GetHealthScore }} (lower = better, 0 = healthy)</li>
<li>Members: {{ .Memberlist.NumMembers }}</li>
</ul>

<h2>KV Store</h2>

<table width="100%" border="1">
<thead>
<tr>
<th>Key</th>
<th>Value Details</th>
<th>Actions</th>
</tr>
</thead>

<tbody>
{{ range $k, $v := .Store }}
<tr>
<td>{{ $k }}</td>
<td>{{ $v }}</td>
<td>
<a href="?viewKey={{ $k }}&format=json">json</a>
| <a href="?viewKey={{ $k }}&format=json-pretty">json-pretty</a>
| <a href="?viewKey={{ $k }}&format=struct">struct</a>
| <a href="?downloadKey={{ $k }}">download</a>
</td>
</tr>
{{ end }}
</tbody>
</table>

<p>Note that value "version" is node-specific. It starts with 0 (on restart), and increases on each received update. Size is in bytes.</p>

<h2>Memberlist Cluster Members</h2>

<table width="100%" border="1">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>State</th>
</tr>
</thead>

<tbody>
{{ range .SortedMembers }}
<tr>
<td>{{ .Name }}</td>
<td>{{ .Address }}</td>
<td>{{ .State }}</td>
</tr>
{{ end }}
</tbody>
</table>

<p>State: 0 = Alive, 1 = Suspect, 2 = Dead, 3 = Left</p>

<h2>Received Messages</h2>

<a href="?deleteMessages=true">Delete All Messages (received and sent)</a>

<table width="100%" border="1">
<thead>
<tr>
<th>ID</th>
<th>Time</th>
<th>Key</th>
<th>Value in the Message</th>
<th>Version After Update (0 = no change)</th>
<th>Changes</th>
<th>Actions</th>
</tr>
</thead>

<tbody>
{{ range .ReceivedMessages }}
<tr>
<td>{{ .ID }}</td>
<td>{{ .Time.Format "15:04:05.000" }}</td>
<td>{{ .Pair.Key }}</td>
<td>size: {{ .Pair.Value | len }}, codec: {{ .Pair.Codec }}</td>
<td>{{ .Version }}</td>
<td>{{ StringsJoin .Changes ", " }}</td>
<td>
<a href="?viewMsg={{ .ID }}&format=json">json</a>
| <a href="?viewMsg={{ .ID }}&format=json-pretty">json-pretty</a>
| <a href="?viewMsg={{ .ID }}&format=struct">struct</a>
</td>
</tr>
{{ end }}
</tbody>
</table>

<h2>Sent Messages</h2>

<a href="?deleteMessages=true">Delete All Messages (received and sent)</a>

<table width="100%" border="1">
<thead>
<tr>
<th>ID</th>
<th>Time</th>
<th>Key</th>
<th>Value</th>
<th>Version</th>
<th>Changes</th>
<th>Actions</th>
</tr>
</thead>

<tbody>
{{ range .SentMessages }}
<tr>
<td>{{ .ID }}</td>
<td>{{ .Time.Format "15:04:05.000" }}</td>
<td>{{ .Pair.Key }}</td>
<td>size: {{ .Pair.Value | len }}, codec: {{ .Pair.Codec }}</td>
<td>{{ .Version }}</td>
<td>{{ StringsJoin .Changes ", " }}</td>
<td>
<a href="?viewMsg={{ .ID }}&format=json">json</a>
| <a href="?viewMsg={{ .ID }}&format=json-pretty">json-pretty</a>
| <a href="?viewMsg={{ .ID }}&format=struct">struct</a>
</td>
</tr>
{{ end }}
</tbody>
</table>
</body>
</html>`
}).Parse(defaultPageContent))
2 changes: 1 addition & 1 deletion kv/memberlist/kv_init_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestPage(t *testing.T) {
_ = ml.Shutdown()
})

require.NoError(t, pageTemplate.Execute(&bytes.Buffer{}, pageData{
require.NoError(t, defaultPageTemplate.Execute(&bytes.Buffer{}, statusPageData{
Now: time.Now(),
Memberlist: ml,
SortedMembers: ml.Members(),
Expand Down
5 changes: 5 additions & 0 deletions kv/memberlist/memberlist_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"flag"
"fmt"
"html/template"
"math"
"strings"
"sync"
Expand Down Expand Up @@ -164,6 +165,10 @@ type KVConfig struct {

// Codecs to register. Codecs need to be registered before joining other members.
Codecs []codec.Codec `yaml:"-"`

// CustomHTTPHandlerTemplate will be rendered by HTTPHandler instead of the embedded default one, if provided.
// This option is set internally and never exposed to the user.
CustomHTTPHandlerTemplate *template.Template `yaml:"-"`
}

// RegisterFlagsWithPrefix registers flags.
Expand Down
143 changes: 143 additions & 0 deletions kv/memberlist/status.gohtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
{{- /*gotype: github.com/grafana/dskit/kv/memberlist.statusPageData */ -}}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Memberlist Status</title>
</head>
<body>
<h1>Memberlist Status</h1>
<p>Current time: {{ .Now }}</p>

<ul>
<li>Health Score: {{ .Memberlist.GetHealthScore }} (lower = better, 0 = healthy)</li>
<li>Members: {{ .Memberlist.NumMembers }}</li>
</ul>

<h2>KV Store</h2>

<table width="100%" border="1">
<thead>
<tr>
<th>Key</th>
<th>Value Details</th>
<th>Actions</th>
</tr>
</thead>

<tbody>
{{ range $k, $v := .Store }}
<tr>
<td>{{ $k }}</td>
<td>{{ $v }}</td>
<td>
<a href="?viewKey={{ $k }}&format=json">json</a>
| <a href="?viewKey={{ $k }}&format=json-pretty">json-pretty</a>
| <a href="?viewKey={{ $k }}&format=struct">struct</a>
| <a href="?downloadKey={{ $k }}">download</a>
</td>
</tr>
{{ end }}
</tbody>
</table>

<p>Note that value "version" is node-specific. It starts with 0 (on restart), and increases on each received update.
Size is in bytes.</p>

<h2>Memberlist Cluster Members</h2>

<table width="100%" border="1">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>State</th>
</tr>
</thead>

<tbody>
{{ range .SortedMembers }}
<tr>
<td>{{ .Name }}</td>
<td>{{ .Address }}</td>
<td>{{ .State }}</td>
</tr>
{{ end }}
</tbody>
</table>

<p>State: 0 = Alive, 1 = Suspect, 2 = Dead, 3 = Left</p>

<h2>Received Messages</h2>

<a href="?deleteMessages=true">Delete All Messages (received and sent)</a>

<table width="100%" border="1">
<thead>
<tr>
<th>ID</th>
<th>Time</th>
<th>Key</th>
<th>Value in the Message</th>
<th>Version After Update (0 = no change)</th>
<th>Changes</th>
<th>Actions</th>
</tr>
</thead>

<tbody>
{{ range .ReceivedMessages }}
<tr>
<td>{{ .ID }}</td>
<td>{{ .Time.Format "15:04:05.000" }}</td>
<td>{{ .Pair.Key }}</td>
<td>size: {{ .Pair.Value | len }}, codec: {{ .Pair.Codec }}</td>
<td>{{ .Version }}</td>
<td>{{ StringsJoin .Changes ", " }}</td>
<td>
<a href="?viewMsg={{ .ID }}&format=json">json</a>
| <a href="?viewMsg={{ .ID }}&format=json-pretty">json-pretty</a>
| <a href="?viewMsg={{ .ID }}&format=struct">struct</a>
</td>
</tr>
{{ end }}
</tbody>
</table>

<h2>Sent Messages</h2>

<a href="?deleteMessages=true">Delete All Messages (received and sent)</a>

<table width="100%" border="1">
<thead>
<tr>
<th>ID</th>
<th>Time</th>
<th>Key</th>
<th>Value</th>
<th>Version</th>
<th>Changes</th>
<th>Actions</th>
</tr>
</thead>

<tbody>
{{ range .SentMessages }}
<tr>
<td>{{ .ID }}</td>
<td>{{ .Time.Format "15:04:05.000" }}</td>
<td>{{ .Pair.Key }}</td>
<td>size: {{ .Pair.Value | len }}, codec: {{ .Pair.Codec }}</td>
<td>{{ .Version }}</td>
<td>{{ StringsJoin .Changes ", " }}</td>
<td>
<a href="?viewMsg={{ .ID }}&format=json">json</a>
| <a href="?viewMsg={{ .ID }}&format=json-pretty">json-pretty</a>
| <a href="?viewMsg={{ .ID }}&format=struct">struct</a>
</td>
</tr>
{{ end }}
</tbody>
</table>
</body>
</html>
6 changes: 5 additions & 1 deletion ring/basic_lifecycler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ring
import (
"context"
"fmt"
"html/template"
"net/http"
"sort"
"sync"
Expand Down Expand Up @@ -55,6 +56,9 @@ type BasicLifecyclerConfig struct {
// If true lifecycler doesn't unregister instance from the ring when it's stopping. Default value is false,
// which means unregistering.
KeepInstanceInTheRingOnShutdown bool

// CustomHTTPHandlerTemplate will be rendered by HTTPHandler instead of the embedded default one, if provided.
CustomHTTPHandlerTemplate *template.Template
}

// BasicLifecycler is a basic ring lifecycler which allows to hook custom
Expand Down Expand Up @@ -507,5 +511,5 @@ func (l *BasicLifecycler) getRing(ctx context.Context) (*Desc, error) {
}

func (l *BasicLifecycler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
newRingPageHandler(l, l.cfg.HeartbeatPeriod).handle(w, req)
newRingPageHandler(l, l.cfg.HeartbeatPeriod, l.cfg.CustomHTTPHandlerTemplate).handle(w, req)
}
Loading