Skip to content

Commit

Permalink
[chore] [receiver/hostmetrics] Don't collect data for disabled metric (
Browse files Browse the repository at this point in the history
…#26474)

If `system.network.connections` metric is disabled, don't collect the
information from the host to not waste CPU cycles

Fixes
#25815
  • Loading branch information
dmitryax authored Sep 6, 2023
1 parent a09b4d7 commit 854550b
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 42 deletions.
22 changes: 22 additions & 0 deletions .chloggen/hostmetrics-dont-collect-disabled-connections-data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: receiver/hostmetrics

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Don't collect connections data from the host if system.network.connections metric is disabled to not waste CPU cycles.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [25815]

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ func (s *scraper) recordNetworkIOMetric(now pcommon.Timestamp, ioCountersSlice [
}

func (s *scraper) recordNetworkConnectionsMetrics() error {
if !s.config.Metrics.SystemNetworkConnections.Enabled {
return nil
}

ctx := context.WithValue(context.Background(), common.EnvKey, s.config.EnvMap)
now := pcommon.NewTimestampFromTime(time.Now())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,20 @@ import (

func TestScrape(t *testing.T) {
type testCase struct {
name string
config Config
bootTimeFunc func(context.Context) (uint64, error)
ioCountersFunc func(context.Context, bool) ([]net.IOCountersStat, error)
connectionsFunc func(context.Context, string) ([]net.ConnectionStat, error)
conntrackFunc func(context.Context) ([]net.FilterStat, error)
expectNetworkMetrics bool
expectedStartTime pcommon.Timestamp
newErrRegex string
initializationErr string
expectedErr string
expectedErrCount int
mutateScraper func(*scraper)
name string
config Config
bootTimeFunc func(context.Context) (uint64, error)
ioCountersFunc func(context.Context, bool) ([]net.IOCountersStat, error)
connectionsFunc func(context.Context, string) ([]net.ConnectionStat, error)
conntrackFunc func(context.Context) ([]net.FilterStat, error)
expectConntrakMetrics bool
expectConnectionsMetric bool
expectedStartTime pcommon.Timestamp
newErrRegex string
initializationErr string
expectedErr string
expectedErrCount int
mutateScraper func(*scraper)
}

testCases := []testCase{
Expand All @@ -45,61 +46,70 @@ func TestScrape(t *testing.T) {
config: Config{
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
},
expectNetworkMetrics: true,
expectConntrakMetrics: true,
expectConnectionsMetric: true,
},
{
name: "Standard with direction removed",
config: Config{
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
},
expectNetworkMetrics: true,
expectConntrakMetrics: true,
expectConnectionsMetric: true,
},
{
name: "Validate Start Time",
config: Config{
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
},
bootTimeFunc: func(context.Context) (uint64, error) { return 100, nil },
expectNetworkMetrics: true,
expectedStartTime: 100 * 1e9,
bootTimeFunc: func(context.Context) (uint64, error) { return 100, nil },
expectConntrakMetrics: true,
expectConnectionsMetric: true,
expectedStartTime: 100 * 1e9,
},
{
name: "Include Filter that matches nothing",
config: Config{
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
Include: MatchConfig{filterset.Config{MatchType: "strict"}, []string{"@*^#&*$^#)"}},
},
expectNetworkMetrics: false,
expectConntrakMetrics: false,
expectConnectionsMetric: true,
},
{
name: "Invalid Include Filter",
config: Config{
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
Include: MatchConfig{Interfaces: []string{"test"}},
},
newErrRegex: "^error creating network interface include filters:",
newErrRegex: "^error creating network interface include filters:",
expectConnectionsMetric: true,
},
{
name: "Invalid Exclude Filter",
config: Config{
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
Exclude: MatchConfig{Interfaces: []string{"test"}},
},
newErrRegex: "^error creating network interface exclude filters:",
newErrRegex: "^error creating network interface exclude filters:",
expectConnectionsMetric: true,
},
{
name: "Boot Time Error",
bootTimeFunc: func(context.Context) (uint64, error) { return 0, errors.New("err1") },
initializationErr: "err1",
name: "Boot Time Error",
bootTimeFunc: func(context.Context) (uint64, error) { return 0, errors.New("err1") },
initializationErr: "err1",
expectConnectionsMetric: true,
},
{
name: "IOCounters Error",
ioCountersFunc: func(context.Context, bool) ([]net.IOCountersStat, error) { return nil, errors.New("err2") },
expectedErr: "failed to read network IO stats: err2",
expectedErrCount: networkMetricsLen,
name: "IOCounters Error",
ioCountersFunc: func(context.Context, bool) ([]net.IOCountersStat, error) { return nil, errors.New("err2") },
expectedErr: "failed to read network IO stats: err2",
expectedErrCount: networkMetricsLen,
expectConnectionsMetric: true,
},
{
name: "Connections Error",
config: Config{MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig()},
connectionsFunc: func(context.Context, string) ([]net.ConnectionStat, error) { return nil, errors.New("err3") },
expectedErr: "failed to read TCP connections: err3",
expectedErrCount: connectionsMetricsLen,
Expand All @@ -109,8 +119,21 @@ func TestScrape(t *testing.T) {
config: Config{
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(), // conntrack metrics are disabled by default
},
conntrackFunc: func(ctx context.Context) ([]net.FilterStat, error) { return nil, errors.New("conntrack failed") },
expectNetworkMetrics: true,
conntrackFunc: func(ctx context.Context) ([]net.FilterStat, error) { return nil, errors.New("conntrack failed") },
expectConntrakMetrics: true,
expectConnectionsMetric: true,
},
{
name: "Connections metrics is disabled",
config: func() Config {
cfg := Config{MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig()}
cfg.MetricsBuilderConfig.Metrics.SystemNetworkConnections.Enabled = false
return cfg
}(),
connectionsFunc: func(ctx context.Context, s string) ([]net.ConnectionStat, error) {
panic("should not be called")
},
expectConntrakMetrics: true,
},
}

Expand Down Expand Up @@ -163,27 +186,30 @@ func TestScrape(t *testing.T) {
}
require.NoError(t, err, "Failed to scrape metrics: %v", err)

expectedMetricCount := 1
if test.expectNetworkMetrics {
expectedMetricCount := 0
if test.expectConntrakMetrics {
expectedMetricCount += 4
}
if test.expectConnectionsMetric {
expectedMetricCount++
}
assert.Equal(t, expectedMetricCount, md.MetricCount())

metrics := md.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics()
idx := 0
assertNetworkConnectionsMetricValid(t, metrics.At(idx))
if test.expectNetworkMetrics {
assertNetworkIOMetricValid(t, metrics.At(idx+1), "system.network.dropped",
if test.expectConnectionsMetric {
assertNetworkConnectionsMetricValid(t, metrics.At(idx))
idx++
}
if test.expectConntrakMetrics {
assertNetworkIOMetricValid(t, metrics.At(idx), "system.network.dropped",
test.expectedStartTime)
assertNetworkIOMetricValid(t, metrics.At(idx+2), "system.network.errors", test.expectedStartTime)
assertNetworkIOMetricValid(t, metrics.At(idx+3), "system.network.io", test.expectedStartTime)
assertNetworkIOMetricValid(t, metrics.At(idx+4), "system.network.packets",
assertNetworkIOMetricValid(t, metrics.At(idx+1), "system.network.errors", test.expectedStartTime)
assertNetworkIOMetricValid(t, metrics.At(idx+2), "system.network.io", test.expectedStartTime)
assertNetworkIOMetricValid(t, metrics.At(idx+3), "system.network.packets",
test.expectedStartTime)
internal.AssertSameTimeStampForMetrics(t, metrics, 1, 5)
idx += 4
internal.AssertSameTimeStampForMetrics(t, metrics, idx, idx+4)
}

internal.AssertSameTimeStampForMetrics(t, metrics, idx, idx+1)
})
}
}
Expand Down

0 comments on commit 854550b

Please sign in to comment.