Compare commits

..

4 commits
v2 ... main

Author SHA1 Message Date
48a7c17448
Fix labels
All checks were successful
Tests / test (push) Successful in 12s
2024-12-22 13:43:31 +01:00
ae6ca5c185
Support for Prometheus SD
All checks were successful
Tests / test (push) Successful in 12s
2024-12-22 13:40:50 +01:00
5dd38b7f18
Prometheus service discovery
All checks were successful
Tests / test (push) Successful in 20s
2024-12-19 21:10:34 +01:00
0a1b9c1305
CGO_ENABLED to zero
All checks were successful
Tests / test (push) Successful in 11s
2024-12-11 01:05:45 +01:00
7 changed files with 95 additions and 2 deletions

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
tmp/ tmp/
__debug* __debug*
main main
bin/
nodes.json

2
.vscode/launch.json vendored
View file

@ -29,7 +29,7 @@
"env": { "env": {
"DUMP_PATH": "../tmp/nodes.json", "DUMP_PATH": "../tmp/nodes.json",
"CONFIG_PATH": "../tmp/config.json", "CONFIG_PATH": "../tmp/config.json",
"NODE_DIR_PATH": "../tmp/node" "NODE_PATH": "../tmp/node.json"
}, },
"args": [ "args": [
"node" "node"

View file

@ -19,4 +19,9 @@ tasks:
cmds: cmds:
- go mod tidy - go mod tidy
- mkdir -p bin - mkdir -p bin
- go build -o bin/lobby2-{{ .VERSION }}-linux-amd64 cli/*.go - env CGO_ENABLED=0 go build -o bin/lobby2-{{ .VERSION }}-linux-amd64 cli/*.go
deploy:
cmds:
- task: build
- scp bin/lobby2-{{ .VERSION }}-linux-amd64 rosti-db:/usr/local/bin/lobby2.tmp
- ssh rosti-db mv /usr/local/bin/lobby2.tmp /usr/local/bin/lobby2

View file

@ -53,6 +53,7 @@ func (a *API) Run() error {
a.e.GET("/nodes", a.listHandler) a.e.GET("/nodes", a.listHandler)
a.e.GET("/nodes/:hostname", a.getHandler) a.e.GET("/nodes/:hostname", a.getHandler)
a.e.POST("/nodes/:hostname", a.refreshHandler) 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 // Start the server in a goroutine so that it doesn't block the signal listening
return a.e.Start(a.listen) return a.e.Start(a.listen)
@ -111,3 +112,18 @@ func (a *API) refreshHandler(c echo.Context) error {
return c.NoContent(http.StatusNoContent) 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 /prometheus/{service} [get]
func (a *API) prometheusHandler(c echo.Context) error {
ss := c.Param("service")
pds := nodes.GetPrometheusSD(a.np, ss)
return c.JSONPretty(http.StatusOK, pds, " ")
}

View file

@ -52,3 +52,23 @@ func printAction(c *cli.Context) error {
return nil return nil
} }
func prometheusAction(c *cli.Context) error {
cfg := GetConfig()
np := nodes.NewNodesProcessor(cfg.DumpPath, cfg.DropAfterSeconds)
pds := nodes.GetPrometheusSD(np, "node")
for _, pd := range pds {
body, err := json.MarshalIndent(pd, "", " ")
if err != nil {
fmt.Printf("failed to marshal prometheus discovery: %v\n", err)
os.Exit(1)
}
fmt.Println(string(body))
}
return nil
}

View file

@ -27,6 +27,11 @@ func main() {
Usage: "Prints all discovered nodes", Usage: "Prints all discovered nodes",
Action: printAction, Action: printAction,
}, },
{
Name: "prometheus",
Usage: "Prints Prometheus Service Discovery",
Action: prometheusAction,
},
}, },
} }

45
nodes/prometheus.go Normal file
View file

@ -0,0 +1,45 @@
package nodes
import (
"fmt"
"strings"
)
type prometheusDiscovery struct {
Labels map[string]string `json:"Labels"`
Targets []string `json:"Targets"`
}
func GetPrometheusSD(p *NodesProcessor, ss string) []prometheusDiscovery {
ns := p.List()
pds := []prometheusDiscovery{
{
Labels: make(map[string]string),
Targets: []string{},
},
}
for _, node := range ns {
port, ok := node.KV["prometheus_port"]
if !ok {
port = "9999"
}
host, ok := node.KV["prometheus_host"]
if !ok {
host = node.HostName
}
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, fmt.Sprintf("%s:%s", host, port))
}
}
}
}
return pds
}