Add Prometheus metrics

This commit is contained in:
Jacob Gunther
2022-03-24 04:02:37 -05:00
parent baed581d69
commit d9feff6c4b
10 changed files with 598 additions and 7 deletions

View File

@@ -3,6 +3,7 @@ package main
import (
"fmt"
"log"
"net/http"
"os"
"strconv"
"time"
@@ -13,14 +14,26 @@ import (
"github.com/mineatar-io/api-server/src/redis"
"github.com/mineatar-io/api-server/src/routes"
"github.com/mineatar-io/api-server/src/util"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/valyala/fasthttp"
)
var (
host string = "127.0.0.1"
port uint16 = 3000
config *conf.Configuration = &conf.Configuration{}
r *redis.Redis = &redis.Redis{}
host string = "127.0.0.1"
port uint16 = 3000
prometheusHost string = "127.0.0.1"
prometheusPort uint16 = 3001
config *conf.Configuration = &conf.Configuration{}
r *redis.Redis = &redis.Redis{}
)
var (
requestCountMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "request_count",
Help: "The total number of requests received",
})
)
func init() {
@@ -42,6 +55,11 @@ func init() {
if value, ok := os.LookupEnv("HOST"); ok {
host = value
prometheusHost = value
}
if value, ok := os.LookupEnv("PROM_HOST"); ok {
prometheusHost = value
}
if value, ok := os.LookupEnv("PORT"); ok {
@@ -52,6 +70,17 @@ func init() {
}
port = uint16(parsedValue)
prometheusPort = uint16(parsedValue + 1)
}
if value, ok := os.LookupEnv("PROM_PORT"); ok {
parsedValue, err := strconv.ParseUint(value, 10, 16)
if err != nil {
log.Fatal(err)
}
prometheusPort = uint16(parsedValue)
}
routes.Init(r, config)
@@ -69,10 +98,19 @@ func middleware(next fasthttp.RequestHandler) fasthttp.RequestHandler {
log.Printf("%s %s (%s) - %s\n", ctx.Method(), ctx.URI(), ctx.RemoteAddr(), ctx.UserAgent())
}
requestCountMetric.Inc()
next(ctx)
}
}
func metrics() {
log.Printf("Prometheus listening on %s:%d\n", prometheusHost, prometheusPort)
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(fmt.Sprintf("%s:%d", prometheusHost, prometheusPort), nil)
}
func main() {
defer r.Close()
@@ -91,5 +129,7 @@ func main() {
log.Printf("Listening on %s:%d\n", host, port)
go metrics()
log.Fatal(fasthttp.ListenAndServe(fmt.Sprintf("%s:%d", host, port), middleware(router.Handler)))
}

View File

@@ -8,6 +8,27 @@ import (
"time"
"github.com/go-redis/redis/v8"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
redisGetMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "redis_get_count",
Help: "The amount of Redis GET requests",
})
redisSetMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "redis_set_count",
Help: "The amount of Redis SET requests",
})
redisExistsMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "redis_exists_count",
Help: "The amount of Redis EXIST requests",
})
redisDeleteMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "redis_delete_count",
Help: "The amount of Redis DELETE requests",
})
)
type Redis struct {
@@ -30,6 +51,8 @@ func (r *Redis) Connect(uri string, database int) error {
}
func (r *Redis) GetString(key string) (string, bool, error) {
redisGetMetric.Inc()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
@@ -50,6 +73,8 @@ func (r *Redis) GetString(key string) (string, bool, error) {
}
func (r *Redis) GetBytes(key string) ([]byte, bool, error) {
redisGetMetric.Inc()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
@@ -76,6 +101,8 @@ func (r *Redis) GetBytes(key string) ([]byte, bool, error) {
}
func (r *Redis) GetNRGBA(key string) (*image.NRGBA, bool, error) {
redisGetMetric.Inc()
value, ok, err := r.GetBytes(key)
if err != nil {
@@ -104,6 +131,8 @@ func (r *Redis) GetNRGBA(key string) (*image.NRGBA, bool, error) {
}
func (r *Redis) Exists(key string) (bool, error) {
redisExistsMetric.Inc()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
@@ -114,6 +143,8 @@ func (r *Redis) Exists(key string) (bool, error) {
}
func (r *Redis) Delete(key string) error {
redisDeleteMetric.Inc()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
@@ -122,6 +153,8 @@ func (r *Redis) Delete(key string) error {
}
func (r *Redis) Set(key string, value interface{}, ttl time.Duration) error {
redisSetMetric.Inc()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

View File

@@ -7,9 +7,34 @@ import (
"github.com/mineatar-io/api-server/src/util"
"github.com/mineatar-io/skin-render"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/valyala/fasthttp"
)
var (
renderFullBodyMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "full_body_render_count",
Help: "The amount of full body renders",
})
renderFrontBodyMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "front_body_render_count",
Help: "The amount of front body renders",
})
renderBackBodyMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "back_body_render_count",
Help: "The amount of back body renders",
})
renderLeftBodyMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "left_body_render_count",
Help: "The amount of left body renders",
})
renderRightBodyMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "right_body_render_count",
Help: "The amount of right body renders",
})
)
func FullBodyHandler(ctx *fasthttp.RequestCtx) {
user := ctx.UserValue("user").(string)
@@ -85,6 +110,8 @@ func FullBodyHandler(ctx *fasthttp.RequestCtx) {
log.Printf("Rendered full body image for '%s'\n", uuid)
}
renderFullBodyMetric.Inc()
data, err := util.EncodePNG(render)
if err != nil {
@@ -189,6 +216,8 @@ func FrontBodyHandler(ctx *fasthttp.RequestCtx) {
log.Printf("Rendered front body image for '%s'\n", uuid)
}
renderFrontBodyMetric.Inc()
data, err := util.EncodePNG(render)
if err != nil {
@@ -293,6 +322,8 @@ func BackBodyHandler(ctx *fasthttp.RequestCtx) {
log.Printf("Rendered back body image for '%s'\n", uuid)
}
renderBackBodyMetric.Inc()
data, err := util.EncodePNG(render)
if err != nil {
@@ -397,6 +428,8 @@ func LeftBodyHandler(ctx *fasthttp.RequestCtx) {
log.Printf("Rendered left body image for '%s'\n", uuid)
}
renderLeftBodyMetric.Inc()
data, err := util.EncodePNG(render)
if err != nil {
@@ -501,6 +534,8 @@ func RightBodyHandler(ctx *fasthttp.RequestCtx) {
log.Printf("Rendered right body image for '%s'\n", uuid)
}
renderRightBodyMetric.Inc()
data, err := util.EncodePNG(render)
if err != nil {

View File

@@ -7,9 +7,18 @@ import (
"github.com/mineatar-io/api-server/src/util"
"github.com/mineatar-io/skin-render"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/valyala/fasthttp"
)
var (
renderFaceMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "face_render_count",
Help: "The amount of face renders",
})
)
func FaceHandler(ctx *fasthttp.RequestCtx) {
user := ctx.UserValue("user").(string)
@@ -85,6 +94,8 @@ func FaceHandler(ctx *fasthttp.RequestCtx) {
log.Printf("Rendered face image for '%s'\n", uuid)
}
renderFaceMetric.Inc()
data, err := util.EncodePNG(render)
if err != nil {

View File

@@ -7,9 +7,18 @@ import (
"github.com/mineatar-io/api-server/src/util"
"github.com/mineatar-io/skin-render"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/valyala/fasthttp"
)
var (
renderHeadMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "head_render_count",
Help: "The amount of head renders",
})
)
func HeadHandler(ctx *fasthttp.RequestCtx) {
user := ctx.UserValue("user").(string)
@@ -85,6 +94,8 @@ func HeadHandler(ctx *fasthttp.RequestCtx) {
log.Printf("Rendered head image for '%s'\n", uuid)
}
renderHeadMetric.Inc()
data, err := util.EncodePNG(render)
if err != nil {

View File

@@ -1,9 +1,20 @@
package routes
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/valyala/fasthttp"
)
var (
pingRequestMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "ping_request_count",
Help: "The amount of ping requests",
})
)
func PingHandler(ctx *fasthttp.RequestCtx) {
pingRequestMetric.Inc()
ctx.SetBodyString("Pong!")
}

View File

@@ -6,9 +6,18 @@ import (
"net/http"
"github.com/mineatar-io/api-server/src/util"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/valyala/fasthttp"
)
var (
requestRawSkinMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "raw_skin_request_count",
Help: "The amount of raw skin requests",
})
)
func SkinHandler(ctx *fasthttp.RequestCtx) {
user := ctx.UserValue("user").(string)
@@ -58,6 +67,8 @@ func SkinHandler(ctx *fasthttp.RequestCtx) {
ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
}
requestRawSkinMetric.Inc()
ctx.SetContentType("image/png")
ctx.SetBody(data)
}

View File

@@ -5,9 +5,18 @@ import (
"net/http"
"github.com/mineatar-io/api-server/src/util"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/valyala/fasthttp"
)
var (
uuidLookupMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "uuid_lookup_request_count",
Help: "The amount of UUID lookup requests",
})
)
func UUIDHandler(ctx *fasthttp.RequestCtx) {
user := ctx.UserValue("user").(string)
@@ -22,6 +31,8 @@ func UUIDHandler(ctx *fasthttp.RequestCtx) {
return
}
uuidLookupMetric.Inc()
if !ok {
ctx.SetStatusCode(404)
ctx.SetBodyString(http.StatusText(http.StatusNotFound))