mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-10-27 22:19:05 +01:00
registry: add a multiplier option to convert values to base units (#2774)
Co-authored-by: Claude <noreply@anthropic.com> Signed-off-by: Claude <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude
parent
4f8fc2baef
commit
5b22eca06a
@@ -31,6 +31,7 @@ import (
|
||||
|
||||
"github.com/alecthomas/kingpin/v2"
|
||||
"github.com/prometheus-community/windows_exporter/internal/mi"
|
||||
"github.com/prometheus-community/windows_exporter/internal/multiplier"
|
||||
"github.com/prometheus-community/windows_exporter/internal/types"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"go.yaml.in/yaml/v3"
|
||||
@@ -233,6 +234,15 @@ func (c *Collector) Build(logger *slog.Logger, _ *mi.Session) error {
|
||||
continue
|
||||
}
|
||||
|
||||
m, err := multiplier.New(value.Multiplier)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("value %q of key %s: %w", value.Name, label, err))
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
value.multiplier = m
|
||||
|
||||
help := value.Help
|
||||
if help == "" {
|
||||
help = "windows_exporter: custom registry metric"
|
||||
@@ -369,7 +379,7 @@ func (c *Collector) collectKey(ch chan<- prometheus.Metric, key Key) error {
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
value.desc,
|
||||
value.metricType,
|
||||
float64(val),
|
||||
value.multiplier.Apply(float64(val)),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -78,6 +78,7 @@ func TestCollectorMetrics(t *testing.T) {
|
||||
// 64-bit handling. Both are well under 2^53, so they convert to float64 exactly.
|
||||
require.NoError(t, k.SetDWordValue("TestDword", 1234))
|
||||
require.NoError(t, k.SetQWordValue("TestQword", 5_000_000_000))
|
||||
require.NoError(t, k.SetDWordValue("TestMilliseconds", 9))
|
||||
require.NoError(t, k.Close())
|
||||
|
||||
t.Cleanup(func() {
|
||||
@@ -105,6 +106,13 @@ func TestCollectorMetrics(t *testing.T) {
|
||||
Type: "counter",
|
||||
Labels: map[string]string{"foo": "bar"},
|
||||
},
|
||||
// The multiplier converts milliseconds to seconds; 9 * 0.001
|
||||
// would be 0.009000000000000001.
|
||||
{
|
||||
Name: "TestMilliseconds",
|
||||
Metric: "windows_registry_test_scaled_seconds",
|
||||
Multiplier: new(0.001),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -137,6 +145,9 @@ windows_registry_key_success{key="testgroup"} 1
|
||||
# HELP windows_registry_test_custom_metric custom registry help text
|
||||
# TYPE windows_registry_test_custom_metric counter
|
||||
windows_registry_test_custom_metric{foo="bar"} 5e+09
|
||||
# HELP windows_registry_test_scaled_seconds windows_exporter: custom registry metric
|
||||
# TYPE windows_registry_test_scaled_seconds gauge
|
||||
windows_registry_test_scaled_seconds 0.009
|
||||
# HELP windows_registry_testgroup_testdword windows_exporter: custom registry metric
|
||||
# TYPE windows_registry_testgroup_testdword gauge
|
||||
windows_registry_testgroup_testdword 1234
|
||||
|
||||
@@ -121,6 +121,16 @@ func TestCollectorBuildErrors(t *testing.T) {
|
||||
},
|
||||
}},
|
||||
},
|
||||
{
|
||||
name: "invalid multiplier",
|
||||
config: registry.Config{Keys: []registry.Key{
|
||||
{
|
||||
Name: "windows_nt",
|
||||
Key: `HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion`,
|
||||
Values: []registry.Value{{Name: "CurrentMajorVersionNumber", Multiplier: new(0.0)}},
|
||||
},
|
||||
}},
|
||||
},
|
||||
{
|
||||
name: "shared metric name with inconsistent help text",
|
||||
config: registry.Config{Keys: []registry.Key{
|
||||
|
||||
@@ -19,6 +19,7 @@ package registry
|
||||
|
||||
import (
|
||||
"github.com/prometheus-community/windows_exporter/internal/config/yamlstring"
|
||||
"github.com/prometheus-community/windows_exporter/internal/multiplier"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"go.yaml.in/yaml/v3"
|
||||
winregistry "golang.org/x/sys/windows/registry"
|
||||
@@ -36,15 +37,17 @@ type Key struct {
|
||||
}
|
||||
|
||||
type Value struct {
|
||||
Name string `json:"name" yaml:"name"`
|
||||
Metric string `json:"metric" yaml:"metric"`
|
||||
Help string `json:"help" yaml:"help"`
|
||||
Type string `json:"type" yaml:"type"`
|
||||
Labels map[string]string `json:"labels" yaml:"labels"`
|
||||
Name string `json:"name" yaml:"name"`
|
||||
Metric string `json:"metric" yaml:"metric"`
|
||||
Help string `json:"help" yaml:"help"`
|
||||
Type string `json:"type" yaml:"type"`
|
||||
Labels map[string]string `json:"labels" yaml:"labels"`
|
||||
Multiplier *float64 `json:"multiplier" yaml:"multiplier"`
|
||||
|
||||
// resolved at Build time
|
||||
desc *prometheus.Desc
|
||||
metricType prometheus.ValueType
|
||||
multiplier multiplier.Multiplier
|
||||
}
|
||||
|
||||
// UnmarshalYAML decodes the registry block of the configuration file, where
|
||||
|
||||
Reference in New Issue
Block a user