Setup basic prom metrics endpoint (#624)

* Setup basic prom metrics endpoint
* Use default prom handler to expose go runtime metrics
This commit is contained in:
Dan Sosedoff
2022-12-20 10:13:42 -06:00
committed by GitHub
parent 837e25be74
commit b31e7f1ea7
9 changed files with 143 additions and 16 deletions

View File

@@ -16,6 +16,7 @@ import (
"github.com/sosedoff/pgweb/pkg/client"
"github.com/sosedoff/pgweb/pkg/command"
"github.com/sosedoff/pgweb/pkg/connection"
"github.com/sosedoff/pgweb/pkg/metrics"
"github.com/sosedoff/pgweb/pkg/shared"
"github.com/sosedoff/pgweb/static"
)
@@ -278,6 +279,12 @@ func Disconnect(c *gin.Context) {
return
}
if command.Opts.Sessions {
result := DbSessions.Remove(getSessionId(c.Request))
successResponse(c, gin.H{"success": result})
return
}
conn := DB(c)
if conn == nil {
badRequest(c, errNotConnected)
@@ -492,6 +499,8 @@ func GetTableConstraints(c *gin.Context) {
// HandleQuery runs the database query
func HandleQuery(query string, c *gin.Context) {
metrics.IncrementQueriesCount()
rawQuery, err := base64.StdEncoding.DecodeString(desanitize64(query))
if err == nil {
query = string(rawQuery)

View File

@@ -4,6 +4,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/sosedoff/pgweb/pkg/command"
"github.com/sosedoff/pgweb/pkg/metrics"
)
func SetupMiddlewares(group *gin.RouterGroup) {
@@ -53,3 +54,9 @@ func SetupRoutes(router *gin.Engine) {
api.GET("/bookmarks", GetBookmarks)
api.GET("/export", DataExport)
}
func SetupMetrics(engine *gin.Engine) {
if command.Opts.MetricsEnabled && command.Opts.MetricsAddr == "" {
engine.GET("/metrics", gin.WrapH(metrics.Handler()))
}
}

View File

@@ -7,6 +7,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/sosedoff/pgweb/pkg/client"
"github.com/sosedoff/pgweb/pkg/metrics"
)
type SessionManager struct {
@@ -58,8 +59,10 @@ func (m *SessionManager) Get(id string) *client.Client {
func (m *SessionManager) Add(id string, conn *client.Client) {
m.mu.Lock()
defer m.mu.Unlock()
m.sessions[id] = conn
m.mu.Unlock()
metrics.SetSessionsCount(len(m.sessions))
}
func (m *SessionManager) Remove(id string) bool {
@@ -72,6 +75,7 @@ func (m *SessionManager) Remove(id string) bool {
delete(m.sessions, id)
}
metrics.SetSessionsCount(len(m.sessions))
return ok
}