[CC-7434] Skip collecting data directory metrics in dev mode (#20521)

* Skip collecting data directory stats in dev mode

In dev mode, the data directory is not set, so this metrics collection would
always fail and logs errors.

* Log collection errors at DEBUG level

There isn't much action a user can take to fix these errors, so
logging them as DEBUG rather than ERROR.
This commit is contained in:
Melissa Kam 2024-02-07 16:59:06 -06:00 committed by GitHub
parent 2ee32b1980
commit c95380e3da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 57 additions and 7 deletions

View File

@ -87,7 +87,7 @@ func (c *Collector) collect() {
// Determine up-time // Determine up-time
uptime, err := host.Uptime() uptime, err := host.Uptime()
if err != nil { 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 uptime = 0
} }
hs.Uptime = uptime hs.Uptime = uptime
@ -95,7 +95,7 @@ func (c *Collector) collect() {
// Collect memory stats // Collect memory stats
mstats, err := c.collectMemoryStats() mstats, err := c.collectMemoryStats()
if err != nil { 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{} mstats = &MemoryStats{}
} }
hs.Memory = mstats hs.Memory = mstats
@ -103,17 +103,19 @@ func (c *Collector) collect() {
// Collect cpu stats // Collect cpu stats
cpus, err := c.collectCPUStats() cpus, err := c.collectCPUStats()
if err != nil { 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{} cpus = []*CPUStats{}
} }
hs.CPU = cpus hs.CPU = cpus
// Collect disk stats // Collect disk stats
diskStats, err := c.collectDiskStats(c.dataDir) if c.dataDir != "" {
if err != nil { diskStats, err := c.collectDiskStats(c.dataDir)
c.logger.Error("failed to collect dataDir disk stats", "error", err) 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. // Update the collected status object.
c.hostStatsLock.Lock() c.hostStatsLock.Lock()

View File

@ -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)
}
})
}
}