Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Commit

Permalink
include .bucket suffix for prom histograms
Browse files Browse the repository at this point in the history
  • Loading branch information
vikramraman committed Jan 14, 2020
1 parent 67b0e67 commit 41c94c4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
15 changes: 11 additions & 4 deletions cmd/wavefront-collector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ func createAgentOrDie(cfg *configuration.Config) *agent.Agent {
// always read from the environment variable
cfg.Daemon = os.Getenv(util.DaemonModeEnvVar) != ""

// backwards compat: used by prometheus sources to format histogram metric names
setEnvVar("omitBucketSuffix", strconv.FormatBool(cfg.OmitBucketSuffix))

clusterName := cfg.ClusterName

kubeClient := createKubeClientOrDie(*cfg.Sources.SummaryConfig)
Expand Down Expand Up @@ -449,10 +452,14 @@ func enableProfiling(enable bool) {
func enableForcedGC(enable bool) {
if enable {
log.Info("enabling forced garbage collection")
err := os.Setenv(util.ForceGC, "true")
if err != nil {
log.Errorf("error setting environment variable %s: %v", util.ForceGC, err)
}
setEnvVar(util.ForceGC, "true")
}
}

func setEnvVar(key, val string) {
err := os.Setenv(key, val)
if err != nil {
log.Errorf("error setting environment variable %s: %v", key, err)
}
}

Expand Down
3 changes: 3 additions & 0 deletions internal/configuration/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ type Config struct {

DiscoveryConfig discovery.Config `yaml:"discovery"`

// whether to omit the .bucket suffix for prometheus histogram metrics. Defaults to false.
OmitBucketSuffix bool `yaml:"omitBucketSuffix"`

// Internal use only
Daemon bool `yaml:"-"`
}
Expand Down
1 change: 1 addition & 0 deletions internal/configuration/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ clusterName: new-collector
enableDiscovery: true
enableEvents: true
defaultCollectionInterval: 10s
omitBucketSuffix: true
sinks:
- proxyAddress: wavefront-proxy.default.svc.cluster.local:2878
Expand Down
35 changes: 25 additions & 10 deletions plugins/sources/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"math"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -52,6 +54,8 @@ type prometheusMetricsSource struct {
client *http.Client
pps gometrics.Counter
eps gometrics.Counter

omitBucketSuffix bool
}

func NewPrometheusMetricsSource(metricsURL, prefix, source, discovered string, tags map[string]string, filters filter.Filter, httpCfg httputil.ClientConfig) (metrics.MetricsSource, error) {
Expand All @@ -65,16 +69,19 @@ func NewPrometheusMetricsSource(metricsURL, prefix, source, discovered string, t
ppsKey := reporting.EncodeKey("target.points.collected", pt)
epsKey := reporting.EncodeKey("target.collect.errors", pt)

omitBucketSuffix, _ := strconv.ParseBool(os.Getenv("omitBucketSuffix"))

return &prometheusMetricsSource{
metricsURL: metricsURL,
prefix: prefix,
source: source,
tags: tags,
buf: bytes.NewBufferString(""),
filters: filters,
client: client,
pps: gometrics.GetOrRegisterCounter(ppsKey, gometrics.DefaultRegistry),
eps: gometrics.GetOrRegisterCounter(epsKey, gometrics.DefaultRegistry),
metricsURL: metricsURL,
prefix: prefix,
source: source,
tags: tags,
buf: bytes.NewBufferString(""),
filters: filters,
client: client,
pps: gometrics.GetOrRegisterCounter(ppsKey, gometrics.DefaultRegistry),
eps: gometrics.GetOrRegisterCounter(epsKey, gometrics.DefaultRegistry),
omitBucketSuffix: omitBucketSuffix,
}, nil
}

Expand Down Expand Up @@ -256,9 +263,10 @@ func (src *prometheusMetricsSource) buildQuantiles(name string, m *dto.Metric, n
// Get Buckets from histogram metric
func (src *prometheusMetricsSource) buildHistos(name string, m *dto.Metric, now int64, tags map[string]string) []*metrics.MetricPoint {
var result []*metrics.MetricPoint
histName := src.histoName(name)
for _, b := range m.GetHistogram().Bucket {
newTags := combineTags(tags, "le", fmt.Sprintf("%v", b.GetUpperBound()))
point := src.metricPoint(name, float64(b.GetCumulativeCount()), now, src.source, newTags)
point := src.metricPoint(histName, float64(b.GetCumulativeCount()), now, src.source, newTags)
result = append(result, point)
}
point := src.metricPoint(name+".count", float64(m.GetHistogram().GetSampleCount()), now, src.source, tags)
Expand Down Expand Up @@ -317,6 +325,13 @@ func combineTags(tags map[string]string, key, val string) map[string]string {
return newTags
}

func (src *prometheusMetricsSource) histoName(name string) string {
if src.omitBucketSuffix {
return name
}
return name + ".bucket"
}

type prometheusProvider struct {
metrics.DefaultMetricsSourceProvider
name string
Expand Down

0 comments on commit 41c94c4

Please sign in to comment.