Skip to content

Commit

Permalink
handle multiple data points
Browse files Browse the repository at this point in the history
  • Loading branch information
vikramraman committed Feb 20, 2019
1 parent 28cb212 commit 10706ec
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
6 changes: 3 additions & 3 deletions pkg/client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 18 additions & 14 deletions pkg/provider/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 10706ec

Please sign in to comment.