add metrics to embedr service
This commit is contained in:
@@ -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",
|
||||||
|
|||||||
@@ -17,25 +17,30 @@ 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
|
||||||
xrpcc *xrpc.Client
|
metricsHttpd *http.Server
|
||||||
dir identity.Directory
|
xrpcc *xrpc.Client
|
||||||
|
dir identity.Directory
|
||||||
}
|
}
|
||||||
|
|
||||||
func serve(cctx *cli.Context) error {
|
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,13 +70,28 @@ 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
|
||||||
//
|
//
|
||||||
server := &Server{
|
server := &Server{
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user