Skip to content

Commit

Permalink
[processor/resourcedetection] Fetch CPU info only if required
Browse files Browse the repository at this point in the history
Signed-off-by: ChrsMark <chrismarkou92@gmail.com>
  • Loading branch information
ChrsMark committed Jun 26, 2024
1 parent fc97472 commit 7b6139b
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 3 deletions.
27 changes: 27 additions & 0 deletions .chloggen/call_cpu_info_only_on_demand.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: resourcedetectionprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fetch CPU info only if related attributes are enabled

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [33774]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
8 changes: 8 additions & 0 deletions internal/metadataproviders/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/docker/docker v25.0.5+incompatible
github.com/hashicorp/consul/api v1.29.1
github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig v0.103.0
github.com/shirou/gopsutil/v4 v4.24.5
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/collector/semconv v0.103.0
go.opentelemetry.io/otel v1.27.0
Expand All @@ -32,6 +33,7 @@ require (
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
Expand All @@ -53,6 +55,7 @@ require (
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand All @@ -69,8 +72,13 @@ require (
github.com/openshift/client-go v0.0.0-20210521082421-73d9475a9142 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.27.0 // indirect
Expand Down
23 changes: 23 additions & 0 deletions internal/metadataproviders/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions internal/metadataproviders/system/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/Showmax/go-fqdn"
"github.com/shirou/gopsutil/v4/cpu"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
Expand Down Expand Up @@ -69,6 +70,9 @@ type Provider interface {

// HostMACs returns the host's MAC addresses
HostMACs() ([]net.HardwareAddr, error)

// CPUInfo returns the host's CPU info
CPUInfo() ([]cpu.InfoStat, error)
}

type systemMetadataProvider struct {
Expand Down Expand Up @@ -216,3 +220,7 @@ func (p systemMetadataProvider) HostMACs() (macs []net.HardwareAddr, err error)
}
return macs, err
}

func (p systemMetadataProvider) CPUInfo() ([]cpu.InfoStat, error) {
return cpu.Info()
}
11 changes: 8 additions & 3 deletions processor/resourcedetectionprocessor/internal/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,14 @@ func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schem
return pcommon.NewResource(), "", fmt.Errorf("failed getting OS description: %w", err)
}

cpuInfo, err := cpu.Info()
if err != nil {
return pcommon.NewResource(), "", fmt.Errorf("failed getting host cpuinfo: %w", err)
var cpuInfo []cpu.InfoStat
if d.cfg.ResourceAttributes.HostCPUCacheL2Size.Enabled || d.cfg.ResourceAttributes.HostCPUFamily.Enabled ||
d.cfg.ResourceAttributes.HostCPUModelID.Enabled || d.cfg.ResourceAttributes.HostCPUVendorID.Enabled ||
d.cfg.ResourceAttributes.HostCPUModelName.Enabled || d.cfg.ResourceAttributes.HostCPUStepping.Enabled {
cpuInfo, err = d.provider.CPUInfo()
if err != nil {
return pcommon.NewResource(), "", fmt.Errorf("failed getting host cpuinfo: %w", err)
}
}

for _, source := range d.cfg.HostnameSources {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net"
"testing"

"github.com/shirou/gopsutil/v4/cpu"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -77,6 +78,11 @@ func (m *mockMetadata) HostMACs() ([]net.HardwareAddr, error) {
return args.Get(0).([]net.HardwareAddr), args.Error(1)
}

func (m *mockMetadata) CPUInfo() ([]cpu.InfoStat, error) {
args := m.MethodCalled("CPUInfo")
return args.Get(0).([]cpu.InfoStat), args.Error(1)
}

var (
testIPsAttribute = []any{"192.168.1.140", "fe80::abc2:4a28:737a:609e"}
testIPsAddresses = []net.IP{net.ParseIP(testIPsAttribute[0].(string)), net.ParseIP(testIPsAttribute[1].(string))}
Expand Down Expand Up @@ -159,6 +165,7 @@ func TestDetectFQDNAvailable(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, conventions.SchemaURL, schemaURL)
md.AssertExpectations(t)
md.AssertNotCalled(t, "CPUInfo")

expected := map[string]any{
conventions.AttributeHostName: "fqdn",
Expand Down Expand Up @@ -330,6 +337,40 @@ func TestDetectError(t *testing.T) {
}, res.Attributes().AsRaw())
}

func TestDetectCPUInfo(t *testing.T) {
md := &mockMetadata{}
md.On("FQDN").Return("fqdn", nil)
md.On("OSDescription").Return("Ubuntu 22.04.2 LTS (Jammy Jellyfish)", nil)
md.On("OSType").Return("darwin", nil)
md.On("HostID").Return("2", nil)
md.On("HostArch").Return("amd64", nil)
md.On("HostIPs").Return(testIPsAddresses, nil)
md.On("HostMACs").Return(testMACsAddresses, nil)
md.On("CPUInfo").Return([]cpu.InfoStat{{Family: "some"}}, nil)

cfg := allEnabledConfig()
cfg.HostCPUFamily.Enabled = true
detector := newTestDetector(md, []string{"dns"}, cfg)
res, schemaURL, err := detector.Detect(context.Background())
require.NoError(t, err)
assert.Equal(t, conventions.SchemaURL, schemaURL)
md.AssertExpectations(t)

expected := map[string]any{
conventions.AttributeHostName: "fqdn",
conventions.AttributeOSDescription: "Ubuntu 22.04.2 LTS (Jammy Jellyfish)",
conventions.AttributeOSType: "darwin",
conventions.AttributeHostID: "2",
conventions.AttributeHostArch: conventions.AttributeHostArchAMD64,
"host.ip": testIPsAttribute,
"host.mac": testMACsAttribute,
"host.cpu.family": "some",
}

assert.Equal(t, expected, res.Attributes().AsRaw())

}

func newTestDetector(mock *mockMetadata, hostnameSources []string, resCfg metadata.ResourceAttributesConfig) *Detector {
return &Detector{
provider: mock,
Expand Down

0 comments on commit 7b6139b

Please sign in to comment.