Skip to content

Commit

Permalink
error metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
psvmcc committed Mar 7, 2024
1 parent dd9dd1a commit c6f1c30
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/discovery/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strconv"
"strings"
"time"

"github.com/VictoriaMetrics/metrics"
)

var ServiceContainers []ContainerInfo
Expand Down Expand Up @@ -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)

Expand All @@ -77,20 +81,23 @@ 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
}()

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
}

client := http.Client{Transport: &http.Transport{Dial: func(_, _ string) (net.Conn, error) { return conn, nil }}}
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()
Expand All @@ -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()
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/handlers/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

0 comments on commit c6f1c30

Please sign in to comment.