Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
Signed-off-by: LamGC <lam827@lamgc.net>
  • Loading branch information
LamGC authored Mar 8, 2023
2 parents d94c3a2 + 2f20a9b commit a6ac060
Show file tree
Hide file tree
Showing 46 changed files with 274 additions and 151 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* [ENHANCEMENT]
* [BUGFIX]

* [BUGFIX] Remove metrics of offline CPUs in CPU collector #2605

## 1.5.0 / 2022-11-29

NOTE: This changes the Go runtime "GOMAXPROCS" to 1. This is done to limit the
Expand Down Expand Up @@ -139,7 +141,7 @@ NOTE: Filesystem collector flags have been renamed. `--collector.filesystem.igno
* [BUGFIX] Fix wrong value for OpenBSD memory buffer cache #2015
* [BUGFIX] Only initiate collectors once #2048
* [BUGFIX] Handle small backwards jumps in CPU idle #2067

## 1.1.2 / 2021-03-05

* [BUGFIX] Handle errors from disabled PSI subsystem #1983
Expand Down
2 changes: 1 addition & 1 deletion Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ PROMU_URL := https://github.com/prometheus/promu/releases/download/v$(PROMU_
SKIP_GOLANGCI_LINT :=
GOLANGCI_LINT :=
GOLANGCI_LINT_OPTS ?=
GOLANGCI_LINT_VERSION ?= v1.50.1
GOLANGCI_LINT_VERSION ?= v1.51.2
# golangci-lint only supports linux, darwin and windows platforms on i386/amd64.
# windows isn't included here because of the path separator being different.
ifeq ($(GOHOSTOS),$(filter $(GOHOSTOS),linux darwin))
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ logind | Exposes session counts from [logind](http://www.freedesktop.org/wiki/So
meminfo\_numa | Exposes memory statistics from `/proc/meminfo_numa`. | Linux
mountstats | Exposes filesystem statistics from `/proc/self/mountstats`. Exposes detailed NFS client statistics. | Linux
network_route | Exposes the routing table as metrics | Linux
ntp | Exposes local NTP daemon health to check [time](./docs/TIME.md) | _any_
perf | Exposes perf based metrics (Warning: Metrics are dependent on kernel configuration and settings). | Linux
processes | Exposes aggregate process statistics from `/proc`. | Linux
qdisc | Exposes [queuing discipline](https://en.wikipedia.org/wiki/Network_scheduler#Linux_kernel) statistics | Linux
Expand All @@ -178,6 +177,14 @@ tcpstat | Exposes TCP connection status information from `/proc/net/tcp` and `/p
wifi | Exposes WiFi device and station statistics. | Linux
zoneinfo | Exposes NUMA memory zone metrics. | Linux

### Deprecated

These colectors are deprecated and will be removed in the next major release.

Name | Description | OS
---------|-------------|----
ntp | Exposes local NTP daemon health to check [time](./docs/TIME.md) | _any_

### Perf Collector

The `perf` collector may not work out of the box on some Linux systems due to kernel
Expand Down
2 changes: 1 addition & 1 deletion collector/arp_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package collector

