-
Notifications
You must be signed in to change notification settings - Fork 707
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pagefile: BREAKING: move paging metrics from os to dedicated collecto…
…r (click PR for more information) (#1735) Signed-off-by: Jan-Otto Kröpke <[email protected]>
- Loading branch information
Showing
13 changed files
with
238 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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 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 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,38 @@ | ||
# pagefile collector | ||
|
||
The pagefile collector exposes metrics about the pagefile usage | ||
|
||
||| | ||
-|- | ||
Metric name prefix | `pagefile` | ||
Classes | [`Win32_OperatingSystem`](https://msdn.microsoft.com/en-us/library/aa394239) | ||
Enabled by default? | Yes | ||
|
||
## Flags | ||
|
||
None | ||
|
||
## Metrics | ||
|
||
| Name | Description | Type | Labels | | ||
|--------------------------------|-----------------------------------------------------------------------------------------------------------------------------|-------|--------| | ||
| `windows_pagefile_free_bytes` | Number of bytes that can be mapped into the operating system paging files without causing any other pages to be swapped out | gauge | `file` | | ||
| `windows_pagefile_limit_bytes` | Number of bytes that can be stored in the operating system paging files. 0 (zero) indicates that there are no paging files | gauge | `file` | | ||
|
||
|
||
### Example metric | ||
|
||
``` | ||
# HELP windows_pagefile_free_bytes OperatingSystem.FreeSpaceInPagingFiles | ||
# TYPE windows_pagefile_free_bytes gauge | ||
windows_pagefile_free_bytes{file="C:\\pagefile.sys"} 6.025797632e+09 | ||
# HELP windows_pagefile_limit_bytes OperatingSystem.SizeStoredInPagingFiles | ||
# TYPE windows_pagefile_limit_bytes gauge | ||
windows_pagefile_limit_bytes{file="C:\\pagefile.sys"} 6.442450944e+09 | ||
``` | ||
|
||
## Useful queries | ||
_This collector does not yet have useful queries, we would appreciate your help adding them!_ | ||
|
||
## Alerting examples | ||
_This collector does not yet have alerting examples, we would appreciate your help adding them!_ |
This file contains 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 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,5 @@ | ||
package pagefile | ||
|
||
const ( | ||
usage = "% Usage" | ||
) |
This file contains 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,136 @@ | ||
//go:build windows | ||
|
||
package pagefile | ||
|
||
import ( | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
"strings" | ||
|
||
"github.com/alecthomas/kingpin/v2" | ||
"github.com/prometheus-community/windows_exporter/internal/headers/psapi" | ||
"github.com/prometheus-community/windows_exporter/internal/mi" | ||
"github.com/prometheus-community/windows_exporter/internal/perfdata" | ||
"github.com/prometheus-community/windows_exporter/internal/types" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
const Name = "pagefile" | ||
|
||
type Config struct{} | ||
|
||
var ConfigDefaults = Config{} | ||
|
||
// A Collector is a Prometheus Collector for WMI metrics. | ||
type Collector struct { | ||
config Config | ||
|
||
perfDataCollector perfdata.Collector | ||
|
||
pagingFreeBytes *prometheus.Desc | ||
pagingLimitBytes *prometheus.Desc | ||
} | ||
|
||
func New(config *Config) *Collector { | ||
if config == nil { | ||
config = &ConfigDefaults | ||
} | ||
|
||
c := &Collector{ | ||
config: *config, | ||
} | ||
|
||
return c | ||
} | ||
|
||
func NewWithFlags(_ *kingpin.Application) *Collector { | ||
return &Collector{} | ||
} | ||
|
||
func (c *Collector) GetName() string { | ||
return Name | ||
} | ||
|
||
func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { | ||
return []string{}, nil | ||
} | ||
|
||
func (c *Collector) Close(_ *slog.Logger) error { | ||
return nil | ||
} | ||
|
||
func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error { | ||
counters := []string{ | ||
usage, | ||
} | ||
|
||
var err error | ||
|
||
c.perfDataCollector, err = perfdata.NewCollector(perfdata.V2, "Paging File", perfdata.AllInstances, counters) | ||
if err != nil { | ||
return fmt.Errorf("failed to create Paging File collector: %w", err) | ||
} | ||
|
||
c.pagingLimitBytes = prometheus.NewDesc( | ||
prometheus.BuildFQName(types.Namespace, Name, "limit_bytes"), | ||
"Number of bytes that can be stored in the operating system paging files. 0 (zero) indicates that there are no paging files", | ||
[]string{"file"}, | ||
nil, | ||
) | ||
|
||
c.pagingFreeBytes = prometheus.NewDesc( | ||
prometheus.BuildFQName(types.Namespace, Name, "free_bytes"), | ||
"Number of bytes that can be mapped into the operating system paging files without causing any other pages to be swapped out", | ||
[]string{"file"}, | ||
nil, | ||
) | ||
|
||
return nil | ||
} | ||
|
||
// Collect sends the metric values for each metric | ||
// to the provided prometheus Metric channel. | ||
func (c *Collector) Collect(_ *types.ScrapeContext, _ *slog.Logger, ch chan<- prometheus.Metric) error { | ||
return c.collectPaging(ch) | ||
} | ||
|
||
func (c *Collector) collectPaging(ch chan<- prometheus.Metric) error { | ||
data, err := c.perfDataCollector.Collect() | ||
if err != nil { | ||
return fmt.Errorf("failed to collect Paging File metrics: %w", err) | ||
} | ||
|
||
gpi, err := psapi.GetPerformanceInfo() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for fileName, pageFile := range data { | ||
fileString := strings.ReplaceAll(fileName, `\??\`, "") | ||
file, err := os.Stat(fileString) | ||
|
||
var fileSize float64 | ||
|
||
// For unknown reasons, Windows doesn't always create a page file. Continue collection rather than aborting. | ||
if err == nil { | ||
fileSize = float64(file.Size()) | ||
} | ||
|
||
ch <- prometheus.MustNewConstMetric( | ||
c.pagingFreeBytes, | ||
prometheus.GaugeValue, | ||
fileSize-(pageFile[usage].FirstValue*float64(gpi.PageSize)), | ||
fileString, | ||
) | ||
|
||
ch <- prometheus.MustNewConstMetric( | ||
c.pagingLimitBytes, | ||
prometheus.GaugeValue, | ||
fileSize, | ||
fileString, | ||
) | ||
} | ||
|
||
return nil | ||
} |
This file contains 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,16 @@ | ||
package pagefile_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prometheus-community/windows_exporter/internal/collector/pagefile" | ||
"github.com/prometheus-community/windows_exporter/internal/testutils" | ||
) | ||
|
||
func BenchmarkCollector(b *testing.B) { | ||
testutils.FuncBenchmarkCollector(b, pagefile.Name, pagefile.NewWithFlags) | ||
} | ||
|
||
func TestCollector(t *testing.T) { | ||
testutils.TestCollector(t, pagefile.New, nil) | ||
} |
This file contains 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 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 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 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.