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 3149af624d
commit a2aa3a60eb
4 changed files with 41 additions and 32 deletions

View File

@ -18,6 +18,7 @@ import (
"github.com/VictoriaMetrics/VictoriaMetrics/lib/auth" "github.com/VictoriaMetrics/VictoriaMetrics/lib/auth"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime" "github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil" "github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger" "github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage" "github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
"github.com/VictoriaMetrics/metrics" "github.com/VictoriaMetrics/metrics"
@ -770,13 +771,13 @@ func QueryHandler(startTime time.Time, at *auth.Token, w http.ResponseWriter, r
} }
ec := promql.EvalConfig{ ec := promql.EvalConfig{
AuthToken: at, AuthToken: at,
Start: start, Start: start,
End: start, End: start,
Step: step, Step: step,
RemoteAddr: r.RemoteAddr, QuotedRemoteAddr: httpserver.GetQuotedRemoteAddr(r),
Deadline: deadline, Deadline: deadline,
LookbackDelta: lookbackDelta, LookbackDelta: lookbackDelta,
DenyPartialResponse: getDenyPartialResponse(r), DenyPartialResponse: getDenyPartialResponse(r),
} }
@ -858,14 +859,14 @@ func queryRangeHandler(startTime time.Time, at *auth.Token, w http.ResponseWrite
} }
ec := promql.EvalConfig{ ec := promql.EvalConfig{
AuthToken: at, AuthToken: at,
Start: start, Start: start,
End: end, End: end,
Step: step, Step: step,
RemoteAddr: r.RemoteAddr, QuotedRemoteAddr: httpserver.GetQuotedRemoteAddr(r),
Deadline: deadline, Deadline: deadline,
MayCache: mayCache, MayCache: mayCache,
LookbackDelta: lookbackDelta, LookbackDelta: lookbackDelta,
DenyPartialResponse: getDenyPartialResponse(r), DenyPartialResponse: getDenyPartialResponse(r),
} }

View File

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

View File

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

View File

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