Skip to content

Commit

Permalink
Improve handling of disabled commands in Zookeeper Metricbeat module (e…
Browse files Browse the repository at this point in the history
…lastic#31013)

* Improve handing of disabled commands in Zookeeper Metricbeat module

* Added fmt library to connection in zookeeper

* Removed the "error" argument in conns.parseCons func

* Update CHANGELOG.next.asciidoc

* Added errors.unwrap() to remove the package error

* added a handler over second return value

* adding string type in errorf statement removing all type

* Added zookeeper asciidoc file

* improved Error message in data.go

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and kush-elastic committed May 2, 2022
1 parent 7f83113 commit 799c1a8
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...main[Check the HEAD dif
- Add back missing metrics to system/linux. {pull}30774[30774]
- GCP metrics query instances with aggregatedList API to improve efficiency. {pull}30154[#30153]
- Fix Jolokia module to print URI for one of the debug logs. {pull}30943[#30943]
- Improve handling of disabled commands in Zookeeper Metricbeat module. {pull}31013[#31013]
- Handle docker reporting different capitalization for disk usage metrics. {pull}30978[#30978]

*Packetbeat*
Expand Down
2 changes: 1 addition & 1 deletion metricbeat/docs/modules/zookeeper.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ metricsets are `mntr` and `server`.
The ZooKeeper metricsets were tested with ZooKeeper 3.4.8, 3.6.0 and 3.7.0. They are expected to work with all versions
>= 3.4.0. Versions prior to 3.4 do not support the `mntr` command.

Note that from ZooKeeper 3.6.0, `mntr` command must be explicitly enabled at ZooKeeper side using the `4lw.commands.whitelist` configuration parameter.
Note that from ZooKeeper 3.6.0, `mntr`, `stat`, `ruok`, `conf`, `isro`, `cons` command must be explicitly enabled at ZooKeeper side using the `4lw.commands.whitelist` configuration parameter.

[float]
=== Dashboard
Expand Down
2 changes: 1 addition & 1 deletion metricbeat/module/zookeeper/_meta/docs.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ metricsets are `mntr` and `server`.
The ZooKeeper metricsets were tested with ZooKeeper 3.4.8, 3.6.0 and 3.7.0. They are expected to work with all versions
>= 3.4.0. Versions prior to 3.4 do not support the `mntr` command.

Note that from ZooKeeper 3.6.0, `mntr` command must be explicitly enabled at ZooKeeper side using the `4lw.commands.whitelist` configuration parameter.
Note that from ZooKeeper 3.6.0, `mntr`, `stat`, `ruok`, `conf`, `isro`, `cons` command must be explicitly enabled at ZooKeeper side using the `4lw.commands.whitelist` configuration parameter.

[float]
=== Dashboard
Expand Down
12 changes: 6 additions & 6 deletions metricbeat/module/zookeeper/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package connection

import (
"github.com/pkg/errors"
"fmt"

"github.com/elastic/beats/v7/metricbeat/mb"
"github.com/elastic/beats/v7/metricbeat/mb/parse"
Expand Down Expand Up @@ -61,21 +61,21 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
func (m *MetricSet) Fetch(reporter mb.ReporterV2) error {
outputReader, err := zookeeper.RunCommand("cons", m.Host(), m.Module().Config().Timeout)
if err != nil {
return errors.Wrap(err, "'cons' command failed")
return fmt.Errorf("'cons' command failed: %w", err)
}

events, err := m.parseCons(outputReader)
events := m.parseCons(outputReader)
if err != nil {
return errors.Wrap(err, "error parsing response from zookeeper")
return fmt.Errorf("error parsing response from zookeeper: %w", err)
}

serverID, err := zookeeper.ServerID(m.Host(), m.Module().Config().Timeout)
if err != nil {
return errors.Wrap(err, "error obtaining server id")
return fmt.Errorf("error obtaining server id %w", err)
}

for _, event := range events {
event.RootFields.Put("service.node.name", serverID)
_, _ = event.RootFields.Put("service.node.name", serverID)
reporter.Event(event)
}

Expand Down
5 changes: 1 addition & 4 deletions metricbeat/module/zookeeper/connection/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ var srvrTestInput = `/172.17.0.1:55218[0](queued=0,recved=1,sent=0)
func TestParser(t *testing.T) {
conns := MetricSet{}

mapStr, err := conns.parseCons(bytes.NewReader([]byte(srvrTestInput)))
if err != nil {
t.Fatal(err)
}
mapStr := conns.parseCons(bytes.NewReader([]byte(srvrTestInput)))
assert.True(t, len(mapStr) == 3)
firstLine := mapStr[0]
secondLine := mapStr[1]
Expand Down
13 changes: 5 additions & 8 deletions metricbeat/module/zookeeper/connection/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@ package connection

import (
"bufio"
"fmt"
"io"
"regexp"
"strconv"

"github.com/pkg/errors"

"github.com/elastic/beats/v7/metricbeat/mb"

"github.com/elastic/beats/v7/libbeat/common"
)

var capturer = regexp.MustCompile(`/(?P<ip>.*):(?P<port>\d+)\[(?P<interest_ops>\d*)]\(queued=(?P<queued>\d*),recved=(?P<received>\d*),sent=(?P<sent>\d*)\)`)

func (m *MetricSet) parseCons(i io.Reader) ([]mb.Event, error) {
func (m *MetricSet) parseCons(i io.Reader) []mb.Event {
scanner := bufio.NewScanner(i)

result := make([]mb.Event, 0)
Expand All @@ -45,7 +44,7 @@ func (m *MetricSet) parseCons(i io.Reader) ([]mb.Event, error) {
oneParsingIsCorrect := false
keyMap, err := lineToMap(line)
if err != nil {
m.Logger().Debugf(err.Error())
m.Logger().Errorf("Error while parsing zookeeper 'cons' command %s", err.Error())
continue
}

Expand All @@ -70,14 +69,14 @@ func (m *MetricSet) parseCons(i io.Reader) ([]mb.Event, error) {
}
}

return result, nil
return result
}

func lineToMap(line string) (map[string]string, error) {
capturedPatterns := capturer.FindStringSubmatch(line)
if len(capturedPatterns) < 1 {
//Nothing captured
return nil, errors.Errorf("no data captured in '%s'", line)
return nil, fmt.Errorf("no data captured,'%s'", line)
}

keyMap := make(map[string]string)
Expand Down Expand Up @@ -105,6 +104,4 @@ func (m *MetricSet) checkRegexAndSetInt(output common.MapStr, capturedData strin
} else {
m.Logger().Errorf("parse error: empty data for key '%s'", key)
}

return
}

0 comments on commit 799c1a8

Please sign in to comment.