Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
Use a proper type assertion when checking for csv.ParseError
Browse files Browse the repository at this point in the history
While the former way seems to work as intended, the behavior is at best
undocumented.
  • Loading branch information
grobie committed Oct 12, 2016
1 parent ea41de6 commit 3dbbc17
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
9 changes: 5 additions & 4 deletions haproxy_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,12 @@ loop:
case nil:
case io.EOF:
break loop
case err.(*csv.ParseError):
log.Errorf("Can't read CSV: %v", err)
e.csvParseFailures.Inc()
continue loop
default:
if _, ok := err.(*csv.ParseError); ok {
log.Errorf("Can't read CSV: %v", err)
e.csvParseFailures.Inc()
continue loop
}
log.Errorf("Unexpected error while reading CSV: %v", err)
e.up.Set(0)
break loop
Expand Down
24 changes: 16 additions & 8 deletions haproxy_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ const testSocket = "/tmp/haproxyexportertest.sock"

type haproxy struct {
*httptest.Server
config []byte
response []byte
}

func newHaproxy(config []byte) *haproxy {
h := &haproxy{config: config}
func newHaproxy(response []byte) *haproxy {
h := &haproxy{response: response}
h.Server = httptest.NewServer(handler(h))
return h
}

func handler(h *haproxy) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Write(h.config)
w.Write(h.response)
}
}

Expand Down Expand Up @@ -113,7 +113,7 @@ func TestServerWithoutChecks(t *testing.T) {

got := 0
for range ch {
got += 1
got++
}
if expect := len(e.serverMetrics) - 1; got != expect {
t.Errorf("expected %d metrics, got %d", expect, got)
Expand Down Expand Up @@ -156,7 +156,7 @@ foo,BACKEND,0,0,0,0,,0,0,0,,0,,0,0,0,0,UP,1,1,0,0,0,5007,0,,1,8,1,,0,,2,0,,0,L4O

got := 0
for range ch {
got += 1
got++
}
if expect := len(e.frontendMetrics) + len(e.backendMetrics); got < expect {
t.Errorf("expected at least %d metrics, got %d", expect, got)
Expand Down Expand Up @@ -194,7 +194,11 @@ func TestDeadline(t *testing.T) {
s.Close()
}()

e, _ := NewExporter(s.URL, serverMetrics, 1*time.Second)
e, err := NewExporter(s.URL, serverMetrics, 1*time.Second)
if err != nil {
t.Fatal(err)
}

ch := make(chan prometheus.Metric)
go func() {
defer close(ch)
Expand Down Expand Up @@ -222,7 +226,11 @@ func TestNotFound(t *testing.T) {
s := httptest.NewServer(http.NotFoundHandler())
defer s.Close()

e, _ := NewExporter(s.URL, serverMetrics, 1*time.Second)
e, err := NewExporter(s.URL, serverMetrics, 1*time.Second)
if err != nil {
t.Fatal(err)
}

ch := make(chan prometheus.Metric)
go func() {
defer close(ch)
Expand Down

0 comments on commit 3dbbc17

Please sign in to comment.