From 2d1d51e829ec66e18131d8bf86fd423a50aab511 Mon Sep 17 00:00:00 2001 From: Eduard Ganiukov Date: Wed, 14 Aug 2019 11:46:57 +0200 Subject: [PATCH] collector/prometheus: add prometheus server (optional) as an annotation in HPA. Signed-off-by: Eduard Ganiukov --- README.md | 4 ++++ pkg/collector/prometheus_collector.go | 21 ++++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2a86410..8e7ee51 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,10 @@ kind: HorizontalPodAutoscaler metadata: name: myapp-hpa annotations: + # This annotation is optional. + # If specified, then this prometheus server is used, + # instead of the prometheus server specified as the CLI argument `--prometheus-server`. + metric-config.external.prometheus-query.prometheus/prometheus-server: http://prometheus.my-namespace.svc # metric-config.../ # == query-name metric-config.external.prometheus-query.prometheus/processed-events-per-second: | diff --git a/pkg/collector/prometheus_collector.go b/pkg/collector/prometheus_collector.go index d2400b3..6f7a9cf 100644 --- a/pkg/collector/prometheus_collector.go +++ b/pkg/collector/prometheus_collector.go @@ -18,8 +18,9 @@ import ( ) const ( - PrometheusMetricName = "prometheus-query" - prometheusQueryNameLabelKey = "query-name" + PrometheusMetricName = "prometheus-query" + prometheusQueryNameLabelKey = "query-name" + prometheusServerAnnotationKey = "prometheus-server" ) type NoResultError struct { @@ -38,7 +39,7 @@ type PrometheusCollectorPlugin struct { func NewPrometheusCollectorPlugin(client kubernetes.Interface, prometheusServer string) (*PrometheusCollectorPlugin, error) { cfg := api.Config{ Address: prometheusServer, - RoundTripper: &http.Transport{}, + RoundTripper: http.DefaultTransport, } promClient, err := api.NewClient(cfg) @@ -101,6 +102,20 @@ func NewPrometheusCollector(client kubernetes.Interface, promAPI promv1.API, hpa } else { return nil, fmt.Errorf("no prometheus query defined for metric") } + + // Use custom Prometheus URL if defined in HPA annotation. + if promServer, ok := config.Config[prometheusServerAnnotationKey]; ok { + cfg := api.Config{ + Address: promServer, + RoundTripper: http.DefaultTransport, + } + + promClient, err := api.NewClient(cfg) + if err != nil { + return nil, err + } + c.promAPI = promv1.NewAPI(promClient) + } } return c, nil