Добавлено логгирование метрик для системы скинов

This commit is contained in:
ErickSkrauch 2016-12-03 01:57:55 +03:00
parent a0d940f8cd
commit 66c61dc3cd
9 changed files with 59 additions and 28 deletions

View File

@ -62,13 +62,16 @@ func (s *SkinItem) Delete() {
func FindSkinByUsername(username string) (SkinItem, error) {
var record SkinItem;
services.Logger.IncCounter("skins.storage.query", 1)
response := services.RedisPool.Cmd("GET", tools.BuildKey(username));
if (response.IsType(redis.Nil)) {
services.Logger.IncCounter("skins.storage.not_found", 1)
return record, SkinNotFound{username}
}
result, err := response.Str()
if (err == nil) {
services.Logger.IncCounter("skins.storage.found", 1)
decodeErr := json.Unmarshal([]byte(result), &record)
if (decodeErr != nil) {
log.Println("Cannot decode record data")

View File

@ -2,18 +2,21 @@ package routes
import (
"io"
"log"
"net/http"
"github.com/gorilla/mux"
"elyby/minecraft-skinsystem/lib/tools"
"elyby/minecraft-skinsystem/lib/data"
"elyby/minecraft-skinsystem/lib/services"
)
func Cape(response http.ResponseWriter, request *http.Request) {
if (mux.Vars(request)["converted"] == "") {
services.Logger.IncCounter("capes.request", 1)
}
username := tools.ParseUsername(mux.Vars(request)["username"])
log.Println("request cape for username " + username)
rec, err := data.FindCapeByUsername(username)
if (err != nil) {
http.Redirect(response, request, "http://skins.minecraft.net/MinecraftCloaks/" + username + ".png", 301)
@ -24,6 +27,7 @@ func Cape(response http.ResponseWriter, request *http.Request) {
}
func CapeGET(w http.ResponseWriter, r *http.Request) {
services.Logger.IncCounter("capes.get-request", 1)
username := r.URL.Query().Get("name")
if username == "" {
w.WriteHeader(http.StatusBadRequest)
@ -31,5 +35,6 @@ func CapeGET(w http.ResponseWriter, r *http.Request) {
}
mux.Vars(r)["username"] = username
mux.Vars(r)["converted"] = "1"
Cape(w, r)
}

View File

@ -1,7 +1,6 @@
package routes
import (
"log"
"net/http"
"github.com/gorilla/mux"
@ -14,7 +13,6 @@ const defaultHash = "default"
func Face(w http.ResponseWriter, r *http.Request) {
username := tools.ParseUsername(mux.Vars(r)["username"])
log.Println("request skin for username " + username);
rec, err := data.FindSkinByUsername(username)
var hash string
if (err != nil || rec.SkinId == 0) {

View File

@ -4,6 +4,8 @@ import (
"net/http"
"github.com/gorilla/mux"
"elyby/minecraft-skinsystem/lib/services"
)
// Метод-наследие от первой версии системы скинов.
@ -18,11 +20,15 @@ func MinecraftPHP(w http.ResponseWriter, r *http.Request) {
}
mux.Vars(r)["username"] = username
mux.Vars(r)["converted"] = "1"
switch required {
case "skin": Skin(w, r)
case "cloack": Cape(w, r)
default: {
case "skin":
services.Logger.IncCounter("skins.minecraft-php-request", 1)
Skin(w, r)
case "cloack":
services.Logger.IncCounter("capes.minecraft-php-request", 1)
Cape(w, r)
default:
w.WriteHeader(http.StatusNotFound)
}
}
}

View File

@ -1,18 +1,21 @@
package routes
import (
"log"
"net/http"
"github.com/gorilla/mux"
"elyby/minecraft-skinsystem/lib/tools"
"elyby/minecraft-skinsystem/lib/data"
"elyby/minecraft-skinsystem/lib/services"
)
func Skin(w http.ResponseWriter, r *http.Request) {
if (mux.Vars(r)["converted"] == "") {
services.Logger.IncCounter("skins.request", 1)
}
username := tools.ParseUsername(mux.Vars(r)["username"])
log.Println("request skin for username " + username);
rec, err := data.FindSkinByUsername(username)
if (err != nil) {
http.Redirect(w, r, "http://skins.minecraft.net/MinecraftSkins/" + username + ".png", 301)
@ -23,6 +26,7 @@ func Skin(w http.ResponseWriter, r *http.Request) {
}
func SkinGET(w http.ResponseWriter, r *http.Request) {
services.Logger.IncCounter("skins.get-request", 1)
username := r.URL.Query().Get("name")
if username == "" {
w.WriteHeader(http.StatusBadRequest)
@ -30,5 +34,6 @@ func SkinGET(w http.ResponseWriter, r *http.Request) {
}
mux.Vars(r)["username"] = username
mux.Vars(r)["converted"] = "1"
Skin(w, r)
}

View File

@ -13,9 +13,8 @@ import (
)
func Textures(w http.ResponseWriter, r *http.Request) {
services.Stats.Incr("textures-requests", 1)
services.Logger.IncCounter("textures.request", 1)
username := tools.ParseUsername(mux.Vars(r)["username"])
log.Println("request textures for username " + username)
rec, err := data.FindSkinByUsername(username)
if (err != nil || rec.SkinId == 0) {

View File

@ -4,7 +4,7 @@ import (
"github.com/mediocregopher/radix.v2/pool"
"github.com/streadway/amqp"
"github.com/gorilla/mux"
"github.com/quipo/statsd"
"github.com/mono83/slf/wd"
)
var Router *mux.Router
@ -15,4 +15,4 @@ var RabbitMQChannel *amqp.Channel
var RootFolder string
var Stats *statsd.StatsdBuffer
var Logger wd.Watchdog

View File

@ -2,11 +2,12 @@ package worker
import (
"elyby/minecraft-skinsystem/lib/data"
"log"
"elyby/minecraft-skinsystem/lib/services"
)
func handleChangeUsername(model usernameChanged) (bool) {
if (model.OldUsername == "") {
services.Logger.IncCounter("worker.change_username.empty_old_username")
record := data.SkinItem{
UserId: model.AccountId,
Username: model.NewUsername,
@ -19,7 +20,7 @@ func handleChangeUsername(model usernameChanged) (bool) {
record, err := data.FindSkinByUsername(model.OldUsername)
if (err != nil) {
log.Println("Exit by not found record")
services.Logger.IncCounter("worker.change_username.username_not_found")
// TODO: я не уверен, что это валидное поведение
// Суть в том, что здесь может возникнуть ошибка в том случае, если записи в базе нету
// а значит его нужно, как минимум, зарегистрировать
@ -29,7 +30,7 @@ func handleChangeUsername(model usernameChanged) (bool) {
record.Username = model.NewUsername
record.Save()
log.Println("all saved!")
services.Logger.IncCounter("worker.change_username.processed")
return true
}
@ -37,6 +38,7 @@ func handleChangeUsername(model usernameChanged) (bool) {
func handleSkinChanged(model skinChanged) (bool) {
record, err := data.FindSkinById(model.AccountId)
if (err != nil) {
services.Logger.IncCounter("worker.skin_changed.id_not_found")
return true
}
@ -48,5 +50,7 @@ func handleSkinChanged(model skinChanged) (bool) {
record.Save()
services.Logger.IncCounter("worker.skin_changed.processed")
return true
}

View File

@ -11,8 +11,10 @@ import (
"github.com/gorilla/mux"
"github.com/streadway/amqp"
"github.com/mediocregopher/radix.v2/pool"
"github.com/quipo/statsd"
//"github.com/mediocregopher/radix.v2/redis"
"github.com/mono83/slf/wd"
"github.com/mono83/slf/rays"
"github.com/mono83/slf/recievers/ansi"
"github.com/mono83/slf/recievers/statsd"
"elyby/minecraft-skinsystem/lib/routes"
"elyby/minecraft-skinsystem/lib/services"
@ -58,16 +60,25 @@ func main() {
}
log.Println("Connected to rabbitmq channel")
// init
statsClient := statsd.NewStatsdClient(statsString, "skinsystem.")
statsErr := statsClient.CreateSocket()
if statsErr != nil {
log.Fatal(statsErr)
// statsd
var statsdString = os.Getenv("STATSD_ADDR")
if (statsdString != "") {
hostname, _ := os.Hostname()
statsdReceiver, err := statsd.NewReceiver(statsd.Config{
Address: statsdString,
Prefix: "ely.skinsystem." + hostname + ".",
FlushEvery: 2,
})
if (err != nil) {
log.Fatal("statsd connection error")
}
interval := 2 * time.Second // aggregate stats and flush every 2 seconds
stats := statsd.NewStatsdBuffer(interval, statsClient)
defer stats.Close()
wd.AddReceiver(statsdReceiver)
} else {
wd.AddReceiver(ansi.New(true, true, false))
}
logger := wd.New("", "").WithParams(rays.Host)
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/skins/{username}", routes.Skin).Methods("GET").Name("skins")
@ -88,7 +99,7 @@ func main() {
services.Router = router
services.RedisPool = redisPool
services.RabbitMQChannel = rabbitChannel
services.Stats = stats
services.Logger = logger
_, file, _, _ := runtime.Caller(0)
services.RootFolder = filepath.Dir(file)