diff --git a/cmd/adapter/adapter.go b/cmd/adapter/adapter.go index 19731222e..46d3275c9 100644 --- a/cmd/adapter/adapter.go +++ b/cmd/adapter/adapter.go @@ -53,6 +53,8 @@ type PrometheusAdapter struct { AdapterConfigFile string // MetricsRelistInterval is the interval at which to relist the set of available metrics MetricsRelistInterval time.Duration + // MetricsStartDuration is the period to query available metrics for + MetricsStartDuration time.Duration metricsConfig *adaptercfg.MetricsDiscoveryConfig } @@ -83,6 +85,8 @@ func (cmd *PrometheusAdapter) addFlags() { "and custom metrics API resources") cmd.Flags().DurationVar(&cmd.MetricsRelistInterval, "metrics-relist-interval", cmd.MetricsRelistInterval, ""+ "interval at which to re-list the set of all available metrics from Prometheus") + cmd.Flags().DurationVar(&cmd.MetricsStartDuration, "metrics-start-duration", cmd.MetricsStartDuration, ""+ + "period for which to query the set of available metrics from Prometheus ('start' parameter)") } func (cmd *PrometheusAdapter) loadConfig() error { @@ -122,7 +126,7 @@ func (cmd *PrometheusAdapter) makeProvider(promClient prom.Client, stopCh <-chan } // construct the provider and start it - cmProvider, runner := cmprov.NewPrometheusProvider(mapper, dynClient, promClient, namers, cmd.MetricsRelistInterval) + cmProvider, runner := cmprov.NewPrometheusProvider(mapper, dynClient, promClient, namers, cmd.MetricsRelistInterval, cmd.MetricsStartDuration) runner.RunUntil(stopCh) return cmProvider, nil @@ -173,6 +177,7 @@ func main() { cmd := &PrometheusAdapter{ PrometheusURL: "https://localhost", MetricsRelistInterval: 10 * time.Minute, + MetricsStartDuration: 10 * time.Minute, } cmd.Name = "prometheus-metrics-adapter" cmd.addFlags() diff --git a/pkg/custom-provider/provider.go b/pkg/custom-provider/provider.go index 96f8831cc..3ffc23c3d 100644 --- a/pkg/custom-provider/provider.go +++ b/pkg/custom-provider/provider.go @@ -55,9 +55,10 @@ type prometheusProvider struct { SeriesRegistry } -func NewPrometheusProvider(mapper apimeta.RESTMapper, kubeClient dynamic.Interface, promClient prom.Client, namers []MetricNamer, updateInterval time.Duration) (provider.CustomMetricsProvider, Runnable) { +func NewPrometheusProvider(mapper apimeta.RESTMapper, kubeClient dynamic.Interface, promClient prom.Client, namers []MetricNamer, updateInterval time.Duration, startDuration time.Duration) (provider.CustomMetricsProvider, Runnable) { lister := &cachingMetricsLister{ updateInterval: updateInterval, + startDuration: startDuration, promClient: promClient, namers: namers, @@ -191,6 +192,7 @@ type cachingMetricsLister struct { promClient prom.Client updateInterval time.Duration + startDuration time.Duration namers []MetricNamer } @@ -212,7 +214,7 @@ type selectorSeries struct { } func (l *cachingMetricsLister) updateMetrics() error { - startTime := pmodel.Now().Add(-1 * l.updateInterval) + startTime := pmodel.Now().Add(-1 * l.startDuration) // don't do duplicate queries when it's just the matchers that change seriesCacheByQuery := make(map[prom.Selector][]prom.Series)