diff --git a/.chloggen/add_host_cpuinfo.yaml b/.chloggen/add_host_cpuinfo.yaml new file mode 100644 index 0000000000000..6cabe05151abb --- /dev/null +++ b/.chloggen/add_host_cpuinfo.yaml @@ -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: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: processor/resourcedetection + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Added support for host's cpuinfo attributes. This information is retrieved from /proc/cpuinfo. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [26532] + +# (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: [] diff --git a/processor/resourcedetectionprocessor/internal/system/internal/metadata/generated_config.go b/processor/resourcedetectionprocessor/internal/system/internal/metadata/generated_config.go index 3abbed5b3b135..4df92d8943341 100644 --- a/processor/resourcedetectionprocessor/internal/system/internal/metadata/generated_config.go +++ b/processor/resourcedetectionprocessor/internal/system/internal/metadata/generated_config.go @@ -14,6 +14,7 @@ type ResourceAttributesConfig struct { HostName ResourceAttributeConfig `mapstructure:"host.name"` OsDescription ResourceAttributeConfig `mapstructure:"os.description"` OsType ResourceAttributeConfig `mapstructure:"os.type"` + CPUInfo ResourceAttributeConfig `mapstructure:"host.cpu""` } func DefaultResourceAttributesConfig() ResourceAttributesConfig { @@ -33,5 +34,8 @@ func DefaultResourceAttributesConfig() ResourceAttributesConfig { OsType: ResourceAttributeConfig{ Enabled: true, }, + CPUInfo: ResourceAttributeConfig{ + Enabled: false, + }, } } diff --git a/processor/resourcedetectionprocessor/internal/system/internal/metadata/generated_resource.go b/processor/resourcedetectionprocessor/internal/system/internal/metadata/generated_resource.go index b28dc2be6b0c5..626c15b0bc1c2 100644 --- a/processor/resourcedetectionprocessor/internal/system/internal/metadata/generated_resource.go +++ b/processor/resourcedetectionprocessor/internal/system/internal/metadata/generated_resource.go @@ -3,6 +3,7 @@ package metadata import ( + "github.com/shirou/gopsutil/v3/cpu" "go.opentelemetry.io/collector/pdata/pcommon" ) @@ -56,6 +57,18 @@ func (rb *ResourceBuilder) SetOsType(val string) { } } +// SetHostCPUInfo sets provided cpuinfo as "host.cpu.*" attributes. +func (rb *ResourceBuilder) SetHostCPUInfo(val cpu.InfoStat) { + if rb.config.CPUInfo.Enabled { + rb.res.Attributes().PutStr("host.cpu.vendor.id", val.VendorID) + rb.res.Attributes().PutStr("host.cpu.family", val.Family) + rb.res.Attributes().PutStr("host.cpu.model.id", val.Model) + rb.res.Attributes().PutStr("host.cpu.model.name", val.ModelName) + rb.res.Attributes().PutInt("host.cpu.stepping", int64(val.Stepping)) + rb.res.Attributes().PutInt("host.cpu.cache.l2.size", int64(val.CacheSize)) + } +} + // Emit returns the built resource and resets the internal builder state. func (rb *ResourceBuilder) Emit() pcommon.Resource { r := rb.res diff --git a/processor/resourcedetectionprocessor/internal/system/system.go b/processor/resourcedetectionprocessor/internal/system/system.go index 0366b62887092..e496df52ee9c6 100644 --- a/processor/resourcedetectionprocessor/internal/system/system.go +++ b/processor/resourcedetectionprocessor/internal/system/system.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" + "github.com/shirou/gopsutil/v3/cpu" "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/processor" conventions "go.opentelemetry.io/collector/semconv/v1.6.1" @@ -74,6 +75,11 @@ 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) + } + for _, source := range d.cfg.HostnameSources { getHostFromSource := hostnameSourcesMap[source] hostname, err = getHostFromSource(d) @@ -89,6 +95,9 @@ func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schem } d.rb.SetHostArch(hostArch) d.rb.SetOsDescription(osDescription) + if len(cpuInfo) > 0 { + d.rb.SetHostCPUInfo(cpuInfo[0]) + } return d.rb.Emit(), conventions.SchemaURL, nil } d.logger.Debug(err.Error())