Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[system] - Add a flag to the system module to control whether metrics failures mark agent as degraded #42160

Merged
merged 14 commits into from
Jan 17, 2025
Merged
3 changes: 3 additions & 0 deletions metricbeat/docs/modules/system.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ metricbeat.modules:
# Only effective for Windows.
# You should use this option if running beats on machins with more than 64 cores.
#use_performance_counters: false

# Enable below config to mark metricset as degraded if partial metrics are emitted
#degrade_on_partial: false
----

[float]
Expand Down
8 changes: 8 additions & 0 deletions metricbeat/mb/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,17 @@ type PartialMetricsError struct {
}

func (p PartialMetricsError) Error() string {
if p.Err == nil {
return "Partial metrics error"
VihasMakwana marked this conversation as resolved.
Show resolved Hide resolved
}
return p.Err.Error()
}

func (p PartialMetricsError) Is(other error) bool {
_, is := other.(PartialMetricsError)
return is
VihasMakwana marked this conversation as resolved.
Show resolved Hide resolved
}

func (p PartialMetricsError) Unwrap() error {
return p.Err
}
3 changes: 3 additions & 0 deletions metricbeat/metricbeat.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ metricbeat.modules:
# You should use this option if running beats on machins with more than 64 cores.
#use_performance_counters: false

# Enable below config to mark metricset as degraded if partial metrics are emitted
#degrade_on_partial: false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This either needs more documentation or a more specific name. Users reading this will have no idea what partial metrics mean or what can lead to this happening.

I like the hostmetrics receiver's mute_* settings as a model: https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/hostmetricsreceiver#process

Maybe we can't detect in code the specific type of error yet, but we can at least document the error classes this actually covers. We can also expect we may need or want to extend this to be more specific.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I'll document this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cmacknz I've documented in detail in reference.yml. I'll also open up a separate ticket to update https://www.elastic.co/guide/en/beats/metricbeat/current/metricbeat-metricset-system-process.html, once we're satisfied with the wordings here. Let me know what do you think


#------------------------------ Aerospike Module ------------------------------
- module: aerospike
metricsets: ["namespace"]
Expand Down
3 changes: 3 additions & 0 deletions metricbeat/module/system/_meta/config.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,6 @@
# Only effective for Windows.
# You should use this option if running beats on machins with more than 64 cores.
#use_performance_counters: false

# Enable below config to mark metricset as degraded if partial metrics are emitted
#degrade_on_partial: false
VihasMakwana marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions metricbeat/module/system/_meta/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
process.include_top_n:
by_cpu: 5 # include top 5 processes by CPU
by_memory: 5 # include top 5 processes by memory
degrade_on_partial: false # mark metricset as degraded if partial metrics are emitted
# Configure the mount point of the host’s filesystem for use in monitoring a host from within a container
# hostfs: "/hostfs"

Expand Down
23 changes: 18 additions & 5 deletions metricbeat/module/system/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ func init() {
// MetricSet that fetches process metrics.
type MetricSet struct {
mb.BaseMetricSet
stats *process.Stats
perCPU bool
setpid int
stats *process.Stats
perCPU bool
setpid int
degradeOnPartial bool
}

// New creates and returns a new MetricSet.
Expand All @@ -72,7 +73,12 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
if config.Pid != 0 && config.Procs[0] != ".*" {
logp.L().Warnf("`process.pid` set to %d, but `processes` is set to a non-default value. Metricset will only report metrics for pid %d", config.Pid, config.Pid)
}

degradedConf := struct {
DegradeOnPartial bool `config:"degrade_on_partial"`
}{}
if err := base.Module().UnpackConfig(&degradedConf); err != nil {
logp.L().Warnf("Failed to unpack config; degraded mode will be disabled for partial metrics: %v", err)
}
m := &MetricSet{
BaseMetricSet: base,
stats: &process.Stats{
Expand All @@ -88,7 +94,8 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
IgnoreRootCgroups: true,
},
},
perCPU: config.IncludePerCPU,
perCPU: config.IncludePerCPU,
degradeOnPartial: degradedConf.DegradeOnPartial,
}

m.setpid = config.Pid
Expand Down Expand Up @@ -119,6 +126,9 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error {
// return only if the error is fatal in nature
return fmt.Errorf("process stats: %w", err)
} else if (err != nil && errors.Is(err, process.NonFatalErr{})) {
if m.degradeOnPartial {
return fmt.Errorf("error fetching process list: %w", err)
}
err = mb.PartialMetricsError{Err: err}
}

Expand All @@ -138,6 +148,9 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error {
// return only if the error is fatal in nature
return fmt.Errorf("error fetching pid %d: %w", m.setpid, err)
} else if (err != nil && errors.Is(err, process.NonFatalErr{})) {
if m.degradeOnPartial {
return fmt.Errorf("error fetching process list: %w", err)
}
err = mb.PartialMetricsError{Err: err}
}
// if error is non-fatal, emit partial metrics.
Expand Down
29 changes: 29 additions & 0 deletions metricbeat/module/system/process/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/stretchr/testify/assert"

"github.com/elastic/beats/v7/metricbeat/mb"
mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing"
_ "github.com/elastic/beats/v7/metricbeat/module/system"
"github.com/elastic/elastic-agent-libs/logp"
Expand Down Expand Up @@ -55,6 +56,34 @@ func TestFetch(t *testing.T) {
events[0].BeatEvent("system", "process").Fields.StringToPrint())
}

