Skip to content

Commit

Permalink
Fix runtimeInfo being falsely reported
Browse files Browse the repository at this point in the history
Signed-off-by: Prem Kumar <prmsrswt@gmail.com>
  • Loading branch information
onprem committed Jul 8, 2020
1 parent a9ca243 commit 69eb3a3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 46 deletions.
41 changes: 21 additions & 20 deletions cmd/thanos/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,29 @@ func runQuery(
GoVersion: version.GoVersion,
}

CWD, err := os.Getwd()
if err != nil {
CWD = "<error retrieving current working directory>"
level.Warn(logger).Log("msg", "failed to retrieve current working directory", "err", err)
}

birth := time.Now()

var runtimeInfo v1.RuntimeInfoFn = func (logger log.Logger) v1.RuntimeInfo {
status := v1.RuntimeInfo{
StartTime: birth,
CWD: CWD,
GoroutineCount: runtime.NumGoroutine(),
GOMAXPROCS: runtime.GOMAXPROCS(0),
GOGC: os.Getenv("GOGC"),
GODEBUG: os.Getenv("GODEBUG"),
}
return status
}

ins := extpromhttp.NewInstrumentationMiddleware(reg)
// TODO(bplotka in PR #513 review): pass all flags, not only the flags needed by prefix rewriting.
ui.NewQueryUI(logger, reg, stores, webExternalPrefix, webPrefixHeaderName).Register(router, ins)
ui.NewQueryUI(logger, reg, stores, webExternalPrefix, webPrefixHeaderName, runtimeInfo, *buildInfo).Register(router, ins)

api := v1.NewAPI(
logger,
Expand Down Expand Up @@ -492,25 +512,6 @@ func runQuery(
return nil
}

func runtimeInfo(logger log.Logger) v1.RuntimeInfo {
cwd, err := os.Getwd()
if err != nil {
cwd = "<error retrieving current working directory>"
level.Warn(logger).Log("msg", "failed to retrieve current working directory", "err", err)
}

status := v1.RuntimeInfo{
StartTime: time.Now().UTC(),
CWD: cwd,
GoroutineCount: runtime.NumGoroutine(),
GOMAXPROCS: runtime.GOMAXPROCS(0),
GOGC: os.Getenv("GOGC"),
GODEBUG: os.Getenv("GODEBUG"),
}

return status
}

func removeDuplicateStoreSpecs(logger log.Logger, duplicatedStores prometheus.Counter, specs []query.StoreSpec) []query.StoreSpec {
set := make(map[string]query.StoreSpec)
for _, spec := range specs {
Expand Down
3 changes: 3 additions & 0 deletions pkg/query/api/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ type RuntimeInfo struct {
GODEBUG string `json:"GODEBUG"`
}

// RuntimeInfoFn returns updated runtime information about Thanos.
type RuntimeInfoFn func(log.Logger) RuntimeInfo

type response struct {
Status status `json:"status"`
Data interface{} `json:"data,omitempty"`
Expand Down
40 changes: 14 additions & 26 deletions pkg/ui/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@ package ui
import (
"html/template"
"net/http"
"os"
"path"
"sort"
"strings"
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/prometheus/common/route"
"github.com/prometheus/common/version"
"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 {
Expand All @@ -29,10 +27,11 @@ type Query struct {

externalPrefix, prefixHeader string

cwd string
birth time.Time
reg prometheus.Registerer
now func() model.Time
cwd string
birth time.Time
version v1.ThanosVersion
reg prometheus.Registerer
now func() model.Time
}

type thanosVersion struct {
Expand All @@ -44,19 +43,15 @@ type thanosVersion struct {
GoVersion string `json:"goVersion"`
}

func NewQueryUI(logger log.Logger, reg prometheus.Registerer, storeSet *query.StoreSet, externalPrefix, prefixHeader string) *Query {
cwd, err := os.Getwd()
if err != nil {
cwd = "<error retrieving current working directory>"
level.Warn(logger).Log("msg", "failed to retrieve current working directory", "err", err)
}
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: cwd,
birth: time.Now(),
cwd: runtimeInfo(logger).CWD,
birth: runtimeInfo(logger).StartTime,
version: buildInfo,
reg: reg,
now: model.Now,
}
Expand Down Expand Up @@ -120,18 +115,11 @@ func (q *Query) status(w http.ResponseWriter, r *http.Request) {
q.executeTemplate(w, "status.html", prefix, struct {
Birth time.Time
CWD string
Version thanosVersion
Version v1.ThanosVersion
}{
Birth: q.birth,
CWD: q.cwd,
Version: thanosVersion{
Version: version.Version,
Revision: version.Revision,
Branch: version.Branch,
BuildUser: version.BuildUser,
BuildDate: version.BuildDate,
GoVersion: version.GoVersion,
},
Birth: q.birth,
CWD: q.cwd,
Version: q.version,
})
}

Expand Down

0 comments on commit 69eb3a3

Please sign in to comment.