chore: Add Prometheus metrics for account registration and concurrent requests

feat: Implement Prometheus metrics and caching for game sessions

fix: Rename Prometheus metric for game modes to include namespace
pull/60/head
Frederico Santos 6 months ago
parent 5480c2ee72
commit 3048e22787

@ -27,6 +27,7 @@ import (
"github.com/pagefaultgames/rogueserver/api/account"
"github.com/pagefaultgames/rogueserver/api/daily"
"github.com/pagefaultgames/rogueserver/db"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func Init(mux *http.ServeMux) error {
@ -73,6 +74,7 @@ func Init(mux *http.ServeMux) error {
mux.HandleFunc("POST /admin/account/googleLink", handleAdminGoogleLink)
mux.HandleFunc("POST /admin/account/googleUnlink", handleAdminGoogleUnlink)
mux.HandleFunc("GET /admin/account/adminSearch", handleAdminSearch)
mux.Handle("GET /metrics", promhttp.Handler())
return nil
}

@ -34,6 +34,8 @@ import (
"github.com/pagefaultgames/rogueserver/api/savedata"
"github.com/pagefaultgames/rogueserver/db"
"github.com/pagefaultgames/rogueserver/defs"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
/*
@ -41,6 +43,14 @@ import (
Handler functions are responsible for checking the validity of this data and returning a result or error.
Handlers should not return serialized JSON, instead return the struct itself.
*/
var (
accountsRegistered = promauto.NewCounter(prometheus.CounterOpts{
Name: "rogueserver_accounts_registered",
Help: "The total number of accounts registered",
})
)
// account
func handleAccountInfo(w http.ResponseWriter, r *http.Request) {
@ -95,7 +105,7 @@ func handleAccountRegister(w http.ResponseWriter, r *http.Request) {
httpError(w, r, err, http.StatusInternalServerError)
return
}
accountsRegistered.Inc()
w.WriteHeader(http.StatusOK)
}

@ -0,0 +1,7 @@
package savedata
import (
"github.com/patrickmn/go-cache"
)
var Cache = cache.New(cache.NoExpiration, cache.NoExpiration)

@ -0,0 +1,16 @@
package savedata
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
gameModeCounter = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "rogueserver_game_mode_total",
Help: "The total number of classic sessions played per 5 minutes",
},
[]string{"gamemode"},
)
)

