more prometheus metrics

This commit is contained in:
NatureFreshMilk 2019-02-15 08:36:48 +01:00
parent 32bc5900d4
commit 55b9fba8fe
16 changed files with 235 additions and 19 deletions

View File

@ -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

View File

@ -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

View File

@ -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)
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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()

View File

@ -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)
}

View File

@ -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

View File

@ -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)
}

View File

@ -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 {

View File

@ -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])

View File

@ -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")

45
server/web/prometheus.go Normal file
View File

@ -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)
}

View File

@ -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])

View File

@ -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
}