-
Notifications
You must be signed in to change notification settings - Fork 204
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
sthaha
merged 1 commit into
sustainable-computing-io:reboot
from
vimalk78:level-metrics
Jun 23, 2025
+1,229
−45
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -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" | ||
) | ||
|
@@ -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 { | ||
|
@@ -91,6 +93,45 @@ | |
} | ||
) | ||
|
||
// MetricsLevelValue is a custom kingpin.Value that parses metrics levels directly into metrics.Level | ||
type MetricsLevelValue struct { | ||
level *metrics.Level | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ( | ||
|
@@ -122,6 +163,7 @@ | |
ExporterPrometheusEnabledFlag = "exporter.prometheus" | ||
// NOTE: not a flag | ||
ExporterPrometheusDebugCollectors = "exporter.prometheus.debug-collectors" | ||
ExporterPrometheusMetricsFlag = "metrics" | ||
|
||
// kubernetes flags | ||
KubernetesFlag = "kube.enable" | ||
|
@@ -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{ | ||
|
@@ -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() | ||
|
@@ -295,6 +341,10 @@ | |
cfg.Exporter.Prometheus.Enabled = prometheusExporterEnabled | ||
} | ||
|
||
if flagsSet[ExporterPrometheusMetricsFlag] { | ||
cfg.Exporter.Prometheus.MetricsLevel = metricsLevel | ||
} | ||
|
||
if flagsSet[KubernetesFlag] { | ||
cfg.Kube.Enabled = kubernetes | ||
} | ||
|
@@ -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)}, | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.