Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/prometheus/exporter/statsd/config.go
4095 views
1
package statsd
2
3
import (
4
"fmt"
5
"os"
6
"time"
7
8
"github.com/grafana/agent/pkg/integrations/statsd_exporter"
9
"github.com/prometheus/statsd_exporter/pkg/mapper"
10
)
11
12
type Arguments struct {
13
ListenUDP string `river:"listen_udp,attr,optional"`
14
ListenTCP string `river:"listen_tcp,attr,optional"`
15
ListenUnixgram string `river:"listen_unixgram,attr,optional"`
16
UnixSocketMode string `river:"unix_socket_mode,attr,optional"`
17
MappingConfig string `river:"mapping_config_path,attr,optional"`
18
19
ReadBuffer int `river:"read_buffer,attr,optional"`
20
CacheSize int `river:"cache_size,attr,optional"`
21
CacheType string `river:"cache_type,attr,optional"`
22
EventQueueSize int `river:"event_queue_size,attr,optional"`
23
EventFlushThreshold int `river:"event_flush_threshold,attr,optional"`
24
EventFlushInterval time.Duration `river:"event_flush_interval,attr,optional"`
25
26
ParseDogStatsd bool `river:"parse_dogstatsd_tags,attr,optional"`
27
ParseInfluxDB bool `river:"parse_influxdb_tags,attr,optional"`
28
ParseLibrato bool `river:"parse_librato_tags,attr,optional"`
29
ParseSignalFX bool `river:"parse_signalfx_tags,attr,optional"`
30
}
31
32
// DefaultConfig holds non-zero default options for the Config when it is
33
// unmarshaled from YAML.
34
//
35
// Some defaults are populated from init functions in the github.com/grafana/agent/pkg/integrations/statsd_exporter package.
36
var DefaultConfig = Arguments{
37
38
ListenUDP: statsd_exporter.DefaultConfig.ListenUDP,
39
ListenTCP: statsd_exporter.DefaultConfig.ListenTCP,
40
UnixSocketMode: statsd_exporter.DefaultConfig.UnixSocketMode,
41
42
CacheSize: statsd_exporter.DefaultConfig.CacheSize,
43
CacheType: statsd_exporter.DefaultConfig.CacheType,
44
EventQueueSize: statsd_exporter.DefaultConfig.EventQueueSize,
45
EventFlushThreshold: statsd_exporter.DefaultConfig.EventFlushThreshold,
46
EventFlushInterval: statsd_exporter.DefaultConfig.EventFlushInterval,
47
48
ParseDogStatsd: statsd_exporter.DefaultConfig.ParseDogStatsd,
49
ParseInfluxDB: statsd_exporter.DefaultConfig.ParseInfluxDB,
50
ParseLibrato: statsd_exporter.DefaultConfig.ParseLibrato,
51
ParseSignalFX: statsd_exporter.DefaultConfig.ParseSignalFX,
52
}
53
54
// Convert gives a config suitable for use with github.com/grafana/agent/pkg/integrations/statsd_exporter.
55
func (c *Arguments) Convert() (*statsd_exporter.Config, error) {
56
mappingConfig, err := readMappingFromYAML(c.MappingConfig)
57
if err != nil {
58
return nil, fmt.Errorf("failed to convert statsd config: %w", err)
59
}
60
61
return &statsd_exporter.Config{
62
ListenUDP: c.ListenUDP,
63
ListenTCP: c.ListenTCP,
64
ListenUnixgram: c.ListenUnixgram,
65
UnixSocketMode: c.UnixSocketMode,
66
ReadBuffer: c.ReadBuffer,
67
CacheSize: c.CacheSize,
68
CacheType: c.CacheType,
69
EventQueueSize: c.EventQueueSize,
70
EventFlushThreshold: c.EventFlushThreshold,
71
EventFlushInterval: c.EventFlushInterval,
72
ParseDogStatsd: c.ParseDogStatsd,
73
ParseInfluxDB: c.ParseInfluxDB,
74
ParseLibrato: c.ParseLibrato,
75
ParseSignalFX: c.ParseSignalFX,
76
MappingConfig: mappingConfig,
77
}, nil
78
}
79
80
// UnmarshalRiver implements River unmarshalling for Config.
81
func (c *Arguments) UnmarshalRiver(f func(interface{}) error) error {
82
*c = DefaultConfig
83
84
type args Arguments
85
return f((*args)(c))
86
}
87
88
// function to read a yaml file from a path and convert it to a mapper.MappingConfig
89
// this is used to convert the MappingConfig field in to a mapper.MappingConfig
90
// which is used by the statsd_exporter
91
func readMappingFromYAML(path string) (*mapper.MetricMapper, error) {
92
yfile, err := os.Open(path)
93
if err != nil {
94
return nil, fmt.Errorf("failed to read mapping config file: %w", err)
95
}
96
97
yBytes := make([]byte, 0)
98
count, err := yfile.Read(yBytes)
99
if err != nil {
100
return nil, fmt.Errorf("failed to read mapping config file: %w", err)
101
}
102
103
statsdMapper := mapper.MetricMapper{}
104
105
err = statsdMapper.InitFromYAMLString(string(yBytes[:count]))
106
if err != nil {
107
return nil, fmt.Errorf("failed to load mapping config: %w", err)
108
}
109
110
return &statsdMapper, nil
111
}
112
113