Skip to content

Commit

Permalink
Add receiver downscale endpoint (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuchen-db authored Nov 14, 2024
2 parents 17f9d7c + 662332a commit 6cf9daa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
17 changes: 16 additions & 1 deletion cmd/thanos/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"context"
"fmt"
"net"
"net/http"
"os"
"path"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -322,6 +324,19 @@ func runReceive(
httpserver.WithGracePeriod(time.Duration(*conf.httpGracePeriod)),
httpserver.WithTLSConfig(*conf.httpTLSConfig),
)
srv.Handle("/-/downscale", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tenants := dbs.GetTenants()
n := len(tenants)
w.Header().Set("Tenant-Count", strconv.Itoa(n))
for _, tname := range tenants {
w.Header().Add("Tenants", tname)
}
if n > 0 {
w.WriteHeader(http.StatusTooEarly)
} else {
w.WriteHeader(http.StatusOK)
}
}))
g.Add(func() error {
statusProber.Healthy()
return srv.ListenAndServe()
Expand Down Expand Up @@ -465,7 +480,7 @@ func runReceive(
ctx, cancel := context.WithCancel(context.Background())
g.Add(func() error {
return runutil.Repeat(conf.topMetricsUpdateInterval, ctx.Done(), func() error {
level.Info(logger).Log("msg", "getting top metrics")
level.Debug(logger).Log("msg", "getting top metrics")
for _, ts := range dbs.TenantStats(conf.numTopMetricsPerTenant, labels.MetricName) {
for _, ms := range ts.Stats.IndexPostingStats.CardinalityMetricsStats {
if ms.Count >= conf.topMetricsMinimumCardinality {
Expand Down
10 changes: 10 additions & 0 deletions pkg/receive/multitsdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ func NewMultiTSDB(
return mt
}

func (t *MultiTSDB) GetTenants() []string {
t.mtx.RLock()
tenants := make([]string, 0, len(t.tenants))
for tname := range t.tenants {
tenants = append(tenants, tname)
}
defer t.mtx.RUnlock()
return tenants
}

type localClient struct {
store *store.TSDBStore

Expand Down

0 comments on commit 6cf9daa

Please sign in to comment.