Skip to content

Commit

Permalink
Merge pull request #153 from signalfx/cpu-timeout
Browse files Browse the repository at this point in the history
Increase CPU metadata query to 10 seconds from default of 3
  • Loading branch information
Jay Camp authored Sep 16, 2019
2 parents 56facc7 + 8fd2dc4 commit 462fd3c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
17 changes: 13 additions & 4 deletions metadata/hostmetadata/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package hostmetadata

import (
"bytes"
"context"
"errors"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"time"

"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/host"
Expand All @@ -24,10 +26,12 @@ var HostEtc = func() string {
return "/etc"
}

const cpuTimeout = 10 * time.Second

// Map library functions to unexported package variables for testing purposes.
// It would be great if we could patch this somehow
var cpuInfo = cpu.Info
var cpuCounts = cpu.Counts
var cpuInfo = cpu.InfoWithContext
var cpuCounts = cpu.CountsWithContext
var memVirtualMemory = mem.VirtualMemory
var hostInfo = host.Info

Expand Down Expand Up @@ -59,14 +63,19 @@ func GetCPU() (info *CPU, err error) {

// get physical cpu stats
var cpus []cpu.InfoStat
if cpus, err = cpuInfo(); err != nil {

// On Windows this can sometimes take longer than the default timeout (3 seconds).
ctx, cancel := context.WithTimeout(context.Background(), cpuTimeout)
defer cancel()

if cpus, err = cpuInfo(ctx); err != nil {
return info, err
}

info.HostPhysicalCPUs = len(cpus)

// get logical cpu stats
if info.HostLogicalCPUs, err = cpuCounts(true); err != nil {
if info.HostLogicalCPUs, err = cpuCounts(ctx, true); err != nil {
return info, err
}

Expand Down
15 changes: 8 additions & 7 deletions metadata/hostmetadata/host_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hostmetadata

import (
"context"
"errors"
"os"
"reflect"
Expand All @@ -14,8 +15,8 @@ import (

func TestGetCPU(t *testing.T) {
type testfixture struct {
cpuInfo func() ([]cpu.InfoStat, error)
cpuCounts func(bool) (int, error)
cpuInfo func(context.Context) ([]cpu.InfoStat, error)
cpuCounts func(context.Context, bool) (int, error)
}
tests := []struct {
name string
Expand All @@ -26,7 +27,7 @@ func TestGetCPU(t *testing.T) {
{
name: "successful host cpu info",
fixtures: testfixture{
cpuInfo: func() ([]cpu.InfoStat, error) {
cpuInfo: func(ctx context.Context) ([]cpu.InfoStat, error) {
return []cpu.InfoStat{
{
ModelName: "testmodelname",
Expand All @@ -38,7 +39,7 @@ func TestGetCPU(t *testing.T) {
},
}, nil
},
cpuCounts: func(bool) (int, error) {
cpuCounts: func(context.Context, bool) (int, error) {
return 2, nil
},
},
Expand All @@ -52,7 +53,7 @@ func TestGetCPU(t *testing.T) {
{
name: "unsuccessful host cpu info (missing cpu info)",
fixtures: testfixture{
cpuInfo: func() ([]cpu.InfoStat, error) {
cpuInfo: func(context.Context) ([]cpu.InfoStat, error) {
return nil, errors.New("bad cpu info")
},
},
Expand All @@ -67,7 +68,7 @@ func TestGetCPU(t *testing.T) {
{
name: "unsuccessful host cpu info (missing cpu counts)",
fixtures: testfixture{
cpuInfo: func() ([]cpu.InfoStat, error) {
cpuInfo: func(context.Context) ([]cpu.InfoStat, error) {
return []cpu.InfoStat{
{
ModelName: "testmodelname",
Expand All @@ -79,7 +80,7 @@ func TestGetCPU(t *testing.T) {
},
}, nil
},
cpuCounts: func(bool) (int, error) {
cpuCounts: func(context.Context, bool) (int, error) {
return 0, errors.New("bad cpu counts")
},
},
Expand Down

0 comments on commit 462fd3c

Please sign in to comment.