-
Notifications
You must be signed in to change notification settings - Fork 2.7k
collector: add tainted collector for /proc/sys/kernel/tainted #3745
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
Open
arunsrini3082
wants to merge
5
commits into
prometheus:master
Choose a base branch
from
arunsrini3082:kernel-tainted
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fca75e8
collector: add tainted collector for /proc/sys/kernel/tainted
d276412
go.mod: bump procfs to pick up KernelTainted() (#844)
873040a
go.mod: bump procfs to pick up linux build tag fix (#847)
fb40152
Merge branch 'master' into kernel-tainted
arunsrini3082 e7d5c0a
collector/tainted: use year-less copyright header
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 12288 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Copyright 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. | ||
|
|
||
| package collector | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log/slog" | ||
| "strconv" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/procfs" | ||
| ) | ||
|
|
||
| type taintedCollector struct { | ||
| logger *slog.Logger | ||
| desc *prometheus.Desc | ||
| } | ||
|
|
||
| func init() { | ||
| registerCollector("tainted", defaultDisabled, NewTaintedCollector) | ||
| } | ||
|
|
||
| // NewTaintedCollector returns a Collector exposing kernel taint flags from | ||
| // /proc/sys/kernel/tainted as a labelled gauge. | ||
| // See https://www.kernel.org/doc/html/latest/admin-guide/tainted-kernels.html | ||
| func NewTaintedCollector(logger *slog.Logger) (Collector, error) { | ||
| return &taintedCollector{ | ||
| logger: logger, | ||
| desc: prometheus.NewDesc( | ||
| prometheus.BuildFQName(namespace, "kernel", "tainted"), | ||
| "Taint flags set on the running Linux kernel, as reported by /proc/sys/kernel/tainted. "+ | ||
| "Value is 1 if the flag is set, 0 otherwise. "+ | ||
| "See https://www.kernel.org/doc/html/latest/admin-guide/tainted-kernels.html for flag meanings.", | ||
| []string{"bit", "flag"}, | ||
| nil, | ||
| ), | ||
| }, nil | ||
| } | ||
|
|
||
| func (c *taintedCollector) Update(ch chan<- prometheus.Metric) error { | ||
| fs, err := procfs.NewFS(*procPath) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to open procfs: %w", err) | ||
| } | ||
|
|
||
| tainted, err := fs.KernelTainted() | ||
| if err != nil { | ||
| return fmt.Errorf("couldn't read kernel tainted state: %w", err) | ||
| } | ||
|
|
||
| for _, b := range tainted.Bits { | ||
| var val float64 | ||
| if b.Set { | ||
| val = 1.0 | ||
| } | ||
| ch <- prometheus.MustNewConstMetric( | ||
| c.desc, | ||
| prometheus.GaugeValue, | ||
| val, | ||
| strconv.Itoa(b.Index), | ||
| b.Flag, | ||
| ) | ||
| } | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // Copyright 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 !notainted | ||
|
|
||
| package collector | ||
|
|
||
| import ( | ||
| "io" | ||
| "log/slog" | ||
| "testing" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| ) | ||
|
|
||
| func TestTaintedCollector(t *testing.T) { | ||
| *procPath = "fixtures/proc" | ||
|
|
||
| logger := slog.New(slog.NewTextHandler(io.Discard, nil)) | ||
| c, err := NewTaintedCollector(logger) | ||
| if err != nil { | ||
| t.Fatalf("failed to create tainted collector: %v", err) | ||
| } | ||
|
|
||
| reg := prometheus.NewPedanticRegistry() | ||
| reg.MustRegister(&taintedCollectorWrapper{c.(*taintedCollector)}) | ||
|
|
||
| mfs, err := reg.Gather() | ||
| if err != nil { | ||
| t.Fatalf("gather failed: %v", err) | ||
| } | ||
| if len(mfs) != 1 { | ||
| t.Fatalf("expected 1 metric family, got %d", len(mfs)) | ||
| } | ||
|
|
||
| mf := mfs[0] | ||
| if got := mf.GetName(); got != "node_kernel_tainted" { | ||
| t.Errorf("metric name: want node_kernel_tainted, got %s", got) | ||
| } | ||
|
|
||
| // Expect one series per known taint bit (20 defined by the kernel). | ||
| const wantBits = 20 | ||
| if got := len(mf.GetMetric()); got != wantBits { | ||
| t.Errorf("metric count: want %d, got %d", wantBits, got) | ||
| } | ||
|
|
||
| // Build bit → value map for assertion. | ||
| // Fixture is 12288 = bit 12 (O) + bit 13 (E). | ||
| // Build flag → value map for assertion (labels: bit, flag). | ||
| flagVals := make(map[string]float64) | ||
| for _, m := range mf.GetMetric() { | ||
| // Each metric has exactly 2 labels: bit and flag. | ||
| for _, lp := range m.GetLabel() { | ||
| if lp.GetName() == "flag" { | ||
| flagVals[lp.GetValue()] = m.GetGauge().GetValue() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Fixture is 12288 = bit 12 (O) + bit 13 (E). | ||
| for _, tc := range []struct { | ||
| flag string | ||
| want float64 | ||
| }{ | ||
| {"O", 1}, // Externally-built (out-of-tree) module — set | ||
| {"E", 1}, // Unsigned module — set | ||
| {"L", 0}, // Soft lockup — must be clear | ||
| {"P", 0}, | ||
| {"T", 0}, | ||
| } { | ||
| got, ok := flagVals[tc.flag] | ||
| if !ok { | ||
| t.Errorf("flag %q not found in metrics", tc.flag) | ||
| continue | ||
| } | ||
| if got != tc.want { | ||
| t.Errorf("flag %q: want %.0f, got %.0f", tc.flag, tc.want, got) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // taintedCollectorWrapper adapts taintedCollector to prometheus.Collector. | ||
| type taintedCollectorWrapper struct { | ||
| c *taintedCollector | ||
| } | ||
|
|
||
| func (w *taintedCollectorWrapper) Describe(ch chan<- *prometheus.Desc) { | ||
| ch <- w.c.desc | ||
| } | ||
|
|
||
| func (w *taintedCollectorWrapper) Collect(ch chan<- prometheus.Metric) { | ||
| _ = w.c.Update(ch) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blocking: this pins procfs to an untagged pseudo-version (
v0.21.2-0.20260717070424-0cd18237af6e). Rebase onto master and point to a tagged release once one includes #844/#847. Can't merge on an untagged commit.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Waiting on procfs release for #844/#847; will update go.mod once tagged.