An ATproto social media client -- with an independent Appview.

add metrics to embedr service

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