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

@@ -19,6 +19,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/util"
)
@@ -199,9 +200,12 @@ func startServer() {
api.SetLogger(logger)
api.SetupRoutes(router)
api.SetupMetrics(router)
fmt.Println("Starting server...")
go func() {
metrics.SetHealty(true)
err := router.Run(fmt.Sprintf("%v:%v", options.HTTPHost, options.HTTPPort))
if err != nil {
fmt.Println("Cant start server:", err)
@@ -213,6 +217,18 @@ func startServer() {
}()
}
func startMetricsServer() {
serverAddr := fmt.Sprintf("%v:%v", command.Opts.HTTPHost, command.Opts.HTTPPort)
if options.MetricsAddr == serverAddr {
return
}
err := metrics.StartServer(logger, options.MetricsPath, options.MetricsAddr)
if err != nil {
logger.WithError(err).Fatal("unable to start prometheus metrics server")
}
}
func handleSignals() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
@@ -265,6 +281,10 @@ func Run() {
}
}
if options.MetricsEnabled && options.MetricsAddr != "" {
go startMetricsServer()
}
startServer()
openPage()
handleSignals()