Prometheus metrics

This commit is contained in:
Adam Štrauch 2021-04-02 18:27:57 +02:00
parent 8eb1f47f2c
commit b8007500e7
Signed by: cx
GPG Key ID: 018304FFA8988F8D
2 changed files with 41 additions and 0 deletions

View File

@ -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)
}

View File

@ -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)