mirror of
https://github.com/prometheus-community/windows_exporter
synced 2025-02-17 04:27:06 +00:00
Strip carriage-returns from textfile input
This commit is contained in:
parent
a0333ee256
commit
2951a9ef80
@ -17,6 +17,7 @@ package collector
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -185,6 +186,30 @@ func (c *textFileCollector) exportMTimes(mtimes map[string]time.Time, ch chan<-
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type carriageReturnFilteringReader struct {
|
||||||
|
r io.Reader
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read returns data from the underlying io.Reader, but with \r filtered out
|
||||||
|
func (cr carriageReturnFilteringReader) Read(p []byte) (int, error) {
|
||||||
|
buf := make([]byte, len(p))
|
||||||
|
n, err := cr.r.Read(buf)
|
||||||
|
|
||||||
|
if err != nil && err != io.EOF {
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
pi := 0
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
if buf[i] != '\r' {
|
||||||
|
p[pi] = buf[i]
|
||||||
|
pi++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return pi, err
|
||||||
|
}
|
||||||
|
|
||||||
// Update implements the Collector interface.
|
// Update implements the Collector interface.
|
||||||
func (c *textFileCollector) Collect(ch chan<- prometheus.Metric) error {
|
func (c *textFileCollector) Collect(ch chan<- prometheus.Metric) error {
|
||||||
error := 0.0
|
error := 0.0
|
||||||
@ -203,6 +228,7 @@ fileLoop:
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
path := filepath.Join(c.path, f.Name())
|
path := filepath.Join(c.path, f.Name())
|
||||||
|
log.Debugf("Processing file %q", path)
|
||||||
file, err := os.Open(path)
|
file, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error opening %q: %v", path, err)
|
log.Errorf("Error opening %q: %v", path, err)
|
||||||
@ -210,7 +236,7 @@ fileLoop:
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
var parser expfmt.TextParser
|
var parser expfmt.TextParser
|
||||||
parsedFamilies, err := parser.TextToMetricFamilies(file)
|
parsedFamilies, err := parser.TextToMetricFamilies(carriageReturnFilteringReader{r: file})
|
||||||
file.Close()
|
file.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error parsing %q: %v", path, err)
|
log.Errorf("Error parsing %q: %v", path, err)
|
||||||
|
20
collector/textfile_test.go
Normal file
20
collector/textfile_test.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package collector
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"strings"
|
||||||
|
"io/ioutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCRFilter(t *testing.T) {
|
||||||
|
sr := strings.NewReader("line 1\r\nline 2")
|
||||||
|
cr := carriageReturnFilteringReader{ r: sr }
|
||||||
|
b, err := ioutil.ReadAll(cr)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if string(b) != "line 1\nline 2" {
|
||||||
|
t.Errorf("Unexpected output %q", b)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user