Skip to content

feat(cpuinfo): Added cpu info collector #2004

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 12 additions & 1 deletion cmd/kepler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,19 @@
apiServer := server.NewAPIServer(
server.WithLogger(logger),
)

collectors, err := prometheus.CreateCollectors(

Check failure on line 159 in cmd/kepler/main.go

View workflow job for this annotation

GitHub Actions / golangci

ineffectual assignment to err (ineffassign)
pm,
prometheus.WithLogger(logger),
prometheus.WithProcFSPath(cfg.Host.ProcFS),
)
// TODO: enable exporters based on config / flags
promExporter := prometheus.NewExporter(pm, apiServer, prometheus.WithLogger(logger))
promExporter := prometheus.NewExporter(
pm,
apiServer,
prometheus.WithLogger(logger),
prometheus.WithCollectors(collectors),
)

return []service.Service{
promExporter,
Expand Down
12 changes: 6 additions & 6 deletions internal/exporter/prometheus/collectors/build_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package collectors

import (
"github.com/prometheus/client_golang/prometheus"
prom "github.com/prometheus/client_golang/prometheus"
"github.com/sustainable-computing-io/kepler/internal/version"
)

Expand All @@ -14,13 +14,13 @@ const (
)

type BuildInfoCollector struct {
buildInfo *prometheus.GaugeVec
buildInfo *prom.GaugeVec
}

// NewBuildInfoCollector creates a new collector for build information
func NewBuildInfoCollector() *BuildInfoCollector {
buildInfo := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
buildInfo := prom.NewGaugeVec(
prom.GaugeOpts{
Namespace: namespace,
Subsystem: buildSubsystem,
Name: "info",
Expand All @@ -34,11 +34,11 @@ func NewBuildInfoCollector() *BuildInfoCollector {
}
}

func (c *BuildInfoCollector) Describe(ch chan<- *prometheus.Desc) {
func (c *BuildInfoCollector) Describe(ch chan<- *prom.Desc) {
c.buildInfo.Describe(ch)
}

func (c *BuildInfoCollector) Collect(ch chan<- prometheus.Metric) {
func (c *BuildInfoCollector) Collect(ch chan<- prom.Metric) {
info := version.Info()

c.buildInfo.WithLabelValues(
Expand Down
89 changes: 89 additions & 0 deletions internal/exporter/prometheus/collectors/cpuinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// SPDX-FileCopyrightText: 2025 The Kepler Authors
// SPDX-License-Identifier: Apache-2.0

package collectors

import (
"fmt"
"sync"

prom "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/procfs"
)

// procFS is an interface for CPUInfo.
type procFS interface {
CPUInfo() ([]procfs.CPUInfo, error)
}

type realProcFS struct {
fs procfs.FS
}

func (r *realProcFS) CPUInfo() ([]procfs.CPUInfo, error) {
return r.fs.CPUInfo()

Check warning on line 24 in internal/exporter/prometheus/collectors/cpuinfo.go

View check run for this annotation

Codecov / codecov/patch

internal/exporter/prometheus/collectors/cpuinfo.go#L23-L24

Added lines #L23 - L24 were not covered by tests
}

func newProcFS(mountPoint string) (procFS, error) {
fs, err := procfs.NewFS(mountPoint)
if err != nil {
return nil, err
}

Check warning on line 31 in internal/exporter/prometheus/collectors/cpuinfo.go

View check run for this annotation

Codecov / codecov/patch

internal/exporter/prometheus/collectors/cpuinfo.go#L30-L31

Added lines #L30 - L31 were not covered by tests
return &realProcFS{fs: fs}, nil
}

// cpuInfoCollector collects CPU info metrics from procfs.
type cpuInfoCollector struct {
sync.Mutex

fs procFS
desc *prom.Desc
}

// NewCPUInfoCollector creates a CPUInfoCollector using a procfs mount path.
func NewCPUInfoCollector(procPath string) (*cpuInfoCollector, error) {
fs, err := newProcFS(procPath)
if err != nil {
return nil, fmt.Errorf("creating procfs failed: %w", err)
}

Check warning on line 48 in internal/exporter/prometheus/collectors/cpuinfo.go

View check run for this annotation

Codecov / codecov/patch

internal/exporter/prometheus/collectors/cpuinfo.go#L47-L48

Added lines #L47 - L48 were not covered by tests
return newCPUInfoCollectorWithFS(fs), nil
}

// newCPUInfoCollectorWithFS injects a procFS interface
func newCPUInfoCollectorWithFS(fs procFS) *cpuInfoCollector {
return &cpuInfoCollector{
fs: fs,
desc: prom.NewDesc(
prom.BuildFQName(namespace, "", "cpu_info"),
"CPU information from procfs",
[]string{"processor", "vendor_id", "model_name", "physical_id", "core_id"},
nil,
),
}
}

func (c *cpuInfoCollector) Describe(ch chan<- *prom.Desc) {
ch <- c.desc
}

func (c *cpuInfoCollector) Collect(ch chan<- prom.Metric) {
c.Lock()
defer c.Unlock()

cpuInfos, err := c.fs.CPUInfo()
if err != nil {
return
}
for _, ci := range cpuInfos {
ch <- prom.MustNewConstMetric(
c.desc,
prom.GaugeValue,
1,
fmt.Sprintf("%d", ci.Processor),
ci.VendorID,
ci.ModelName,
ci.PhysicalID,
ci.CoreID,
)
}
}
189 changes: 189 additions & 0 deletions internal/exporter/prometheus/collectors/cpuinfo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
// SPDX-FileCopyrightText: 2025 The Kepler Authors
// SPDX-License-Identifier: Apache-2.0

package collectors

import (
"errors"
"sync"
"testing"

"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/procfs"
"github.com/stretchr/testify/assert"
)

// mockProcFS is a mock implementation of the procFS interface for testing.
type mockProcFS struct {
cpuInfoFunc func() ([]procfs.CPUInfo, error)
}

func (m *mockProcFS) CPUInfo() ([]procfs.CPUInfo, error) {
return m.cpuInfoFunc()
}

// sampleCPUInfo returns a sample CPUInfo slice for testing.
func sampleCPUInfo() []procfs.CPUInfo {
return []procfs.CPUInfo{
{
Processor: 0,
VendorID: "GenuineIntel",
ModelName: "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz",
PhysicalID: "0",
CoreID: "0",
},
{
Processor: 1,
VendorID: "GenuineIntel",
ModelName: "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz",
PhysicalID: "0",
CoreID: "1",
},
}
}

func expectedLabels() map[string]string {
return map[string]string{
"processor": "",
"vendor_id": "",
"model_name": "",
"physical_id": "",
"core_id": "",
}
}

// TestNewCPUInfoCollector tests the creation of a new CPUInfoCollector.
func TestNewCPUInfoCollector(t *testing.T) {
// Test successful creation with a mock procfs
collector, err := NewCPUInfoCollector("/proc")
assert.NoError(t, err)
assert.NotNil(t, collector)
assert.NotNil(t, collector.fs)
assert.NotNil(t, collector.desc)
}

// TestNewCPUInfoCollectorWithFS tests the creation with an injected procFS.
func TestNewCPUInfoCollectorWithFS(t *testing.T) {
mockFS := &mockProcFS{
cpuInfoFunc: func() ([]procfs.CPUInfo, error) {
return sampleCPUInfo(), nil
},
}
collector := newCPUInfoCollectorWithFS(mockFS)
assert.NotNil(t, collector)
assert.Equal(t, mockFS, collector.fs)
assert.NotNil(t, collector.desc)
assert.Contains(t, collector.desc.String(), "kepler_cpu_info")
assert.Contains(t, collector.desc.String(), "variableLabels: {processor,vendor_id,model_name,physical_id,core_id}")
}

// TestCPUInfoCollector_Describe tests the Describe method.
func TestCPUInfoCollector_Describe(t *testing.T) {
mockFS := &mockProcFS{
cpuInfoFunc: func() ([]procfs.CPUInfo, error) {
return sampleCPUInfo(), nil
},
}
collector := newCPUInfoCollectorWithFS(mockFS)

ch := make(chan *prometheus.Desc, 1)
collector.Describe(ch)
close(ch)

desc := <-ch
assert.Equal(t, collector.desc, desc)
}

// TestCPUInfoCollector_Collect_Success tests the Collect method with valid CPU info.
func TestCPUInfoCollector_Collect_Success(t *testing.T) {
mockFS := &mockProcFS{
cpuInfoFunc: func() ([]procfs.CPUInfo, error) {
return sampleCPUInfo(), nil
},
}
collector := newCPUInfoCollectorWithFS(mockFS)

ch := make(chan prometheus.Metric, 10)
collector.Collect(ch)
close(ch)

var metrics []prometheus.Metric
for m := range ch {
metrics = append(metrics, m)
}

assert.Len(t, metrics, 2, "expected two CPU info metrics")

el := expectedLabels()

for _, m := range metrics {
dtoMetric := &dto.Metric{}
err := m.Write(dtoMetric)
assert.NoError(t, err)
assert.NotNil(t, dtoMetric.Gauge)
assert.NotNil(t, dtoMetric.Gauge.Value)
assert.Equal(t, 1.0, *dtoMetric.Gauge.Value)
assert.NotNil(t, dtoMetric.Label)
for _, l := range dtoMetric.Label {
assert.NotNil(t, l.Name)
delete(el, *l.Name)
}
}
assert.Empty(t, el, "all expected labels not received")
}

// TestCPUInfoCollector_Collect_Error tests the Collect method when CPUInfo fails.
func TestCPUInfoCollector_Collect_Error(t *testing.T) {
mockFS := &mockProcFS{
cpuInfoFunc: func() ([]procfs.CPUInfo, error) {
return nil, errors.New("failed to read CPU info")
},
}
collector := newCPUInfoCollectorWithFS(mockFS)

ch := make(chan prometheus.Metric, 10)
collector.Collect(ch)
close(ch)

var metrics []prometheus.Metric
for m := range ch {
metrics = append(metrics, m)
}

assert.Len(t, metrics, 0, "expected no metrics on error")
}

// TestCPUInfoCollector_Collect_Concurrency tests concurrent calls to Collect.
func TestCPUInfoCollector_Collect_Concurrency(t *testing.T) {
mockFS := &mockProcFS{
cpuInfoFunc: func() ([]procfs.CPUInfo, error) {
return sampleCPUInfo(), nil
},
}
collector := newCPUInfoCollectorWithFS(mockFS)

const numGoroutines = 10
var wg sync.WaitGroup
ch := make(chan prometheus.Metric, numGoroutines*len(sampleCPUInfo()))

for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
collector.Collect(ch)
}()
}

wg.Wait()
close(ch)

var metrics []prometheus.Metric
for m := range ch {
metrics = append(metrics, m)
}

// Expect numGoroutines * number of CPUs metrics
expectedMetrics := numGoroutines * len(sampleCPUInfo())
assert.Equal(t, expectedMetrics, len(metrics), "expected metrics from all goroutines")
}
Loading