From 68121fda1570448eeb39b9723f34edec4b3b7fc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C5=A0trauch?= Date: Fri, 6 May 2022 18:40:22 +0200 Subject: [PATCH] Making metrics faster --- glue/main.go | 27 +++++++++++++++------------ handlers.go | 4 ++-- handlers_nats.go | 2 +- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/glue/main.go b/glue/main.go index b626ff9..9025e6f 100644 --- a/glue/main.go +++ b/glue/main.go @@ -102,24 +102,27 @@ func (p *Processor) waitForApp() error { } // List returns list of apps -func (p *Processor) List() (apps.Apps, error) { +// noUpdate skips stats gathering to speed things up +func (p *Processor) List(noUpdate bool) (apps.Apps, error) { appList := apps.Apps{} - statsProcessor := StatsProcessor{ - DB: p.DB, - DockerSock: p.DockerSock, - BindIPHTTP: p.BindIPHTTP, - BindIPSSH: p.BindIPSSH, - AppsPath: p.AppsPath, - } + if !noUpdate { + statsProcessor := StatsProcessor{ + DB: p.DB, + DockerSock: p.DockerSock, + BindIPHTTP: p.BindIPHTTP, + BindIPSSH: p.BindIPSSH, + AppsPath: p.AppsPath, + } - err := statsProcessor.GatherStates() - if err != nil { - return appList, fmt.Errorf("backend error: %v", err) + err := statsProcessor.GatherStates() + if err != nil { + return appList, fmt.Errorf("backend error: %v", err) + } } processor := p.getAppProcessor() - appList, err = processor.List() + appList, err := processor.List() if err != nil { return appList, fmt.Errorf("backend error: %v", err) diff --git a/handlers.go b/handlers.go index 5d7abd2..e4ded65 100644 --- a/handlers.go +++ b/handlers.go @@ -29,7 +29,7 @@ func listAppsHandler(c echo.Context) error { AppsPath: config.AppsPath, } - applications, err := processor.List() + applications, err := processor.List(false) if err != nil { return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) } @@ -430,7 +430,7 @@ func metricsHandler(c echo.Context) error { metrics += fmt.Sprintf("rosti_node_memory_index{hostname=\"%s\"} %f\n", hostname, node.MemoryIndex) metrics += fmt.Sprintf("rosti_node_sold_memory{hostname=\"%s\"} %d\n", hostname, node.SoldMemory) - apps, err := processor.List() + apps, err := processor.List(true) if err != nil { return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) } diff --git a/handlers_nats.go b/handlers_nats.go index 01f5a69..321623c 100644 --- a/handlers_nats.go +++ b/handlers_nats.go @@ -101,7 +101,7 @@ func listEventHandler(m *nats.Msg, message *RequestMessage) error { AppsPath: config.AppsPath, } - applications, err := processor.List() + applications, err := processor.List(false) if err != nil { return errorReplyFormater(m, "backend error", err) }