Skip to content

Commit

Permalink
[hostmetricsreceiver]: support reporting amount of available CPUs
Browse files Browse the repository at this point in the history
Signed-off-by: Benedikt Bongartz <bongartz@klimlive.de>
  • Loading branch information
frzifus committed Jun 19, 2023
1 parent 5fff104 commit 0a4e378
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .chloggen/hostmetricsreceiver_support_cpu_count.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use this changelog template to create an entry for release notes.
# If your change doesn't affect end users, such as a test fix or a tooling change,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

# 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: hostmetricsreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Report number of CPUs as metric.

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

# (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:
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,15 @@ func (s *scraper) scrape(_ context.Context) (pmetric.Metrics, error) {
return pmetric.NewMetrics(), scrapererror.NewPartialScrapeError(err, metricsLen)
}

numCPU, err := cpu.Counts(true)
if err != nil {
return pmetric.NewMetrics(), scrapererror.NewPartialScrapeError(err, metricsLen)
}
machineID, err := getMachineID()
if err != nil {
return pmetric.NewMetrics(), scrapererror.NewPartialScrapeError(err, metricsLen)
}
s.recordCPUCountDataPoint(now, numCPU, machineID)

return s.mb.Emit(), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
package cpuscraper // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/cpuscraper"

import (
"io/ioutil"
"strings"

"github.com/shirou/gopsutil/v3/cpu"
"go.opentelemetry.io/collector/pdata/pcommon"

Expand Down Expand Up @@ -35,3 +38,16 @@ func (s *scraper) recordCPUUtilization(now pcommon.Timestamp, cpuUtilization uca
s.mb.RecordSystemCPUUtilizationDataPoint(now, cpuUtilization.Steal, cpuUtilization.CPU, metadata.AttributeStateSteal)
s.mb.RecordSystemCPUUtilizationDataPoint(now, cpuUtilization.Iowait, cpuUtilization.CPU, metadata.AttributeStateWait)
}

func (s *scraper) recordCPUCountDataPoint(now pcommon.Timestamp, count int, machineID string) {
s.mb.RecordSystemCPUCountDataPoint(now, int64(count), machineID)
}

func getMachineID() (string, error) {
machineID, err := ioutil.ReadFile("/etc/machine-id")
if err != nil {
return "", err
}

return strings.TrimSpace(string(machineID)), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ func (s *scraper) recordCPUUtilization(now pcommon.Timestamp, cpuUtilization uca
s.mb.RecordSystemCPUUtilizationDataPoint(now, cpuUtilization.Idle, cpuUtilization.CPU, metadata.AttributeStateIdle)
s.mb.RecordSystemCPUUtilizationDataPoint(now, cpuUtilization.Irq, cpuUtilization.CPU, metadata.AttributeStateInterrupt)
}

func (s *scraper) recordCPUCountDataPoint(now pcommon.Timestamp, count int, machineID string) {}

func getMachineID() (string, error) {
return "", nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ metrics:
enabled: true
```
### system.cpu.count
Number of available CPUs.
| Unit | Metric Type | Value Type |
| ---- | ----------- | ---------- |
| 1 | Gauge | Int |
#### Attributes
| Name | Description | Values |
| ---- | ----------- | ------ |
| machine-id | Machine identifier. | Any Str |
### system.cpu.utilization
Percentage of CPU time broken down by different states.
Expand Down

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.

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
@@ -1,12 +1,16 @@
default:
all_set:
metrics:
system.cpu.count:
enabled: true
system.cpu.time:
enabled: true
system.cpu.utilization:
enabled: true
none_set:
metrics:
system.cpu.count:
enabled: false
system.cpu.time:
enabled: false
system.cpu.utilization:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ attributes:
type: string
enum: [idle, interrupt, nice, softirq, steal, system, user, wait]

machine-id:
description: Machine identifier.
type: string

metrics:
system.cpu.time:
enabled: true
Expand All @@ -30,3 +34,11 @@ metrics:
gauge:
value_type: double
attributes: [cpu, state]

system.cpu.count:
enabled: false
description: Number of available CPUs.
unit: 1
gauge:
value_type: int
attributes: [machine-id]

0 comments on commit 0a4e378

Please sign in to comment.