Skip to content

Commit

Permalink
cleanup bucket and root redirection
Browse files Browse the repository at this point in the history
Signed-off-by: Augustin Husson <husson.augustin@gmail.com>

Co-authored-by: Prem Saraswat <prmsrswt@gmail.com>
  • Loading branch information
Nexucis and onprem committed May 5, 2021
1 parent 026973a commit 56124ac
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 68 deletions.
13 changes: 1 addition & 12 deletions cmd/thanos/compact.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,6 @@ func runCompact(
)
}
var (
compactorView = ui.NewBucketUI(
logger,
conf.label,
conf.webConf.externalPrefix,
conf.webConf.prefixHeaderName,
"/loaded",
component,
)
api = blocksAPI.NewBlocksAPI(logger, conf.webConf.disableCORS, conf.label, flagsMap)
sy *compact.Syncer
)
Expand All @@ -267,7 +259,6 @@ func runCompact(
}, []block.MetadataModifier{block.NewReplicaLabelRemover(logger, conf.dedupReplicaLabels)},
)
cf.UpdateOnChange(func(blocks []metadata.Meta, err error) {
compactorView.Set(blocks, err)
api.SetLoaded(blocks, err)
})
sy, err = compact.NewMetaSyncer(
Expand Down Expand Up @@ -481,10 +472,9 @@ func runCompact(
r := route.New()

ins := extpromhttp.NewInstrumentationMiddleware(reg, nil)
compactorView.Register(r, true, ins)

global := ui.NewBucketUI(logger, conf.label, conf.webConf.externalPrefix, conf.webConf.prefixHeaderName, "/global", component)
global.Register(r, false, ins)
global.Register(r, ins)

// Configure Request Logging for HTTP calls.
opts := []logging.Option{logging.WithDecider(func(_ string, _ error) logging.Decision {
Expand All @@ -497,7 +487,6 @@ func runCompact(
// TODO(bwplotka): Allow Bucket UI to visualize the state of the block as well.
f := baseMetaFetcher.NewMetaFetcher(extprom.WrapRegistererWithPrefix("thanos_bucket_ui", reg), nil, nil, "component", "globalBucketUI")
f.UpdateOnChange(func(blocks []metadata.Meta, err error) {
global.Set(blocks, err)
api.SetGlobal(blocks, err)
})

Expand Down
3 changes: 1 addition & 2 deletions cmd/thanos/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,15 +407,14 @@ func runStore(
ins := extpromhttp.NewInstrumentationMiddleware(reg, nil)

compactorView := ui.NewBucketUI(logger, "", conf.webConfig.externalPrefix, conf.webConfig.prefixHeaderName, "/loaded", conf.component)
compactorView.Register(r, true, ins)
compactorView.Register(r, ins)

// Configure Request Logging for HTTP calls.
logMiddleware := logging.NewHTTPServerMiddleware(logger, httpLogOpts...)
api := blocksAPI.NewBlocksAPI(logger, conf.webConfig.disableCORS, "", flagsMap)
api.Register(r.WithPrefix("/api/v1"), tracer, logger, ins, logMiddleware)

metaFetcher.UpdateOnChange(func(blocks []metadata.Meta, err error) {
compactorView.Set(blocks, err)
api.SetLoaded(blocks, err)
})
srv.Handle("/", r)
Expand Down
3 changes: 1 addition & 2 deletions cmd/thanos/tools_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func registerBucketWeb(app extkingpin.AppClause, objStoreConfig *extflag.PathOrC
ins := extpromhttp.NewInstrumentationMiddleware(reg, nil)

bucketUI := ui.NewBucketUI(logger, *label, *webExternalPrefix, *webPrefixHeaderName, "", component.Bucket)
bucketUI.Register(router, true, ins)
bucketUI.Register(router, ins)

flagsMap := getFlagsMap(cmd.Flags())

Expand Down Expand Up @@ -416,7 +416,6 @@ func registerBucketWeb(app extkingpin.AppClause, objStoreConfig *extflag.PathOrC
return err
}
fetcher.UpdateOnChange(func(blocks []metadata.Meta, err error) {
bucketUI.Set(blocks, err)
api.SetGlobal(blocks, err)
})

Expand Down
53 changes: 8 additions & 45 deletions pkg/ui/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@
package ui

import (
"encoding/json"
"html/template"
"net/http"
"path"
"strings"
"time"

"github.com/go-kit/kit/log"
"github.com/prometheus/common/route"
"github.com/thanos-io/thanos/pkg/block/metadata"
"github.com/thanos-io/thanos/pkg/component"
extpromhttp "github.com/thanos-io/thanos/pkg/extprom/http"
)
Expand All @@ -23,13 +19,9 @@ type Bucket struct {
*BaseUI

externalPrefix, prefixHeader string
uiPrefix string
// Unique Prometheus label that identifies each shard, used as the title. If
// not present, all labels are displayed externally as a legend.
Label string
Blocks template.JS
RefreshedAt time.Time
Err error
Err error
}

func NewBucketUI(logger log.Logger, label, externalPrefix, prefixHeader, uiPrefix string, comp component.Component) *Bucket {
Expand All @@ -44,46 +36,17 @@ func NewBucketUI(logger log.Logger, label, externalPrefix, prefixHeader, uiPrefi

return &Bucket{
BaseUI: NewBaseUI(log.With(logger, "component", "bucketUI"), "bucket_menu.html", tmplFuncs, tmplVariables, externalPrefix, prefixHeader, comp),
Blocks: "[]",
Label: label,
externalPrefix: externalPrefix,
prefixHeader: prefixHeader,
uiPrefix: uiPrefix,
}
}

// Register registers http routes for bucket UI.
func (b *Bucket) Register(r *route.Router, registerNewUI bool, ins extpromhttp.InstrumentationMiddleware) {
if registerNewUI {
// Redirect the original React UI's path (under "/new") to its new path at the root.
r.Get("/new/*path", func(w http.ResponseWriter, r *http.Request) {
p := route.Param(r.Context(), "path")
http.Redirect(w, r, path.Join(GetWebPrefix(b.logger, b.externalPrefix, b.prefixHeader, r), strings.TrimPrefix(p, "/new"))+"?"+r.URL.RawQuery, http.StatusFound)
})

r.Get("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, path.Join(GetWebPrefix(b.logger, b.externalPrefix, b.prefixHeader, r), b.uiPrefix), http.StatusFound)
})

registerReactApp(r, ins, b.BaseUI)
}
}

func (b *Bucket) Set(blocks []metadata.Meta, err error) {
if err != nil {
// Last view is maintained.
b.RefreshedAt = time.Now()
b.Err = err
return
}

data := "[]"
dataB, err := json.Marshal(blocks)
if err == nil {
data = string(dataB)
}

b.RefreshedAt = time.Now()
b.Blocks = template.JS(data)
b.Err = err
func (b *Bucket) Register(r *route.Router, ins extpromhttp.InstrumentationMiddleware) {
// Redirect the original React UI's path (under "/new") to its new path at the root.
r.Get("/new/*path", func(w http.ResponseWriter, r *http.Request) {
p := route.Param(r.Context(), "path")
http.Redirect(w, r, path.Join(GetWebPrefix(b.logger, b.externalPrefix, b.prefixHeader, r), strings.TrimPrefix(p, "/new"))+"?"+r.URL.RawQuery, http.StatusFound)
})
registerReactApp(r, ins, b.BaseUI)
}
3 changes: 0 additions & 3 deletions pkg/ui/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ func queryTmplFuncs() template.FuncMap {

// Register registers new GET routes for subpages and redirects from / to /graph.
func (q *Query) Register(r *route.Router, ins extpromhttp.InstrumentationMiddleware) {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, path.Join(GetWebPrefix(q.logger, q.externalPrefix, q.prefixHeader, r), "/graph"), http.StatusFound)
})
// Redirect the original React UI's path (under "/new") to its new path at the root.
r.Get("/new/*path", func(w http.ResponseWriter, r *http.Request) {
p := route.Param(r.Context(), "path")
Expand Down
4 changes: 0 additions & 4 deletions pkg/ui/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ func ruleTmplFuncs(queryURL string) template.FuncMap {
}

func (ru *Rule) Register(r *route.Router, ins extpromhttp.InstrumentationMiddleware) {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, path.Join(GetWebPrefix(ru.logger, ru.externalPrefix, ru.prefixHeader, r), "/alerts"), http.StatusFound)
})

// Redirect the original React UI's path (under "/new") to its new path at the root.
r.Get("/new/*path", func(w http.ResponseWriter, r *http.Request) {
p := route.Param(r.Context(), "path")
Expand Down
1 change: 1 addition & 0 deletions pkg/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
)

var reactRouterPaths = []string{
"/",
"/alerts",
"/blocks",
"/config",
Expand Down

0 comments on commit 56124ac

Please sign in to comment.