tangled mirror of catsky-🐱 Soothing soft social-app fork with all the niche toggles! (Unofficial); for issues and PRs please put them on github:NekoDrone/catsky-social

add metrics to embedr service

Changed files
+43 -9
bskyweb
cmd
+7
bskyweb/cmd/embedr/main.go
··· 46 46 Value: ":8100", 47 47 EnvVars: []string{"HTTP_ADDRESS"}, 48 48 }, 49 + &cli.StringFlag{ 50 + Name: "metrics-address", 51 + Usage: "Specify the local IP/port to bind the metrics server to", 52 + Required: false, 53 + Value: ":9090", 54 + EnvVars: []string{"METRICS_HTTP_ADDRESS"}, 55 + }, 49 56 &cli.BoolFlag{ 50 57 Name: "debug", 51 58 Usage: "Enable debug mode",
+36 -9
bskyweb/cmd/embedr/server.go
··· 17 17 "github.com/bluesky-social/indigo/util/cliutil" 18 18 "github.com/bluesky-social/indigo/xrpc" 19 19 "github.com/bluesky-social/social-app/bskyweb" 20 + "github.com/prometheus/client_golang/prometheus/promhttp" 20 21 21 - "github.com/klauspost/compress/gzhttp" 22 - "github.com/klauspost/compress/gzip" 22 + "github.com/labstack/echo-contrib/echoprometheus" 23 23 "github.com/labstack/echo/v4" 24 24 "github.com/labstack/echo/v4/middleware" 25 + 26 + "github.com/klauspost/compress/gzhttp" 27 + "github.com/klauspost/compress/gzip" 25 28 "github.com/urfave/cli/v2" 26 29 ) 27 30 28 31 type Server struct { 29 - echo *echo.Echo 30 - httpd *http.Server 31 - xrpcc *xrpc.Client 32 - dir identity.Directory 32 + echo *echo.Echo 33 + httpd *http.Server 34 + metricsHttpd *http.Server 35 + xrpcc *xrpc.Client 36 + dir identity.Directory 33 37 } 34 38 35 39 func serve(cctx *cli.Context) error { 36 40 debug := cctx.Bool("debug") 37 41 httpAddress := cctx.String("http-address") 38 42 appviewHost := cctx.String("appview-host") 43 + metricsAddress := cctx.String("metrics-address") 39 44 40 45 // Echo 41 46 e := echo.New() ··· 65 70 return err 66 71 } 67 72 73 + metricsMux := http.NewServeMux() 74 + metricsMux.Handle("/metrics", promhttp.Handler()) 75 + 76 + metricsHttpd := &http.Server{ 77 + Addr: metricsAddress, 78 + Handler: metricsMux, 79 + } 80 + 81 + go func() { 82 + if err := metricsHttpd.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { 83 + log.Error("failed to start metrics server", "error", err) 84 + } 85 + }() 86 + 68 87 // 69 88 // server 70 89 // 71 90 server := &Server{ 72 - echo: e, 73 - xrpcc: xrpcc, 74 - dir: identity.DefaultDirectory(), 91 + echo: e, 92 + xrpcc: xrpcc, 93 + dir: identity.DefaultDirectory(), 94 + metricsHttpd: metricsHttpd, 75 95 } 76 96 77 97 // Create the HTTP server. ··· 131 151 e.Use(middleware.RemoveTrailingSlashWithConfig(middleware.TrailingSlashConfig{ 132 152 RedirectCode: http.StatusFound, 133 153 })) 154 + 155 + e.Use(echoprometheus.NewMiddleware("")) 134 156 135 157 // 136 158 // configure routes ··· 219 241 220 242 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) 221 243 defer cancel() 244 + 245 + // Shutdown metrics server too 246 + if srv.metricsHttpd != nil { 247 + srv.metricsHttpd.Shutdown(ctx) 248 + } 222 249 223 250 return srv.httpd.Shutdown(ctx) 224 251 }