From 10706ecdc16805f3c4069fe724db96f210a25046 Mon Sep 17 00:00:00 2001 From: Vikram Raman Date: Tue, 19 Feb 2019 16:20:50 -0800 Subject: [PATCH] handle multiple data points --- Makefile | 11 ++++++++--- pkg/client/api.go | 6 +++--- pkg/provider/provider.go | 5 +++-- pkg/provider/translator.go | 32 ++++++++++++++++++-------------- 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index 472e5d2..f5f82f5 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,10 @@ OUT_DIR?=./_output TEMP_DIR:=$(shell mktemp -d) DOCKER_REPO=wavefronthq DOCKER_IMAGE=wavefront-hpa-adapter -VERSION=0.9 +VERSION=0.9.1 + +# for testing, the built image will also be tagged with this name +OVERRIDE_IMAGE_NAME?=vikramraman/wavefront-adapter .PHONY: all test verify-gofmt gofmt verify @@ -40,9 +43,11 @@ container: build-linux cp deploy/Dockerfile $(TEMP_DIR) cp $(OUT_DIR)/$(ARCH)/wavefront-adapter-linux $(TEMP_DIR)/wavefront-adapter cd $(TEMP_DIR) - docker build -t $(DOCKER_REPO)/$(DOCKER_IMAGE)-$(ARCH):$(VERSION) $(TEMP_DIR) - docker tag $(DOCKER_REPO)/$(DOCKER_IMAGE)-$(ARCH):$(VERSION) $(DOCKER_REPO)/$(DOCKER_IMAGE):latest + docker build -t $(DOCKER_REPO)/$(DOCKER_IMAGE):$(VERSION) $(TEMP_DIR) rm -rf $(TEMP_DIR) +ifneq ($(OVERRIDE_IMAGE_NAME),) + docker tag $(DOCKER_REPO)/$(DOCKER_IMAGE):$(VERSION) $(OVERRIDE_IMAGE_NAME) +endif clean: rm -rf $(OUT_DIR) diff --git a/pkg/client/api.go b/pkg/client/api.go index 68e99fc..9d81d22 100644 --- a/pkg/client/api.go +++ b/pkg/client/api.go @@ -95,8 +95,8 @@ func (w DefaultWavefrontClient) ListMetrics(prefix string) ([]string, error) { return result.Metrics, nil } -func (w DefaultWavefrontClient) Query(ts int64, query string) (QueryResult, error) { - glog.V(4).Infof("DEBUG:---WavefrontClient.Query: ts=%s, query=%s", ts, query) +func (w DefaultWavefrontClient) Query(start int64, query string) (QueryResult, error) { + glog.V(4).Infof("DEBUG:---WavefrontClient.Query: start=%s, query=%s", start, query) if query == "" { return QueryResult{}, &Error{ Type: ErrBadData, @@ -106,7 +106,7 @@ func (w DefaultWavefrontClient) Query(ts int64, query string) (QueryResult, erro vals := url.Values{} vals.Set(queryKey, query) - vals.Set(startTime, strconv.FormatInt(ts, 10)) + vals.Set(startTime, strconv.FormatInt(start, 10)) vals.Set(granularity, "m") resp, err := w.Do("GET", chartEndpoint, vals) diff --git a/pkg/provider/provider.go b/pkg/provider/provider.go index ab9cad0..3f04210 100644 --- a/pkg/provider/provider.go +++ b/pkg/provider/provider.go @@ -73,8 +73,9 @@ func (p *wavefrontProvider) query(info provider.CustomMetricInfo, namespace stri } func (p *wavefrontProvider) doQuery(query string) (wave.QueryResult, error) { - now := time.Now().Unix() - queryResult, err := p.waveClient.Query(now, query) + now := time.Now() + start := now.Add(time.Duration(-30) * time.Second) + queryResult, err := p.waveClient.Query(start.Unix(), query) if err != nil { glog.Errorf("unable to fetch metrics from wavefront: %v", err) // don't leak implementation details to the user diff --git a/pkg/provider/translator.go b/pkg/provider/translator.go index ec70dfa..f375fa1 100644 --- a/pkg/provider/translator.go +++ b/pkg/provider/translator.go @@ -101,25 +101,29 @@ func (t WavefrontTranslator) ExternalMetricsFor(metricNames []string) []provider } func (t WavefrontTranslator) ExternalValuesFor(queryResult wave.QueryResult, name string) (*external_metrics.ExternalMetricValueList, error) { - matchingMetrics := []external_metrics.ExternalMetricValue{} + var matchingMetrics []external_metrics.ExternalMetricValue for _, timeseries := range queryResult.Timeseries { - if len(timeseries.Data) == 0 { + length := len(timeseries.Data) + if length == 0 { return nil, fmt.Errorf("no data for external metric: %s", name) } - for _, metric := range timeseries.Data { - value, err := trimFloat(metric[1]) - if err != nil { - glog.Errorf("error converting external metric: %s value: %f", name, metric[1]) - continue - } - metricValue := external_metrics.ExternalMetricValue{ - MetricName: name, - Value: *resource.NewMilliQuantity(int64(1000*value), resource.DecimalSI), - Timestamp: metav1.Now(), - } - matchingMetrics = append(matchingMetrics, metricValue) + // use the last data point + point := timeseries.Data[length-1] + if len(point) != 2 { + return nil, fmt.Errorf("invalid data point for external metric: %s", name) + } + value, err := trimFloat(point[1]) + if err != nil { + glog.Errorf("error converting external metric: %s value: %f", name, point[1]) + continue + } + metricValue := external_metrics.ExternalMetricValue{ + MetricName: name, + Value: *resource.NewMilliQuantity(int64(1000*value), resource.DecimalSI), + Timestamp: metav1.Now(), } + matchingMetrics = append(matchingMetrics, metricValue) } return &external_metrics.ExternalMetricValueList{ Items: matchingMetrics,