Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions collector/runtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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 (
"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
}
Comment on lines +27 to +29

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the collectors' enable/disabled state also needs to be part of the runtime object so multiple runtimes don't affect each other.


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
}
66 changes: 66 additions & 0 deletions collector/runtime_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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 (
"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")
}
}
54 changes: 54 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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 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
Comment on lines +27 to +29

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These configuration options don't seem related to configuring the metric collection, could we leave them out?

CollectorDisableDefaults bool
RuntimeGoMaxProcs int

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one also seems unrelated to metric collection

EnabledCollectors []string

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides enabling/disabling collectors, it seems like we're missing a lot of config pieces that are specific to one single collector that also need to be here.

For example, we have --collector.textfile.directory, and this isn't present here, therefore we won't be able to configure this option whem embedding node exporter

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can take a look at another similar PR in another exporter: prometheus/mysqld_exporter#1043

we have things like this:

type Config struct {
    Collectors            map[string]bool
    InfoSchemaProcesslist InfoSchemaProcesslistConfig
    // ...
}

type InfoSchemaProcesslistConfig struct {
    MinTime         int
    ProcessesByUser bool
    ProcessesByHost bool
}

where infoSchemaProcesslistConfig is one of many collectors in MySQL exporter

}

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
}
65 changes: 65 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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 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")
}
}
Loading