From 412050bb745905e312bebb2a5bbce73349b8668f Mon Sep 17 00:00:00 2001 From: Max Howell Date: Tue, 30 Jul 2024 19:26:39 +0100 Subject: [PATCH] Return http 500 on collection failure --- main.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 60c8be2..f798bea 100644 --- a/main.go +++ b/main.go @@ -320,6 +320,14 @@ var ( newCounterMetric("napatech", "dispatch_drop_packets_total", "", "pkts"), newCounterMetric("napatech", "dispatch_drop_bytes_total", "", "byte"), } + + // Metric desc used for reporting collection failures + FailedCollectionDesc = prometheus.NewDesc( + prometheus.BuildFQName("suricata", "collection", "failure"), + "invalid metric for reporting collection failures", + []string{}, + nil, + ) ) func NewSuricataClient(socketPath string) *SuricataClient { @@ -792,6 +800,7 @@ func (sc *suricataCollector) Collect(ch chan<- prometheus.Metric) { if err := sc.client.EnsureConnection(); err != nil { log.Printf("ERROR: Failed to connect to %v", err) + ch <- prometheus.NewInvalidMetric(FailedCollectionDesc, fmt.Errorf("Failed to connect")) return } @@ -828,7 +837,9 @@ func main() { r := prometheus.NewRegistry() r.MustRegister(&suricataCollector{NewSuricataClient(*socketPath), sync.Mutex{}}) - http.Handle(*path, promhttp.HandlerFor(r, promhttp.HandlerOpts{})) + http.Handle(*path, promhttp.HandlerFor(r, promhttp.HandlerOpts{ + ErrorHandling: promhttp.HTTPErrorOnError, + })) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { _, err := w.Write([]byte(` Suricata Exporter