Skip to content

Commit

Permalink
Merge branch 'feature/scrape-error-handling' of https://github.com/mh…
Browse files Browse the repository at this point in the history
…owell24/suricata_exporter

* 'feature/scrape-error-handling' of https://github.com/mhowell24/suricata_exporter:
  Return http 500 on collection failure
  • Loading branch information
awelzel committed Aug 5, 2024
2 parents 998e7d9 + 412050b commit ce213f4
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main
import (
"bufio"
"encoding/json"
"errors"
"flag"
"fmt"
"log"
Expand Down Expand Up @@ -320,6 +321,18 @@ 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,
)

// Errors used for above metric
errConnect = errors.New("failed to connect")
errDumpCounters = errors.New("failed to dump-counters")
)

func NewSuricataClient(socketPath string) *SuricataClient {
Expand Down Expand Up @@ -792,12 +805,16 @@ 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, errConnect)

return
}

counters, err := sc.client.DumpCounters()
if err != nil {
log.Printf("ERROR: Failed to dump-counters: %v", err)
ch <- prometheus.NewInvalidMetric(FailedCollectionDesc, errDumpCounters)

return
}

Expand Down Expand Up @@ -828,7 +845,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(`<html>
<head><title>Suricata Exporter</title></head>
Expand Down

0 comments on commit ce213f4

Please sign in to comment.