diff --git a/api/common.go b/api/common.go index 0f93ca4..0bf4be4 100644 --- a/api/common.go +++ b/api/common.go @@ -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 } diff --git a/api/endpoints.go b/api/endpoints.go index 478b434..57263ca 100644 --- a/api/endpoints.go +++ b/api/endpoints.go @@ -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) } diff --git a/api/savedata/cache.go b/api/savedata/cache.go new file mode 100644 index 0000000..e0d30b0 --- /dev/null +++ b/api/savedata/cache.go @@ -0,0 +1,7 @@ +package savedata + +import ( + "github.com/patrickmn/go-cache" +) + +var Cache = cache.New(cache.NoExpiration, cache.NoExpiration) diff --git a/api/savedata/prometheus.go b/api/savedata/prometheus.go new file mode 100644 index 0000000..385a4d5 --- /dev/null +++ b/api/savedata/prometheus.go @@ -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"}, + ) +) diff --git a/api/savedata/update.go b/api/savedata/update.go index f4e633c..64f20f4 100644 --- a/api/savedata/update.go +++ b/api/savedata/update.go @@ -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() + } +} diff --git a/docker-compose.Example.yml b/docker-compose.Example.yml index 289e074..1f1e5aa 100644 --- a/docker-compose.Example.yml +++ b/docker-compose.Example.yml @@ -10,10 +10,6 @@ services: dbname: pokeroguedb gameurl: http://localhost:8000 callbackurl: http://localhost:8001 - AWS_ACCESS_KEY_ID: - AWS_SECRET_ACCESS_KEY: - AWS_REGION: - AWS_ENDPOINT_URL_S3: 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: diff --git a/docker-compose.Local.yml b/docker-compose.Local.yml new file mode 100644 index 0000000..0bbc06f --- /dev/null +++ b/docker-compose.Local.yml @@ -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: diff --git a/go.mod b/go.mod index c21b2ee..eae4151 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 6f2e494..f37136c 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/prometheus.yml b/prometheus.yml new file mode 100644 index 0000000..c264869 --- /dev/null +++ b/prometheus.yml @@ -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'] \ No newline at end of file diff --git a/rogueserver.go b/rogueserver.go index b69631d..0263828 100644 --- a/rogueserver.go +++ b/rogueserver.go @@ -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() }) }