Path: blob/main/component/prometheus/exporter/statsd/config.go
4095 views
package statsd12import (3"fmt"4"os"5"time"67"github.com/grafana/agent/pkg/integrations/statsd_exporter"8"github.com/prometheus/statsd_exporter/pkg/mapper"9)1011type Arguments struct {12ListenUDP string `river:"listen_udp,attr,optional"`13ListenTCP string `river:"listen_tcp,attr,optional"`14ListenUnixgram string `river:"listen_unixgram,attr,optional"`15UnixSocketMode string `river:"unix_socket_mode,attr,optional"`16MappingConfig string `river:"mapping_config_path,attr,optional"`1718ReadBuffer int `river:"read_buffer,attr,optional"`19CacheSize int `river:"cache_size,attr,optional"`20CacheType string `river:"cache_type,attr,optional"`21EventQueueSize int `river:"event_queue_size,attr,optional"`22EventFlushThreshold int `river:"event_flush_threshold,attr,optional"`23EventFlushInterval time.Duration `river:"event_flush_interval,attr,optional"`2425ParseDogStatsd bool `river:"parse_dogstatsd_tags,attr,optional"`26ParseInfluxDB bool `river:"parse_influxdb_tags,attr,optional"`27ParseLibrato bool `river:"parse_librato_tags,attr,optional"`28ParseSignalFX bool `river:"parse_signalfx_tags,attr,optional"`29}3031// DefaultConfig holds non-zero default options for the Config when it is32// unmarshaled from YAML.33//34// Some defaults are populated from init functions in the github.com/grafana/agent/pkg/integrations/statsd_exporter package.35var DefaultConfig = Arguments{3637ListenUDP: statsd_exporter.DefaultConfig.ListenUDP,38ListenTCP: statsd_exporter.DefaultConfig.ListenTCP,39UnixSocketMode: statsd_exporter.DefaultConfig.UnixSocketMode,4041CacheSize: statsd_exporter.DefaultConfig.CacheSize,42CacheType: statsd_exporter.DefaultConfig.CacheType,43EventQueueSize: statsd_exporter.DefaultConfig.EventQueueSize,44EventFlushThreshold: statsd_exporter.DefaultConfig.EventFlushThreshold,45EventFlushInterval: statsd_exporter.DefaultConfig.EventFlushInterval,4647ParseDogStatsd: statsd_exporter.DefaultConfig.ParseDogStatsd,48ParseInfluxDB: statsd_exporter.DefaultConfig.ParseInfluxDB,49ParseLibrato: statsd_exporter.DefaultConfig.ParseLibrato,50ParseSignalFX: statsd_exporter.DefaultConfig.ParseSignalFX,51}5253// Convert gives a config suitable for use with github.com/grafana/agent/pkg/integrations/statsd_exporter.54func (c *Arguments) Convert() (*statsd_exporter.Config, error) {55mappingConfig, err := readMappingFromYAML(c.MappingConfig)56if err != nil {57return nil, fmt.Errorf("failed to convert statsd config: %w", err)58}5960return &statsd_exporter.Config{61ListenUDP: c.ListenUDP,62ListenTCP: c.ListenTCP,63ListenUnixgram: c.ListenUnixgram,64UnixSocketMode: c.UnixSocketMode,65ReadBuffer: c.ReadBuffer,66CacheSize: c.CacheSize,67CacheType: c.CacheType,68EventQueueSize: c.EventQueueSize,69EventFlushThreshold: c.EventFlushThreshold,70EventFlushInterval: c.EventFlushInterval,71ParseDogStatsd: c.ParseDogStatsd,72ParseInfluxDB: c.ParseInfluxDB,73ParseLibrato: c.ParseLibrato,74ParseSignalFX: c.ParseSignalFX,75MappingConfig: mappingConfig,76}, nil77}7879// UnmarshalRiver implements River unmarshalling for Config.80func (c *Arguments) UnmarshalRiver(f func(interface{}) error) error {81*c = DefaultConfig8283type args Arguments84return f((*args)(c))85}8687// function to read a yaml file from a path and convert it to a mapper.MappingConfig88// this is used to convert the MappingConfig field in to a mapper.MappingConfig89// which is used by the statsd_exporter90func readMappingFromYAML(path string) (*mapper.MetricMapper, error) {91yfile, err := os.Open(path)92if err != nil {93return nil, fmt.Errorf("failed to read mapping config file: %w", err)94}9596yBytes := make([]byte, 0)97count, err := yfile.Read(yBytes)98if err != nil {99return nil, fmt.Errorf("failed to read mapping config file: %w", err)100}101102statsdMapper := mapper.MetricMapper{}103104err = statsdMapper.InitFromYAMLString(string(yBytes[:count]))105if err != nil {106return nil, fmt.Errorf("failed to load mapping config: %w", err)107}108109return &statsdMapper, nil110}111112113