From d4f74661d554b04a3715202c2ada5eec4254c752 Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Sun, 7 Jan 2024 16:37:41 +0100 Subject: [PATCH] feat: Allow renaming domains --- main.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index e202f51..178a68a 100644 --- a/main.go +++ b/main.go @@ -28,7 +28,7 @@ type CertificateMetrics struct { IsValid prometheus.Gauge } -func NewCertificateMetrics(domain string, port int, alpn []string) CertificateMetrics { +func NewCertificateMetrics(domain string, port int, alpn []string, name string) CertificateMetrics { metrics := CertificateMetrics{ Domain: domain, Port: port, @@ -36,13 +36,13 @@ func NewCertificateMetrics(domain string, port int, alpn []string) CertificateMe ExpiryIn: prometheus.NewGauge( prometheus.GaugeOpts{ Name: "cert_status_expiry_in", - ConstLabels: prometheus.Labels{"domain": domain}, + ConstLabels: prometheus.Labels{"name": name}, }, ), IsValid: prometheus.NewGauge( prometheus.GaugeOpts{ Name: "cert_status_is_valid", - ConstLabels: prometheus.Labels{"domain": domain}, + ConstLabels: prometheus.Labels{"name": name}, }, ), } @@ -112,11 +112,18 @@ func run(ctx *cli.Context) error { for _, d := range domains { log.Debugf("Parsing '%s'...", d) parts := strings.Split(d, ":") - if len(parts) != 3 { + if len(parts) < 3 { log.Errorf("Invalid domain format for '%s'", d) return errors.New("Invalid domain format: Expects ::") } + name := "" + if len(parts) == 4 { + name = parts[3] + } else { + name = parts[0] + } + port, err := strconv.Atoi(parts[1]) if err != nil { log.Errorf("Failed to parse port of '%s'", d) @@ -126,11 +133,12 @@ func run(ctx *cli.Context) error { // Create the metric, and register it // TODO: Make this prettier alpn := strings.Split(parts[2], ";") - log.Debugf("Using ALPNs: %v", alpn) + log.Debugf("Registering: domain='%s' port='%d' alpn=%v name='%s'", parts[0], port, alpn, name) metric := NewCertificateMetrics( parts[0], port, alpn, + name, ) metric.register(registry) metrics = append(metrics, metric)