Skip to content

Commit

Permalink
chore(): 采用 gopsutil 采集cpu核数 (#19)
Browse files Browse the repository at this point in the history
Co-authored-by: jimchen <jimchen@easyops.cn>
  • Loading branch information
JimChenWYU and jimchen authored Sep 13, 2023
1 parent 5364ffc commit b8f1cb5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ package infoscraper // import "github.com/open-telemetry/opentelemetry-collector
import (
"context"
"os"
"runtime"
"time"

"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/host"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
Expand Down Expand Up @@ -40,7 +40,7 @@ func newInfoScraper(_ context.Context, settings receiver.CreateSettings, cfg *Co

now: time.Now,
hostname: os.Hostname,
cpuNum: runtime.NumCPU,
cpuNum: numCPU,
system: getSystemInfo,
bootTime: host.BootTime,
}
Expand Down Expand Up @@ -87,3 +87,8 @@ func (s *scraper) scrape(_ context.Context) (pmetric.Metrics, error) {

return s.mb.Emit(), nil
}

func numCPU() int {
num, _ := cpu.Counts(true)
return num
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,8 @@ func Test_scraper_scrape(t *testing.T) {
})
}
}

func Test_numCPU(t *testing.T) {
t.Logf("cpu num: %d", numCPU())
assert.True(t, numCPU() > 0)
}

0 comments on commit b8f1cb5

Please sign in to comment.