diff --git a/.gitignore b/.gitignore index 26ea98b..5e37f6b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ tmp/ __debug* main +bin/ +nodes.json diff --git a/.vscode/launch.json b/.vscode/launch.json index 6bf7ab0..534cd3d 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_PATH": "../tmp/node" + "NODE_PATH": "../tmp/node.json" }, "args": [ "node" diff --git a/Taskfile.yml b/Taskfile.yml index 0cafd84..0495b8d 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -20,3 +20,8 @@ tasks: - go mod tidy - mkdir -p bin - 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 diff --git a/api/main.go b/api/main.go index 2ad664e..8f9a411 100644 --- a/api/main.go +++ b/api/main.go @@ -3,7 +3,6 @@ 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" @@ -120,30 +119,11 @@ func (a *API) refreshHandler(c echo.Context) error { // @Success 200 {array} []prometheusDiscovery "Node details" // @Failure 401 {object} Message "Forbidden access" // @Security Bearer -// @Router /nodes/{hostname} [get] +// @Router /prometheus/{service} [get] func (a *API) prometheusHandler(c echo.Context) error { - ss := c.QueryParam("service") + ss := c.Param("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) - } - } - } - } + pds := nodes.GetPrometheusSD(a.np, ss) return c.JSONPretty(http.StatusOK, pds, " ") } diff --git a/api/types.go b/api/types.go index 3a3cfe3..42eb56d 100644 --- a/api/types.go +++ b/api/types.go @@ -10,8 +10,3 @@ 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"` -} diff --git a/cli/actions.go b/cli/actions.go index 134b4d4..7ae949b 100644 --- a/cli/actions.go +++ b/cli/actions.go @@ -52,3 +52,23 @@ func printAction(c *cli.Context) error { 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 +} diff --git a/cli/main.go b/cli/main.go index 5457dd3..32f6ee5 100644 --- a/cli/main.go +++ b/cli/main.go @@ -27,6 +27,11 @@ func main() { Usage: "Prints all discovered nodes", Action: printAction, }, + { + Name: "prometheus", + Usage: "Prints Prometheus Service Discovery", + Action: prometheusAction, + }, }, } diff --git a/nodes/prometheus.go b/nodes/prometheus.go new file mode 100644 index 0000000..e83fe01 --- /dev/null +++ b/nodes/prometheus.go @@ -0,0 +1,45 @@ +package nodes + +import ( + "fmt" + "strings" +) + +type prometheusDiscovery struct { + Labels Labels `json:"Labels"` + Targets []string `json:"Targets"` +} + +func GetPrometheusSD(p *NodesProcessor, ss string) []prometheusDiscovery { + ns := p.List() + + pds := []prometheusDiscovery{ + { + Labels: Labels{}, + 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 +}