From 611c4c7e4cd82449c934d168339e50c2f53cef63 Mon Sep 17 00:00:00 2001 From: Mathieu Lecarme Date: Tue, 1 Mar 2022 11:45:29 +0100 Subject: [PATCH 1/5] Naive HTTP Admin --- application/application.go | 27 ++++++++++++++++++++++++++- application/home.go | 27 +++++++++++++++++++-------- conf/conf.go | 1 + main.go | 7 +++++++ 4 files changed, 53 insertions(+), 9 deletions(-) diff --git a/application/application.go b/application/application.go index 39b2f55..cb6f81c 100644 --- a/application/application.go +++ b/application/application.go @@ -39,12 +39,14 @@ type Application struct { Domain string GitlabURL string Router http.Handler + AdminRouter http.Handler storage storage.Storage volumes *volumes.Volumes logger *zap.Logger queue *queue.Queue Sink events.Sink Server *http.Server + AdminServer *http.Server Stopper chan (os.Signal) } @@ -77,6 +79,14 @@ func New(cfg *conf.Conf) (*Application, error) { logger.Info("There is no Sentry set") } + ar := chi.NewRouter() + ar.Use(middleware.Logger) + ar.Use(middleware.Recoverer) + ar.Get("/", AdminHomeHandler) + ar.Get("/metrics", promhttp.Handler().ServeHTTP) + ar.Get("/robots.txt", RobotsHandler) + ar.Get("/favicon.png", FaviconHandler) + sessions := sessions.New() err = sessions.Start(15) if err != nil { @@ -117,6 +127,7 @@ func New(cfg *conf.Conf) (*Application, error) { serviceFolder: cfg.Services, storage: s, Router: MagicPathHandler(r), + AdminRouter: ar, volumes: v, logger: logger, queue: &q, @@ -137,7 +148,6 @@ func New(cfg *conf.Conf) (*Application, error) { r.Get("/", a.HomeHandler()) r.Get("/robots.txt", RobotsHandler) r.Get("/favicon.png", FaviconHandler) - r.Get("/metrics", promhttp.Handler().ServeHTTP) r.Get("/oauth/callback", oauth.CallbackHandler(&cfg.OAuth, &sessions)) r.Get("/services", a.ServicesHandler) @@ -266,6 +276,21 @@ func (a *Application) Run(listen string) error { return nil } +func (a *Application) AdminRun(listen string) error { + a.AdminServer = &http.Server{ + Addr: listen, + Handler: a.AdminRouter, + } + + // start and serve + go func() { + if err := a.AdminServer.ListenAndServe(); err != nil && err != http.ErrServerClosed { + a.logger.Fatal("fatal error when running server", zap.Error(err)) + } + }() + return nil +} + // Shutdown the server and put running tasks into interrupted state func (a *Application) Shutdown() error { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) diff --git a/application/home.go b/application/home.go index 66d58ad..1ebf879 100644 --- a/application/home.go +++ b/application/home.go @@ -33,6 +33,15 @@ func acceptsHTML(r *http.Request) bool { return false } +const logo = ` + _ _ _ + ___| |__ ___ ___| | __ _ __ ___ _ _ __ _____| |__ +/ __| '_ \ / _ \/ __| |/ / | '_ ' _ \| | | | \ \ /\ / / _ \ '_ \ +| (__| | | | __/ (__| < | | | | | | |_| | \ V V / __/ |_) | +\ ___|_| |_|\___|\___|_|\_\ |_| |_| |_|\__, | \_/\_/ \___|_.__/ + |___/ + ` + // HomeHandler display the home page func (a *Application) HomeHandler() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { @@ -44,14 +53,7 @@ func (a *Application) HomeHandler() http.HandlerFunc { // nice geeky ascii art if !acceptsHTML(r) { w.Header().Set("content-type", "text/plain") - w.Write([]byte(` - _ _ _ - ___| |__ ___ ___| | __ _ __ ___ _ _ __ _____| |__ -/ __| '_ \ / _ \/ __| |/ / | '_ ' _ \| | | | \ \ /\ / / _ \ '_ \ -| (__| | | | __/ (__| < | | | | | | |_| | \ V V / __/ |_) | -\ ___|_| |_|\___|\___|_|\_\ |_| |_| |_|\__, | \_/\_/ \___|_.__/ - |___/ - `)) + w.Write([]byte(logo)) fmt.Fprintf(w, "Version: %s", version.Version()) return } @@ -72,3 +74,12 @@ func (a *Application) HomeHandler() http.HandlerFunc { } } } + +func AdminHomeHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("content-type", "text/plain") + w.Write([]byte(logo)) + fmt.Fprintf(w, "Version: %s", version.Version()) + w.Write([]byte(` +/metrics Prometheus export +`)) +} diff --git a/conf/conf.go b/conf/conf.go index b2573cc..845b68b 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -12,6 +12,7 @@ type Conf struct { Services string `yaml:"services"` // Service folder JWKProvider string `yaml:"jwk_provider"` Listen string `yaml:"listen"` // http listen address + AdminListen string `yaml:"admin_listen"` DataPath string `yaml:"data_path"` Hosts []string `yaml:"hosts"` } diff --git a/main.go b/main.go index 4878b75..b9d7246 100644 --- a/main.go +++ b/main.go @@ -57,6 +57,13 @@ func main() { l.Error("Run", zap.Error(err)) os.Exit(1) } + if cfg.AdminListen != "" { + err = a.AdminRun(cfg.AdminListen) + if err != nil { + l.Error("AdminRun", zap.Error(err)) + os.Exit(1) + } + } <-a.Stopper From 65d7f2deb7b2c97fc6b58f4361d6461ab6d8ae7e Mon Sep 17 00:00:00 2001 From: Mathieu Lecarme Date: Tue, 1 Mar 2022 11:57:07 +0100 Subject: [PATCH 2/5] More linux. --- Makefile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Makefile b/Makefile index baf0036..9b53913 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,10 @@ build: -ldflags "-X github.com/factorysh/microdensity/version.version=$(GIT_VERSION)" \ . +build-linux: + make build GOOS=linux + upx microdensity + TESTS= github.com/factorysh/microdensity/task \ github.com/factorysh/microdensity/middlewares/jwt \ github.com/factorysh/microdensity/middlewares/project\ From bced5515ad0b075b96973308a494374804d9f9df Mon Sep 17 00:00:00 2001 From: Mathieu Lecarme Date: Tue, 1 Mar 2022 11:57:41 +0100 Subject: [PATCH 3/5] Default admin url --- main.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index b9d7246..da8dca0 100644 --- a/main.go +++ b/main.go @@ -57,12 +57,13 @@ func main() { l.Error("Run", zap.Error(err)) os.Exit(1) } - if cfg.AdminListen != "" { - err = a.AdminRun(cfg.AdminListen) - if err != nil { - l.Error("AdminRun", zap.Error(err)) - os.Exit(1) - } + if cfg.AdminListen == "" { + cfg.AdminListen = "127.0.0.1:3615" + } + err = a.AdminRun(cfg.AdminListen) + if err != nil { + l.Error("AdminRun", zap.Error(err)) + os.Exit(1) } <-a.Stopper From 34a15f76438eb5592cf2a90c1e81739c8fefc2d5 Mon Sep 17 00:00:00 2001 From: Mathieu Lecarme Date: Tue, 1 Mar 2022 13:48:39 +0100 Subject: [PATCH 4/5] Documenting hosts configuration. --- conf/conf.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conf/conf.go b/conf/conf.go index 845b68b..306f0f1 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -14,7 +14,7 @@ type Conf struct { Listen string `yaml:"listen"` // http listen address AdminListen string `yaml:"admin_listen"` DataPath string `yaml:"data_path"` - Hosts []string `yaml:"hosts"` + Hosts []string `yaml:"hosts"` // private hostnames for exposing private services, like browserless } func (c *Conf) Defaults() { From cd287397ed49a8e844e2c5eb2e12d2021ca2c1d4 Mon Sep 17 00:00:00 2001 From: Mathieu Lecarme Date: Tue, 1 Mar 2022 14:07:55 +0100 Subject: [PATCH 5/5] Count runs. --- run/run.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/run/run.go b/run/run.go index 88a7868..796fa53 100644 --- a/run/run.go +++ b/run/run.go @@ -8,6 +8,15 @@ import ( "github.com/factorysh/microdensity/task" "github.com/factorysh/microdensity/volumes" "github.com/google/uuid" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" +) + +var ( + serviceRun = promauto.NewCounterVec(prometheus.CounterOpts{ + Name: "run_total", + Help: "Total tasks run", + }, []string{"service", "project"}) ) // Context is a run context, with a STDOUT and a STDERR @@ -86,6 +95,8 @@ func (r *Runner) Run(t *task.Task) (int, error) { if !found { return 0, fmt.Errorf("task with id `%s` not found in runner", t.Id) } - + defer serviceRun.With(prometheus.Labels{ + "service": t.Service, + "project": t.Project}).Inc() return ctx.run.Run(ctx.Stdout, ctx.Stderr) }