Continue scrape with duplicated metrics

Problems of a single collector, like duplicated metrics read via the
textfile collector, should not fail the collection and export of other
metrics.
This commit is contained in:
Tobias Schmidt 2017-03-13 23:55:19 -03:00
parent a0a0dbaad0
commit dace41e3d4
2 changed files with 93 additions and 49 deletions

View File

@ -158,10 +158,16 @@ func main() {
log.Infof(" - %s", n) log.Infof(" - %s", n)
} }
prometheus.MustRegister(NodeCollector{collectors: collectors}) if err := prometheus.Register(NodeCollector{collectors: collectors}); err != nil {
log.Fatalf("Couldn't register collector: %s", err)
}
handler := promhttp.HandlerFor(prometheus.DefaultGatherer, handler := promhttp.HandlerFor(prometheus.DefaultGatherer,
promhttp.HandlerOpts{ErrorLog: log.NewErrorLogger()}) promhttp.HandlerOpts{
ErrorLog: log.NewErrorLogger(),
ErrorHandling: promhttp.ContinueOnError,
})
// TODO(ts): Remove deprecated and problematic InstrumentHandler usage.
http.Handle(*metricsPath, prometheus.InstrumentHandler("prometheus", handler)) http.Handle(*metricsPath, prometheus.InstrumentHandler("prometheus", handler))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html> w.Write([]byte(`<html>

View File

@ -2,21 +2,23 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"testing" "testing"
"time" "time"
"github.com/prometheus/procfs" "github.com/prometheus/procfs"
) )
func TestFileDescriptorLeak(t *testing.T) { const (
const ( binary = "./node_exporter"
binary = "./node_exporter" address = "localhost:19100"
address = "localhost:9100" )
)
func TestFileDescriptorLeak(t *testing.T) {
if _, err := os.Stat(binary); err != nil { if _, err := os.Stat(binary); err != nil {
t.Skipf("node_exporter binary not available, try to run `make build` first: %s", err) t.Skipf("node_exporter binary not available, try to run `make build` first: %s", err)
} }
@ -24,75 +26,111 @@ func TestFileDescriptorLeak(t *testing.T) {
t.Skipf("proc filesystem is not available, but currently required to read number of open file descriptors: %s", err) t.Skipf("proc filesystem is not available, but currently required to read number of open file descriptors: %s", err)
} }
errc := make(chan error)
exporter := exec.Command(binary, "-web.listen-address", address) exporter := exec.Command(binary, "-web.listen-address", address)
go func() { test := func(pid int) error {
if err := exporter.Run(); err != nil { if err := queryExporter(address); err != nil {
errc <- fmt.Errorf("execution of node_exporter failed: %s", err) return err
} else {
errc <- nil
}
}()
select {
case err := <-errc:
t.Fatal(err)
case <-time.After(100 * time.Millisecond):
}
go func(pid int, url string) {
if err := queryExporter(url); err != nil {
errc <- err
return
} }
proc, err := procfs.NewProc(pid) proc, err := procfs.NewProc(pid)
if err != nil { if err != nil {
errc <- err return err
return
} }
fdsBefore, err := proc.FileDescriptors() fdsBefore, err := proc.FileDescriptors()
if err != nil { if err != nil {
errc <- err return err
return
} }
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
if err := queryExporter(url); err != nil { if err := queryExporter(address); err != nil {
errc <- err return err
return
} }
} }
fdsAfter, err := proc.FileDescriptors() fdsAfter, err := proc.FileDescriptors()
if err != nil { if err != nil {
errc <- err return err
return
} }
if want, have := len(fdsBefore), len(fdsAfter); want != have { if want, have := len(fdsBefore), len(fdsAfter); want != have {
errc <- fmt.Errorf("want %d open file descriptors after metrics scrape, have %d", want, have) return fmt.Errorf("want %d open file descriptors after metrics scrape, have %d", want, have)
} }
errc <- nil return nil
}(exporter.Process.Pid, fmt.Sprintf("http://%s/metrics", address)) }
select { if err := runCommandAndTests(exporter, test); err != nil {
case err := <-errc: t.Error(err)
if exporter.Process != nil {
exporter.Process.Kill()
}
if err != nil {
t.Fatal(err)
}
} }
} }
func queryExporter(url string) error { func TestHandlingOfDuplicatedMetrics(t *testing.T) {
resp, err := http.Get(url) if _, err := os.Stat(binary); err != nil {
t.Skipf("node_exporter binary not available, try to run `make build` first: %s", err)
}
dir, err := ioutil.TempDir("", "node-exporter")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
content := []byte("dummy_metric 1\n")
if err := ioutil.WriteFile(filepath.Join(dir, "a.prom"), content, 0600); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(dir, "b.prom"), content, 0600); err != nil {
t.Fatal(err)
}
exporter := exec.Command(binary, "-web.listen-address", address, "-collector.textfile.directory", dir)
test := func(_ int) error {
return queryExporter(address)
}
if err := runCommandAndTests(exporter, test); err != nil {
t.Error(err)
}
}
func queryExporter(address string) error {
resp, err := http.Get(fmt.Sprintf("http://%s/metrics", address))
if err != nil { if err != nil {
return err return err
} }
if err := resp.Body.Close(); err != nil { if err := resp.Body.Close(); err != nil {
return err return err
} }
if want, have := resp.StatusCode, http.StatusOK; want != have { if want, have := http.StatusOK, resp.StatusCode; want != have {
return fmt.Errorf("want /metrics status code %d, have %d", want, have) return fmt.Errorf("want /metrics status code %d, have %d", want, have)
} }
return nil return nil
} }
func runCommandAndTests(cmd *exec.Cmd, fn func(pid int) error) error {
errc := make(chan error)
go func() {
if err := cmd.Run(); err != nil {
errc <- fmt.Errorf("execution of command failed: %s", err)
} else {
errc <- nil
}
}()
// Allow the process to start before running any tests.
select {
case err := <-errc:
return err
case <-time.After(100 * time.Millisecond):
}
go func(pid int) {
errc <- fn(pid)
}(cmd.Process.Pid)
select {
case err := <-errc:
if cmd.Process != nil {
cmd.Process.Kill()
}
if err != nil {
return err
}
}
return nil
}