diff --git a/.vscode/launch.json b/.vscode/launch.json index acea204..6bf7ab0 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -29,7 +29,7 @@ "env": { "DUMP_PATH": "../tmp/nodes.json", "CONFIG_PATH": "../tmp/config.json", - "NODE_DIR_PATH": "../tmp/node" + "NODE_PATH": "../tmp/node" }, "args": [ "node" diff --git a/api/main.go b/api/main.go index a84f08f..2ad664e 100644 --- a/api/main.go +++ b/api/main.go @@ -3,6 +3,7 @@ package api import ( "log" "net/http" + "strings" _ "gitea.ceperka.net/rosti/lobby2/docs" // This line is necessary for swag to find your docs! "gitea.ceperka.net/rosti/lobby2/nodes" @@ -53,6 +54,7 @@ func (a *API) Run() error { a.e.GET("/nodes", a.listHandler) a.e.GET("/nodes/:hostname", a.getHandler) a.e.POST("/nodes/:hostname", a.refreshHandler) + a.e.GET("/prometheus/:service", a.prometheusHandler) // Start the server in a goroutine so that it doesn't block the signal listening return a.e.Start(a.listen) @@ -111,3 +113,37 @@ func (a *API) refreshHandler(c echo.Context) error { return c.NoContent(http.StatusNoContent) } + +// @Summary Prometheus service discovery +// @Description Return one nodes based on given hostname +// @Produce application/json +// @Success 200 {array} []prometheusDiscovery "Node details" +// @Failure 401 {object} Message "Forbidden access" +// @Security Bearer +// @Router /nodes/{hostname} [get] +func (a *API) prometheusHandler(c echo.Context) error { + ss := c.QueryParam("service") + + ns := a.np.List() + + pds := []prometheusDiscovery{ + { + Labels: nodes.Labels{}, + Targets: []string{}, + }, + } + + for _, node := range ns { + v, ok := node.KV["prometheus_exporters"] + if ok { + services := strings.Split(v, ",") + for _, service := range services { + if ss == service { + pds[0].Targets = append(pds[0].Targets, node.HostName) + } + } + } + } + + return c.JSONPretty(http.StatusOK, pds, " ") +} diff --git a/api/types.go b/api/types.go index 42eb56d..3a3cfe3 100644 --- a/api/types.go +++ b/api/types.go @@ -10,3 +10,8 @@ type params struct { Labels nodes.Labels `json:"labels"` KV nodes.KV `json:"kv"` } + +type prometheusDiscovery struct { + Labels nodes.Labels `json:"labels"` + Targets []string `json:"targets"` +}