From 5f52b29d47ad903768a4af8940296bff8dd500b9 Mon Sep 17 00:00:00 2001 From: Solly Ross Date: Tue, 2 Oct 2018 17:50:47 -0400 Subject: [PATCH] Disable the custom metrics API with no rules This disables the custom metrics API when no custom metrics rules are given (the resource metrics API acts equivalently). This allows a given adapter to serve only one of the APIs, if desired. --- cmd/adapter/adapter.go | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/cmd/adapter/adapter.go b/cmd/adapter/adapter.go index 22f70374..19731222 100644 --- a/cmd/adapter/adapter.go +++ b/cmd/adapter/adapter.go @@ -85,18 +85,26 @@ func (cmd *PrometheusAdapter) addFlags() { "interval at which to re-list the set of all available metrics from Prometheus") } -func (cmd *PrometheusAdapter) makeProvider(promClient prom.Client, stopCh <-chan struct{}) (provider.CustomMetricsProvider, error) { +func (cmd *PrometheusAdapter) loadConfig() error { // load metrics discovery configuration if cmd.AdapterConfigFile == "" { - return nil, fmt.Errorf("no metrics discovery configuration file specified (make sure to use --config)") + return fmt.Errorf("no metrics discovery configuration file specified (make sure to use --config)") } metricsConfig, err := adaptercfg.FromFile(cmd.AdapterConfigFile) if err != nil { - return nil, fmt.Errorf("unable to load metrics discovery configuration: %v", err) + return fmt.Errorf("unable to load metrics discovery configuration: %v", err) } cmd.metricsConfig = metricsConfig + return nil +} + +func (cmd *PrometheusAdapter) makeProvider(promClient prom.Client, stopCh <-chan struct{}) (provider.CustomMetricsProvider, error) { + if len(cmd.metricsConfig.Rules) == 0 { + return nil, nil + } + // grab the mapper and dynamic client mapper, err := cmd.RESTMapper() if err != nil { @@ -108,7 +116,7 @@ func (cmd *PrometheusAdapter) makeProvider(promClient prom.Client, stopCh <-chan } // extract the namers - namers, err := cmprov.NamersFromConfig(metricsConfig, mapper) + namers, err := cmprov.NamersFromConfig(cmd.metricsConfig, mapper) if err != nil { return nil, fmt.Errorf("unable to construct naming scheme from metrics rules: %v", err) } @@ -177,17 +185,23 @@ func main() { glog.Fatalf("unable to construct Prometheus client: %v", err) } + // load the config + if err := cmd.loadConfig(); err != nil { + glog.Fatalf("unable to load metrics discovery config: %v", err) + } + // construct the provider cmProvider, err := cmd.makeProvider(promClient, wait.NeverStop) if err != nil { glog.Fatalf("unable to construct custom metrics provider: %v", err) } - // attach the provider to the server - cmd.WithCustomMetrics(cmProvider) + // attach the provider to the server, if it's needed + if cmProvider != nil { + cmd.WithCustomMetrics(cmProvider) + } - // attach resource metrics support - // TODO: make this optional + // attach resource metrics support, if it's needed if err := cmd.addResourceMetricsAPI(promClient); err != nil { glog.Fatalf("unable to install resource metrics API: %v", err) }