Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[processor/resourcedetection] Fetch CPU info only if required #33774

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
mx-psi marked this conversation as resolved.
Show resolved Hide resolved
}

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