Skip to content

feat(metrics): add configurable metrics level filtering for Prometheus exporter #2174

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 1 commit into from
Jun 23, 2025
Merged
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
4 changes: 4 additions & 0 deletions cmd/kepler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,15 @@ func createServices(logger *slog.Logger, cfg *config.Config) ([]service.Service,
func createPrometheusExporter(logger *slog.Logger, cfg *config.Config, apiServer *server.APIServer, pm *monitor.PowerMonitor) (*prometheus.Exporter, error) {
logger.Debug("Creating Prometheus exporter")

// Use metrics level from configuration (already parsed)
metricsLevel := cfg.Exporter.Prometheus.MetricsLevel

collectors, err := prometheus.CreateCollectors(
pm,
prometheus.WithLogger(logger),
prometheus.WithProcFSPath(cfg.Host.ProcFS),
prometheus.WithNodeName(cfg.Kube.Node),
prometheus.WithMetricsLevel(metricsLevel),
)
if err != nil {
return nil, fmt.Errorf("failed to create Prometheus collectors: %w", err)
Expand Down
4 changes: 4 additions & 0 deletions compose/dev/kepler-dev/etc/kepler/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ exporter:
debugCollectors:
- go
- process
metricsLevel:
- node
- container
- process

debug: # debug related config
pprof: # pprof related config
Expand Down
55 changes: 53 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"time"

"github.com/alecthomas/kingpin/v2"
"github.com/sustainable-computing-io/kepler/internal/exporter/prometheus/metrics"
"gopkg.in/yaml.v3"
"k8s.io/utils/ptr"
)
Expand Down Expand Up @@ -53,8 +54,9 @@
}

PrometheusExporter struct {
Enabled *bool `yaml:"enabled"`
DebugCollectors []string `yaml:"debugCollectors"`
Enabled *bool `yaml:"enabled"`
DebugCollectors []string `yaml:"debugCollectors"`
MetricsLevel metrics.Level `yaml:"metricsLevel"`
}

Exporter struct {
Expand Down Expand Up @@ -91,6 +93,45 @@
}
)

// MetricsLevelValue is a custom kingpin.Value that parses metrics levels directly into metrics.Level
type MetricsLevelValue struct {
level *metrics.Level
Copy link
Collaborator

Choose a reason for hiding this comment

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

should this be pointer? won't value suffice?

}

// NewMetricsLevelValue creates a new MetricsLevelValue with the given target
func NewMetricsLevelValue(target *metrics.Level) *MetricsLevelValue {
return &MetricsLevelValue{level: target}
}

// Set implements kingpin.Value interface - parses and accumulates metrics levels
func (m *MetricsLevelValue) Set(value string) error {
// Parse the single value into a level
level, err := metrics.ParseLevel([]string{value})
if err != nil {
return err
}

// If this is the first value, initialize to 0 first to clear any default
allLevels := metrics.MetricsLevelNode | metrics.MetricsLevelProcess | metrics.MetricsLevelContainer | metrics.MetricsLevelVM | metrics.MetricsLevelPod
if *m.level == allLevels {
*m.level = 0
}

// Accumulate the level using bitwise OR
*m.level |= level
return nil
}

// String implements kingpin.Value interface
func (m *MetricsLevelValue) String() string {
return m.level.String()
}

// IsCumulative implements kingpin.Value interface to support multiple values
func (m *MetricsLevelValue) IsCumulative() bool {
return true
}

type SkipValidation int

const (
Expand Down Expand Up @@ -122,6 +163,7 @@
ExporterPrometheusEnabledFlag = "exporter.prometheus"
// NOTE: not a flag
ExporterPrometheusDebugCollectors = "exporter.prometheus.debug-collectors"
ExporterPrometheusMetricsFlag = "metrics"

// kubernetes flags
KubernetesFlag = "kube.enable"
Expand Down Expand Up @@ -156,6 +198,7 @@
Prometheus: PrometheusExporter{
Enabled: ptr.To(true),
DebugCollectors: []string{"go"},
MetricsLevel: metrics.MetricsLevelNode | metrics.MetricsLevelProcess | metrics.MetricsLevelContainer | metrics.MetricsLevelVM | metrics.MetricsLevelPod,
},
},
Debug: Debug{
Expand Down Expand Up @@ -252,6 +295,9 @@

prometheusExporterEnabled := app.Flag(ExporterPrometheusEnabledFlag, "Enable Prometheus exporter").Default("true").Bool()

var metricsLevel = metrics.MetricsLevelNode | metrics.MetricsLevelProcess | metrics.MetricsLevelContainer | metrics.MetricsLevelVM | metrics.MetricsLevelPod
app.Flag(ExporterPrometheusMetricsFlag, "Metrics levels to export (node,process,container,vm,pod)").SetValue(NewMetricsLevelValue(&metricsLevel))

kubernetes := app.Flag(KubernetesFlag, "Monitor kubernetes").Default("false").Bool()
kubeconfig := app.Flag(KubeConfigFlag, "Path to a kubeconfig. Only required if out-of-cluster.").ExistingFile()
nodeName := app.Flag(KubeNodeNameFlag, "Name of kubernetes node on which kepler is running.").String()
Expand Down Expand Up @@ -295,6 +341,10 @@
cfg.Exporter.Prometheus.Enabled = prometheusExporterEnabled
}

if flagsSet[ExporterPrometheusMetricsFlag] {
cfg.Exporter.Prometheus.MetricsLevel = metricsLevel
}

Check warning on line 346 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L345-L346

Added lines #L345 - L346 were not covered by tests

if flagsSet[KubernetesFlag] {
cfg.Kube.Enabled = kubernetes
}
Expand Down Expand Up @@ -468,6 +518,7 @@
{ExporterStdoutEnabledFlag, fmt.Sprintf("%v", c.Exporter.Stdout.Enabled)},
{ExporterPrometheusEnabledFlag, fmt.Sprintf("%v", c.Exporter.Prometheus.Enabled)},
{ExporterPrometheusDebugCollectors, strings.Join(c.Exporter.Prometheus.DebugCollectors, ", ")},
{ExporterPrometheusMetricsFlag, c.Exporter.Prometheus.MetricsLevel.String()},
{pprofEnabledFlag, fmt.Sprintf("%v", c.Debug.Pprof.Enabled)},
{KubeConfigFlag, fmt.Sprintf("%v", c.Kube.Config)},
}
Expand Down
Loading
Loading