about summary refs log tree commit diff
path: root/bskyweb
diff options
context:
space:
mode:
authorAustin McKinley <austin@blueskyweb.xyz>2025-07-29 17:36:58 -0700
committerAustin McKinley <austin@blueskyweb.xyz>2025-07-29 17:36:58 -0700
commitea226da2754d70b50ef9bfa88daaf3dee77fc8fd (patch)
treebe8623c8c5a4f05f40982c0c990f8fefc561d6b6 /bskyweb
parent332dbc1bb4632e9fd1ffed6ea8566b8df8984f64 (diff)
downloadvoidsky-ea226da2754d70b50ef9bfa88daaf3dee77fc8fd.tar.zst
add metrics to embedr service
Diffstat (limited to 'bskyweb')
-rw-r--r--bskyweb/cmd/embedr/main.go7
-rw-r--r--bskyweb/cmd/embedr/server.go45
2 files changed, 43 insertions, 9 deletions
diff --git a/bskyweb/cmd/embedr/main.go b/bskyweb/cmd/embedr/main.go
index 9f75ed69a..4a6be142b 100644
--- a/bskyweb/cmd/embedr/main.go
+++ b/bskyweb/cmd/embedr/main.go
@@ -46,6 +46,13 @@ func run(args []string) {
 					Value:    ":8100",
 					EnvVars:  []string{"HTTP_ADDRESS"},
 				},
+				&cli.StringFlag{
+					Name:     "metrics-address",
+					Usage:    "Specify the local IP/port to bind the metrics server to",
+					Required: false,
+					Value:    ":9090",
+					EnvVars:  []string{"METRICS_HTTP_ADDRESS"},
+				},
 				&cli.BoolFlag{
 					Name:     "debug",
 					Usage:    "Enable debug mode",
diff --git a/bskyweb/cmd/embedr/server.go b/bskyweb/cmd/embedr/server.go
index a9a8ab57f..07c25afad 100644
--- a/bskyweb/cmd/embedr/server.go
+++ b/bskyweb/cmd/embedr/server.go
@@ -17,25 +17,30 @@ import (
 	"github.com/bluesky-social/indigo/util/cliutil"
 	"github.com/bluesky-social/indigo/xrpc"
 	"github.com/bluesky-social/social-app/bskyweb"
+	"github.com/prometheus/client_golang/prometheus/promhttp"
 
-	"github.com/klauspost/compress/gzhttp"
-	"github.com/klauspost/compress/gzip"
+	"github.com/labstack/echo-contrib/echoprometheus"
 	"github.com/labstack/echo/v4"
 	"github.com/labstack/echo/v4/middleware"
+
+	"github.com/klauspost/compress/gzhttp"
+	"github.com/klauspost/compress/gzip"
 	"github.com/urfave/cli/v2"
 )
 
 type Server struct {
-	echo  *echo.Echo
-	httpd *http.Server
-	xrpcc *xrpc.Client
-	dir   identity.Directory
+	echo         *echo.Echo
+	httpd        *http.Server
+	metricsHttpd *http.Server
+	xrpcc        *xrpc.Client
+	dir          identity.Directory
 }
 
 func serve(cctx *cli.Context) error {
 	debug := cctx.Bool("debug")
 	httpAddress := cctx.String("http-address")
 	appviewHost := cctx.String("appview-host")
+	metricsAddress := cctx.String("metrics-address")
 
 	// Echo
 	e := echo.New()
@@ -65,13 +70,28 @@ func serve(cctx *cli.Context) error {
 		return err
 	}
 
+	metricsMux := http.NewServeMux()
+	metricsMux.Handle("/metrics", promhttp.Handler())
+
+	metricsHttpd := &http.Server{
+		Addr:    metricsAddress,
+		Handler: metricsMux,
+	}
+
+	go func() {
+		if err := metricsHttpd.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
+			log.Error("failed to start metrics server", "error", err)
+		}
+	}()
+
 	//
 	// server
 	//
 	server := &Server{
-		echo:  e,
-		xrpcc: xrpcc,
-		dir:   identity.DefaultDirectory(),
+		echo:         e,
+		xrpcc:        xrpcc,
+		dir:          identity.DefaultDirectory(),
+		metricsHttpd: metricsHttpd,
 	}
 
 	// Create the HTTP server.
@@ -132,6 +152,8 @@ func serve(cctx *cli.Context) error {
 		RedirectCode: http.StatusFound,
 	}))
 
+	e.Use(echoprometheus.NewMiddleware(""))
+
 	//
 	// configure routes
 	//
@@ -220,6 +242,11 @@ func (srv *Server) Shutdown() error {
 	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
 	defer cancel()
 
+	// Shutdown metrics server too
+	if srv.metricsHttpd != nil {
+		srv.metricsHttpd.Shutdown(ctx)
+	}
+
 	return srv.httpd.Shutdown(ctx)
 }