add metrics to embedr service

This commit is contained in:
Austin McKinley
2025-07-29 17:36:58 -07:00
parent 332dbc1bb4
commit ea226da275
2 changed files with 43 additions and 9 deletions
+7
View File
@@ -46,6 +46,13 @@ func run(args []string) {
Value: ":8100", Value: ":8100",
EnvVars: []string{"HTTP_ADDRESS"}, 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{ &cli.BoolFlag{
Name: "debug", Name: "debug",
Usage: "Enable debug mode", Usage: "Enable debug mode",
+29 -2
View File
@@ -17,17 +17,21 @@ import (
"github.com/bluesky-social/indigo/util/cliutil" "github.com/bluesky-social/indigo/util/cliutil"
"github.com/bluesky-social/indigo/xrpc" "github.com/bluesky-social/indigo/xrpc"
"github.com/bluesky-social/social-app/bskyweb" "github.com/bluesky-social/social-app/bskyweb"
"github.com/prometheus/client_golang/prometheus/promhttp"
"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/gzhttp"
"github.com/klauspost/compress/gzip" "github.com/klauspost/compress/gzip"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
type Server struct { type Server struct {
echo *echo.Echo echo *echo.Echo
httpd *http.Server httpd *http.Server
metricsHttpd *http.Server
xrpcc *xrpc.Client xrpcc *xrpc.Client
dir identity.Directory dir identity.Directory
} }
@@ -36,6 +40,7 @@ func serve(cctx *cli.Context) error {
debug := cctx.Bool("debug") debug := cctx.Bool("debug")
httpAddress := cctx.String("http-address") httpAddress := cctx.String("http-address")
appviewHost := cctx.String("appview-host") appviewHost := cctx.String("appview-host")
metricsAddress := cctx.String("metrics-address")
// Echo // Echo
e := echo.New() e := echo.New()
@@ -65,6 +70,20 @@ func serve(cctx *cli.Context) error {
return err 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
// //
@@ -72,6 +91,7 @@ func serve(cctx *cli.Context) error {
echo: e, echo: e,
xrpcc: xrpcc, xrpcc: xrpcc,
dir: identity.DefaultDirectory(), dir: identity.DefaultDirectory(),
metricsHttpd: metricsHttpd,
} }
// Create the HTTP server. // Create the HTTP server.
@@ -132,6 +152,8 @@ func serve(cctx *cli.Context) error {
RedirectCode: http.StatusFound, RedirectCode: http.StatusFound,
})) }))
e.Use(echoprometheus.NewMiddleware(""))
// //
// configure routes // configure routes
// //
@@ -220,6 +242,11 @@ func (srv *Server) Shutdown() error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() defer cancel()
// Shutdown metrics server too
if srv.metricsHttpd != nil {
srv.metricsHttpd.Shutdown(ctx)
}
return srv.httpd.Shutdown(ctx) return srv.httpd.Shutdown(ctx)
} }