app/vmselect: show X-Forwarded-For contents on /api/v1/status/active_queries page

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/659
This commit is contained in:
Aliaksandr Valialkin 2020-07-31 18:00:21 +03:00
parent 5e71fab8a6
commit 509d12643b
4 changed files with 37 additions and 28 deletions

View file

@ -16,6 +16,7 @@ import (
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmselect/netstorage"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmselect/promql"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
"github.com/VictoriaMetrics/metrics"
@ -690,12 +691,12 @@ func QueryHandler(startTime time.Time, w http.ResponseWriter, r *http.Request) e
}
ec := promql.EvalConfig{
Start: start,
End: start,
Step: step,
RemoteAddr: r.RemoteAddr,
Deadline: deadline,
LookbackDelta: lookbackDelta,
Start: start,
End: start,
Step: step,
QuotedRemoteAddr: httpserver.GetQuotedRemoteAddr(r),
Deadline: deadline,
LookbackDelta: lookbackDelta,
}
result, err := promql.Exec(&ec, query, true)
if err != nil {
@ -775,13 +776,13 @@ func queryRangeHandler(startTime time.Time, w http.ResponseWriter, query string,
}
ec := promql.EvalConfig{
Start: start,
End: end,
Step: step,
RemoteAddr: r.RemoteAddr,
Deadline: deadline,
MayCache: mayCache,
LookbackDelta: lookbackDelta,
Start: start,
End: end,
Step: step,
QuotedRemoteAddr: httpserver.GetQuotedRemoteAddr(r),
Deadline: deadline,
MayCache: mayCache,
LookbackDelta: lookbackDelta,
}
result, err := promql.Exec(&ec, query, false)
if err != nil {

View file

@ -20,8 +20,8 @@ func WriteActiveQueries(w io.Writer) {
now := time.Now()
for _, aqe := range aqes {
d := now.Sub(aqe.startTime)
fmt.Fprintf(w, "\tduration: %.3fs, id=%016X, remote_addr=%q, query=%q, start=%d, end=%d, step=%d\n",
d.Seconds(), aqe.qid, aqe.remoteAddr, aqe.q, aqe.start, aqe.end, aqe.step)
fmt.Fprintf(w, "\tduration: %.3fs, id=%016X, remote_addr=%s, query=%q, start=%d, end=%d, step=%d\n",
d.Seconds(), aqe.qid, aqe.quotedRemoteAddr, aqe.q, aqe.start, aqe.end, aqe.step)
}
}
@ -33,13 +33,13 @@ type activeQueries struct {
}
type activeQueryEntry struct {
start int64
end int64
step int64
qid uint64
remoteAddr string
q string
startTime time.Time
start int64
end int64
step int64
qid uint64
quotedRemoteAddr string
q string
startTime time.Time
}
func newActiveQueries() *activeQueries {
@ -54,7 +54,7 @@ func (aq *activeQueries) Add(ec *EvalConfig, q string) uint64 {
aqe.end = ec.End
aqe.step = ec.Step
aqe.qid = atomic.AddUint64(&nextActiveQueryID, 1)
aqe.remoteAddr = ec.RemoteAddr
aqe.quotedRemoteAddr = ec.QuotedRemoteAddr
aqe.q = q
aqe.startTime = time.Now()

View file

@ -81,8 +81,10 @@ type EvalConfig struct {
End int64
Step int64
RemoteAddr string
Deadline netstorage.Deadline
// QuotedRemoteAddr contains quoted remote address.
QuotedRemoteAddr string
Deadline netstorage.Deadline
MayCache bool

View file

@ -481,13 +481,19 @@ var (
requestsTotal = metrics.NewCounter(`vm_http_requests_all_total`)
)
// Errorf writes formatted error message to w and to logger.
func Errorf(w http.ResponseWriter, r *http.Request, format string, args ...interface{}) {
errStr := fmt.Sprintf(format, args...)
// GetQuotedRemoteAddr returns quoted remote address.
func GetQuotedRemoteAddr(r *http.Request) string {
remoteAddr := strconv.Quote(r.RemoteAddr) // quote remoteAddr and X-Forwarded-For, since they may contain untrusted input
if addr := r.Header.Get("X-Forwarded-For"); addr != "" {
remoteAddr += ", X-Forwarded-For: " + strconv.Quote(addr)
}
return remoteAddr
}
// Errorf writes formatted error message to w and to logger.
func Errorf(w http.ResponseWriter, r *http.Request, format string, args ...interface{}) {
errStr := fmt.Sprintf(format, args...)
remoteAddr := GetQuotedRemoteAddr(r)
errStr = fmt.Sprintf("remoteAddr: %s; %s", remoteAddr, errStr)
logger.WarnfSkipframes(1, "%s", errStr)