-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add /proc/swaps collector #3428
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
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e494031
Add /proc/swaps collector
fabiand a32e918
swap: Disable by default
fabiand 02a8a2d
swap: Refactor and add tests
fabiand 15c6026
swap: Adjust copyright date
fabiand 08b1b86
swap: Drop year
fabiand 0361693
swap: Add to README and CHANGELOG
fabiand ad75e79
swap: Provide initial length
fabiand c774f2d
swap: Move priority to Gauge
fabiand aa71c94
swap: Specify field names
fabiand 075c3d4
swap: Ref unusual size
fabiand 1983b75
swap: Adjust README
fabiand 2d6e574
swap: Drop changelog
fabiand 2a3a3c7
swap: Drop needless build info
fabiand 4386d1a
swap: Fix indentation
fabiand af29cfd
swap: Adjust build config
fabiand b55b4e4
swap: Rename label `type` to `swap_type`
fabiand 4697358
swap: Golanigfy
fabiand 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
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,2 @@ | ||
| Filename Type Size Used Priority | ||
| /dev/zram0 partition 8388604 76 100 |
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,129 @@ | ||
| // 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 !noswap | ||
|
|
||
| package collector | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log/slog" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/procfs" | ||
| ) | ||
|
|
||
| const ( | ||
| swapSubsystem = "swap" | ||
| ) | ||
|
|
||
| var swapLabelNames = []string{"device", "swap_type"} | ||
|
|
||
| type swapCollector struct { | ||
| fs procfs.FS | ||
| logger *slog.Logger | ||
| } | ||
|
|
||
| func init() { | ||
| registerCollector("swap", defaultDisabled, NewSwapCollector) | ||
| } | ||
|
|
||
| // NewSwapCollector returns a new Collector exposing swap device statistics. | ||
| func NewSwapCollector(logger *slog.Logger) (Collector, error) { | ||
| fs, err := procfs.NewFS(*procPath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to open procfs: %w", err) | ||
| } | ||
|
|
||
| return &swapCollector{ | ||
| fs: fs, | ||
| logger: logger, | ||
| }, nil | ||
| } | ||
|
|
||
| type SwapsEntry struct { | ||
| Device string | ||
| Type string | ||
| Priority int | ||
| Size int | ||
| Used int | ||
| } | ||
|
|
||
| func (c *swapCollector) getSwapInfo() ([]SwapsEntry, error) { | ||
| swaps, err := c.fs.Swaps() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("couldn't get proc/swap information: %w", err) | ||
| } | ||
|
|
||
| metrics := make([]SwapsEntry, 0, len(swaps)) | ||
|
|
||
| for _, swap := range swaps { | ||
| metrics = append(metrics, SwapsEntry{Device: swap.Filename, Type: swap.Type, | ||
| Priority: swap.Priority, Size: swap.Size, Used: swap.Used}) | ||
| } | ||
|
|
||
| return metrics, nil | ||
| } | ||
|
|
||
| func (c *swapCollector) Update(ch chan<- prometheus.Metric) error { | ||
| swaps, err := c.getSwapInfo() | ||
| if err != nil { | ||
| return fmt.Errorf("couldn't get swap information: %w", err) | ||
| } | ||
|
|
||
| for _, swap := range swaps { | ||
| swapLabelValues := []string{swap.Device, swap.Type} | ||
|
|
||
| // Export swap size in bytes | ||
| ch <- prometheus.MustNewConstMetric( | ||
| prometheus.NewDesc( | ||
| prometheus.BuildFQName(namespace, swapSubsystem, "size_bytes"), | ||
| "Swap device size in bytes.", | ||
| []string{"device", "swap_type"}, nil, | ||
| ), | ||
| prometheus.GaugeValue, | ||
| // Size is provided in kbytes (not bytes), translate to bytes | ||
| // see https://github.com/torvalds/linux/blob/fd94619c43360eb44d28bd3ef326a4f85c600a07/mm/swapfile.c#L3079-L3080 | ||
| float64(swap.Size*1024), | ||
| swapLabelValues..., | ||
| ) | ||
|
|
||
| // Export swap used in bytes | ||
| ch <- prometheus.MustNewConstMetric( | ||
| prometheus.NewDesc( | ||
| prometheus.BuildFQName(namespace, swapSubsystem, "used_bytes"), | ||
| "Swap device used in bytes.", | ||
| swapLabelNames, nil, | ||
| ), | ||
| prometheus.GaugeValue, | ||
| // Swap used is also provided in kbytes, translate to bytes | ||
| float64(swap.Used*1024), | ||
| swapLabelValues..., | ||
| ) | ||
|
|
||
| // Export swap priority | ||
| ch <- prometheus.MustNewConstMetric( | ||
| prometheus.NewDesc( | ||
| prometheus.BuildFQName(namespace, swapSubsystem, "priority"), | ||
| "Swap device priority.", | ||
| swapLabelNames, nil, | ||
| ), | ||
| prometheus.GaugeValue, | ||
| float64(swap.Priority), | ||
| swapLabelValues..., | ||
| ) | ||
|
|
||
| } | ||
|
|
||
| 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,58 @@ | ||
| // 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 !noswap | ||
| // +build !noswap | ||
|
|
||
| package collector | ||
|
|
||
| import ( | ||
| "io" | ||
| "log/slog" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestSwap(t *testing.T) { | ||
| *procPath = "fixtures/proc" | ||
| logger := slog.New(slog.NewTextHandler(io.Discard, nil)) | ||
|
|
||
| collector, err := NewSwapCollector(logger) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| swapInfo, err := collector.(*swapCollector).getSwapInfo() | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| if want, got := "/dev/zram0", swapInfo[0].Device; want != got { | ||
| t.Errorf("want swap device %s, got %s", want, got) | ||
| } | ||
|
|
||
| if want, got := "partition", swapInfo[0].Type; want != got { | ||
| t.Errorf("want swap type %s, got %s", want, got) | ||
| } | ||
|
|
||
| if want, got := 100, swapInfo[0].Priority; want != got { | ||
| t.Errorf("want swap priority %d, got %d", want, got) | ||
| } | ||
|
|
||
| if want, got := 8388604, swapInfo[0].Size; want != got { | ||
| t.Errorf("want swap size %d, got %d", want, got) | ||
| } | ||
|
|
||
| if want, got := 76, swapInfo[0].Used; want != got { | ||
| t.Errorf("want swpa used %d, got %d", want, got) | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.