Small label processing enhancement

No dulicated labels coming from envvars. Possibility to omit port
in the prometheus output.
This commit is contained in:
Adam Štrauch 2021-09-04 00:09:03 +02:00
parent 5461af2902
commit 07a70b8285
Signed by: cx
GPG Key ID: 018304FFA8988F8D
6 changed files with 54 additions and 15 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@
# Binaries
lobby_*
bin/

11
LICENCE Normal file
View File

@ -0,0 +1,11 @@
Copyright 2021 Adam Štrauch <cx@initd.cz>
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

13
Makefile Normal file
View File

@ -0,0 +1,13 @@
.PHONY: all
all: build
.PHONY: clean
clean:
rm -rf bin
.PHONY: build
build:
mkdir -p ./bin
export CGO_ENABLED=0
go build -o ./bin/lobbyd daemon/*.go

View File

@ -100,6 +100,9 @@ Let's check this:
prometheus:nodeexporter:host:192.168.1.1
prometheus:nodeexporter:port:9100
prometheus:nodeexporter:location:prague
If you set port to *-* lobby daemon omits port entirely from the output.
When you open URL http://localhost:1313/v1/prometheus/nodeexporter it returns this:
@ -140,21 +143,15 @@ So far the REST API is super simple and it has only two endpoints:
GET / # Returns list of all discovered servers and their labels.
GET /v1/ # Same as /
GET /v1/?labels=LABELS # output will be filtered based on one or multiple labels separated by comma
GET /v1/prometheus/:name # Generates output for Prometheus's SD config, name is group of the monitoring services described above.
## TODO
* [X] filtering based on labels
* [X] Output for prometheus
https://prometheus.io/docs/prometheus/latest/configuration/configuration/#http_sd_config
~~This should be implemented as a template in /etc/lobby/templates~~
* [X] labels in directory
/etc/lobby/labels
One file per one label
* [X] Deregistration
* [X] Deregister when the daemon exits
* [X] Separate the NATS code so it can support multiple backend/drivers
* [X] Documentation
* [ ] Tests
* [ ] Command hooks - script or list of scripts that are triggered when discovery status has changed
* [ ] Support for multiple active backend drivers
* [ ] SNS driver

View File

@ -13,10 +13,11 @@ import (
"github.com/shirou/gopsutil/v3/host"
)
// getIdentification assembles the discovery packet that contains hotname and set of labels describing a single server, in this case the local server.
func getIdentification() (server.Discovery, error) {
discovery := server.Discovery{}
localLabels, err := loadLocalLabels()
localLabels, err := loadLocalLabels(config.Labels)
if err != nil {
return discovery, err
}
@ -38,8 +39,9 @@ func getIdentification() (server.Discovery, error) {
// loadLocalLabels scans local directory where labels are stored and adds them to the labels configured as environment variables.
// Filename in LabelsPath is not importent and each file can contain multiple labels, one per each line.
func loadLocalLabels() ([]string, error) {
func loadLocalLabels(skipLabels []string) ([]string, error) {
labels := []string{}
var found bool
if _, err := os.Stat(config.LabelsPath); !os.IsNotExist(err) {
files, err := ioutil.ReadDir(config.LabelsPath)
@ -68,7 +70,16 @@ func loadLocalLabels() ([]string, error) {
}
line = strings.TrimSpace(line)
if len(line) > 0 {
labels = append(labels, line)
found = false
for _, skipLabel := range skipLabels {
if skipLabel == line {
found = true
break
}
}
if !found {
labels = append(labels, line)
}
}
}
}

View File

@ -65,8 +65,14 @@ func preparePrometheusOutput(name string, discoveries []server.Discovery) Promet
}
if add {
// Omit port part if "-" is set
target := host + ":" + port
if port == "-" {
target = host
}
service := PrometheusService{
Targets: []string{host + ":" + port},
Targets: []string{target},
Labels: labels,
}