Skip to content

Commit 527ab5f

Browse files
committed
[receiver/hostmetricsreceiver] Add support for cpu frequency metric
**Description:** : Added support for host's cpu frequency as part of the hostmetricsreceiver. **Link to tracking Issue:** #26532 Signed-off-by: ChrsMark <[email protected]>
1 parent acae6fe commit 527ab5f

File tree

12 files changed

+181
-1
lines changed

12 files changed

+181
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: receiver/hostmetricsreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Added support for host's cpuinfo frequnecies.
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [27445]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext: In Linux the current frequency is populated using the values from /proc/cpuinfo. An os specific implementation will be needed for Windows and others.
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

receiver/hostmetricsreceiver/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.88.0
99
github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter v0.88.0
1010
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.88.0
11+
github.com/prometheus/procfs v0.11.1
1112
github.com/shirou/gopsutil/v3 v3.23.9
1213
github.com/stretchr/testify v1.8.4
1314
github.com/yusufpapurcu/wmi v1.2.3
@@ -77,7 +78,6 @@ require (
7778
github.com/prometheus/client_golang v1.17.0 // indirect
7879
github.com/prometheus/client_model v0.5.0 // indirect
7980
github.com/prometheus/common v0.45.0 // indirect
80-
github.com/prometheus/procfs v0.11.1 // indirect
8181
github.com/prometheus/statsd_exporter v0.22.7 // indirect
8282
github.com/shoenig/go-m1cpu v0.1.6 // indirect
8383
github.com/sirupsen/logrus v1.9.0 // indirect

receiver/hostmetricsreceiver/internal/scraper/cpuscraper/cpu_scraper.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package cpuscraper // import "github.com/open-telemetry/opentelemetry-collector-
55

66
import (
77
"context"
8+
"fmt"
89
"time"
910

1011
"github.com/shirou/gopsutil/v3/common"
@@ -21,6 +22,7 @@ import (
2122
)
2223

2324
const metricsLen = 2
25+
const hzInAMHz = 1_000_000
2426

2527
// scraper for CPU Metrics
2628
type scraper struct {
@@ -35,6 +37,11 @@ type scraper struct {
3537
now func() time.Time
3638
}
3739

40+
type cpuInfo struct {
41+
frequency float64
42+
processor uint
43+
}
44+
3845
// newCPUScraper creates a set of CPU related metrics
3946
func newCPUScraper(_ context.Context, settings receiver.CreateSettings, cfg *Config) *scraper {
4047
return &scraper{settings: settings, config: cfg, bootTime: host.BootTimeWithContext, times: cpu.TimesWithContext, ucal: &ucal.CPUUtilizationCalculator{}, now: time.Now}
@@ -79,5 +86,15 @@ func (s *scraper) scrape(ctx context.Context) (pmetric.Metrics, error) {
7986
}
8087
s.mb.RecordSystemCPULogicalCountDataPoint(now, int64(numCPU))
8188

89+
if s.config.MetricsBuilderConfig.Metrics.SystemCPUFrequency.Enabled {
90+
cpuInfos, err := s.getCPUInfo()
91+
if err != nil {
92+
return pmetric.NewMetrics(), scrapererror.NewPartialScrapeError(err, metricsLen)
93+
}
94+
for _, cInfo := range cpuInfos {
95+
s.mb.RecordSystemCPUFrequencyDataPoint(now, cInfo.frequency*hzInAMHz, fmt.Sprintf("cpu%d", cInfo.processor))
96+
}
97+
}
98+
8299
return s.mb.Emit(), nil
83100
}

receiver/hostmetricsreceiver/internal/scraper/cpuscraper/cpu_scraper_linux.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
package cpuscraper // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/cpuscraper"
88

99
import (
10+
"github.com/prometheus/procfs"
1011
"github.com/shirou/gopsutil/v3/cpu"
1112
"go.opentelemetry.io/collector/pdata/pcommon"
13+
"go.opentelemetry.io/collector/receiver/scrapererror"
1214

1315
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/internal/metadata"
1416
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/ucal"
@@ -35,3 +37,23 @@ func (s *scraper) recordCPUUtilization(now pcommon.Timestamp, cpuUtilization uca
3537
s.mb.RecordSystemCPUUtilizationDataPoint(now, cpuUtilization.Steal, cpuUtilization.CPU, metadata.AttributeStateSteal)
3638
s.mb.RecordSystemCPUUtilizationDataPoint(now, cpuUtilization.Iowait, cpuUtilization.CPU, metadata.AttributeStateWait)
3739
}
40+
41+
func (s *scraper) getCPUInfo() ([]cpuInfo, error) {
42+
var cpuInfos []cpuInfo
43+
fs, err := procfs.NewDefaultFS()
44+
if err != nil {
45+
return nil, scrapererror.NewPartialScrapeError(err, metricsLen)
46+
}
47+
cInf, err := fs.CPUInfo()
48+
if err != nil {
49+
return nil, scrapererror.NewPartialScrapeError(err, metricsLen)
50+
}
51+
for _, cInfo := range cInf {
52+
c := cpuInfo{
53+
frequency: cInfo.CPUMHz,
54+
processor: cInfo.Processor,
55+
}
56+
cpuInfos = append(cpuInfos, c)
57+
}
58+
return cpuInfos, nil
59+
}

receiver/hostmetricsreceiver/internal/scraper/cpuscraper/cpu_scraper_others.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ func (s *scraper) recordCPUUtilization(now pcommon.Timestamp, cpuUtilization uca
2727
s.mb.RecordSystemCPUUtilizationDataPoint(now, cpuUtilization.Idle, cpuUtilization.CPU, metadata.AttributeStateIdle)
2828
s.mb.RecordSystemCPUUtilizationDataPoint(now, cpuUtilization.Irq, cpuUtilization.CPU, metadata.AttributeStateInterrupt)
2929
}
30+
31+
func (s *scraper) getCPUInfo() ([]cpuInfo, error) {
32+
var cpuInfos []cpuInfo
33+
return cpuInfos, nil
34+
}

receiver/hostmetricsreceiver/internal/scraper/cpuscraper/documentation.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ metrics:
3939
enabled: true
4040
```
4141
42+
### system.cpu.frequency
43+
44+
Current frequency of the CPU core in Hz.
45+
46+
| Unit | Metric Type | Value Type |
47+
| ---- | ----------- | ---------- |
48+
| Hz | Gauge | Double |
49+
50+
#### Attributes
51+
52+
| Name | Description | Values |
53+
| ---- | ----------- | ------ |
54+
| cpu | Logical CPU number starting at 0. | Any Str |
55+
4256
### system.cpu.logical.count
4357
4458
Number of available logical CPUs.

receiver/hostmetricsreceiver/internal/scraper/cpuscraper/internal/metadata/generated_config.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/hostmetricsreceiver/internal/scraper/cpuscraper/internal/metadata/generated_config_test.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/hostmetricsreceiver/internal/scraper/cpuscraper/internal/metadata/generated_metrics.go

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/hostmetricsreceiver/internal/scraper/cpuscraper/internal/metadata/generated_metrics_test.go

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/hostmetricsreceiver/internal/scraper/cpuscraper/internal/metadata/testdata/config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
default:
22
all_set:
33
metrics:
4+
system.cpu.frequency:
5+
enabled: true
46
system.cpu.logical.count:
57
enabled: true
68
system.cpu.physical.count:
@@ -11,6 +13,8 @@ all_set:
1113
enabled: true
1214
none_set:
1315
metrics:
16+
system.cpu.frequency:
17+
enabled: false
1418
system.cpu.logical.count:
1519
enabled: false
1620
system.cpu.physical.count:

receiver/hostmetricsreceiver/internal/scraper/cpuscraper/metadata.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,11 @@ metrics:
5050
value_type: int
5151
monotonic: false
5252
aggregation_temporality: cumulative
53+
54+
system.cpu.frequency:
55+
enabled: false
56+
description: Current frequency of the CPU core in Hz.
57+
unit: "Hz"
58+
gauge:
59+
value_type: double
60+
attributes: [cpu]

0 commit comments

Comments
 (0)