Skip to content

Commit

Permalink
Allow suppressing metrics to be stored in InfluxDB
Browse files Browse the repository at this point in the history
  • Loading branch information
TamiTakamiya committed Apr 23, 2020
1 parent 98adc1c commit 566b0e6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
33 changes: 19 additions & 14 deletions stats/influxdb/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@ const (
var _ lib.Collector = &Collector{}

type Collector struct {
Client client.Client
Config Config
BatchConf client.BatchPointsConfig

buffer []stats.Sample
bufferLock sync.Mutex
wg sync.WaitGroup
semaphoreCh chan struct{}
fieldKinds map[string]fieldKind
Client client.Client
Config Config
BatchConf client.BatchPointsConfig

buffer []stats.Sample
bufferLock sync.Mutex
wg sync.WaitGroup
semaphoreCh chan struct{}
fieldKinds map[string]fieldKind
metricsSuppressed map[string]bool
}

func New(conf Config) (*Collector, error) {
Expand All @@ -67,11 +68,12 @@ func New(conf Config) (*Collector, error) {
return nil, errors.New("influxdb's ConcurrentWrites must be a positive number")
}
return &Collector{
Client: cl,
Config: conf,
BatchConf: batchConf,
semaphoreCh: make(chan struct{}, conf.ConcurrentWrites.Int64),
fieldKinds: MakeFieldKinds(conf),
Client: cl,
Config: conf,
BatchConf: batchConf,
semaphoreCh: make(chan struct{}, conf.ConcurrentWrites.Int64),
fieldKinds: MakeFieldKinds(conf),
metricsSuppressed: MakeMetricsSuppressed(conf),
}, nil
}

Expand Down Expand Up @@ -184,6 +186,9 @@ func (c *Collector) batchFromSamples(samples []stats.Sample) (client.BatchPoints
}
cache := map[*stats.SampleTags]cacheItem{}
for _, sample := range samples {
if _, found := c.metricsSuppressed[sample.Metric.Name]; found {
continue
}
var tags map[string]string
var values = make(map[string]interface{})
if cached, ok := cache[sample.Tags]; ok {
Expand Down
17 changes: 9 additions & 8 deletions stats/influxdb/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ type Config struct {
ConcurrentWrites null.Int `json:"concurrentWrites,omitempty" envconfig:"K6_INFLUXDB_CONCURRENT_WRITES"`

// Samples.
DB null.String `json:"db" envconfig:"K6_INFLUXDB_DB"`
Precision null.String `json:"precision,omitempty" envconfig:"K6_INFLUXDB_PRECISION"`
Retention null.String `json:"retention,omitempty" envconfig:"K6_INFLUXDB_RETENTION"`
Consistency null.String `json:"consistency,omitempty" envconfig:"K6_INFLUXDB_CONSISTENCY"`
TagsAsFields []string `json:"tagsAsFields,omitempty" envconfig:"K6_INFLUXDB_TAGS_AS_FIELDS"`
BoolFields []string `json:"boolFields,omitempty" envconfig:"K6_INFLUXDB_BOOL_FIELDS"`
FloatFields []string `json:"floatFields,omitempty" envconfig:"K6_INFLUXDB_FLOAT_FIELDS"`
IntFields []string `json:"intFields,omitempty" envconfig:"K6_INFLUXDB_INT_FIELDS"`
DB null.String `json:"db" envconfig:"K6_INFLUXDB_DB"`
Precision null.String `json:"precision,omitempty" envconfig:"K6_INFLUXDB_PRECISION"`
Retention null.String `json:"retention,omitempty" envconfig:"K6_INFLUXDB_RETENTION"`
Consistency null.String `json:"consistency,omitempty" envconfig:"K6_INFLUXDB_CONSISTENCY"`
TagsAsFields []string `json:"tagsAsFields,omitempty" envconfig:"K6_INFLUXDB_TAGS_AS_FIELDS"`
BoolFields []string `json:"boolFields,omitempty" envconfig:"K6_INFLUXDB_BOOL_FIELDS"`
FloatFields []string `json:"floatFields,omitempty" envconfig:"K6_INFLUXDB_FLOAT_FIELDS"`
IntFields []string `json:"intFields,omitempty" envconfig:"K6_INFLUXDB_INT_FIELDS"`
MetricsSuppressed []string `json:"intFields,omitempty" envconfig:"K6_INFLUXDB_METRICS_SUPPRESSED"`
}

func NewConfig() *Config {
Expand Down
8 changes: 8 additions & 0 deletions stats/influxdb/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,11 @@ func MakeFieldKinds(conf Config) map[string]fieldKind {
}
return fieldKinds
}

func MakeMetricsSuppressed(conf Config) map[string]bool {
metricsSuppressed := make(map[string]bool)
for _, metric:= range conf.MetricsSuppressed {
metricsSuppressed[metric] = true
}
return metricsSuppressed
}

0 comments on commit 566b0e6

Please sign in to comment.