diff --git a/instrumentation/opentelemetry/internal/metrics/linux_metrics.go b/instrumentation/opentelemetry/internal/metrics/linux_metrics.go index 4139291..6ac10eb 100644 --- a/instrumentation/opentelemetry/internal/metrics/linux_metrics.go +++ b/instrumentation/opentelemetry/internal/metrics/linux_metrics.go @@ -32,26 +32,23 @@ type linuxMetrics struct { cpuSecondsTotal float64 } -func newSystemMetrics() systemMetrics { - return &linuxMetrics{} -} - -func (lm *linuxMetrics) getCurrentMetrics() error { +func newSystemMetrics() (systemMetrics, error) { + lm := &linuxMetrics{} stats, err := lm.processStatsFromPid(os.Getpid()) if err != nil { - return err + return nil, err } lm.memory = stats.rss * pageSize lm.cpuSecondsTotal = (stats.stime + stats.utime + stats.cstime + stats.cutime) / clkTck - return nil + return lm, nil } -func (lm *linuxMetrics) getMemory() (float64, error) { - return lm.memory, nil +func (lm *linuxMetrics) getMemory() float64 { + return lm.memory } -func (lm *linuxMetrics) getCPU() (float64, error) { - return lm.cpuSecondsTotal, nil +func (lm *linuxMetrics) getCPU() float64 { + return lm.cpuSecondsTotal } func (lm *linuxMetrics) processStatsFromPid(pid int) (*processStats, error) { diff --git a/instrumentation/opentelemetry/internal/metrics/metrics.go b/instrumentation/opentelemetry/internal/metrics/metrics.go index 73841eb..1661cb6 100644 --- a/instrumentation/opentelemetry/internal/metrics/metrics.go +++ b/instrumentation/opentelemetry/internal/metrics/metrics.go @@ -11,9 +11,8 @@ import ( const meterName = "goagent.hypertrace.org/metrics" type systemMetrics interface { - getMemory() (float64, error) - getCPU() (float64, error) - getCurrentMetrics() error + getMemory() float64 + getCPU() float64 } func InitializeSystemMetrics() { @@ -40,21 +39,12 @@ func setUpMetricRecorder(meter metric.Meter) error { // Register the callback function for both cpu_seconds and memory observable gauges _, err = meter.RegisterCallback( func(ctx context.Context, result metric.Observer) error { - sysMetrics := newSystemMetrics() - err := sysMetrics.getCurrentMetrics() + sysMetrics, err := newSystemMetrics() if err != nil { return err } - cpus, err := sysMetrics.getCPU() - if err != nil { - return err - } - mem, err := sysMetrics.getMemory() - if err != nil { - return err - } - result.ObserveFloat64(cpuSeconds, cpus) - result.ObserveFloat64(memory, mem) + result.ObserveFloat64(cpuSeconds, sysMetrics.getCPU()) + result.ObserveFloat64(memory, sysMetrics.getMemory()) return nil }, cpuSeconds, memory, diff --git a/instrumentation/opentelemetry/internal/metrics/noop_metrics.go b/instrumentation/opentelemetry/internal/metrics/noop_metrics.go index 2bfea27..0688b52 100644 --- a/instrumentation/opentelemetry/internal/metrics/noop_metrics.go +++ b/instrumentation/opentelemetry/internal/metrics/noop_metrics.go @@ -4,18 +4,14 @@ package metrics type noopMetrics struct{} -func newSystemMetrics() systemMetrics { - return &noopMetrics{} +func newSystemMetrics() (systemMetrics, error) { + return &noopMetrics{}, nil } -func (nm *noopMetrics) getMemory() (float64, error) { - return 0, nil +func (nm *noopMetrics) getMemory() float64 { + return 0 } -func (nm *noopMetrics) getCPU() (float64, error) { - return 0, nil -} - -func (nm *noopMetrics) getCurrentMetrics() error { - return nil +func (nm *noopMetrics) getCPU() float64 { + return 0 }