diff --git a/handlers.go b/handlers.go index 83259cc..a7c0e6c 100644 --- a/handlers.go +++ b/handlers.go @@ -1,9 +1,11 @@ package main import ( + "fmt" "io/ioutil" "log" "net/http" + "os" "github.com/labstack/echo" "github.com/rosti-cz/node-api/apps" @@ -485,3 +487,39 @@ func getAppProcessesHandler(c echo.Context) error { return c.JSON(http.StatusOK, processes) } + +// Return metrics that are available for running node +func metricsHandler(c echo.Context) error { + var metrics string + + // Node indexes + node, err := node.GetNodeInfo() + if err != nil { + return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) + } + + hostname, err := os.Hostname() + if err != nil { + return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) + } + + metrics += fmt.Sprintf("rosti_node_index{hostname=\"%s\"} %f\n", hostname, node.Index) + metrics += fmt.Sprintf("rosti_node_disk_space_index{hostname=\"%s\"} %f\n", hostname, node.DiskSpaceIndex) + metrics += fmt.Sprintf("rosti_node_load1_index{hostname=\"%s\"} %f\n", hostname, node.Load1Index) + metrics += fmt.Sprintf("rosti_node_load5_index{hostname=\"%s\"} %f\n", hostname, node.Load5Index) + metrics += fmt.Sprintf("rosti_node_load15_index{hostname=\"%s\"} %f\n", hostname, node.Load15Index) + 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 := apps.List() + if err != nil { + return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) + } + + for _, app := range *apps { + metrics += fmt.Sprintf("rosti_node_app_disk_usage_bytes{hostname=\"%s\", app=\"%s\"} %d\n", hostname, app.Name, app.DiskUsageBytes) + metrics += fmt.Sprintf("rosti_node_app_disk_usage_inodes{hostname=\"%s\", app=\"%s\"} %d\n", hostname, app.Name, app.DiskUsageInodes) + } + + return c.String(http.StatusOK, metrics) +} diff --git a/main.go b/main.go index 853f3a4..a3e5fbc 100644 --- a/main.go +++ b/main.go @@ -55,6 +55,9 @@ func main() { // UI e.GET("/", homeHandler) + // Prometheus metrics + e.GET("/metrics", metricsHandler) + // Returns list of apps e.GET("/v1/apps", listAppsHandler)