From 7f0c40e9166fbbb0fd2c5cb48991feab97e3e6d6 Mon Sep 17 00:00:00 2001 From: hc-github-team-consul-core Date: Wed, 7 Feb 2024 18:25:46 -0500 Subject: [PATCH] Backport of [CC-7434] Skip collecting data directory metrics in dev mode into release/1.17.x (#20528) * backport of commit 5d591bbdfbf9deade355042ab6b3b1a791fc3914 * backport of commit cdfb10b454067f6fe6d7f09f465554d5ac066576 --------- Co-authored-by: Melissa Kam --- lib/hoststats/collector.go | 16 ++++++----- lib/hoststats/collector_test.go | 48 +++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 lib/hoststats/collector_test.go diff --git a/lib/hoststats/collector.go b/lib/hoststats/collector.go index fb316e0dce..97b0bd5770 100644 --- a/lib/hoststats/collector.go +++ b/lib/hoststats/collector.go @@ -87,7 +87,7 @@ func (c *Collector) collect() { // Determine up-time uptime, err := host.Uptime() if err != nil { - c.logger.Error("failed to collect uptime stats", "error", err) + c.logger.Debug("failed to collect uptime stats", "error", err) uptime = 0 } hs.Uptime = uptime @@ -95,7 +95,7 @@ func (c *Collector) collect() { // Collect memory stats mstats, err := c.collectMemoryStats() if err != nil { - c.logger.Error("failed to collect memory stats", "error", err) + c.logger.Debug("failed to collect memory stats", "error", err) mstats = &MemoryStats{} } hs.Memory = mstats @@ -103,17 +103,19 @@ func (c *Collector) collect() { // Collect cpu stats cpus, err := c.collectCPUStats() if err != nil { - c.logger.Error("failed to collect cpu stats", "error", err) + c.logger.Debug("failed to collect cpu stats", "error", err) cpus = []*CPUStats{} } hs.CPU = cpus // Collect disk stats - diskStats, err := c.collectDiskStats(c.dataDir) - if err != nil { - c.logger.Error("failed to collect dataDir disk stats", "error", err) + if c.dataDir != "" { + diskStats, err := c.collectDiskStats(c.dataDir) + if err != nil { + c.logger.Debug("failed to collect dataDir disk stats", "error", err) + } + hs.DataDirStats = diskStats } - hs.DataDirStats = diskStats // Update the collected status object. c.hostStatsLock.Lock() diff --git a/lib/hoststats/collector_test.go b/lib/hoststats/collector_test.go new file mode 100644 index 0000000000..359046b660 --- /dev/null +++ b/lib/hoststats/collector_test.go @@ -0,0 +1,48 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: BUSL-1.1 + +package hoststats + +import ( + "testing" + + "github.com/hashicorp/go-hclog" + "github.com/stretchr/testify/require" + + "github.com/hashicorp/consul/sdk/testutil" +) + +// TestCollector_collect validates that metrics for host resource usage +// are collected as expected. +func TestCollector_collect(t *testing.T) { + testcases := map[string]struct { + skipDataDir bool + }{ + "WithDataDirectory": {}, + "NoDataDirectory": { + skipDataDir: true, + }, + } + for name, tc := range testcases { + t.Run(name, func(t *testing.T) { + dataDir := "" + if !tc.skipDataDir { + dataDir = testutil.TempDir(t, "consul-config") + } + + collector := initCollector(hclog.NewNullLogger(), dataDir) + collector.collect() + + hs := collector.hostStats + require.NotNil(t, hs) + require.Greater(t, hs.Uptime, uint64(0)) + require.NotNil(t, hs.Memory) + require.NotNil(t, hs.CPU) + if !tc.skipDataDir { + require.NotNil(t, hs.DataDirStats) + } else { + require.Nil(t, hs.DataDirStats) + } + }) + } +}