Multiple prometheus services support
All checks were successful
continuous-integration/drone/push Build is passing

* Prometheus export test
* Version bump to 1.5
This commit is contained in:
Adam Štrauch 2022-02-21 10:20:06 +01:00
parent 038741b87e
commit c2ed658aaa
Signed by: cx
GPG Key ID: 018304FFA8988F8D
4 changed files with 63 additions and 11 deletions

View File

@ -1,4 +1,4 @@
VERSION=1.4 VERSION=1.5
.PHONY: all .PHONY: all

View File

@ -137,6 +137,8 @@ Let's check this:
If you set port to *-* lobby daemon omits port entirely from the output. If you set port to *-* lobby daemon omits port entirely from the output.
There can be multiple `host` labels but only one `port` label and all prometheus labels (last line) will be common for all hosts labels. If port is omitted then default 9100 is used or port can also be part of the host label.
When you open URL http://localhost:1313/v1/prometheus/nodeexporter it returns this: When you open URL http://localhost:1313/v1/prometheus/nodeexporter it returns this:
```json ```json

View File

@ -34,10 +34,10 @@ func preparePrometheusOutput(name string, discoveries []server.Discovery) Promet
for _, discovery := range discoveries { for _, discovery := range discoveries {
port := strconv.Itoa(int(config.NodeExporterPort)) port := strconv.Itoa(int(config.NodeExporterPort))
host := discovery.Hostname hosts := []string{}
var add bool // add to the prometheus output when there is at least one prometheus related label var add bool // add to the prometheus output when there is at least one prometheus related label
labels := map[string]string{} labels := map[string]string{} // These are prometheus labels, not Lobby's labels
for _, label := range discovery.FindLabelsByPrefix("prometheus:" + name + ":") { for _, label := range discovery.FindLabelsByPrefix("prometheus:" + name + ":") {
trimmed := strings.TrimPrefix(label.String(), "prometheus:"+name+":") trimmed := strings.TrimPrefix(label.String(), "prometheus:"+name+":")
@ -46,7 +46,7 @@ func preparePrometheusOutput(name string, discoveries []server.Discovery) Promet
if parts[0] == "port" { if parts[0] == "port" {
port = parts[1] port = parts[1]
} else if parts[0] == "host" { } else if parts[0] == "host" {
host = parts[1] hosts = append(hosts, parts[1])
} else { } else {
labels[parts[0]] = parts[1] labels[parts[0]] = parts[1]
} }
@ -65,20 +65,23 @@ func preparePrometheusOutput(name string, discoveries []server.Discovery) Promet
} }
if add { if add {
// Omit port part if "-" is set targets := []string{}
for _, host := range hosts {
// Omit port part if "-" is set or port is part of the host
target := host + ":" + port target := host + ":" + port
if port == "-" { if strings.Contains(host, ":") || port == "-" {
target = host target = host
} }
targets = append(targets, target)
}
service := PrometheusService{ service := PrometheusService{
Targets: []string{target}, Targets: targets,
Labels: labels, Labels: labels,
} }
services = append(services, service) services = append(services, service)
} }
} }
return services return services

47
daemon/prometheus_test.go Normal file
View File

@ -0,0 +1,47 @@
package main
import (
"testing"
"github.com/by-cx/lobby/server"
"github.com/stretchr/testify/assert"
)
func TestPreparePrometheusOutput(t *testing.T) {
discoveries := []server.Discovery{
{
Hostname: "test.server",
Labels: server.Labels{
// test1
"prometheus:test1:label1:l1",
"prometheus:test1:host:srv1:1234",
"prometheus:test1:host:srv1:1235",
"prometheus:test1:host:srv1",
"prometheus:test1:label2:l2",
// test2
"prometheus:test2:host:srv2",
"prometheus:test2:host:srv2:1235",
"prometheus:test2:host:srv2:1236",
"prometheus:test2:host:srv2:1237",
"prometheus:test2:label1:l3",
},
},
}
services := preparePrometheusOutput("test1", discoveries)
assert.Equal(t, 1, len(services))
assert.Equal(t, 3, len(services[0].Targets))
assert.Contains(t, services[0].Targets, "srv1:9100")
assert.Contains(t, services[0].Targets, "srv1:1234")
assert.Contains(t, services[0].Targets, "srv1:1235")
assert.Contains(t, services[0].Labels["label1"], "l1")
assert.Contains(t, services[0].Labels["label2"], "l2")
services = preparePrometheusOutput("test2", discoveries)
assert.Equal(t, 1, len(services))
assert.Equal(t, 4, len(services[0].Targets))
assert.Contains(t, services[0].Targets, "srv2:9100")
assert.Contains(t, services[0].Targets, "srv2:1235")
assert.Contains(t, services[0].Targets, "srv2:1236")
assert.Contains(t, services[0].Targets, "srv2:1237")
assert.Contains(t, services[0].Labels["label1"], "l3")
}