Merge pull request #480 from prometheus/grobie/gosimple

Simplify go code
pull/481/head
Tobias Schmidt 2017-02-28 13:59:01 -04:00 committed by GitHub
commit 195b4d596c
10 changed files with 16 additions and 17 deletions

View File

@ -217,7 +217,7 @@ func parseDiskStats(r io.Reader) (map[string]map[int]string, error) {
) )
for scanner.Scan() { for scanner.Scan() {
parts := strings.Fields(string(scanner.Text())) parts := strings.Fields(scanner.Text())
if len(parts) < 4 { // we strip major, minor and dev if len(parts) < 4 { // we strip major, minor and dev
return nil, fmt.Errorf("invalid line in %s: %s", procFilePath("diskstats"), scanner.Text()) return nil, fmt.Errorf("invalid line in %s: %s", procFilePath("diskstats"), scanner.Text())
} }

View File

@ -76,7 +76,7 @@ func parseFileFDStats(r io.Reader, fileName string) (map[string]string, error) {
var scanner = bufio.NewScanner(r) var scanner = bufio.NewScanner(r)
scanner.Scan() scanner.Scan()
// The file-nr proc file is separated by tabs, not spaces. // The file-nr proc file is separated by tabs, not spaces.
line := strings.Split(string(scanner.Text()), "\u0009") line := strings.Split(scanner.Text(), "\u0009")
var fileFDStat = map[string]string{} var fileFDStat = map[string]string{}
// The file-nr proc is only 1 line with 3 values. // The file-nr proc is only 1 line with 3 values.
fileFDStat["allocated"] = line[0] fileFDStat["allocated"] = line[0]

View File

@ -73,11 +73,10 @@ func parseInterrupts(r io.Reader) (map[string]interrupt, error) {
if !scanner.Scan() { if !scanner.Scan() {
return nil, errors.New("interrupts empty") return nil, errors.New("interrupts empty")
} }
cpuNum := len(strings.Fields(string(scanner.Text()))) // one header per cpu cpuNum := len(strings.Fields(scanner.Text())) // one header per cpu
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() parts := strings.Fields(scanner.Text())
parts := strings.Fields(string(line))
if len(parts) < cpuNum+2 { // irq + one column per cpu + details, if len(parts) < cpuNum+2 { // irq + one column per cpu + details,
continue // we ignore ERR and MIS for now continue // we ignore ERR and MIS for now
} }

View File

@ -39,12 +39,12 @@ func parseMemInfo(r io.Reader) (map[string]float64, error) {
var ( var (
memInfo = map[string]float64{} memInfo = map[string]float64{}
scanner = bufio.NewScanner(r) scanner = bufio.NewScanner(r)
re = regexp.MustCompile("\\((.*)\\)") re = regexp.MustCompile(`\((.*)\)`)
) )
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
parts := strings.Fields(string(line)) parts := strings.Fields(line)
fv, err := strconv.ParseFloat(parts[1], 64) fv, err := strconv.ParseFloat(parts[1], 64)
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid value in meminfo: %s", err) return nil, fmt.Errorf("invalid value in meminfo: %s", err)

View File

@ -123,7 +123,7 @@ func parseMemInfoNuma(r io.Reader) ([]meminfoMetric, error) {
var ( var (
memInfo []meminfoMetric memInfo []meminfoMetric
scanner = bufio.NewScanner(r) scanner = bufio.NewScanner(r)
re = regexp.MustCompile("\\((.*)\\)") re = regexp.MustCompile(`\((.*)\)`)
) )
for scanner.Scan() { for scanner.Scan() {
@ -131,7 +131,7 @@ func parseMemInfoNuma(r io.Reader) ([]meminfoMetric, error) {
if line == "" { if line == "" {
continue continue
} }
parts := strings.Fields(string(line)) parts := strings.Fields(line)
fv, err := strconv.ParseFloat(parts[3], 64) fv, err := strconv.ParseFloat(parts[3], 64)
if err != nil { if err != nil {
@ -165,7 +165,7 @@ func parseMemInfoNumaStat(r io.Reader, nodeNumber string) ([]meminfoMetric, erro
if line == "" { if line == "" {
continue continue
} }
parts := strings.Fields(string(line)) parts := strings.Fields(line)
if len(parts) != 2 { if len(parts) != 2 {
return nil, fmt.Errorf("line scan did not return 2 fields: %s", line) return nil, fmt.Errorf("line scan did not return 2 fields: %s", line)
} }

View File

@ -44,7 +44,7 @@ func parseNetDevStats(r io.Reader, ignore *regexp.Regexp) (map[string]map[string
scanner := bufio.NewScanner(r) scanner := bufio.NewScanner(r)
scanner.Scan() // skip first header scanner.Scan() // skip first header
scanner.Scan() scanner.Scan()
parts := strings.Split(string(scanner.Text()), "|") parts := strings.Split(scanner.Text(), "|")
if len(parts) != 3 { // interface + receive + transmit if len(parts) != 3 { // interface + receive + transmit
return nil, fmt.Errorf("invalid header line in net/dev: %s", return nil, fmt.Errorf("invalid header line in net/dev: %s",
scanner.Text()) scanner.Text())
@ -53,7 +53,7 @@ func parseNetDevStats(r io.Reader, ignore *regexp.Regexp) (map[string]map[string
header := strings.Fields(parts[1]) header := strings.Fields(parts[1])
netDev := map[string]map[string]string{} netDev := map[string]map[string]string{}
for scanner.Scan() { for scanner.Scan() {
line := strings.TrimLeft(string(scanner.Text()), " ") line := strings.TrimLeft(scanner.Text(), " ")
parts := procNetDevFieldSep.Split(line, -1) parts := procNetDevFieldSep.Split(line, -1)
if len(parts) != 2*len(header)+1 { if len(parts) != 2*len(header)+1 {
return nil, fmt.Errorf("invalid line in net/dev: %s", scanner.Text()) return nil, fmt.Errorf("invalid line in net/dev: %s", scanner.Text())

View File

@ -47,7 +47,7 @@ func TestNetDevStats(t *testing.T) {
t.Errorf("want count of devices to be %d, got %d", want, got) t.Errorf("want count of devices to be %d, got %d", want, got)
} }
if _, ok := netStats["veth4B09XN"]["transmit_bytes"]; ok != false { if _, ok := netStats["veth4B09XN"]["transmit_bytes"]; ok {
t.Error("want fixture interface veth4B09XN to not exist, but it does") t.Error("want fixture interface veth4B09XN to not exist, but it does")
} }
} }

View File

@ -93,9 +93,9 @@ func parseNetStats(r io.Reader, fileName string) (map[string]map[string]string,
) )
for scanner.Scan() { for scanner.Scan() {
nameParts := strings.Split(string(scanner.Text()), " ") nameParts := strings.Split(scanner.Text(), " ")
scanner.Scan() scanner.Scan()
valueParts := strings.Split(string(scanner.Text()), " ") valueParts := strings.Split(scanner.Text(), " ")
// Remove trailing :. // Remove trailing :.
protocol := nameParts[0][:len(nameParts[0])-1] protocol := nameParts[0][:len(nameParts[0])-1]
netStats[protocol] = map[string]string{} netStats[protocol] = map[string]string{}

View File

@ -85,7 +85,7 @@ func parseSockStats(r io.Reader, fileName string) (map[string]map[string]string,
) )
for scanner.Scan() { for scanner.Scan() {
line := strings.Split(string(scanner.Text()), " ") line := strings.Split(scanner.Text(), " ")
// Remove trailing ':'. // Remove trailing ':'.
protocol := line[0][:len(line[0])-1] protocol := line[0][:len(line[0])-1]
sockStat[protocol] = map[string]string{} sockStat[protocol] = map[string]string{}

View File

@ -87,7 +87,7 @@ func (c *zfsCollector) constSysctlMetric(subsystem string, sysctl zfsSysctl, val
return prometheus.MustNewConstMetric( return prometheus.MustNewConstMetric(
prometheus.NewDesc( prometheus.NewDesc(
prometheus.BuildFQName(Namespace, string(subsystem), metricName), prometheus.BuildFQName(Namespace, subsystem, metricName),
string(sysctl), string(sysctl),
nil, nil,
nil, nil,