Skip to content

Commit

Permalink
statsd/datadog: stop using envconfig for setting default values
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Feb 1, 2019
1 parent 1cd780b commit 8af8dc3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
5 changes: 3 additions & 2 deletions cmd/collectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
jsonc "github.com/loadimpact/k6/stats/json"
"github.com/loadimpact/k6/stats/kafka"
"github.com/loadimpact/k6/stats/statsd"
"github.com/loadimpact/k6/stats/statsd/common"
"github.com/pkg/errors"
"github.com/spf13/afero"
)
Expand Down Expand Up @@ -98,13 +99,13 @@ func newCollector(collectorName, arg string, src *lib.SourceData, conf Config) (
}
return kafka.New(config)
case collectorStatsD:
config := conf.Collectors.StatsD
config := common.NewConfig().Apply(conf.Collectors.StatsD)
if err := envconfig.Process("k6_statsd", &config); err != nil {
return nil, err
}
return statsd.New(config)
case collectorDatadog:
config := conf.Collectors.Datadog
config := datadog.NewConfig().Apply(conf.Collectors.Datadog)
if err := envconfig.Process("k6_datadog", &config); err != nil {
return nil, err
}
Expand Down
33 changes: 14 additions & 19 deletions stats/datadog/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,59 +21,54 @@
package datadog

import (
"sort"
"strings"

"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/stats/statsd/common"
null "gopkg.in/guregu/null.v3"
)

type tagHandler sort.StringSlice
type tagHandler lib.TagSet

func (t tagHandler) processTags(tags map[string]string) []string {
var res []string

for key, value := range tags {
if value != "" && t.contains(key) {
if value != "" && t[key] {
res = append(res, key+":"+value)
}
}
return res
}

func (t tagHandler) contains(key string) bool {
var n = ((sort.StringSlice)(t)).Search(key)
return n != len(t) && t[n] == key
}

// Config defines the datadog configuration
type Config struct {
common.Config

TagWhitelist null.String `json:"tag_whitelist,omitempty" envconfig:"tag_whitelist" default:"status, method"`
TagWhitelist lib.TagSet `json:"tag_whitelist,omitempty" envconfig:"TAG_WHITELIST"`
}

// Apply saves config non-zero config values from the passed config in the receiver.
func (c Config) Apply(cfg Config) Config {
c.Config.Apply(cfg.Config)

if cfg.TagWhitelist.Valid {
if cfg.TagWhitelist != nil {
c.TagWhitelist = cfg.TagWhitelist
}

return c
}

// NewConfig creates a new Config instance with default values for some fields.
func NewConfig() Config {
return Config{
Config: common.NewConfig(),
TagWhitelist: lib.GetTagSet("status, method", "group"),
}
}

// New creates a new statsd connector client
func New(conf Config) (*common.Collector, error) {
var tagsWhitelist = sort.StringSlice(strings.Split(conf.TagWhitelist.String, ","))
for index := range tagsWhitelist {
tagsWhitelist[index] = strings.TrimSpace(tagsWhitelist[index])
}
tagsWhitelist.Sort()
return &common.Collector{
Config: conf.Config,
Type: "datadog",
ProcessTags: tagHandler(tagsWhitelist).processTags,
ProcessTags: tagHandler(conf.TagWhitelist).processTags,
}, nil
}
20 changes: 16 additions & 4 deletions stats/statsd/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,28 @@
package common

import (
"time"

"github.com/loadimpact/k6/lib/types"
null "gopkg.in/guregu/null.v3"
)

// Config defines the statsd configuration
type Config struct {
Addr null.String `json:"addr,omitempty" default:"localhost:8125"`
BufferSize null.Int `json:"buffer_size,omitempty" default:"20"`
Namespace null.String `json:"namespace,omitempty" default:"k6."`
PushInterval types.NullDuration `json:"push_interval,omitempty" default:"1s" envconfig:"PUSH_INTERVAL"`
Addr null.String `json:"addr,omitempty" envconfig:"ADDR"`
BufferSize null.Int `json:"buffer_size,omitempty" envconfig:"BUFFER_SIZE"`
Namespace null.String `json:"namespace,omitempty" envconfig:"NAMESPACE"`
PushInterval types.NullDuration `json:"push_interval,omitempty" envconfig:"PUSH_INTERVAL"`
}

// NewConfig creates a new Config instance with default values for some fields.
func NewConfig() Config {
return Config{
Addr: null.StringFrom("localhost:8125"),
BufferSize: null.IntFrom(20),
Namespace: null.StringFrom("k6."),
PushInterval: types.NullDurationFrom(1 * time.Second),
}
}

// Apply saves config non-zero config values from the passed config in the receiver.
Expand Down

0 comments on commit 8af8dc3

Please sign in to comment.