Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ at least one host is required, other params are optional
# Set your TiKV PD addresses (required)
export TIKV_PD_ADDRS="127.0.0.1:2379"

# Optional: slow down backend metrics scraping to reduce network traffic
export TIKV_UI_METRICS_SCRAPE_INTERVAL="30s"

# Run the server
./bin/tikv-ui

Expand Down
21 changes: 20 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"

Expand All @@ -23,6 +24,8 @@ func main() {
if pdAddrsEnv == "" {
log.Fatal("TIKV_PD_ADDRS env var is required (comma-separated PD addresses)")
}
metricsScrapeInterval := getDurationEnv("TIKV_UI_METRICS_SCRAPE_INTERVAL", 5*time.Second)

clusters := utils.GetClusters(pdAddrsEnv)
if len(clusters) == 0 {
log.Fatal("no clusters found")
Expand Down Expand Up @@ -54,7 +57,7 @@ func main() {
}
return result
},
5*time.Second,
metricsScrapeInterval,
cache,
)
metrics.Start(ctx)
Expand Down Expand Up @@ -136,7 +139,23 @@ func main() {

log.Println("TiKV explorer API listening on :" + port)
log.Println("Cluster management: POST /api/clusters/connect, GET /api/clusters, POST /api/clusters/switch")
log.Printf("Metrics scrape interval: %s", metricsScrapeInterval)
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("server error: %v", err)
}
}

func getDurationEnv(key string, fallback time.Duration) time.Duration {
value := strings.TrimSpace(os.Getenv(key))
if value == "" {
return fallback
}

duration, err := time.ParseDuration(value)
if err != nil || duration <= 0 {
log.Printf("invalid %s=%q, using default %s", key, value, fallback)
return fallback
}

return duration
}
3 changes: 2 additions & 1 deletion pkg/services/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ func (m *Monitor) Start(ctx context.Context) {
case <-ctx.Done():
return
case <-ticker.C:
go m.pollAllClusters(ctx)
// Run scrapes serially so slow polls do not stack up.
m.pollAllClusters(ctx)
}
}
}()
Expand Down
Loading