2021-09-01 21:18:52 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2021-09-06 22:46:48 +00:00
|
|
|
"github.com/by-cx/lobby/server"
|
2021-09-01 21:18:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// [
|
|
|
|
// {
|
|
|
|
// "targets": [ "<host>", ... ],
|
|
|
|
// "labels": {
|
|
|
|
// "<labelname>": "<labelvalue>", ...
|
|
|
|
// }
|
|
|
|
// },
|
|
|
|
// ...
|
|
|
|
// ]
|
|
|
|
|
|
|
|
// PrometheusServices holds multiple PrometheusService structs
|
|
|
|
type PrometheusServices []PrometheusService
|
|
|
|
|
|
|
|
// PrometheusService represents a single set of targets and labels for Prometheus
|
|
|
|
type PrometheusService struct {
|
|
|
|
Targets []string
|
|
|
|
Labels map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// preparePrometheusOutput returns PrometheusServices which is struct compatible to what Prometheus expects
|
|
|
|
// labels starting "ne:" will be used as NodeExporter labels. Label "ne:port:9123" will be used as port
|
|
|
|
// used in the targets field. Same for "ne:host:1.2.3.4".
|
2021-09-02 21:05:57 +00:00
|
|
|
func preparePrometheusOutput(name string, discoveries []server.Discovery) PrometheusServices {
|
2021-09-01 21:18:52 +00:00
|
|
|
services := PrometheusServices{}
|
|
|
|
|
|
|
|
for _, discovery := range discoveries {
|
|
|
|
port := strconv.Itoa(int(config.NodeExporterPort))
|
|
|
|
host := discovery.Hostname
|
2021-09-02 21:05:57 +00:00
|
|
|
var add bool // add to the prometheus output when there is at least one prometheus related label
|
2021-09-01 21:18:52 +00:00
|
|
|
|
|
|
|
labels := map[string]string{}
|
|
|
|
|
2021-09-20 14:10:52 +00:00
|
|
|
for _, label := range discovery.FindLabelsByPrefix("prometheus:" + name + ":") {
|
2021-09-04 12:16:14 +00:00
|
|
|
trimmed := strings.TrimPrefix(label.String(), "prometheus:"+name+":")
|
2021-09-01 21:18:52 +00:00
|
|
|
parts := strings.SplitN(trimmed, ":", 2)
|
|
|
|
if len(parts) == 2 {
|
|
|
|
if parts[0] == "port" {
|
|
|
|
port = parts[1]
|
|
|
|
} else if parts[0] == "host" {
|
|
|
|
host = parts[1]
|
|
|
|
} else {
|
|
|
|
labels[parts[0]] = parts[1]
|
|
|
|
}
|
2021-09-02 21:05:57 +00:00
|
|
|
add = true
|
2021-09-01 21:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-02 21:05:57 +00:00
|
|
|
// This has to be checked here again because FindLabels adds : at the end of the label name.
|
|
|
|
if !add {
|
|
|
|
for _, label := range discovery.Labels {
|
2021-09-04 12:16:14 +00:00
|
|
|
if label.String() == "prometheus:"+name {
|
2021-09-02 21:05:57 +00:00
|
|
|
add = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-09-01 21:18:52 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 21:05:57 +00:00
|
|
|
if add {
|
2021-09-03 22:09:03 +00:00
|
|
|
// Omit port part if "-" is set
|
|
|
|
target := host + ":" + port
|
|
|
|
if port == "-" {
|
|
|
|
target = host
|
|
|
|
}
|
|
|
|
|
2021-09-02 21:05:57 +00:00
|
|
|
service := PrometheusService{
|
2021-09-03 22:09:03 +00:00
|
|
|
Targets: []string{target},
|
2021-09-02 21:05:57 +00:00
|
|
|
Labels: labels,
|
|
|
|
}
|
|
|
|
|
|
|
|
services = append(services, service)
|
|
|
|
}
|
2021-09-01 21:18:52 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return services
|
|
|
|
}
|