From f2787ace72c96a45b5862ee8b80af570a6c8aa93 Mon Sep 17 00:00:00 2001 From: Nicolas Takashi Date: Tue, 19 May 2026 16:03:54 +0100 Subject: [PATCH 1/2] config: add reusable exporter config package Signed-off-by: Nicolas Takashi --- config/config.go | 54 +++++++++++++++++++++++++++++++++++ config/config_test.go | 65 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 config/config.go create mode 100644 config/config_test.go diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000000..f0f169d33c --- /dev/null +++ b/config/config.go @@ -0,0 +1,54 @@ +// Copyright 2026 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 config + +import "fmt" + +const ( + DefaultWebTelemetryPath = "/metrics" + DefaultWebMaxRequests = 40 + DefaultRuntimeGoMaxProcs = 1 +) + +// Config contains the user-facing configuration currently adapted by the +// node_exporter binary into the reusable runtime and HTTP handler layers. +type Config struct { + WebTelemetryPath string + WebDisableExporterMetrics bool + WebMaxRequests int + CollectorDisableDefaults bool + RuntimeGoMaxProcs int + EnabledCollectors []string +} + +func NewConfigWithDefaults() Config { + return Config{ + WebTelemetryPath: DefaultWebTelemetryPath, + WebMaxRequests: DefaultWebMaxRequests, + RuntimeGoMaxProcs: DefaultRuntimeGoMaxProcs, + } +} + +func (c Config) Validate() error { + if c.WebTelemetryPath == "" { + return fmt.Errorf("web telemetry path must not be empty") + } + if c.WebMaxRequests < 0 { + return fmt.Errorf("web max requests must be greater than or equal to zero") + } + if c.RuntimeGoMaxProcs <= 0 { + return fmt.Errorf("runtime gomaxprocs must be greater than zero") + } + return nil +} diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 0000000000..a39dbf9521 --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,65 @@ +// Copyright 2026 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 config + +import "testing" + +func TestNewConfigWithDefaults(t *testing.T) { + cfg := NewConfigWithDefaults() + + if got, want := cfg.WebTelemetryPath, DefaultWebTelemetryPath; got != want { + t.Errorf("Expected: %q, Got: %q", want, got) + } + if got, want := cfg.WebMaxRequests, DefaultWebMaxRequests; got != want { + t.Errorf("Expected: %d, Got: %d", want, got) + } + if got, want := cfg.RuntimeGoMaxProcs, DefaultRuntimeGoMaxProcs; got != want { + t.Errorf("Expected: %d, Got: %d", want, got) + } +} + +func TestConfigValidate(t *testing.T) { + cfg := NewConfigWithDefaults() + + if err := cfg.Validate(); err != nil { + t.Fatalf("expected nil error, got %v", err) + } +} + +func TestConfigValidateRequiresTelemetryPath(t *testing.T) { + cfg := NewConfigWithDefaults() + cfg.WebTelemetryPath = "" + + if err := cfg.Validate(); err == nil { + t.Fatal("expected error, got nil") + } +} + +func TestConfigValidateRejectsNegativeMaxRequests(t *testing.T) { + cfg := NewConfigWithDefaults() + cfg.WebMaxRequests = -1 + + if err := cfg.Validate(); err == nil { + t.Fatal("expected error, got nil") + } +} + +func TestConfigValidateRejectsNonPositiveGoMaxProcs(t *testing.T) { + cfg := NewConfigWithDefaults() + cfg.RuntimeGoMaxProcs = 0 + + if err := cfg.Validate(); err == nil { + t.Fatal("expected error, got nil") + } +} From bd1a15ee90a8ce7a13e950804c37b871d51f5aa2 Mon Sep 17 00:00:00 2001 From: Nicolas Takashi Date: Tue, 19 May 2026 16:16:33 +0100 Subject: [PATCH 2/2] collector: add reusable runtime wrapper Signed-off-by: Nicolas Takashi --- collector/runtime.go | 69 +++++++++++++++++++++++++++++++++++++++ collector/runtime_test.go | 66 +++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 collector/runtime.go create mode 100644 collector/runtime_test.go diff --git a/collector/runtime.go b/collector/runtime.go new file mode 100644 index 0000000000..4dc25bbc7f --- /dev/null +++ b/collector/runtime.go @@ -0,0 +1,69 @@ +// Copyright 2026 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 ( + "log/slog" + "sort" + + "github.com/prometheus/client_golang/prometheus" + + "github.com/prometheus/node_exporter/config" +) + +// Runtime represents a single exporter runtime instance built from a reusable +// Config. +type Runtime struct { + collector *NodeCollector +} + +func NewRuntime(cfg config.Config, logger *slog.Logger) (*Runtime, error) { + if err := cfg.Validate(); err != nil { + return nil, err + } + + if cfg.CollectorDisableDefaults { + DisableDefaultCollectors() + } + + nodeCollector, err := NewNodeCollector(logger, cfg.EnabledCollectors...) + if err != nil { + return nil, err + } + + return &Runtime{collector: nodeCollector}, nil +} + +func (r *Runtime) Collectors() []prometheus.Collector { + return []prometheus.Collector{r.collector} +} + +func (r *Runtime) Registry() (*prometheus.Registry, error) { + registry := prometheus.NewRegistry() + for _, c := range r.Collectors() { + if err := registry.Register(c); err != nil { + return nil, err + } + } + return registry, nil +} + +func (r *Runtime) EnabledCollectors() []string { + enabled := make([]string, 0, len(r.collector.Collectors)) + for name := range r.collector.Collectors { + enabled = append(enabled, name) + } + sort.Strings(enabled) + return enabled +} diff --git a/collector/runtime_test.go b/collector/runtime_test.go new file mode 100644 index 0000000000..e3bc30b04e --- /dev/null +++ b/collector/runtime_test.go @@ -0,0 +1,66 @@ +// Copyright 2026 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 ( + "io" + "log/slog" + "testing" + + "github.com/prometheus/node_exporter/config" +) + +func TestNewRuntimeCollectors(t *testing.T) { + runtime, err := NewRuntime(config.NewConfigWithDefaults(), slog.New(slog.NewTextHandler(io.Discard, nil))) + if err != nil { + t.Fatalf("expected nil error, got %v", err) + } + + if got, want := len(runtime.Collectors()), 1; got != want { + t.Errorf("Expected: %d, Got: %d", want, got) + } + + if got := len(runtime.EnabledCollectors()); got == 0 { + t.Fatal("expected at least one enabled collector") + } +} + +func TestNewRuntimeValidateConfig(t *testing.T) { + cfg := config.NewConfigWithDefaults() + cfg.RuntimeGoMaxProcs = 0 + + if _, err := NewRuntime(cfg, slog.New(slog.NewTextHandler(io.Discard, nil))); err == nil { + t.Fatal("expected error, got nil") + } +} + +func TestRuntimeRegistry(t *testing.T) { + runtime, err := NewRuntime(config.NewConfigWithDefaults(), slog.New(slog.NewTextHandler(io.Discard, nil))) + if err != nil { + t.Fatalf("expected nil error, got %v", err) + } + + registry, err := runtime.Registry() + if err != nil { + t.Fatalf("expected nil error, got %v", err) + } + + metrics, err := registry.Gather() + if err != nil { + t.Fatalf("expected nil error, got %v", err) + } + if got := len(metrics); got == 0 { + t.Fatal("expected gathered metrics, got none") + } +}