Skip to content

Commit

Permalink
Fix doc build issues happening in linux/memory (#27381)
Browse files Browse the repository at this point in the history
* fix doc build issues

* add changelog

* move tests to linux-only
  • Loading branch information
fearful-symmetry authored Aug 16, 2021
1 parent 35454a7 commit b906aab
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 64 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Improve `perfmon` metricset performance. {pull}26886[26886]
- Preserve annotations in a kubernetes namespace metadata {pull}27045[27045]
- Allow conditional processing in `decode_xml` and `decode_xml_wineventlog`. {pull}27159[27159]
- Fix build constraint that caused issues with doc builds. {pull}27381[27381]

*Auditbeat*

Expand Down
79 changes: 79 additions & 0 deletions metricbeat/internal/metrics/memory/memory_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package memory

import (
"bufio"
"bytes"
"io"
"io/ioutil"
"path/filepath"
"strconv"
"strings"

"github.com/pkg/errors"
)

// ParseMeminfo parses the contents of /proc/meminfo into a hashmap
func ParseMeminfo(rootfs string) (map[string]uint64, error) {
table := map[string]uint64{}

meminfoPath := filepath.Join(rootfs, "/proc/meminfo")
err := readFile(meminfoPath, func(line string) bool {
fields := strings.Split(line, ":")

if len(fields) != 2 {
return true // skip on errors
}

valueUnit := strings.Fields(fields[1])
value, err := strconv.ParseUint(valueUnit[0], 10, 64)
if err != nil {
return true // skip on errors
}

if len(valueUnit) > 1 && valueUnit[1] == "kB" {
value *= 1024
}
table[fields[0]] = value

return true
})
return table, err
}

func readFile(file string, handler func(string) bool) error {
contents, err := ioutil.ReadFile(file)
if err != nil {
return errors.Wrapf(err, "error reading file %s", file)
}

reader := bufio.NewReader(bytes.NewBuffer(contents))

for {
line, _, err := reader.ReadLine()
if err == io.EOF {
break
}
if !handler(string(line)) {
break
}
}

return nil
}
57 changes: 0 additions & 57 deletions metricbeat/internal/metrics/memory/memory_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@
package memory

import (
"bufio"
"bytes"
"io"
"io/ioutil"
"path/filepath"
"strconv"
"strings"

"github.com/pkg/errors"

"github.com/elastic/beats/v7/libbeat/opt"
Expand Down Expand Up @@ -92,52 +84,3 @@ func get(rootfs string) (Memory, error) {
return memData, nil

}

// ParseMeminfo parses the contents of /proc/meminfo into a hashmap
func ParseMeminfo(rootfs string) (map[string]uint64, error) {
table := map[string]uint64{}

meminfoPath := filepath.Join(rootfs, "/proc/meminfo")
err := readFile(meminfoPath, func(line string) bool {
fields := strings.Split(line, ":")

if len(fields) != 2 {
return true // skip on errors
}

valueUnit := strings.Fields(fields[1])
value, err := strconv.ParseUint(valueUnit[0], 10, 64)
if err != nil {
return true // skip on errors
}

if len(valueUnit) > 1 && valueUnit[1] == "kB" {
value *= 1024
}
table[fields[0]] = value

return true
})
return table, err
}

func readFile(file string, handler func(string) bool) error {
contents, err := ioutil.ReadFile(file)
if err != nil {
return errors.Wrapf(err, "error reading file %s", file)
}

reader := bufio.NewReader(bytes.NewBuffer(contents))

for {
line, _, err := reader.ReadLine()
if err == io.EOF {
break
}
if !handler(string(line)) {
break
}
}

return nil
}
4 changes: 1 addition & 3 deletions metricbeat/module/linux/memory/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
// specific language governing permissions and limitations
// under the License.

// +build linux

package memory

import (
Expand Down Expand Up @@ -139,7 +137,7 @@ func GetVMStat() (*sysinfotypes.VMStatInfo, error) {
}
vmstatHandle, ok := h.(sysinfotypes.VMStat)
if !ok {
return nil, errors.Wrap(err, "VMStat not available for platform")
return nil, errors.New("VMStat not available for platform")
}
info, err := vmstatHandle.VMStat()
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions metricbeat/module/linux/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
// specific language governing permissions and limitations
// under the License.

// +build linux

package memory

import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
// specific language governing permissions and limitations
// under the License.

// +build linux

package memory

import (
Expand Down

0 comments on commit b906aab

Please sign in to comment.