Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix runtimeInfo being falsely reported
Signed-off-by: Prem Kumar <prmsrswt@gmail.com>
  • Loading branch information
onprem committed Jul 8, 2020
commit 69eb3a31f3bef9ee0e67cda863e2077235717226
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could just return this struct directly, no need for a variable. But I'd guess that the compiler already does this optimization for us. Plus, I'm surprised that none of our linters caught this. Perhaps we could improve the linters to catch functions that look like:

func foo() test {
  a := test{}
  return a
}

In the distant future? 😄

}

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