From 1bd627c2b176f0691f72dfd06f267cedc7414cc5 Mon Sep 17 00:00:00 2001 From: Arthur Chaloin Date: Wed, 14 Oct 2020 14:50:25 +0200 Subject: [PATCH] feat(exporter): --trim-path-components option --- cmd/x509-exporter/main.go | 14 +++++++------- cmd/x509-exporter/main_test.go | 12 ++++++------ internal/collector.go | 11 ++++++++--- internal/exporter.go | 12 ++++++------ 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/cmd/x509-exporter/main.go b/cmd/x509-exporter/main.go index 9cb1302..d07e735 100644 --- a/cmd/x509-exporter/main.go +++ b/cmd/x509-exporter/main.go @@ -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 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() diff --git a/cmd/x509-exporter/main_test.go b/cmd/x509-exporter/main_test.go index 7c141a9..8c0e6f3 100644 --- a/cmd/x509-exporter/main_test.go +++ b/cmd/x509-exporter/main_test.go @@ -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) }) diff --git a/internal/collector.go b/internal/collector.go index ca84e0e..a7e69ee 100644 --- a/internal/collector.go +++ b/internal/collector.go @@ -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", diff --git a/internal/exporter.go b/internal/exporter.go index a1737b2..26d5f24 100644 --- a/internal/exporter.go +++ b/internal/exporter.go @@ -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