Prometheus service discovery
All checks were successful
Tests / test (push) Successful in 20s

This commit is contained in:
Adam Štrauch 2024-12-19 21:10:34 +01:00
parent 0a1b9c1305
commit 5dd38b7f18
Signed by: cx
GPG key ID: 7262DAFE292BCE20
3 changed files with 42 additions and 1 deletions

2
.vscode/launch.json vendored
View file

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

View file

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

View file

@ -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"`
}