func TestFetchDegradeOnPartial(t *testing.T) {
logp.DevelopmentSetup()
config := getConfig()
config["degrade_on_partial"] = true

f := mbtest.NewReportingMetricSetV2Error(t, config)
events, errs := mbtest.ReportingFetchV2Error(f)
if len(errs) > 0 {
for _, err := range errs {
assert.NotErrorIsf(t, err, mb.PartialMetricsError{}, "Expected non-fatal error, got %v", err)
}
} else {
assert.NotEmpty(t, events)

time.Sleep(2 * time.Second)
VihasMakwana marked this conversation as resolved.
Show resolved Hide resolved

events, errs = mbtest.ReportingFetchV2Error(f)
for _, err := range errs {
assert.ErrorIsf(t, err, process.NonFatalErr{}, "Expected non-fatal error, got %v", err)
}
assert.NotEmpty(t, events)

t.Logf("fetched %d events, showing events[0]:", len(events))
t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(),
events[0].BeatEvent("system", "process").Fields.StringToPrint())
}
}

func TestFetchSinglePid(t *testing.T) {
logp.DevelopmentSetup()

Expand Down
23 changes: 19 additions & 4 deletions metricbeat/module/system/process_summary/process_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/elastic/beats/v7/libbeat/common/transform/typeconv"
"github.com/elastic/beats/v7/metricbeat/mb"
"github.com/elastic/beats/v7/metricbeat/mb/parse"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/mapstr"
"github.com/elastic/elastic-agent-system-metrics/metric/system/process"
"github.com/elastic/elastic-agent-system-metrics/metric/system/resolve"
Expand All @@ -50,17 +51,28 @@ func init() {
// multiple fetch calls.
type MetricSet struct {
mb.BaseMetricSet
sys resolve.Resolver
sys resolve.Resolver
degradeOnPartial bool
}

// New create a new instance of the MetricSet
// Part of new is also setting up the configuration by processing additional
// configuration entries if needed.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
sys := base.Module().(resolve.Resolver)
sys, ok := base.Module().(resolve.Resolver)
if !ok {
return nil, fmt.Errorf("resolver cannot be cast from the module")
}
degradedConf := struct {
DegradeOnPartial bool `config:"degrade_on_partial"`
}{}
if err := base.Module().UnpackConfig(&degradedConf); err != nil {
logp.L().Warnf("Failed to unpack config; degraded mode will be disabled for partial metrics: %v", err)
}
return &MetricSet{
BaseMetricSet: base,
sys: sys,
BaseMetricSet: base,
sys: sys,
degradeOnPartial: degradedConf.DegradeOnPartial,
}, nil
}

Expand All @@ -74,6 +86,9 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error {
// return only if the error is fatal in nature
return fmt.Errorf("error fetching process list: %w", degradeErr)
} else if (degradeErr != nil && errors.Is(degradeErr, process.NonFatalErr{})) {
if m.degradeOnPartial {
return fmt.Errorf("error fetching process list: %w", degradeErr)
}
degradeErr = mb.PartialMetricsError{Err: degradeErr}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elastic/beats/v7/metricbeat/mb"
mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing"
_ "github.com/elastic/beats/v7/metricbeat/module/system"
"github.com/elastic/elastic-agent-libs/logp"
Expand Down Expand Up @@ -58,6 +59,27 @@ func TestFetch(t *testing.T) {
require.NoError(t, err)

}
func TestFetchDegradeOnPartial(t *testing.T) {
logp.DevelopmentSetup()
config := getConfig()
config["degrade_on_partial"] = true

f := mbtest.NewReportingMetricSetV2Error(t, config)
events, errs := mbtest.ReportingFetchV2Error(f)
if len(errs) > 0 {
for _, err := range errs {
assert.NotErrorIsf(t, err, mb.PartialMetricsError{}, "Expected non-fatal error, got %v", err)
}
} else {
require.NotEmpty(t, events)
event := events[0].BeatEvent("system", "process_summary").Fields
t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(),
event.StringToPrint())

_, err := event.GetValue("system.process.summary")
require.NoError(t, err)
}
}

func TestStateNames(t *testing.T) {
logp.DevelopmentSetup()
Expand All @@ -80,7 +102,8 @@ func TestStateNames(t *testing.T) {
assert.NotZero(t, event["total"])

var sum int
total := event["total"].(int)
total, ok := event["total"].(int)
require.Truef(t, ok, "Expected int got %T", event["total"])
for key, val := range event {
if key == "total" {
continue
Expand Down
1 change: 1 addition & 0 deletions metricbeat/modules.d/system.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
process.include_top_n:
by_cpu: 5 # include top 5 processes by CPU
by_memory: 5 # include top 5 processes by memory
degrade_on_partial: false # mark metricset as degraded if partial metrics are emitted
# Configure the mount point of the host’s filesystem for use in monitoring a host from within a container
# hostfs: "/hostfs"

Expand Down
3 changes: 3 additions & 0 deletions x-pack/metricbeat/metricbeat.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ metricbeat.modules:
# You should use this option if running beats on machins with more than 64 cores.
#use_performance_counters: false

# Enable below config to mark metricset as degraded if partial metrics are emitted
#degrade_on_partial: false

#------------------------------- ActiveMQ Module -------------------------------
- module: activemq
metricsets: ['broker', 'queue', 'topic']
Expand Down
Loading