From c6f1c30b38d9eb72bd81fd579de699e2a2b35b31 Mon Sep 17 00:00:00 2001 From: Sergii Putko Date: Thu, 7 Mar 2024 08:35:35 +0200 Subject: [PATCH] error metrics --- pkg/discovery/docker.go | 14 +++++++++++++- pkg/handlers/dns.go | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pkg/discovery/docker.go b/pkg/discovery/docker.go index 57b0e07..6200103 100644 --- a/pkg/discovery/docker.go +++ b/pkg/discovery/docker.go @@ -10,6 +10,8 @@ import ( "strconv" "strings" "time" + + "github.com/VictoriaMetrics/metrics" ) var ServiceContainers []ContainerInfo @@ -48,11 +50,13 @@ func DockerEventListener(socketPath string, eventChan chan<- string) { var conn net.Conn conn, err = connectToSocket(socketPath) if err != nil { + metrics.GetOrCreateCounter(`disconter_discovery_errors{type="connectToSocket"}`).Inc() continue } ServiceContainers, err = ListContainers(socketPath) if err != nil { fmt.Println("Error:", err) + metrics.GetOrCreateCounter(`disconter_discovery_errors{type="ListContainers"}`).Inc() } go listenForEvents(conn, eventChan, done) @@ -77,6 +81,7 @@ func listenForEvents(conn net.Conn, eventChan chan<- string, done chan<- bool) { err = conn.Close() if err != nil { fmt.Printf("Error: %v", err) + metrics.GetOrCreateCounter(`disconter_discovery_errors{type="listenForEvents"}`).Inc() } done <- true }() @@ -84,6 +89,7 @@ func listenForEvents(conn net.Conn, eventChan chan<- string, done chan<- bool) { req, err := http.NewRequest("GET", "http://localhost/events?filter={\"type\":[\"container\"]}", http.NoBody) if err != nil { fmt.Printf("Error creating HTTP request: %v\n", err) + metrics.GetOrCreateCounter(`disconter_discovery_errors{type="listenForEvents"}`).Inc() return } @@ -91,6 +97,7 @@ func listenForEvents(conn net.Conn, eventChan chan<- string, done chan<- bool) { resp, err := client.Do(req) if err != nil { fmt.Printf("Error sending HTTP request: %v\n", err) + metrics.GetOrCreateCounter(`disconter_discovery_errors{type="listenForEvents"}`).Inc() return } defer resp.Body.Close() @@ -102,17 +109,22 @@ func listenForEvents(conn net.Conn, eventChan chan<- string, done chan<- bool) { err = json.Unmarshal([]byte(eventData), &event) if err != nil { fmt.Printf("Error parsing Docker event: %v\n", err) + metrics.GetOrCreateCounter(`disconter_discovery_errors{type="listenForEvents"}`).Inc() continue } if event.Type == "container" && event.Actor.Attributes.DisconterService != "" && (event.Action == "start" || event.Action == "die") { ServiceContainers, err = ListContainers(DockerSocket) - + if err != nil { + fmt.Println("Error:", err) + metrics.GetOrCreateCounter(`disconter_discovery_errors{type="ListContainers"}`).Inc() + } eventChan <- fmt.Sprint(event) } } if err := scanner.Err(); err != nil { fmt.Printf("Error reading response body: %v\n", err) + metrics.GetOrCreateCounter(`disconter_discovery_errors{type="listenForEvents"}`).Inc() } } diff --git a/pkg/handlers/dns.go b/pkg/handlers/dns.go index 36e9248..9e5c50b 100644 --- a/pkg/handlers/dns.go +++ b/pkg/handlers/dns.go @@ -70,5 +70,6 @@ func HandleDNSRequest(w dns.ResponseWriter, r *dns.Msg) { err := w.WriteMsg(m) if err != nil { fmt.Println("[ERROR] DNS reply:", err) + metrics.GetOrCreateCounter("disconter_dns_errors").Inc() } }