From 07a70b8285005d4cd25975b7bfed7fceb94267a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C5=A0trauch?= Date: Sat, 4 Sep 2021 00:09:03 +0200 Subject: [PATCH] Small label processing enhancement No dulicated labels coming from envvars. Possibility to omit port in the prometheus output. --- .gitignore | 1 + LICENCE | 11 +++++++++++ Makefile | 13 +++++++++++++ README.md | 19 ++++++++----------- daemon/identification.go | 17 ++++++++++++++--- daemon/prometheus.go | 8 +++++++- 6 files changed, 54 insertions(+), 15 deletions(-) create mode 100644 LICENCE create mode 100644 Makefile diff --git a/.gitignore b/.gitignore index 92f0e3d..87bdbf1 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ # Binaries lobby_* +bin/ diff --git a/LICENCE b/LICENCE new file mode 100644 index 0000000..d2c92b5 --- /dev/null +++ b/LICENCE @@ -0,0 +1,11 @@ +Copyright 2021 Adam Štrauch + +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. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3108869 --- /dev/null +++ b/Makefile @@ -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 + diff --git a/README.md b/README.md index 952b8ba..5eac61e 100644 --- a/README.md +++ b/README.md @@ -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 + + diff --git a/daemon/identification.go b/daemon/identification.go index 59f990a..bf369c7 100644 --- a/daemon/identification.go +++ b/daemon/identification.go @@ -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) + } } } } diff --git a/daemon/prometheus.go b/daemon/prometheus.go index 135ef2f..8b8f2fa 100644 --- a/daemon/prometheus.go +++ b/daemon/prometheus.go @@ -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, }