Skip to content

Commit

Permalink
chore: merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
VihasMakwana committed Dec 3, 2024
1 parent 3d0e745 commit eb83e60
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 41 deletions.
8 changes: 4 additions & 4 deletions metric/cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ func WithPerformanceCounter() OptionFunc {

// Fetch collects a new sample of the CPU usage metrics.
// This will overwrite the currently stored samples.
func (m *Monitor) Fetch(opts ...OptionFunc) (Metrics, error) {
metric, err := Get(m.Hostfs, opts...)
func (m *Monitor) Fetch() (Metrics, error) {
metric, err := Get(m)
if err != nil {
return Metrics{}, fmt.Errorf("error fetching CPU metrics: %w", err)
}
Expand All @@ -107,8 +107,8 @@ func (m *Monitor) Fetch(opts ...OptionFunc) (Metrics, error) {

// FetchCores collects a new sample of CPU usage metrics per-core
// This will overwrite the currently stored samples.
func (m *Monitor) FetchCores(opts ...OptionFunc) ([]Metrics, error) {
metric, err := Get(m.Hostfs, opts...)
func (m *Monitor) FetchCores() ([]Metrics, error) {
metric, err := Get(m)
if err != nil {
return nil, fmt.Errorf("error fetching CPU metrics: %w", err)
}
Expand Down
3 changes: 1 addition & 2 deletions metric/cpu/metrics_aix.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"os"

"github.com/elastic/elastic-agent-libs/opt"
"github.com/elastic/elastic-agent-system-metrics/metric/system/resolve"
)

func init() {
Expand All @@ -58,7 +57,7 @@ func tick2msec(val uint64) uint64 {
}

// Get returns a metrics object for CPU data
func Get(_ resolve.Resolver, _ ...OptionFunc) (CPUMetrics, error) {
func Get(m *Monitor) (CPUMetrics, error) {

totals, err := getCPUTotals()
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions metric/cpu/metrics_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ import (
"github.com/shirou/gopsutil/v4/cpu"

"github.com/elastic/elastic-agent-libs/opt"
"github.com/elastic/elastic-agent-system-metrics/metric/system/resolve"
)

// Get is the Darwin implementation of Get
func Get(_ resolve.Resolver, _ ...OptionFunc) (CPUMetrics, error) {
func Get(m *Monitor) (CPUMetrics, error) {
// We're using the gopsutil library here.
// The code used by both gosigar and go-sysinfo appears to be
// the same code as gopsutil, including copy-pasted comments.
Expand Down
3 changes: 1 addition & 2 deletions metric/cpu/metrics_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ import (
"unsafe"

"github.com/elastic/elastic-agent-libs/opt"
"github.com/elastic/elastic-agent-system-metrics/metric/system/resolve"
)

// Get is the OpenBSD implementation of get
func Get(_ resolve.Resolver, _ ...OptionFunc) (CPUMetrics, error) {
func Get(m *Monitor) (CPUMetrics, error) {

// see man 2 sysctl
loadGlobal := [C.CPUSTATES]C.long{
Expand Down
6 changes: 3 additions & 3 deletions metric/cpu/metrics_procfs_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ import (
"os"
"strconv"
"strings"

"github.com/elastic/elastic-agent-system-metrics/metric/system/resolve"
)

// Get returns a metrics object for CPU data
func Get(procfs resolve.Resolver, _ ...OptionFunc) (CPUMetrics, error) {
func Get(m *Monitor) (CPUMetrics, error) {
procfs := m.Hostfs

path := procfs.ResolveHostFS("/proc/stat")
fd, err := os.Open(path)
defer func() {
Expand Down
6 changes: 3 additions & 3 deletions metric/cpu/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ func TestMonitorSample(t *testing.T) {
}

func TestCoresMonitorSample(t *testing.T) {
cores, err := New(systemtests.DockerTestResolver())
require.NoError(t, err)

cpuMetrics, err := Get(systemtests.DockerTestResolver())
cpuMetrics, err := Get(cores)
assert.NoError(t, err, "error in Get()")

cores, err := New(systemtests.DockerTestResolver())
require.NoError(t, err)
cores.lastSample = CPUMetrics{list: make([]CPU, len(cpuMetrics.list))}
sample, err := cores.FetchCores()
require.NoError(t, err)
Expand Down
31 changes: 6 additions & 25 deletions metric/cpu/metrics_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ import (
"fmt"
"time"

"github.com/elastic/elastic-agent-libs/helpers/windows/pdh"
"github.com/elastic/elastic-agent-libs/opt"
"github.com/elastic/elastic-agent-system-metrics/metric/system/resolve"
"github.com/elastic/gosigar/sys/windows"
)

Expand All @@ -39,43 +37,26 @@ var (
totalUserTimeCounter = fmt.Sprintf(processorInformationCounter, "*", "% User Time")
)

var (
query *pdh.Query
qError error
)

// Get fetches Windows CPU system times
func Get(_ resolve.Resolver, opts ...OptionFunc) (CPUMetrics, error) {
op := option{}
for _, o := range opts {
o(&op)
}
if !op.usePerformanceCounter {
func Get(m *Monitor) (CPUMetrics, error) {
if m.query == nil {
return defaultGet()
}
globalMetrics := CPUMetrics{}

// Check if the query has already been initialized, if not, initialize it
if query == nil {
query, qError = buildQuery() // Build query if not already done
if qError != nil {
return CPUMetrics{}, qError
}
}

if err := query.CollectData(); err != nil {
if err := m.query.CollectData(); err != nil {
return globalMetrics, err
}

kernelRawData, err := query.GetRawCounterArray(totalKernelTimeCounter, true)
kernelRawData, err := m.query.GetRawCounterArray(totalKernelTimeCounter, true)
if err != nil {
return globalMetrics, fmt.Errorf("error calling GetRawCounterArray for kernel counter: %w", err)
}
idleRawData, err := query.GetRawCounterArray(totalIdleTimeCounter, true)
idleRawData, err := m.query.GetRawCounterArray(totalIdleTimeCounter, true)
if err != nil {
return globalMetrics, fmt.Errorf("error calling GetRawCounterArray for idle counter: %w", err)
}
userRawData, err := query.GetRawCounterArray(totalUserTimeCounter, true)
userRawData, err := m.query.GetRawCounterArray(totalUserTimeCounter, true)
if err != nil {
return globalMetrics, fmt.Errorf("error calling GetRawCounterArray for user counter: %w", err)
}
Expand Down

0 comments on commit eb83e60

Please sign in to comment.