Skip to content

Commit

Permalink
[processor/resourcedetection] Add feature gate to change family and m…
Browse files Browse the repository at this point in the history
…odel to string (#29152)

**Description:** 

Add feature gate to change family and CPU model to string, to adapt to
open-telemetry/semantic-conventions/issues/495.

**Link to tracking Issue:** Fixes #29025

**Testing:** Tested with the sample configuration on #26533.
  • Loading branch information
mx-psi authored Nov 13, 2023
1 parent 770f1c0 commit 39878d2
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 18 deletions.
28 changes: 28 additions & 0 deletions .chloggen/mx-psi_change-to-string.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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: Add `processor.resourcedetection.hostCPUModelAndFamilyAsString` feature gate to change the type of `host.cpu.family` and `host.cpu.model.id` attributes from `int` to `string`.

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

# (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: |
This feature gate will graduate to beta in the next release.
# 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: []

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package metadata // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/system/internal/metadata"

// SetHostCPUFamilyAsInt sets provided value as "host.cpu.family" attribute as int.
func (rb *ResourceBuilder) SetHostCPUFamilyAsInt(val int64) {
if rb.config.HostCPUFamily.Enabled {
rb.res.Attributes().PutInt("host.cpu.family", val)
}
}

// SetHostCPUModelIDAsInt sets provided value as "host.cpu.model.id" attribute as int.
func (rb *ResourceBuilder) SetHostCPUModelIDAsInt(val int64) {
if rb.config.HostCPUModelID.Enabled {
rb.res.Attributes().PutInt("host.cpu.model.id", val)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ resource_attributes:
enabled: false
host.cpu.family:
description: The host.cpu.family
type: int
type: string
enabled: false
host.cpu.model.id:
description: The host.cpu.model.id
type: int
type: string
enabled: false
host.cpu.model.name:
description: The host.cpu.model.name
Expand Down
46 changes: 38 additions & 8 deletions processor/resourcedetectionprocessor/internal/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strconv"

"github.com/shirou/gopsutil/v3/cpu"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/processor"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
Expand All @@ -20,6 +21,17 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/system/internal/metadata"
)

var (
hostCPUModelAndFamilyAsStringID = "processor.resourcedetection.hostCPUModelAndFamilyAsString"
hostCPUModelAndFamilyAsStringFeatureGate = featuregate.GlobalRegistry().MustRegister(
hostCPUModelAndFamilyAsStringID,
featuregate.StageAlpha,
featuregate.WithRegisterDescription("Change type of host.cpu.model.id and host.cpu.model.family to string."),
featuregate.WithRegisterFromVersion("v0.89.0"),
featuregate.WithRegisterReferenceURL("https://github.com/open-telemetry/semantic-conventions/issues/495"),
)
)

const (
// TypeStr is type of detector.
TypeStr = "system"
Expand Down Expand Up @@ -147,21 +159,39 @@ func reverseLookupHost(d *Detector) (string, error) {
func setHostCPUInfo(d *Detector, cpuInfo cpu.InfoStat) error {
d.logger.Debug("getting host's cpuinfo", zap.String("coreID", cpuInfo.CoreID))
d.rb.SetHostCPUVendorID(cpuInfo.VendorID)
family, err := strconv.ParseInt(cpuInfo.Family, 10, 64)
if err != nil {
return fmt.Errorf("failed to convert cpuinfo family to integer: %w", err)
if hostCPUModelAndFamilyAsStringFeatureGate.IsEnabled() {
d.rb.SetHostCPUFamily(cpuInfo.Family)
} else {
// https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/29025
d.logger.Warn("This attribute will change from int to string. Switch now using the feature gate.",
zap.String("attribute", "host.cpu.family"),
zap.String("feature gate", hostCPUModelAndFamilyAsStringID),
)
family, err := strconv.ParseInt(cpuInfo.Family, 10, 64)
if err != nil {
return fmt.Errorf("failed to convert cpuinfo family to integer: %w", err)
}
d.rb.SetHostCPUFamilyAsInt(family)
}
d.rb.SetHostCPUFamily(family)

// For windows, this field is left blank. See https://github.com/shirou/gopsutil/blob/v3.23.9/cpu/cpu_windows.go#L113
// Skip setting modelId if the field is blank.
// ISSUE: https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/27675
if cpuInfo.Model != "" {
model, err := strconv.ParseInt(cpuInfo.Model, 10, 64)
if err != nil {
return fmt.Errorf("failed to convert cpuinfo model to integer: %w", err)
if hostCPUModelAndFamilyAsStringFeatureGate.IsEnabled() {
d.rb.SetHostCPUModelID(cpuInfo.Model)
} else {
// https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/29025
d.logger.Warn("This attribute will change from int to string. Switch now using the feature gate.",
zap.String("attribute", "host.cpu.model.id"),
zap.String("feature gate", hostCPUModelAndFamilyAsStringID),
)
model, err := strconv.ParseInt(cpuInfo.Model, 10, 64)
if err != nil {
return fmt.Errorf("failed to convert cpuinfo model to integer: %w", err)
}
d.rb.SetHostCPUModelIDAsInt(model)
}
d.rb.SetHostCPUModelID(model)
}

d.rb.SetHostCPUModelName(cpuInfo.ModelName)
Expand Down

0 comments on commit 39878d2

Please sign in to comment.