Compare commits

..

No commits in common. "main" and "v5" have entirely different histories.
main ... v5

8 changed files with 29 additions and 81 deletions

2
.gitignore vendored
View file

@ -1,5 +1,3 @@
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_PATH": "../tmp/node.json" "NODE_PATH": "../tmp/node"
}, },
"args": [ "args": [
"node" "node"

View file

@ -20,8 +20,3 @@ tasks:
- go mod tidy - go mod tidy
- mkdir -p bin - mkdir -p bin
- env CGO_ENABLED=0 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

@ -3,6 +3,7 @@ package api
import ( import (
"log" "log"
"net/http" "net/http"
"strings"
_ "gitea.ceperka.net/rosti/lobby2/docs" // This line is necessary for swag to find your docs! _ "gitea.ceperka.net/rosti/lobby2/docs" // This line is necessary for swag to find your docs!
"gitea.ceperka.net/rosti/lobby2/nodes" "gitea.ceperka.net/rosti/lobby2/nodes"
@ -119,11 +120,30 @@ func (a *API) refreshHandler(c echo.Context) error {
// @Success 200 {array} []prometheusDiscovery "Node details" // @Success 200 {array} []prometheusDiscovery "Node details"
// @Failure 401 {object} Message "Forbidden access" // @Failure 401 {object} Message "Forbidden access"
// @Security Bearer // @Security Bearer
// @Router /prometheus/{service} [get] // @Router /nodes/{hostname} [get]
func (a *API) prometheusHandler(c echo.Context) error { func (a *API) prometheusHandler(c echo.Context) error {
ss := c.Param("service") ss := c.QueryParam("service")
pds := nodes.GetPrometheusSD(a.np, ss) 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, " ") return c.JSONPretty(http.StatusOK, pds, " ")
} }

View file

@ -10,3 +10,8 @@ type params struct {
Labels nodes.Labels `json:"labels"` Labels nodes.Labels `json:"labels"`
KV nodes.KV `json:"kv"` KV nodes.KV `json:"kv"`
} }
type prometheusDiscovery struct {
Labels nodes.Labels `json:"labels"`
Targets []string `json:"targets"`
}

View file

@ -52,23 +52,3 @@ 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,11 +27,6 @@ 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,
},
}, },
} }

View file

@ -1,45 +0,0 @@
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
}