Skip to content

Commit

Permalink
Merge pull request #193 from x13n/scrape-metrics
Browse files Browse the repository at this point in the history
Fix metric whitelist updating in timeSeriesBuilder
  • Loading branch information
x13n authored Aug 16, 2018
2 parents 27fb44a + 5fd0c53 commit f46f940
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion prometheus-to-sd/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ WORKDIR ${GOPATH}/src/github.com/GoogleCloudPlatform/k8s-stackdriver/prometheus-
COPY . ./
RUN CGO_ENABLED=0 GOOS=linux go build -o /monitor

FROM gcr.io/google-containers/debian-base-amd64:0.3
FROM gcr.io/google-containers/debian-base-amd64:0.3.2
RUN clean-install ca-certificates
# nobody:nobody
USER 65534:65534
Expand Down
2 changes: 1 addition & 1 deletion prometheus-to-sd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ all: build

ENVVAR = GOOS=linux GOARCH=amd64 CGO_ENABLED=0
PREFIX = staging-k8s.gcr.io
TAG = v0.3.0
TAG = v0.3.1

build:
$(ENVVAR) go build -a -o monitor
Expand Down
3 changes: 2 additions & 1 deletion prometheus-to-sd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func getSourceConfigs(gceConfig *config.GceConfig) []config.SourceConfig {
return append(staticSourceConfigs, dynamicSourceConfigs...)
}

// TODO(x13n): factor out all the logic below and add tests, this doesn't really belong in main.
func scrapeMetrics(commonConfig *config.CommonConfig, sourceConfig *config.SourceConfig, metricDescriptorCache *translator.MetricDescriptorCache) (map[string]*dto.MetricFamily, error) {
metrics, err := translator.GetPrometheusMetrics(sourceConfig)
if err != nil {
Expand All @@ -168,7 +169,7 @@ func readAndPushDataToStackdriver(stackdriverService *v3.Service, gceConf *confi
metricDescriptorCache := translator.NewMetricDescriptorCache(stackdriverService, commonConfig, sourceConfig.Component)
signal := time.After(0)
useWhitelistedMetricsAutodiscovery := *autoWhitelistMetrics && len(sourceConfig.Whitelisted) == 0
timeSeriesBuilder := translator.NewTimeSeriesBuilder(commonConfig, sourceConfig.Whitelisted, metricDescriptorCache)
timeSeriesBuilder := translator.NewTimeSeriesBuilder(commonConfig, &sourceConfig, metricDescriptorCache)
exportTicker := time.Tick(*exportInterval)

for range time.Tick(*scrapeInterval) {
Expand Down
14 changes: 7 additions & 7 deletions prometheus-to-sd/translator/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ var supportedMetricTypes = map[dto.MetricType]bool{
const falseValueEpsilon = 0.001

type timeSeriesBuilder struct {
config *config.CommonConfig
whitelisted []string
commonConfig *config.CommonConfig
sourceConfig *config.SourceConfig
cache *MetricDescriptorCache
metricFamilies map[string]*metricFamilyWithTimestamp
}
Expand All @@ -55,10 +55,10 @@ type metricFamilyWithTimestamp struct {
}

// NewTimeSeriesBuilder creates new builder object that keeps intermediate state of metrics.
func NewTimeSeriesBuilder(config *config.CommonConfig, whitelisted []string, cache *MetricDescriptorCache) *timeSeriesBuilder {
func NewTimeSeriesBuilder(commonConfig *config.CommonConfig, sourceConfig *config.SourceConfig, cache *MetricDescriptorCache) *timeSeriesBuilder {
return &timeSeriesBuilder{
config: config,
whitelisted: whitelisted,
commonConfig: commonConfig,
sourceConfig: sourceConfig,
cache: cache,
metricFamilies: make(map[string]*metricFamilyWithTimestamp),
}
Expand All @@ -75,13 +75,13 @@ func (t *timeSeriesBuilder) Update(metrics map[string]*dto.MetricFamily) {
func (t *timeSeriesBuilder) Build() []*v3.TimeSeries {
var ts []*v3.TimeSeries
startTime := getStartTime(t.metricFamilies)
t.metricFamilies = filterWhitelisted(t.metricFamilies, t.whitelisted)
t.metricFamilies = filterWhitelisted(t.metricFamilies, t.sourceConfig.Whitelisted)

for name, metric := range t.metricFamilies {
if t.cache.IsMetricBroken(name) {
continue
}
f, err := translateFamily(t.config, metric, startTime, t.cache)
f, err := translateFamily(t.commonConfig, metric, startTime, t.cache)
if err != nil {
glog.Warningf("Error while processing metric %s: %v", name, err)
} else {
Expand Down
12 changes: 8 additions & 4 deletions prometheus-to-sd/translator/translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,11 @@ var metricDescriptors = map[string]*v3.MetricDescriptor{

func TestTranslatePrometheusToStackdriver(t *testing.T) {
cache := buildCacheForTesting()
whitelistedMetrics := []string{testMetricName, testMetricHistogram, booleanMetricName, floatMetricName}
sourceConfig := &config.SourceConfig{
Whitelisted: []string{testMetricName, testMetricHistogram, booleanMetricName, floatMetricName},
}

tsb := NewTimeSeriesBuilder(commonConfig, whitelistedMetrics, cache)
tsb := NewTimeSeriesBuilder(commonConfig, sourceConfig, cache)
tsb.Update(metrics)
ts := tsb.Build()

Expand Down Expand Up @@ -318,8 +320,10 @@ func TestTranslatePrometheusToStackdriver(t *testing.T) {
}

func TestMergeScrapes(t *testing.T) {
whitelistedMetrics := []string{testMetricName, floatMetricName}
tsb := NewTimeSeriesBuilder(commonConfig, whitelistedMetrics, buildCacheForTesting())
sourceConfig := &config.SourceConfig{
Whitelisted: []string{testMetricName, floatMetricName},
}
tsb := NewTimeSeriesBuilder(commonConfig, sourceConfig, buildCacheForTesting())
scrape := map[string]*dto.MetricFamily{
testMetricName: {
Name: &testMetricName,
Expand Down

0 comments on commit f46f940

Please sign in to comment.