mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-16 10:40:12 +00:00
feat(stats): query http gateway, wait until gateway up to collect discord stats
This commit is contained in:
parent
e4ed354536
commit
9ff824c37b
7 changed files with 130 additions and 17 deletions
|
|
@ -5,8 +5,93 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"io"
|
||||
"os"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type httpstats struct {
|
||||
Up bool `json:"up"`
|
||||
GuildCount int `json:"guild_count"`
|
||||
ChannelCount int `json:"channel_count"`
|
||||
}
|
||||
|
||||
func query_http_cache() []httpstats {
|
||||
var values []httpstats
|
||||
|
||||
url := os.Getenv("CONSUL_URL")
|
||||
if url == "" {
|
||||
panic("missing CONSUL_URL in environment")
|
||||
}
|
||||
|
||||
expected_gateway_count, err := strconv.Atoi(os.Getenv("EXPECTED_GATEWAY_COUNT"))
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("missing or invalid EXPECTED_GATEWAY_COUNT in environment"))
|
||||
}
|
||||
|
||||
resp, err := http.Get(fmt.Sprintf("%v/v1/health/service/pluralkit-gateway", url))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
panic(fmt.Sprintf("got status %v trying to query consul for all_gateway_instances", resp.Status))
|
||||
}
|
||||
|
||||
var ips []string
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var cs []any
|
||||
err = json.Unmarshal(data, &cs)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if len(cs) != expected_gateway_count {
|
||||
panic(fmt.Sprintf("got unexpected number of gateway instances from consul (expected %v, got %v)", expected_gateway_count, len(cs)))
|
||||
}
|
||||
|
||||
for idx, itm := range cs {
|
||||
if ip, ok := itm.(map[string]any)["Service"].(map[string]any)["Address"].(string); ok {
|
||||
ips = append(ips, ip)
|
||||
} else {
|
||||
panic(fmt.Sprintf("got bad data from consul for all_gateway_instances, at index %v", idx))
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("querying %v gateway clusters for discord stats\n", len(ips))
|
||||
|
||||
for _, ip := range ips {
|
||||
resp, err := http.Get("http://"+ip+":5000/stats")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusFound {
|
||||
panic(fmt.Sprintf("got status %v trying to query %v:5000", resp.Status, ip))
|
||||
}
|
||||
var s httpstats
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = json.Unmarshal(data, &s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if s.Up == false {
|
||||
panic("gateway is not up yet, skipping stats collection")
|
||||
}
|
||||
values = append(values, s)
|
||||
}
|
||||
|
||||
return values
|
||||
}
|
||||
|
||||
type rstatval struct {
|
||||
GuildCount int `json:"GuildCount"`
|
||||
ChannelCount int `json:"ChannelCount"`
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ func update_db_message_meta() {
|
|||
}
|
||||
|
||||
func get_discord_counts() (int, int) {
|
||||
redisStats := run_redis_query()
|
||||
redisStats := query_http_cache()
|
||||
|
||||
guild_count := 0
|
||||
channel_count := 0
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue