Skip to content

Commit

Permalink
improved prefix handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vikramraman committed Sep 17, 2020
1 parent 0485444 commit 7c64d4f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ OUT_DIR?=./_output
DOCKER_REPO=wavefronthq
DOCKER_IMAGE=wavefront-hpa-adapter

VERSION=0.9.5
VERSION=0.9.6
GOLANG_VERSION?=1.13
BINARY_NAME=wavefront-adapter
GIT_COMMIT:=$(shell git rev-parse --short HEAD)
Expand Down
3 changes: 2 additions & 1 deletion cmd/wavefront-adapter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/url"
"os"
"runtime"
"strings"
"time"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -78,7 +79,7 @@ func (a *WavefrontAdapter) makeProviderOrDie() customprovider.MetricsProvider {
KubeClient: kubeClient,
Mapper: mapper,
WaveClient: waveClient,
Prefix: a.CustomMetricPrefix,
Prefix: strings.Trim(a.CustomMetricPrefix, "."),
ListInterval: a.MetricsRelistInterval,
ExternalCfg: a.AdapterConfigFile,
})
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ require (
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v0.0.2-0.20180319062004-c439c4fa0937 // indirect
github.com/spf13/pflag v1.0.1 // indirect
github.com/stretchr/testify v1.4.0 // indirect
github.com/stretchr/testify v1.4.0
github.com/ugorji/go v0.0.0-20170107133203-ded73eae5db7 // indirect
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
go.etcd.io/bbolt v1.3.3 // indirect
Expand Down
4 changes: 3 additions & 1 deletion pkg/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ package provider

import (
"fmt"
"testing"

"github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/provider"
"github.com/wavefronthq/wavefront-kubernetes-adapter/pkg/client"

"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/dynamic/fake"
"testing"
)

func TestListAllMetrics(t *testing.T) {
Expand Down
13 changes: 8 additions & 5 deletions pkg/provider/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (t wavefrontTranslator) MatchValuesToNames(queryResult wave.QueryResult, gr
func (t wavefrontTranslator) CustomMetricsFor(metricNames []string) []provider.CustomMetricInfo {
var customMetrics []provider.CustomMetricInfo
for _, metricName := range metricNames {
resourceName, metric := splitMetric(metricName)
resourceName, metric := splitMetric(t.prefix, metricName)
if resourceName == "" || metric == "" {
continue
}
Expand Down Expand Up @@ -219,12 +219,15 @@ func namespaced(resourceName string) bool {
}

// splits a metric such as "kubernetes.pod.cpu.limit" into "pod" and "cpu.limit"
func splitMetric(metricName string) (string, string) {
parts := strings.SplitN(metricName, ".", 3)
if len(parts) != 3 {
func splitMetric(prefix, metricName string) (string, string) {
if strings.HasPrefix(metricName, prefix) {
metricName = metricName[len(prefix)+1:]
}
parts := strings.SplitN(metricName, ".", 2)
if len(parts) != 2 {
return "", ""
}
return parts[1], parts[2]
return parts[0], parts[1]
}

// trims a float64 to 3 decimal digits
Expand Down
17 changes: 17 additions & 0 deletions pkg/provider/translator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package provider

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSplitMetric(t *testing.T) {
res, metric := splitMetric("kubernetes", "kubernetes.pod.cpu.usage")
assert.Equal(t, res, "pod")
assert.Equal(t, metric, "cpu.usage")

res, metric = splitMetric("pks.kubernetes", "pks.kubernetes.pod.cpu.usage")
assert.Equal(t, res, "pod")
assert.Equal(t, metric, "cpu.usage")
}

0 comments on commit 7c64d4f

Please sign in to comment.