import (
"fmt"
"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/procfs"
"gopkg.in/alecthomas/kingpin.v2"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion collector/bcache_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package collector
import (
"fmt"

"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/procfs/bcache"
"gopkg.in/alecthomas/kingpin.v2"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
"sync"
"time"

"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)

// Namespace defines the common namespace to be used by all metrics.
Expand Down
12 changes: 11 additions & 1 deletion collector/cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ import (
"strconv"
"sync"

"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/procfs"
"github.com/prometheus/procfs/sysfs"
"gopkg.in/alecthomas/kingpin.v2"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)

type cpuCollector struct {
Expand Down Expand Up @@ -424,4 +426,12 @@ func (c *cpuCollector) updateCPUStats(newStats map[int64]procfs.CPUStat) {

c.cpuStats[i] = cpuStats
}

// Remove offline CPUs.
if len(newStats) != len(c.cpuStats) {
onlineCPUIds := maps.Keys(newStats)
maps.DeleteFunc(c.cpuStats, func(key int64, item procfs.CPUStat) bool {
return !slices.Contains(onlineCPUIds, key)
})
}
}
85 changes: 85 additions & 0 deletions collector/cpu_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,88 @@ func TestCPU(t *testing.T) {
t.Fatalf("should have %v CPU Stat: got %v", resetIdle, got)
}
}

func TestCPUOffline(t *testing.T) {
// CPU 1 goes offline.
firstCPUStat := map[int64]procfs.CPUStat{
0: {
User: 100.0,
Nice: 100.0,
System: 100.0,
Idle: 100.0,
Iowait: 100.0,
IRQ: 100.0,
SoftIRQ: 100.0,
Steal: 100.0,
Guest: 100.0,
GuestNice: 100.0,
},
1: {
User: 101.0,
Nice: 101.0,
System: 101.0,
Idle: 101.0,
Iowait: 101.0,
IRQ: 101.0,
SoftIRQ: 101.0,
Steal: 101.0,
Guest: 101.0,
GuestNice: 101.0,
},
}

c := makeTestCPUCollector(firstCPUStat)
want := map[int64]procfs.CPUStat{
0: {
User: 100.0,
Nice: 100.0,
System: 100.0,
Idle: 100.0,
Iowait: 100.0,
IRQ: 100.0,
SoftIRQ: 100.0,
Steal: 100.0,
Guest: 100.0,
GuestNice: 100.0,
},
}
c.updateCPUStats(want)
got := c.cpuStats
if !reflect.DeepEqual(want, got) {
t.Fatalf("should have %v CPU Stat: got %v", want, got)
}

// CPU 1 comes back online.
want = map[int64]procfs.CPUStat{
0: {
User: 100.0,
Nice: 100.0,
System: 100.0,
Idle: 100.0,
Iowait: 100.0,
IRQ: 100.0,
SoftIRQ: 100.0,
Steal: 100.0,
Guest: 100.0,
GuestNice: 100.0,
},
1: {
User: 101.0,
Nice: 101.0,
System: 101.0,
Idle: 101.0,
Iowait: 101.0,
IRQ: 101.0,
SoftIRQ: 101.0,
Steal: 101.0,
Guest: 101.0,
GuestNice: 101.0,
},
}
c.updateCPUStats(want)
got = c.cpuStats
if !reflect.DeepEqual(want, got) {
t.Fatalf("should have %v CPU Stat: got %v", want, got)
}

}
54 changes: 54 additions & 0 deletions collector/cpufreq_common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2023 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !nocpu
// +build !nocpu

package collector

import (
"github.com/prometheus/client_golang/prometheus"
)

var (
cpuFreqHertzDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "frequency_hertz"),
"Current cpu thread frequency in hertz.",
[]string{"cpu"}, nil,
)
cpuFreqMinDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "frequency_min_hertz"),
"Minimum cpu thread frequency in hertz.",
[]string{"cpu"}, nil,
)
cpuFreqMaxDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "frequency_max_hertz"),
"Maximum cpu thread frequency in hertz.",
[]string{"cpu"}, nil,
)
cpuFreqScalingFreqDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "scaling_frequency_hertz"),
"Current scaled CPU thread frequency in hertz.",
[]string{"cpu"}, nil,
)
cpuFreqScalingFreqMinDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "scaling_frequency_min_hertz"),
"Minimum scaled CPU thread frequency in hertz.",
[]string{"cpu"}, nil,
)
cpuFreqScalingFreqMaxDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "scaling_frequency_max_hertz"),
"Maximum scaled CPU thread frequency in hertz.",
[]string{"cpu"}, nil,
)
)
54 changes: 9 additions & 45 deletions collector/cpufreq_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,8 @@ import (
)

