feat(exporter): --trim-path-components option

This commit is contained in:
Arthur Chaloin
2020-10-14 14:50:25 +02:00
parent 7eafaaa209
commit 1bd627c2b1
4 changed files with 27 additions and 22 deletions
+7 -7
View File
@@ -21,7 +21,7 @@ func main() {
help := getopt.BoolLong("help", 'h', "show this help message and exit")
port := getopt.IntLong("port", 'p', 9090, "prometheus exporter listening port")
debug := getopt.BoolLong("debug", 0, "enable debug mode")
trimPath := getopt.StringLong("trim-path", 0, "", "remove leading elements from path(s) in label(s)")
trimPathComponents := getopt.IntLong("trim-path-components", 0, 0, "remove <n> leading component(s) from path(s) in label(s)")
files := stringArrayFlag{}
getopt.FlagLong(&files, "watch-file", 'f', "watch one or more x509 certificate file")
@@ -55,12 +55,12 @@ func main() {
}
exporter := exporter.Exporter{
Port: *port,
Files: files,
Directories: directories,
YAMLs: kubeconfigs,
YAMLPaths: exporter.DefaultYamlPaths,
TrimPath: *trimPath,
Port: *port,
Files: files,
Directories: directories,
YAMLs: kubeconfigs,
YAMLPaths: exporter.DefaultYamlPaths,
TrimPathComponents: *trimPathComponents,
}
exporter.ListenAndServe()
+6 -6
View File
@@ -389,21 +389,21 @@ func TestTrimPath(t *testing.T) {
generateCertificate(certPath, time.Now())
testRequest(t, &exporter.Exporter{
Port: port,
Files: []string{certPath},
TrimPath: "/tmp/",
Port: port,
Files: []string{certPath},
TrimPathComponents: 1,
}, func(metrics []model.MetricFamily) {
foundMetrics := getMetricsForName(metrics, "x509_cert_expired")
assert.Len(t, foundMetrics, 1, "missing x509_cert_expired metric(s)")
checkLabels(t, foundMetrics[0].GetLabel(), "test.pem")
checkLabels(t, foundMetrics[0].GetLabel(), "/test.pem")
foundNbMetrics := getMetricsForName(metrics, "x509_cert_not_before")
assert.Len(t, foundNbMetrics, 1, "missing x509_cert_not_before metric(s)")
checkLabels(t, foundNbMetrics[0].GetLabel(), "test.pem")
checkLabels(t, foundNbMetrics[0].GetLabel(), "/test.pem")
foundNaMetrics := getMetricsForName(metrics, "x509_cert_not_after")
assert.Len(t, foundNaMetrics, 1, "missing x509_cert_not_after metric(s)")
checkLabels(t, foundNaMetrics[0].GetLabel(), "test.pem")
checkLabels(t, foundNaMetrics[0].GetLabel(), "/test.pem")
os.Remove(certPath)
})
+8 -3
View File
@@ -3,6 +3,7 @@ package internal
import (
"crypto/x509/pkix"
"fmt"
"path"
"path/filepath"
"strings"
"time"
@@ -66,10 +67,14 @@ func (collector *collector) Collect(ch chan<- prometheus.Metric) {
}
func (collector *collector) getMetricsForCertificate(certData *parsedCertificate, ref *certificateRef) []prometheus.Metric {
trimmedFilePath := ref.path
if ref.path[:len(collector.exporter.TrimPath)] == collector.exporter.TrimPath {
trimmedFilePath = trimmedFilePath[len(collector.exporter.TrimPath):]
trimComponentsCount := collector.exporter.TrimPathComponents
pathComponents := strings.Split(ref.path, "/")
prefix := ""
if pathComponents[0] == "" {
trimComponentsCount++
prefix = "/"
}
trimmedFilePath := path.Join(prefix, path.Join(pathComponents[trimComponentsCount:]...))
baseLabels := []string{
"filename",
+6 -6
View File
@@ -15,12 +15,12 @@ import (
// Exporter : Configuration (from command-line)
type Exporter struct {
Port int
Files []string
Directories []string
YAMLs []string
YAMLPaths []YAMLCertRef
TrimPath string
Port int
Files []string
Directories []string
YAMLs []string
YAMLPaths []YAMLCertRef
TrimPathComponents int
listener net.Listener
handler *http.Handler