2020-07-08 15:55:25 +00:00
|
|
|
package promql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-07-19 23:26:00 +00:00
|
|
|
"net/http"
|
2020-07-08 15:55:25 +00:00
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-07-20 18:18:19 +00:00
|
|
|
// ActiveQueriesHandler returns response to /api/v1/status/active_queries
|
2020-07-08 15:55:25 +00:00
|
|
|
//
|
2023-07-20 18:18:19 +00:00
|
|
|
// It writes a JSON with active queries to w.
|
2023-09-01 07:34:16 +00:00
|
|
|
func ActiveQueriesHandler(w http.ResponseWriter, _ *http.Request) {
|
2020-07-08 15:55:25 +00:00
|
|
|
aqes := activeQueriesV.GetAll()
|
2023-07-19 23:26:00 +00:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2020-07-08 15:55:25 +00:00
|
|
|
sort.Slice(aqes, func(i, j int) bool {
|
|
|
|
return aqes[i].startTime.Sub(aqes[j].startTime) < 0
|
|
|
|
})
|
|
|
|
now := time.Now()
|
2023-07-19 22:47:21 +00:00
|
|
|
fmt.Fprintf(w, `{"status":"ok","data":[`)
|
|
|
|
for i, aqe := range aqes {
|
2020-07-08 15:55:25 +00:00
|
|
|
d := now.Sub(aqe.startTime)
|
2023-07-19 22:47:21 +00:00
|
|
|
fmt.Fprintf(w, `{"duration":"%.3fs","id":"%016X","remote_addr":%s,"query":%q,"start":%d,"end":%d,"step":%d}`,
|
2023-08-11 12:04:13 +00:00
|
|
|
d.Seconds(), aqe.qid, aqe.quotedRemoteAddr, aqe.q, aqe.start, aqe.end, aqe.step)
|
2023-07-19 22:47:21 +00:00
|
|
|
if i+1 < len(aqes) {
|
|
|
|
fmt.Fprintf(w, `,`)
|
|
|
|
}
|
2020-07-08 15:55:25 +00:00
|
|
|
}
|
2023-07-19 22:47:21 +00:00
|
|
|
fmt.Fprintf(w, `]}`)
|
2020-07-08 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var activeQueriesV = newActiveQueries()
|
|
|
|
|
|
|
|
type activeQueries struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
m map[uint64]activeQueryEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
type activeQueryEntry struct {
|
2020-07-31 15:00:21 +00:00
|
|
|
start int64
|
|
|
|
end int64
|
|
|
|
step int64
|
|
|
|
qid uint64
|
|
|
|
quotedRemoteAddr string
|
|
|
|
q string
|
|
|
|
startTime time.Time
|
2020-07-08 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newActiveQueries() *activeQueries {
|
|
|
|
return &activeQueries{
|
|
|
|
m: make(map[uint64]activeQueryEntry),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (aq *activeQueries) Add(ec *EvalConfig, q string) uint64 {
|
|
|
|
var aqe activeQueryEntry
|
|
|
|
aqe.start = ec.Start
|
|
|
|
aqe.end = ec.End
|
|
|
|
aqe.step = ec.Step
|
2024-02-24 00:44:19 +00:00
|
|
|
aqe.qid = nextActiveQueryID.Add(1)
|
2020-07-31 15:00:21 +00:00
|
|
|
aqe.quotedRemoteAddr = ec.QuotedRemoteAddr
|
2020-07-08 15:55:25 +00:00
|
|
|
aqe.q = q
|
|
|
|
aqe.startTime = time.Now()
|
|
|
|
|
|
|
|
aq.mu.Lock()
|
|
|
|
aq.m[aqe.qid] = aqe
|
|
|
|
aq.mu.Unlock()
|
|
|
|
return aqe.qid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (aq *activeQueries) Remove(qid uint64) {
|
|
|
|
aq.mu.Lock()
|
|
|
|
delete(aq.m, qid)
|
|
|
|
aq.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (aq *activeQueries) GetAll() []activeQueryEntry {
|
|
|
|
aq.mu.Lock()
|
|
|
|
aqes := make([]activeQueryEntry, 0, len(aq.m))
|
|
|
|
for _, aqe := range aq.m {
|
|
|
|
aqes = append(aqes, aqe)
|
|
|
|
}
|
|
|
|
aq.mu.Unlock()
|
|
|
|
return aqes
|
|
|
|
}
|
|
|
|
|
2024-02-24 00:44:19 +00:00
|
|
|
var nextActiveQueryID = func() *atomic.Uint64 {
|
|
|
|
var x atomic.Uint64
|
|
|
|
x.Store(uint64(time.Now().UnixNano()))
|
|
|
|
return &x
|
|
|
|
}()
|