Skip to content

Commit

Permalink
review comments fix
Browse files Browse the repository at this point in the history
  • Loading branch information
thugrock7 committed Jul 25, 2024
1 parent 8e84588 commit 0646f58
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 36 deletions.
19 changes: 8 additions & 11 deletions instrumentation/opentelemetry/internal/metrics/linux_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 39 in instrumentation/opentelemetry/internal/metrics/linux_metrics.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/linux_metrics.go#L39

Added line #L39 was not covered by tests
}
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) {
Expand Down
20 changes: 5 additions & 15 deletions instrumentation/opentelemetry/internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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

Check warning on line 44 in instrumentation/opentelemetry/internal/metrics/metrics.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/internal/metrics/metrics.go#L44

Added line #L44 was not covered by tests
}
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,
Expand Down
16 changes: 6 additions & 10 deletions instrumentation/opentelemetry/internal/metrics/noop_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 0646f58

Please sign in to comment.