-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.go
154 lines (134 loc) · 4.01 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"context"
"fmt"
"runtime"
"time"
"github.com/armon/go-metrics"
"github.com/dustin/go-humanize"
"go.uber.org/zap"
"golang.org/x/text/language"
"golang.org/x/text/message"
"github.com/bwmarrin/discordgo"
"github.com/shirou/gopsutil/mem"
)
var humanfmt *message.Printer
func init() {
humanfmt = message.NewPrinter(language.English)
}
// process init time, set during init()
var initTime time.Time
func init() {
initTime = time.Now()
}
func makeStatsEmbed(_ context.Context) []*discordgo.MessageEmbed {
guilds, _, _ := guildCount(DiceGolem)
rolls, err := DiceGolem.Redis.Get("rolls:total").Int64()
if err != nil {
logger.Warn("stats", zap.String("error", "can't retrieve roll count"))
rolls = -1
}
var totalExpressions int64
keys := DiceGolem.Redis.Keys(fmt.Sprintf(KeyUserGlobalExpressionsFmt, "*")).Val()
for _, key := range keys {
totalExpressions += DiceGolem.Redis.HLen(key).Val()
}
return []*discordgo.MessageEmbed{
{
Timestamp: time.Now().Local().Format(time.RFC3339),
Fields: []*discordgo.MessageEmbedField{
{
Name: "Rolls",
Value: humanfmt.Sprintf("%d", rolls),
},
{
Name: "Guilds",
Value: humanfmt.Sprintf("%d", guilds),
Inline: true,
},
{
Name: "Expressions",
Value: humanfmt.Sprintf("%d", totalExpressions),
Inline: true,
},
},
},
}
}
func makeHealthEmbed(ctx context.Context) []*discordgo.MessageEmbed {
stateGuilds, statesShards, _ := guildCount(DiceGolem)
memstats := runtime.MemStats{}
runtime.ReadMemStats(&memstats)
sysmem, _ := mem.VirtualMemoryWithContext(ctx)
return []*discordgo.MessageEmbed{
{
Fields: []*discordgo.MessageEmbedField{
{
Name: "Uptime",
Value: fmt.Sprintf("<t:%d> (%s)", initTime.Unix(), time.Since(initTime).Round(time.Second).String()),
Inline: true,
},
{
Name: "Memory",
Value: fmt.Sprintf("%s (%.2f%%)", humanize.Bytes(memstats.Alloc), 100.0*float64(memstats.Alloc)/float64(sysmem.Available)),
Inline: true,
},
{
Name: "Goroutines",
Value: humanfmt.Sprintf("%d", runtime.NumGoroutine()),
Inline: true,
},
{
Name: "Guilds",
Value: humanfmt.Sprintf("%d", stateGuilds),
Inline: true,
},
{
Name: "Shards",
Value: fmt.Sprintf("`%v`", statesShards),
},
},
Footer: &discordgo.MessageEmbedFooter{
Text: "Version " + Revision,
},
},
}
}
// emitStats emits telemetry metrics. It will try to emit as
// many metrics as possible.
func emitStats(b *Bot) {
defer metrics.MeasureSince([]string{"core", "metrics"}, time.Now())
metrics.SetGauge([]string{"core", "heartbeat"}, float32(b.Sessions[0].HeartbeatLatency()/time.Millisecond))
guilds, _, err := guildCount(DiceGolem)
if err == nil {
metrics.SetGauge([]string{"guilds", "total"}, float32(guilds))
}
// redis cache metrics
if DiceGolem.Redis == nil {
return
}
var totalExpressions int64
expressionsKeys := DiceGolem.Redis.Keys(fmt.Sprintf(KeyUserGlobalExpressionsFmt, "*")).Val()
for _, key := range expressionsKeys {
totalExpressions += DiceGolem.Redis.HLen(key).Val()
}
metrics.SetGauge([]string{"storage", "expressions", "user_count"}, float32(len(expressionsKeys)))
metrics.SetGauge([]string{"storage", "expressions", "count"}, float32(totalExpressions))
var totalCache int64
cacheKeys := DiceGolem.Redis.Keys(fmt.Sprintf(KeyUserRecentFmt, "*")).Val()
for _, key := range cacheKeys {
totalCache += DiceGolem.Redis.ZCard(key).Val()
}
metrics.SetGauge([]string{"storage", "recent", "user_count"}, float32(len(cacheKeys)))
metrics.SetGauge([]string{"storage", "recent", "count"}, float32(totalCache))
go func() {
t := time.Now() // defers don't work properly in a goroutine
_ = DiceGolem.Redis.Ping()
metrics.MeasureSince([]string{"redis", "ping"}, t)
}()
if rolls, err := DiceGolem.Redis.Get("rolls:total").Int64(); err == nil {
metrics.SetGauge([]string{"rolls", "total"}, float32(rolls))
} else {
logger.Warn("metrics", zap.String("error", "can't retrieve roll count"))
}
}