2020-02-16 15:53:47 +05:30
|
|
|
package eventsubscribers
|
|
|
|
|
|
|
|
import (
|
2020-05-01 05:16:12 +05:30
|
|
|
"context"
|
2020-02-16 15:53:47 +05:30
|
|
|
"net/http"
|
|
|
|
"strings"
|
2020-03-30 15:00:06 +05:30
|
|
|
"sync"
|
|
|
|
"time"
|
2020-02-16 15:53:47 +05:30
|
|
|
|
|
|
|
"github.com/mono83/slf"
|
|
|
|
|
2020-03-30 15:00:06 +05:30
|
|
|
"github.com/elyby/chrly/api/mojang"
|
2020-02-16 15:53:47 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
type StatsReporter struct {
|
2020-04-04 01:33:15 +05:30
|
|
|
slf.StatsReporter
|
|
|
|
Prefix string
|
2020-03-30 15:00:06 +05:30
|
|
|
|
2020-04-04 01:33:15 +05:30
|
|
|
timersMap map[string]time.Time
|
|
|
|
timersMutex sync.Mutex
|
2020-02-16 15:53:47 +05:30
|
|
|
}
|
|
|
|
|
2020-05-01 05:16:12 +05:30
|
|
|
type Reporter interface {
|
|
|
|
Enable(reporter slf.StatsReporter)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ReporterFunc func(reporter slf.StatsReporter)
|
|
|
|
|
|
|
|
func (f ReporterFunc) Enable(reporter slf.StatsReporter) {
|
|
|
|
f(reporter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: rework all reporters in the same style as AvailableRedisPoolSizeReporter
|
2020-04-04 00:50:56 +05:30
|
|
|
func (s *StatsReporter) ConfigureWithDispatcher(d Subscriber) {
|
2020-04-04 01:33:15 +05:30
|
|
|
s.timersMap = make(map[string]time.Time)
|
|
|
|
|
2020-03-30 15:00:06 +05:30
|
|
|
// Per request events
|
2020-02-16 15:53:47 +05:30
|
|
|
d.Subscribe("skinsystem:before_request", s.handleBeforeRequest)
|
|
|
|
d.Subscribe("skinsystem:after_request", s.handleAfterRequest)
|
|
|
|
|
2020-03-30 15:00:06 +05:30
|
|
|
// Authentication events
|
2020-02-16 15:53:47 +05:30
|
|
|
d.Subscribe("authenticator:success", s.incCounterHandler("authentication.challenge")) // TODO: legacy, remove in v5
|
|
|
|
d.Subscribe("authenticator:success", s.incCounterHandler("authentication.success"))
|
|
|
|
d.Subscribe("authentication:error", s.incCounterHandler("authentication.challenge")) // TODO: legacy, remove in v5
|
|
|
|
d.Subscribe("authentication:error", s.incCounterHandler("authentication.failed"))
|
2020-03-30 15:00:06 +05:30
|
|
|
|
|
|
|
// Mojang signed textures source events
|
|
|
|
d.Subscribe("mojang_textures:call", s.incCounterHandler("mojang_textures.request"))
|
2020-04-28 20:27:51 +05:30
|
|
|
d.Subscribe("mojang_textures:usernames:after_cache", func(username string, uuid string, found bool, err error) {
|
|
|
|
if err != nil || !found {
|
2020-03-30 15:00:06 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if uuid == "" {
|
2020-04-30 03:14:31 +05:30
|
|
|
s.IncCounter("mojang_textures.usernames.cache_hit_nil", 1)
|
2020-03-30 15:00:06 +05:30
|
|
|
} else {
|
2020-04-30 03:14:31 +05:30
|
|
|
s.IncCounter("mojang_textures.usernames.cache_hit", 1)
|
2020-03-30 15:00:06 +05:30
|
|
|
}
|
|
|
|
})
|
|
|
|
d.Subscribe("mojang_textures:textures:after_cache", func(uuid string, textures *mojang.SignedTexturesResponse, err error) {
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if textures != nil {
|
2020-04-04 21:44:12 +05:30
|
|
|
s.IncCounter("mojang_textures.textures.cache_hit", 1)
|
2020-03-30 15:00:06 +05:30
|
|
|
}
|
|
|
|
})
|
|
|
|
d.Subscribe("mojang_textures:already_processing", s.incCounterHandler("mojang_textures.already_scheduled"))
|
|
|
|
d.Subscribe("mojang_textures:usernames:after_call", func(username string, profile *mojang.ProfileInfo, err error) {
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if profile == nil {
|
2020-04-04 21:44:12 +05:30
|
|
|
s.IncCounter("mojang_textures.usernames.uuid_miss", 1)
|
2020-03-30 15:00:06 +05:30
|
|
|
} else {
|
2020-04-04 21:44:12 +05:30
|
|
|
s.IncCounter("mojang_textures.usernames.uuid_hit", 1)
|
2020-03-30 15:00:06 +05:30
|
|
|
}
|
|
|
|
})
|
|
|
|
d.Subscribe("mojang_textures:textures:before_call", s.incCounterHandler("mojang_textures.textures.request"))
|
|
|
|
d.Subscribe("mojang_textures:textures:after_call", func(uuid string, textures *mojang.SignedTexturesResponse, err error) {
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if textures == nil {
|
2020-04-04 21:44:12 +05:30
|
|
|
s.IncCounter("mojang_textures.usernames.textures_miss", 1)
|
2020-03-30 15:00:06 +05:30
|
|
|
} else {
|
2020-04-04 21:44:12 +05:30
|
|
|
s.IncCounter("mojang_textures.usernames.textures_hit", 1)
|
2020-03-30 15:00:06 +05:30
|
|
|
}
|
|
|
|
})
|
|
|
|
d.Subscribe("mojang_textures:before_result", func(username string, uuid string) {
|
|
|
|
s.startTimeRecording("mojang_textures_result_time_" + username)
|
|
|
|
})
|
|
|
|
d.Subscribe("mojang_textures:after_result", func(username string, textures *mojang.SignedTexturesResponse, err error) {
|
|
|
|
s.finalizeTimeRecording("mojang_textures_result_time_"+username, "mojang_textures.result_time")
|
|
|
|
})
|
|
|
|
d.Subscribe("mojang_textures:textures:before_call", func(uuid string) {
|
|
|
|
s.startTimeRecording("mojang_textures_provider_time_" + uuid)
|
|
|
|
})
|
|
|
|
d.Subscribe("mojang_textures:textures:after_call", func(uuid string, textures *mojang.SignedTexturesResponse, err error) {
|
|
|
|
s.finalizeTimeRecording("mojang_textures_provider_time_"+uuid, "mojang_textures.textures.request_time")
|
|
|
|
})
|
|
|
|
|
|
|
|
// Mojang UUIDs batch provider metrics
|
|
|
|
d.Subscribe("mojang_textures:batch_uuids_provider:queued", s.incCounterHandler("mojang_textures.usernames.queued"))
|
|
|
|
d.Subscribe("mojang_textures:batch_uuids_provider:round", func(usernames []string, queueSize int) {
|
2020-04-04 21:44:12 +05:30
|
|
|
s.UpdateGauge("mojang_textures.usernames.iteration_size", int64(len(usernames)))
|
|
|
|
s.UpdateGauge("mojang_textures.usernames.queue_size", int64(queueSize))
|
2020-04-26 19:04:46 +05:30
|
|
|
if len(usernames) != 0 {
|
|
|
|
s.startTimeRecording("batch_uuids_provider_round_time_" + strings.Join(usernames, "|"))
|
|
|
|
}
|
2020-03-30 15:00:06 +05:30
|
|
|
})
|
2020-04-26 19:04:46 +05:30
|
|
|
d.Subscribe("mojang_textures:batch_uuids_provider:result", func(usernames []string, profiles []*mojang.ProfileInfo, err error) {
|
|
|
|
s.finalizeTimeRecording("batch_uuids_provider_round_time_"+strings.Join(usernames, "|"), "mojang_textures.usernames.round_time")
|
2020-03-30 15:00:06 +05:30
|
|
|
})
|
2020-02-16 15:53:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StatsReporter) handleBeforeRequest(req *http.Request) {
|
|
|
|
var key string
|
|
|
|
m := req.Method
|
|
|
|
p := req.URL.Path
|
|
|
|
if p == "/skins" {
|
|
|
|
key = "skins.get_request"
|
|
|
|
} else if strings.HasPrefix(p, "/skins/") {
|
|
|
|
key = "skins.request"
|
|
|
|
} else if p == "/cloaks" {
|
|
|
|
key = "capes.get_request"
|
|
|
|
} else if strings.HasPrefix(p, "/cloaks/") {
|
|
|
|
key = "capes.request"
|
|
|
|
} else if strings.HasPrefix(p, "/textures/signed/") {
|
|
|
|
key = "signed_textures.request"
|
|
|
|
} else if strings.HasPrefix(p, "/textures/") {
|
|
|
|
key = "textures.request"
|
2023-12-13 06:26:40 +05:30
|
|
|
} else if strings.HasPrefix(p, "/profile/") {
|
|
|
|
key = "profiles.request"
|
2020-02-16 15:53:47 +05:30
|
|
|
} else if m == http.MethodPost && p == "/api/skins" {
|
|
|
|
key = "api.skins.post.request"
|
|
|
|
} else if m == http.MethodDelete && strings.HasPrefix(p, "/api/skins/") {
|
|
|
|
key = "api.skins.delete.request"
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-04 21:44:12 +05:30
|
|
|
s.IncCounter(key, 1)
|
2020-02-16 15:53:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StatsReporter) handleAfterRequest(req *http.Request, code int) {
|
|
|
|
var key string
|
|
|
|
m := req.Method
|
|
|
|
p := req.URL.Path
|
|
|
|
if m == http.MethodPost && p == "/api/skins" && code == http.StatusCreated {
|
|
|
|
key = "api.skins.post.success"
|
|
|
|
} else if m == http.MethodPost && p == "/api/skins" && code == http.StatusBadRequest {
|
|
|
|
key = "api.skins.post.validation_failed"
|
|
|
|
} else if m == http.MethodDelete && strings.HasPrefix(p, "/api/skins/") && code == http.StatusNoContent {
|
|
|
|
key = "api.skins.delete.success"
|
|
|
|
} else if m == http.MethodDelete && strings.HasPrefix(p, "/api/skins/") && code == http.StatusNotFound {
|
|
|
|
key = "api.skins.delete.not_found"
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-04 21:44:12 +05:30
|
|
|
s.IncCounter(key, 1)
|
2020-02-16 15:53:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StatsReporter) incCounterHandler(name string) func(...interface{}) {
|
|
|
|
return func(...interface{}) {
|
2020-04-04 21:44:12 +05:30
|
|
|
s.IncCounter(name, 1)
|
2020-02-16 15:53:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 15:00:06 +05:30
|
|
|
func (s *StatsReporter) startTimeRecording(timeKey string) {
|
2020-04-04 01:33:15 +05:30
|
|
|
s.timersMutex.Lock()
|
|
|
|
defer s.timersMutex.Unlock()
|
|
|
|
s.timersMap[timeKey] = time.Now()
|
2020-03-30 15:00:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StatsReporter) finalizeTimeRecording(timeKey string, statName string) {
|
2020-04-04 01:33:15 +05:30
|
|
|
s.timersMutex.Lock()
|
|
|
|
defer s.timersMutex.Unlock()
|
|
|
|
startedAt, ok := s.timersMap[timeKey]
|
2020-03-30 15:00:06 +05:30
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-04 01:33:15 +05:30
|
|
|
delete(s.timersMap, timeKey)
|
2020-03-30 15:00:06 +05:30
|
|
|
|
2020-04-04 21:44:12 +05:30
|
|
|
s.RecordTimer(statName, time.Since(startedAt))
|
2020-02-16 15:53:47 +05:30
|
|
|
}
|
2020-05-01 05:16:12 +05:30
|
|
|
|
|
|
|
type RedisPoolCheckable interface {
|
|
|
|
Avail() int
|
|
|
|
}
|
|
|
|
|
|
|
|
func AvailableRedisPoolSizeReporter(pool RedisPoolCheckable, d time.Duration, stop context.Context) ReporterFunc {
|
|
|
|
return func(reporter slf.StatsReporter) {
|
|
|
|
go func() {
|
|
|
|
ticker := time.NewTicker(d)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stop.Done():
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
reporter.UpdateGauge("redis.pool.available", int64(pool.Avail()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|