type cpuFreqCollector struct {
fs sysfs.FS
cpuFreq *prometheus.Desc
cpuFreqMin *prometheus.Desc
cpuFreqMax *prometheus.Desc
scalingFreq *prometheus.Desc
scalingFreqMin *prometheus.Desc
scalingFreqMax *prometheus.Desc
logger log.Logger
fs sysfs.FS
logger log.Logger
}

func init() {
Expand All @@ -47,37 +41,7 @@ func NewCPUFreqCollector(logger log.Logger) (Collector, error) {
}

return &cpuFreqCollector{
fs: fs,
cpuFreq: prometheus.NewDesc(
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "frequency_hertz"),
"Current cpu thread frequency in hertz.",
[]string{"cpu"}, nil,
),
cpuFreqMin: prometheus.NewDesc(
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "frequency_min_hertz"),
"Minimum cpu thread frequency in hertz.",
[]string{"cpu"}, nil,
),
cpuFreqMax: prometheus.NewDesc(
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "frequency_max_hertz"),
"Maximum cpu thread frequency in hertz.",
[]string{"cpu"}, nil,
),
scalingFreq: prometheus.NewDesc(
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "scaling_frequency_hertz"),
"Current scaled CPU thread frequency in hertz.",
[]string{"cpu"}, nil,
),
scalingFreqMin: prometheus.NewDesc(
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "scaling_frequency_min_hertz"),
"Minimum scaled CPU thread frequency in hertz.",
[]string{"cpu"}, nil,
),
scalingFreqMax: prometheus.NewDesc(
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "scaling_frequency_max_hertz"),
"Maximum scaled CPU thread frequency in hertz.",
[]string{"cpu"}, nil,
),
fs: fs,
logger: logger,
}, nil
}
Expand All @@ -94,47 +58,47 @@ func (c *cpuFreqCollector) Update(ch chan<- prometheus.Metric) error {
for _, stats := range cpuFreqs {
if stats.CpuinfoCurrentFrequency != nil {
ch <- prometheus.MustNewConstMetric(
c.cpuFreq,
cpuFreqHertzDesc,
prometheus.GaugeValue,
float64(*stats.CpuinfoCurrentFrequency)*1000.0,
stats.Name,
)
}
if stats.CpuinfoMinimumFrequency != nil {
ch <- prometheus.MustNewConstMetric(
c.cpuFreqMin,
cpuFreqMinDesc,
prometheus.GaugeValue,
float64(*stats.CpuinfoMinimumFrequency)*1000.0,
stats.Name,
)
}
if stats.CpuinfoMaximumFrequency != nil {
ch <- prometheus.MustNewConstMetric(
c.cpuFreqMax,
cpuFreqMaxDesc,
prometheus.GaugeValue,
float64(*stats.CpuinfoMaximumFrequency)*1000.0,
stats.Name,
)
}
if stats.ScalingCurrentFrequency != nil {
ch <- prometheus.MustNewConstMetric(
c.scalingFreq,
cpuFreqScalingFreqDesc,
prometheus.GaugeValue,
float64(*stats.ScalingCurrentFrequency)*1000.0,
stats.Name,
)
}
if stats.ScalingMinimumFrequency != nil {
ch <- prometheus.MustNewConstMetric(
c.scalingFreqMin,
cpuFreqScalingFreqMinDesc,
prometheus.GaugeValue,
float64(*stats.ScalingMinimumFrequency)*1000.0,
stats.Name,
)
}
if stats.ScalingMaximumFrequency != nil {
ch <- prometheus.MustNewConstMetric(
c.scalingFreqMax,
cpuFreqScalingFreqMaxDesc,
prometheus.GaugeValue,
float64(*stats.ScalingMaximumFrequency)*1000.0,
stats.Name,
Expand Down
Loading

0 comments on commit a6ac060

Please sign in to comment.