mirror of
https://github.com/enix/x509-certificate-exporter.git
synced 2026-08-23 22:16:39 +00:00
feat(exporter): --trim-path-components option
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user