From 55b9fba8fe17779d34d3bf68421e47118a5249c3 Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Fri, 15 Feb 2019 08:36:48 +0100 Subject: [PATCH] more prometheus metrics --- server/mapblockaccessor/get.go | 7 +++++ server/mapblockaccessor/mtime.go | 3 ++ server/mapblockaccessor/prometheus.go | 38 ++++++++++++++++++++++ server/mapblockparser/parse.go | 5 +++ server/mapblockparser/prometheus.go | 24 ++++++++++++++ server/mapblockrenderer/prometheus.go | 24 ++++++++++++++ server/mapblockrenderer/renderer.go | 5 +++ server/tiledb/prometheus.go | 29 +++++++++++++++++ server/tiledb/tiledb.go | 11 +++++++ server/tilerenderer/prometheus.go | 25 +++++++++++++++ server/tilerenderer/renderer.go | 7 +++++ server/web/mapobjects.go | 4 +++ server/web/minetest.go | 3 ++ server/web/prometheus.go | 45 +++++++++++++++++++++++++++ server/web/tiles.go | 15 +++------ server/web/ws.go | 9 ------ 16 files changed, 235 insertions(+), 19 deletions(-) create mode 100644 server/mapblockaccessor/prometheus.go create mode 100644 server/mapblockparser/prometheus.go create mode 100644 server/mapblockrenderer/prometheus.go create mode 100644 server/tiledb/prometheus.go create mode 100644 server/tilerenderer/prometheus.go create mode 100644 server/web/prometheus.go diff --git a/server/mapblockaccessor/get.go b/server/mapblockaccessor/get.go index ea663b8..7647444 100644 --- a/server/mapblockaccessor/get.go +++ b/server/mapblockaccessor/get.go @@ -6,6 +6,7 @@ import ( "mapserver/mapblockparser" cache "github.com/patrickmn/go-cache" + "github.com/prometheus/client_golang/prometheus" ) func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapblockparser.MapBlock, error) { @@ -13,9 +14,15 @@ func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapblockpar cachedblock, found := a.c.Get(key) if found { + getCacheHitCount.Inc() return cachedblock.(*mapblockparser.MapBlock), nil } + getCacheMissCount.Inc() + + timer := prometheus.NewTimer(dbGetDuration) + defer timer.ObserveDuration() + block, err := a.accessor.GetBlock(pos) if err != nil { return nil, err diff --git a/server/mapblockaccessor/mtime.go b/server/mapblockaccessor/mtime.go index 331a947..d0f7b33 100644 --- a/server/mapblockaccessor/mtime.go +++ b/server/mapblockaccessor/mtime.go @@ -8,6 +8,7 @@ import ( cache "github.com/patrickmn/go-cache" "github.com/sirupsen/logrus" + "github.com/prometheus/client_golang/prometheus" ) type FindMapBlocksByMtimeResult struct { @@ -26,7 +27,9 @@ func (a *MapBlockAccessor) FindMapBlocksByMtime(lastmtime int64, limit int, laye } logrus.WithFields(fields).Debug("FindMapBlocksByMtime") + timer := prometheus.NewTimer(dbGetMtimeDuration) blocks, err := a.accessor.FindBlocksByMtime(lastmtime, limit) + timer.ObserveDuration() if err != nil { return nil, err diff --git a/server/mapblockaccessor/prometheus.go b/server/mapblockaccessor/prometheus.go new file mode 100644 index 0000000..d12b842 --- /dev/null +++ b/server/mapblockaccessor/prometheus.go @@ -0,0 +1,38 @@ +package mapblockaccessor + +import ( + "github.com/prometheus/client_golang/prometheus" +) + +var ( + getCacheHitCount = prometheus.NewCounter( + prometheus.CounterOpts{ + Name: "dbcache_hit_count", + Help: "Count of db cache hits", + }, + ) + getCacheMissCount = prometheus.NewCounter( + prometheus.CounterOpts{ + Name: "dbcache_miss_count", + Help: "Count of db cache miss", + }, + ) + dbGetDuration = prometheus.NewHistogram(prometheus.HistogramOpts{ + Name: "db_get_duration", + Help: "Histogram for db mapblock get durations", + Buckets: prometheus.LinearBuckets(0.001, 0.005, 10), + }) + dbGetMtimeDuration = prometheus.NewHistogram(prometheus.HistogramOpts{ + Name: "db_getmtime_duration", + Help: "Histogram for db mapblock get-by-mtime durations", + Buckets: prometheus.LinearBuckets(0.01, 0.01, 10), + }) +) + +func init() { + prometheus.MustRegister(getCacheHitCount) + prometheus.MustRegister(getCacheMissCount) + + prometheus.MustRegister(dbGetDuration) + prometheus.MustRegister(dbGetMtimeDuration) +} diff --git a/server/mapblockparser/parse.go b/server/mapblockparser/parse.go index 9fe866e..fbed23b 100644 --- a/server/mapblockparser/parse.go +++ b/server/mapblockparser/parse.go @@ -4,6 +4,7 @@ import ( "errors" "mapserver/coords" "strconv" + "github.com/prometheus/client_golang/prometheus" ) func Parse(data []byte, mtime int64, pos *coords.MapBlockCoords) (*MapBlock, error) { @@ -11,6 +12,9 @@ func Parse(data []byte, mtime int64, pos *coords.MapBlockCoords) (*MapBlock, err return nil, errors.New("no data") } + timer := prometheus.NewTimer(parseDuration) + defer timer.ObserveDuration() + mapblock := NewMapblock() mapblock.Mtime = mtime mapblock.Pos = pos @@ -96,5 +100,6 @@ func Parse(data []byte, mtime int64, pos *coords.MapBlockCoords) (*MapBlock, err mapblock.BlockMapping[nodeId] = blockName } + parsedMapBlocks.Inc() return mapblock, nil } diff --git a/server/mapblockparser/prometheus.go b/server/mapblockparser/prometheus.go new file mode 100644 index 0000000..30c147a --- /dev/null +++ b/server/mapblockparser/prometheus.go @@ -0,0 +1,24 @@ +package mapblockparser + +import ( + "github.com/prometheus/client_golang/prometheus" +) + +var ( + parsedMapBlocks = prometheus.NewCounter( + prometheus.CounterOpts{ + Name: "mapblocks_parsed_count", + Help: "Overall count of parsed mapblocks", + }, + ) + parseDuration = prometheus.NewHistogram(prometheus.HistogramOpts{ + Name: "mapblock_parse_time", + Help: "Histogram for mapblock parse timings", + Buckets: prometheus.LinearBuckets(0.001, 0.002, 10), + }) +) + +func init() { + prometheus.MustRegister(parsedMapBlocks) + prometheus.MustRegister(parseDuration) +} diff --git a/server/mapblockrenderer/prometheus.go b/server/mapblockrenderer/prometheus.go new file mode 100644 index 0000000..25a4b2e --- /dev/null +++ b/server/mapblockrenderer/prometheus.go @@ -0,0 +1,24 @@ +package mapblockrenderer + +import ( + "github.com/prometheus/client_golang/prometheus" +) + +var ( + renderedMapblocks = prometheus.NewCounter( + prometheus.CounterOpts{ + Name: "mapblock_rendered_count", + Help: "Overall count of rendered mapblocks", + }, + ) + renderDuration = prometheus.NewHistogram(prometheus.HistogramOpts{ + Name: "mapblock_render_time", + Help: "Histogram for mapblock render timings", + Buckets: prometheus.LinearBuckets(0.01, 0.02, 10), + }) +) + +func init() { + prometheus.MustRegister(renderedMapblocks) + prometheus.MustRegister(renderDuration) +} diff --git a/server/mapblockrenderer/renderer.go b/server/mapblockrenderer/renderer.go index 9769aa4..e7b349d 100644 --- a/server/mapblockrenderer/renderer.go +++ b/server/mapblockrenderer/renderer.go @@ -11,6 +11,7 @@ import ( "time" "github.com/sirupsen/logrus" + "github.com/prometheus/client_golang/prometheus" ) type MapBlockRenderer struct { @@ -79,6 +80,10 @@ func (r *MapBlockRenderer) Render(pos1, pos2 *coords.MapBlockCoords) (*image.NRG return nil, errors.New("Z does not line up") } + renderedMapblocks.Inc() + timer := prometheus.NewTimer(renderDuration) + defer timer.ObserveDuration() + start := time.Now() defer func() { t := time.Now() diff --git a/server/tiledb/prometheus.go b/server/tiledb/prometheus.go new file mode 100644 index 0000000..d7bfe68 --- /dev/null +++ b/server/tiledb/prometheus.go @@ -0,0 +1,29 @@ +package tiledb + +import ( + "github.com/prometheus/client_golang/prometheus" +) + +var ( + setDuration = prometheus.NewHistogram(prometheus.HistogramOpts{ + Name: "tiledb_set_duration", + Help: "Histogram for tiledb set timings", + Buckets: prometheus.LinearBuckets(0.001, 0.01, 10), + }) + getDuration = prometheus.NewHistogram(prometheus.HistogramOpts{ + Name: "tiledb_get_duration", + Help: "Histogram for tiledb get timings", + Buckets: prometheus.LinearBuckets(0.001, 0.01, 10), + }) + removeDuration = prometheus.NewHistogram(prometheus.HistogramOpts{ + Name: "tiledb_remove_duration", + Help: "Histogram for tiledb remove timings", + Buckets: prometheus.LinearBuckets(0.001, 0.01, 10), + }) +) + +func init() { + prometheus.MustRegister(setDuration) + prometheus.MustRegister(getDuration) + prometheus.MustRegister(removeDuration) +} diff --git a/server/tiledb/tiledb.go b/server/tiledb/tiledb.go index 9437bad..7b223a4 100644 --- a/server/tiledb/tiledb.go +++ b/server/tiledb/tiledb.go @@ -5,6 +5,8 @@ import ( "mapserver/coords" "github.com/dgraph-io/badger" + "github.com/prometheus/client_golang/prometheus" + ) func New(path string) (*TileDB, error) { @@ -40,6 +42,9 @@ func (this *TileDB) GC() { } func (this *TileDB) GetTile(pos *coords.TileCoords) ([]byte, error) { + timer := prometheus.NewTimer(getDuration) + defer timer.ObserveDuration() + var tile []byte err := this.db.View(func(txn *badger.Txn) error { item, err := txn.Get(getKey(pos)) @@ -56,6 +61,9 @@ func (this *TileDB) GetTile(pos *coords.TileCoords) ([]byte, error) { } func (this *TileDB) SetTile(pos *coords.TileCoords, tile []byte) error { + timer := prometheus.NewTimer(setDuration) + defer timer.ObserveDuration() + err := this.db.Update(func(txn *badger.Txn) error { err := txn.Set(getKey(pos), tile) return err @@ -65,6 +73,9 @@ func (this *TileDB) SetTile(pos *coords.TileCoords, tile []byte) error { } func (this *TileDB) RemoveTile(pos *coords.TileCoords) error { + timer := prometheus.NewTimer(removeDuration) + defer timer.ObserveDuration() + err := this.db.Update(func(txn *badger.Txn) error { err := txn.Delete(getKey(pos)) return err diff --git a/server/tilerenderer/prometheus.go b/server/tilerenderer/prometheus.go new file mode 100644 index 0000000..8be6c07 --- /dev/null +++ b/server/tilerenderer/prometheus.go @@ -0,0 +1,25 @@ +package tilerenderer + +import ( + "github.com/prometheus/client_golang/prometheus" +) + +var ( + renderedTiles = prometheus.NewCounterVec( + prometheus.CounterOpts{ + Name: "tiles_rendered_count", + Help: "Overall count of rendered tiles", + }, + []string{"zoom"}, + ) + renderDuration = prometheus.NewHistogram(prometheus.HistogramOpts{ + Name: "tiles_render_time", + Help: "Histogram for tile render timings", + Buckets: prometheus.LinearBuckets(0.01, 0.05, 10), + }) +) + +func init() { + prometheus.MustRegister(renderedTiles) + prometheus.MustRegister(renderDuration) +} diff --git a/server/tilerenderer/renderer.go b/server/tilerenderer/renderer.go index c50ee3d..898da2e 100644 --- a/server/tilerenderer/renderer.go +++ b/server/tilerenderer/renderer.go @@ -13,9 +13,12 @@ import ( "mapserver/mapblockrenderer" "mapserver/tiledb" "time" + "strconv" "github.com/disintegration/imaging" "github.com/sirupsen/logrus" + + "github.com/prometheus/client_golang/prometheus" ) type TileRenderer struct { @@ -95,6 +98,10 @@ func (tr *TileRenderer) RenderImage(tc *coords.TileCoords, recursionDepth int) ( log.WithFields(logrus.Fields{"x": tc.X, "y": tc.Y, "zoom": tc.Zoom}).Debug("RenderImage") + renderedTiles.With(prometheus.Labels{"zoom":strconv.Itoa(tc.Zoom)}).Inc() + timer := prometheus.NewTimer(renderDuration) + defer timer.ObserveDuration() + var layer *layer.Layer for _, l := range tr.layers { diff --git a/server/web/mapobjects.go b/server/web/mapobjects.go index 03a0c29..a2c9b66 100644 --- a/server/web/mapobjects.go +++ b/server/web/mapobjects.go @@ -8,6 +8,7 @@ import ( "net/http" "strconv" "strings" + "github.com/prometheus/client_golang/prometheus" ) type MapObjects struct { @@ -24,6 +25,9 @@ func (t *MapObjects) ServeHTTP(resp http.ResponseWriter, req *http.Request) { return } + timer := prometheus.NewTimer(mapobjectServeDuration) + defer timer.ObserveDuration() + x1, _ := strconv.Atoi(parts[0]) y1, _ := strconv.Atoi(parts[1]) z1, _ := strconv.Atoi(parts[2]) diff --git a/server/web/minetest.go b/server/web/minetest.go index ebf13cc..a1c4b4c 100644 --- a/server/web/minetest.go +++ b/server/web/minetest.go @@ -49,6 +49,9 @@ func (this *Minetest) ServeHTTP(resp http.ResponseWriter, req *http.Request) { return } + mintestPlayers.Set(float64(len(data.Players))) + mintestMaxLag.Set(data.MaxLag) + this.ctx.WebEventbus.Emit("minetest-info", data) json.NewEncoder(resp).Encode("stub") diff --git a/server/web/prometheus.go b/server/web/prometheus.go new file mode 100644 index 0000000..064c5f5 --- /dev/null +++ b/server/web/prometheus.go @@ -0,0 +1,45 @@ +package web + +import ( + "github.com/prometheus/client_golang/prometheus" +) + +var ( + tilesCumulativeSize = prometheus.NewCounter( + prometheus.CounterOpts{ + Name: "tiles_cumulative_size_served", + Help: "Overall sent bytes of tiles", + }, + ) + tileServeDuration = prometheus.NewHistogram(prometheus.HistogramOpts{ + Name: "tiles_serve_durations", + Help: "Histogram for tile serve timings", + Buckets: prometheus.LinearBuckets(0.005, 0.01, 10), + }) + mapobjectServeDuration = prometheus.NewHistogram(prometheus.HistogramOpts{ + Name: "mapobject_serve_durations", + Help: "Histogram for mapobject serve timings", + Buckets: prometheus.LinearBuckets(0.005, 0.01, 10), + }) + wsClients = prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "ws_client_count", + Help: "Websocket client count", + }) + mintestPlayers = prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "minetest_player_count", + Help: "game player count", + }) + mintestMaxLag = prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "minetest_max_lag", + Help: "Max lag", + }) +) + +func init() { + prometheus.MustRegister(tilesCumulativeSize) + prometheus.MustRegister(tileServeDuration) + prometheus.MustRegister(mapobjectServeDuration) + prometheus.MustRegister(wsClients) + prometheus.MustRegister(mintestPlayers) + prometheus.MustRegister(mintestMaxLag) +} diff --git a/server/web/tiles.go b/server/web/tiles.go index 1ea7e33..687ff88 100644 --- a/server/web/tiles.go +++ b/server/web/tiles.go @@ -8,18 +8,10 @@ import ( "net/http" "strconv" "strings" - "github.com/prometheus/client_golang/prometheus" ) -var ( - tilesCumulativeSize = prometheus.NewCounter( - prometheus.CounterOpts{ - Name: "tiles_cumulative_size_served", - Help: "Overall sent bytes of tiles", - }, - ) -) + type Tiles struct { ctx *app.App @@ -28,7 +20,6 @@ type Tiles struct { func (t *Tiles) Init() { t.blank = tilerenderer.CreateBlankTile(color.RGBA{255, 255, 255, 255}) - prometheus.MustRegister(tilesCumulativeSize) } func (t *Tiles) ServeHTTP(resp http.ResponseWriter, req *http.Request) { @@ -41,6 +32,10 @@ func (t *Tiles) ServeHTTP(resp http.ResponseWriter, req *http.Request) { return } + timer := prometheus.NewTimer(tileServeDuration) + defer timer.ObserveDuration() + + layerid, _ := strconv.Atoi(parts[0]) x, _ := strconv.Atoi(parts[1]) y, _ := strconv.Atoi(parts[2]) diff --git a/server/web/ws.go b/server/web/ws.go index 5278bf7..17c94c9 100644 --- a/server/web/ws.go +++ b/server/web/ws.go @@ -9,14 +9,6 @@ import ( "sync" "github.com/gorilla/websocket" - "github.com/prometheus/client_golang/prometheus" -) - -var ( - wsClients = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "ws_client_count", - Help: "Websocket client count", - }) ) type WS struct { @@ -30,7 +22,6 @@ func NewWS(ctx *app.App) *WS { ws := WS{} ws.mutex = &sync.RWMutex{} ws.channels = make(map[int]chan []byte) - prometheus.MustRegister(wsClients) return &ws }