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.
This commit is contained in:
Solly Ross 2018-10-02 17:50:47 -04:00
parent 6c8f44623e
commit 5f52b29d47

View file

@ -85,18 +85,26 @@ func (cmd *PrometheusAdapter) addFlags() {
"interval at which to re-list the set of all available metrics from Prometheus") "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 // load metrics discovery configuration
if cmd.AdapterConfigFile == "" { 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) metricsConfig, err := adaptercfg.FromFile(cmd.AdapterConfigFile)
if err != nil { 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 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 // grab the mapper and dynamic client
mapper, err := cmd.RESTMapper() mapper, err := cmd.RESTMapper()
if err != nil { if err != nil {
@ -108,7 +116,7 @@ func (cmd *PrometheusAdapter) makeProvider(promClient prom.Client, stopCh <-chan
} }
// extract the namers // extract the namers
namers, err := cmprov.NamersFromConfig(metricsConfig, mapper) namers, err := cmprov.NamersFromConfig(cmd.metricsConfig, mapper)
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to construct naming scheme from metrics rules: %v", err) 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) 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 // construct the provider
cmProvider, err := cmd.makeProvider(promClient, wait.NeverStop) cmProvider, err := cmd.makeProvider(promClient, wait.NeverStop)
if err != nil { if err != nil {
glog.Fatalf("unable to construct custom metrics provider: %v", err) glog.Fatalf("unable to construct custom metrics provider: %v", err)
} }
// attach the provider to the server // attach the provider to the server, if it's needed
cmd.WithCustomMetrics(cmProvider) if cmProvider != nil {
cmd.WithCustomMetrics(cmProvider)
}
// attach resource metrics support // attach resource metrics support, if it's needed
// TODO: make this optional
if err := cmd.addResourceMetricsAPI(promClient); err != nil { if err := cmd.addResourceMetricsAPI(promClient); err != nil {
glog.Fatalf("unable to install resource metrics API: %v", err) glog.Fatalf("unable to install resource metrics API: %v", err)
} }