Skip to content

Commit

Permalink
[metricbeat] fix system/service filtering issues (#17415) (#17489)
Browse files Browse the repository at this point in the history
* fix fitlering issues

* add changelog entry

* update tests

(cherry picked from commit d1a9176)
  • Loading branch information
fearful-symmetry authored Apr 6, 2020
1 parent c8cfd68 commit 8d381b6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Fix Disk Used and Disk Usage visualizations in the Metricbeat System dashboards. {issue}12435[12435] {pull}17272[17272]
- Fix missing Accept header for Prometheus and OpenMetrics module. {issue}16870[16870] {pull}17291[17291]
- Combine cloudwatch aggregated metrics into single event. {pull}17345[17345]
- Fix how we filter services by name in system/service {pull}17400[17400]
- Fix cloudwatch metricset missing tags collection. {issue}17419[17419] {pull}17424[17424]
- check if cpuOptions field is nil in DescribeInstances output in ec2 metricset. {pull}17418[17418]

Expand Down
14 changes: 13 additions & 1 deletion metricbeat/module/system/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package service

import (
"path/filepath"

"github.com/coreos/go-systemd/v22/dbus"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
Expand Down Expand Up @@ -86,7 +88,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
// of an error set the Error field of mb.Event or simply call report.Error().
func (m *MetricSet) Fetch(report mb.ReporterV2) error {

units, err := m.unitList(m.conn, m.cfg.StateFilter, append([]string{"*.service"}, m.cfg.PatternFilter...))
units, err := m.unitList(m.conn, m.cfg.StateFilter, m.cfg.PatternFilter)
if err != nil {
return errors.Wrap(err, "error getting list of running units")
}
Expand All @@ -97,6 +99,16 @@ func (m *MetricSet) Fetch(report mb.ReporterV2) error {
continue
}

match, err := filepath.Match("*.service", unit.Name)
if err != nil {
m.Logger().Errorf("Error matching unit service %s: %s", unit.Name, err)
continue
}
// If we don't have a *.service, skip
if !match {
continue
}

props, err := getProps(m.conn, unit.Name)
if err != nil {
m.Logger().Errorf("error getting properties for service: %s", err)
Expand Down
32 changes: 32 additions & 0 deletions metricbeat/module/system/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ import (
"github.com/elastic/beats/v7/libbeat/common"
)

var exampleUnits = []dbus.UnitStatus{
dbus.UnitStatus{
Name: "sshd.service",
},
dbus.UnitStatus{
Name: "metricbeat.service",
},
dbus.UnitStatus{
Name: "filebeat.service",
},
}

func TestFormProps(t *testing.T) {
testUnit := dbus.UnitStatus{
Name: "test.service",
Expand Down Expand Up @@ -72,3 +84,23 @@ func TestFormProps(t *testing.T) {
assert.Equal(t, event.MetricSetFields["state_since"], testEvent["state_since"])
assert.NotEmpty(t, event.RootFields)
}

func TestFilterEmpty(t *testing.T) {

filtersBad := []string{
"asdf",
}
shouldNotMatch, err := matchUnitPatterns(filtersBad, exampleUnits)
assert.NoError(t, err)
assert.Empty(t, shouldNotMatch)
}

func TestFilterMatches(t *testing.T) {
filtersMatch := []string{
"ssh*",
}

shouldMatch, err := matchUnitPatterns(filtersMatch, exampleUnits)
assert.NoError(t, err)
assert.Len(t, shouldMatch, 1)
}

0 comments on commit 8d381b6

Please sign in to comment.