2020-02-16 15:53:47 +05:30
|
|
|
package eventsubscribers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-12-14 06:45:59 +05:30
|
|
|
// TODO: rework all reporters in the same style as it was there: https://github.com/elyby/chrly/blob/1543e98b/di/db.go#L48-L52
|
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"))
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|