Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
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
9 changes: 9 additions & 0 deletions pkg/data/gensyncmap/gensyncmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,12 @@ func (m *Map[K, V]) Len() int {
})
return n
}

func (m *Map[K, V]) IsEmpty() bool {
isEmpty := true
m.m.Range(func(_, _ any) bool {
isEmpty = false
return false
})
return isEmpty
}
1 change: 1 addition & 0 deletions services/httpd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type Config struct {
MaxConcurrentWriteLimit int `toml:"max-concurrent-write-limit"`
MaxEnqueuedWriteLimit int `toml:"max-enqueued-write-limit"`
EnqueuedWriteTimeout time.Duration `toml:"enqueued-write-timeout"`
UserQueryBytesEnabled bool `toml:"user-query-bytes-enabled"`
TLS *tls.Config `toml:"-"`
}

Expand Down
78 changes: 63 additions & 15 deletions services/httpd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/influxdata/influxdb/models"
"github.com/influxdata/influxdb/monitor"
"github.com/influxdata/influxdb/monitor/diagnostics"
"github.com/influxdata/influxdb/pkg/data/gensyncmap"
"github.com/influxdata/influxdb/prometheus"
"github.com/influxdata/influxdb/query"
"github.com/influxdata/influxdb/services/meta"
Expand Down Expand Up @@ -143,12 +144,13 @@ type Handler struct {
Controller Controller
CompilerMappings flux.CompilerMappings

Config *Config
Logger *zap.Logger
CLFLogger *log.Logger
accessLog *os.File
accessLogFilters StatusFilters
stats *Statistics
Config *Config
Logger *zap.Logger
CLFLogger *log.Logger
accessLog *os.File
accessLogFilters StatusFilters
stats *Statistics
queryBytesPerUser gensyncmap.Map[string, *atomic.Int64]

requestTracker *RequestTracker
writeThrottler *Throttler
Expand Down Expand Up @@ -322,7 +324,12 @@ func NewHandler(c Config) *Handler {
return func(w http.ResponseWriter, r *http.Request, user meta.User) {
// TODO: This is the only place we use AuthorizeUnrestricted. It would be better to use an explicit permission
if user == nil || !user.AuthorizeUnrestricted() {
h.Logger.Info("Unauthorized request", zap.String("user", user.ID()), zap.String("path", r.URL.Path))
// Don't panic
id := ""
if user != nil {
id = user.ID()
}
h.Logger.Info("Unauthorized request", zap.String("user", id), zap.String("path", r.URL.Path))
h.httpError(w, "error authorizing admin access", http.StatusForbidden)
return
}
Expand Down Expand Up @@ -442,7 +449,7 @@ type Statistics struct {

// Statistics returns statistics for periodic monitoring.
func (h *Handler) Statistics(tags map[string]string) []models.Statistic {
return []models.Statistic{{
stats := []models.Statistic{{
Name: "httpd",
Tags: tags,
Values: map[string]interface{}{
Expand Down Expand Up @@ -472,6 +479,35 @@ func (h *Handler) Statistics(tags map[string]string) []models.Statistic {
statFluxQueryRequestBytesTransmitted: atomic.LoadInt64(&h.stats.FluxQueryRequestBytesTransmitted),
},
}}

// Add per-user query bytes as separate statistics (one per user) if enabled
if h.Config.UserQueryBytesEnabled && !h.queryBytesPerUser.IsEmpty() {
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

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

The IsEmpty() check adds an extra full sync.Map traversal before the real Range() traversal. Since Range() is safe on an empty map, this doubles the work on every Statistics() call once any user entries exist. Consider removing the !h.queryBytesPerUser.IsEmpty() guard and just Range() when UserQueryBytesEnabled is true (or track a separate atomic entry count if you need a cheap emptiness check).

Suggested change
if h.Config.UserQueryBytesEnabled && !h.queryBytesPerUser.IsEmpty() {
if h.Config.UserQueryBytesEnabled {

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

IsEmpty() does not require a full traversal of the sync.Map. This comment is wrong

h.queryBytesPerUser.Range(func(user string, counter *atomic.Int64) bool {
userTag := user
if user == "" {
userTag = StatAnonymousUser
}
userTags := models.NewTags(tags).Merge(map[string]string{StatUserTagKey: userTag}).Map()
stats = append(stats, models.Statistic{
Name: "userquerybytes",
Tags: userTags,
Values: map[string]interface{}{statQueryRespBytes: counter.Load()},
})
return true
})
}

return stats
}

// addQueryBytesForUser atomically adds bytes to the per-user query bytes counter.
// This is a no-op if UserQueryBytesEnabled is false.
func (h *Handler) addQueryBytesForUser(user string, n int64) {
if !h.Config.UserQueryBytesEnabled {
return
}
counter, _ := h.queryBytesPerUser.LoadOrStore(user, &atomic.Int64{})
counter.Add(n)
}

// AddRoutes sets the provided routes on the handler.
Expand Down Expand Up @@ -782,6 +818,7 @@ func (h *Handler) serveQuery(w http.ResponseWriter, r *http.Request, user meta.U
Results: []*query.Result{r},
})
atomic.AddInt64(&h.stats.QueryRequestBytesTransmitted, int64(n))
h.addQueryBytesForUser(userName, int64(n))
w.(http.Flusher).Flush()
continue
}
Expand Down Expand Up @@ -873,6 +910,7 @@ func (h *Handler) serveQuery(w http.ResponseWriter, r *http.Request, user meta.U
if !chunked {
n, _ := rw.WriteResponse(resp)
atomic.AddInt64(&h.stats.QueryRequestBytesTransmitted, int64(n))
h.addQueryBytesForUser(userName, int64(n))
}
}

Expand Down Expand Up @@ -2031,6 +2069,11 @@ func (h *Handler) servePromRead(w http.ResponseWriter, r *http.Request, user met
return
}

var userName string
if user != nil {
userName = user.ID()
}

respond := func(resp *prompb.ReadResponse) {
data, err := resp.Marshal()
if err != nil {
Expand All @@ -2048,6 +2091,7 @@ func (h *Handler) servePromRead(w http.ResponseWriter, r *http.Request, user met
}

atomic.AddInt64(&h.stats.QueryRequestBytesTransmitted, int64(len(compressed)))
h.addQueryBytesForUser(userName, int64(len(compressed)))
}

ctx := context.Background()
Expand Down Expand Up @@ -2134,6 +2178,11 @@ func (h *Handler) serveFluxQuery(w http.ResponseWriter, r *http.Request, user me
atomic.AddInt64(&h.stats.FluxQueryRequestDuration, time.Since(start).Nanoseconds())
}(time.Now())

var userName string
if user != nil {
userName = user.ID()
}

req, err := decodeQueryRequest(r)
if err != nil {
h.httpError(w, err.Error(), http.StatusBadRequest)
Expand Down Expand Up @@ -2201,14 +2250,13 @@ func (h *Handler) serveFluxQuery(w http.ResponseWriter, r *http.Request, user me
defer results.Release()

n, err = encoder.Encode(w, results)
if err != nil {
if n == 0 {
// If the encoder did not write anything, we can write an error header.
h.httpError(w, err.Error(), http.StatusInternalServerError)
} else {
atomic.AddInt64(&h.stats.FluxQueryRequestBytesTransmitted, int64(n))
}
if err != nil && n == 0 {
// If the encoder did not write anything, we can write an error header.
h.httpError(w, err.Error(), http.StatusInternalServerError)
return
}
atomic.AddInt64(&h.stats.FluxQueryRequestBytesTransmitted, int64(n))
h.addQueryBytesForUser(userName, int64(n))
}
}

Expand Down
Loading
Loading