Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add receiver downscale endpoint #88

Merged
merged 19 commits into from
Nov 14, 2024
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
Loading