@ -20,6 +20,7 @@ package savedata
import (
"fmt"
"log"
"time"
"github.com/pagefaultgames/rogueserver/db"
"github.com/pagefaultgames/rogueserver/defs"
@ -27,6 +28,7 @@ import (
// /savedata/update - update save data
func Update(uuid []byte, slot int, save any) error {
err := db.UpdateAccountLastActivity(uuid)
if err != nil {
log.Print("failed to update account last activity")
@ -55,3 +57,26 @@ func Update(uuid []byte, slot int, save any) error {
return fmt.Errorf("invalid data type")
}
}
func ProcessSystemMetrics(save defs.SystemSaveData, uuid []byte) {
}
func ProcessSessionMetrics(save defs.SessionSaveData, uuid []byte) {
err := Cache.Add(fmt.Sprintf("session-%x-%d", uuid, save.GameMode), uuid, time.Minute*5)
if err != nil {
return
}
switch save.GameMode {
case 0:
gameModeCounter.WithLabelValues("classic").Inc()
case 1:
gameModeCounter.WithLabelValues("endless").Inc()
case 2:
gameModeCounter.WithLabelValues("spliced-endless").Inc()
case 3:
gameModeCounter.WithLabelValues("daily").Inc()
case 4:
gameModeCounter.WithLabelValues("challenge").Inc()
}
}

@ -10,10 +10,6 @@ services:
dbname: pokeroguedb
gameurl: http://localhost:8000
callbackurl: http://localhost:8001
AWS_ACCESS_KEY_ID: <access>
AWS_SECRET_ACCESS_KEY: <secret>
AWS_REGION: <region>
AWS_ENDPOINT_URL_S3: <endpoint>
depends_on:
db:
@ -60,6 +56,19 @@ services:
volumes:
- /etc/localtime:/etc/localtime:ro
- /var/run/docker.sock:/var/run/docker.sock
# Prometheus monitoring stack for the server and db services above
prometheus:
image: prom/prometheus:latest
restart: unless-stopped
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
command:
- '--config.file=/etc/prometheus/prometheus.yml'
networks:
- internal
volumes:
database:

@ -0,0 +1,51 @@
services:
server:
command: --debug --dbaddr db --dbuser pokerogue --dbpass pokerogue --dbname pokeroguedb
image: rogueserver:latest
restart: unless-stopped
depends_on:
db:
condition: service_healthy
networks:
- internal
ports:
- "8001:8001"
db:
image: mariadb:11
restart: unless-stopped
healthcheck:
test: [ "CMD", "healthcheck.sh", "--su-mysql", "--connect", "--innodb_initialized" ]
start_period: 10s
start_interval: 10s
interval: 1m
timeout: 5s
retries: 3
environment:
MYSQL_ROOT_PASSWORD: admin
MYSQL_DATABASE: pokeroguedb
MYSQL_USER: pokerogue
MYSQL_PASSWORD: pokerogue
volumes:
- database:/var/lib/mysql
networks:
- internal
# Prometheus monitoring stack for the server and db services above
prometheus:
image: prom/prometheus:latest
restart: unless-stopped
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
command:
- '--config.file=/etc/prometheus/prometheus.yml'
networks:
- internal
volumes:
database:
networks:
internal:

@ -15,6 +15,8 @@ require (
github.com/bwmarrin/discordgo v0.28.1
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/klauspost/compress v1.17.9
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/prometheus/client_golang v1.19.1
)
require (
@ -33,6 +35,12 @@ require (
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.2 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.32.2 // indirect
github.com/aws/smithy-go v1.22.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
golang.org/x/sys v0.19.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)

@ -34,16 +34,34 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.32.2 h1:CiS7i0+FUe+/YY1GvIBLLrR/XNGZ
github.com/aws/aws-sdk-go-v2/service/sts v1.32.2/go.mod h1:HtaiBI8CjYoNVde8arShXb94UbQQi9L4EMr6D+xGBwo=
github.com/aws/smithy-go v1.22.0 h1:uunKnWlcoL3zO7q+gG2Pk53joueEOsnNB28QdMsmiMM=
github.com/aws/smithy-go v1.22.0/go.mod h1:irrKGvNn1InZwb2d7fkIRNucdfwR8R+Ts3wxYa/cJHg=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4=
github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/prometheus/client_golang v1.19.1 h1:wZWJDwK+NameRJuPGDhlnFgx8e8HN3XHQeLaYJFJBOE=
github.com/prometheus/client_golang v1.19.1/go.mod h1:mP78NwGzrVks5S2H6ab8+ZZGJLZUq1hoULYBAYBw1Ho=
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE=
github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc=
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
@ -56,3 +74,5 @@ golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=

@ -0,0 +1,12 @@
global:
scrape_interval: "10s"
scrape_timeout: "10s"
evaluation_interval: "1m"
scrape_configs:
- job_name: 'rogueserver'
static_configs:
- targets: ['server:8001']
- job_name: 'mariadb'
static_configs:
- targets: ['db:3306']

@ -29,6 +29,15 @@ import (
"github.com/pagefaultgames/rogueserver/api"
"github.com/pagefaultgames/rogueserver/api/account"
"github.com/pagefaultgames/rogueserver/db"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
concurrentRequests = promauto.NewGauge(prometheus.GaugeOpts{
Name: "rogueserver_concurrent_requests",
Help: "The number of concurrent requests being handled",
})
)
func main() {
@ -141,7 +150,9 @@ func prodHandler(router *http.ServeMux, clienturl string) http.Handler {
return
}
concurrentRequests.Inc()
router.ServeHTTP(w, r)
concurrentRequests.Dec()
})
}
@ -156,7 +167,9 @@ func debugHandler(router *http.ServeMux) http.Handler {
return
}
concurrentRequests.Inc()
router.ServeHTTP(w, r)
concurrentRequests.Dec()
})
}

Loading…
Cancel
Save