Skip to content

Commit 58946b5

Browse files
committed
Put Agent Config into the config package
1 parent 1f9d65e commit 58946b5

File tree

3 files changed

+69
-88
lines changed

3 files changed

+69
-88
lines changed

agent.go

+24-74
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"sync"
1010
"time"
1111

12-
"github.com/influxdb/telegraf/internal"
1312
"github.com/influxdb/telegraf/internal/config"
1413
"github.com/influxdb/telegraf/outputs"
1514
"github.com/influxdb/telegraf/plugins"
@@ -19,77 +18,27 @@ import (
1918

2019
// Agent runs telegraf and collects data based on the given config
2120
type Agent struct {
22-
23-
// Interval at which to gather information
24-
Interval internal.Duration
25-
26-
// RoundInterval rounds collection interval to 'interval'.
27-
// ie, if Interval=10s then always collect on :00, :10, :20, etc.
28-
RoundInterval bool
29-
30-
// Interval at which to flush data
31-
FlushInterval internal.Duration
32-
33-
// FlushRetries is the number of times to retry each data flush
34-
FlushRetries int
35-
36-
// FlushJitter tells
37-
FlushJitter internal.Duration
38-
39-
// TODO(cam): Remove UTC and Precision parameters, they are no longer
40-
// valid for the agent config. Leaving them here for now for backwards-
41-
// compatability
42-
43-
// Option for outputting data in UTC
44-
UTC bool `toml:"utc"`
45-
46-
// Precision to write data at
47-
// Valid values for Precision are n, u, ms, s, m, and h
48-
Precision string
49-
50-
// Option for running in debug mode
51-
Debug bool
52-
Hostname string
53-
54-
Tags map[string]string
55-
5621
Config *config.Config
5722
}
5823

5924
// NewAgent returns an Agent struct based off the given Config
6025
func NewAgent(config *config.Config) (*Agent, error) {
61-
agent := &Agent{
62-
Tags: make(map[string]string),
63-
Config: config,
64-
Interval: internal.Duration{10 * time.Second},
65-
RoundInterval: true,
66-
FlushInterval: internal.Duration{10 * time.Second},
67-
FlushRetries: 2,
68-
FlushJitter: internal.Duration{5 * time.Second},
26+
a := &Agent{
27+
Config: config,
6928
}
7029

71-
// Apply the toml table to the agent config, overriding defaults
72-
err := config.ApplyAgent(agent)
73-
if err != nil {
74-
return nil, err
75-
}
76-
77-
if agent.Hostname == "" {
30+
if a.Config.Agent.Hostname == "" {
7831
hostname, err := os.Hostname()
7932
if err != nil {
8033
return nil, err
8134
}
8235

83-
agent.Hostname = hostname
84-
}
85-
86-
if config.Tags == nil {
87-
config.Tags = map[string]string{}
36+
a.Config.Agent.Hostname = hostname
8837
}
8938

90-
config.Tags["host"] = agent.Hostname
39+
config.Tags["host"] = a.Config.Agent.Hostname
9140

92-
return agent, nil
41+
return a, nil
9342
}
9443

9544
// Connect connects to all configured outputs
@@ -104,7 +53,7 @@ func (a *Agent) Connect() error {
10453
}
10554
}
10655

107-
if a.Debug {
56+
if a.Config.Agent.Debug {
10857
log.Printf("Attempting connection to output: %s\n", o.Name)
10958
}
11059
err := o.Output.Connect()
@@ -116,7 +65,7 @@ func (a *Agent) Connect() error {
11665
return err
11766
}
11867
}
119-
if a.Debug {
68+
if a.Config.Agent.Debug {
12069
log.Printf("Successfully connected to output: %s\n", o.Name)
12170
}
12271
}
@@ -154,9 +103,9 @@ func (a *Agent) gatherParallel(pointChan chan *client.Point) error {
154103
defer wg.Done()
155104

156105
acc := NewAccumulator(plugin.Config, pointChan)
157-
acc.SetDebug(a.Debug)
106+
acc.SetDebug(a.Config.Agent.Debug)
158107
acc.SetPrefix(plugin.Name + "_")
159-
acc.SetDefaultTags(a.Tags)
108+
acc.SetDefaultTags(a.Config.Tags)
160109

161110
if err := plugin.Plugin.Gather(acc); err != nil {
162111
log.Printf("Error in plugin [%s]: %s", plugin.Name, err)
@@ -169,7 +118,7 @@ func (a *Agent) gatherParallel(pointChan chan *client.Point) error {
169118

170119
elapsed := time.Since(start)
171120
log.Printf("Gathered metrics, (%s interval), from %d plugins in %s\n",
172-
a.Interval, counter, elapsed)
121+
a.Config.Agent.Interval, counter, elapsed)
173122
return nil
174123
}
175124

@@ -187,9 +136,9 @@ func (a *Agent) gatherSeparate(
187136
start := time.Now()
188137

189138
acc := NewAccumulator(plugin.Config, pointChan)
190-
acc.SetDebug(a.Debug)
139+
acc.SetDebug(a.Config.Agent.Debug)
191140
acc.SetPrefix(plugin.Name + "_")
192-
acc.SetDefaultTags(a.Tags)
141+
acc.SetDefaultTags(a.Config.Tags)
193142

194143
if err := plugin.Plugin.Gather(acc); err != nil {
195144
log.Printf("Error in plugin [%s]: %s", plugin.Name, err)
@@ -273,7 +222,7 @@ func (a *Agent) writeOutput(
273222
return
274223
}
275224
retry := 0
276-
retries := a.FlushRetries
225+
retries := a.Config.Agent.FlushRetries
277226
start := time.Now()
278227

279228
for {
@@ -299,8 +248,8 @@ func (a *Agent) writeOutput(
299248
} else if err != nil {
300249
// Sleep for a retry
301250
log.Printf("Error in output [%s]: %s, retrying in %s",
302-
ro.Name, err.Error(), a.FlushInterval.Duration)
303-
time.Sleep(a.FlushInterval.Duration)
251+
ro.Name, err.Error(), a.Config.Agent.FlushInterval.Duration)
252+
time.Sleep(a.Config.Agent.FlushInterval.Duration)
304253
}
305254
}
306255

@@ -330,7 +279,7 @@ func (a *Agent) flusher(shutdown chan struct{}, pointChan chan *client.Point) er
330279
// the flusher will flush after metrics are collected.
331280
time.Sleep(time.Millisecond * 100)
332281

333-
ticker := time.NewTicker(a.FlushInterval.Duration)
282+
ticker := time.NewTicker(a.Config.Agent.FlushInterval.Duration)
334283
points := make([]*client.Point, 0)
335284

336285
for {
@@ -373,22 +322,23 @@ func jitterInterval(ininterval, injitter time.Duration) time.Duration {
373322
func (a *Agent) Run(shutdown chan struct{}) error {
374323
var wg sync.WaitGroup
375324

376-
a.FlushInterval.Duration = jitterInterval(a.FlushInterval.Duration,
377-
a.FlushJitter.Duration)
325+
a.Config.Agent.FlushInterval.Duration = jitterInterval(a.Config.Agent.FlushInterval.Duration,
326+
a.Config.Agent.FlushJitter.Duration)
378327

379328
log.Printf("Agent Config: Interval:%s, Debug:%#v, Hostname:%#v, "+
380329
"Flush Interval:%s\n",
381-
a.Interval, a.Debug, a.Hostname, a.FlushInterval)
330+
a.Config.Agent.Interval, a.Config.Agent.Debug,
331+
a.Config.Agent.Hostname, a.Config.Agent.FlushInterval)
382332

383333
// channel shared between all plugin threads for accumulating points
384334
pointChan := make(chan *client.Point, 1000)
385335

386336
// Round collection to nearest interval by sleeping
387-
if a.RoundInterval {
388-
i := int64(a.Interval.Duration)
337+
if a.Config.Agent.RoundInterval {
338+
i := int64(a.Config.Agent.Interval.Duration)
389339
time.Sleep(time.Duration(i - (time.Now().UnixNano() % i)))
390340
}
391-
ticker := time.NewTicker(a.Interval.Duration)
341+
ticker := time.NewTicker(a.Config.Agent.Interval.Duration)
392342

393343
wg.Add(1)
394344
go func() {

cmd/telegraf/telegraf.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func main() {
108108
}
109109

110110
if *fDebug {
111-
ag.Debug = true
111+
ag.Config.Agent.Debug = true
112112
}
113113

114114
if *fTest {

internal/config/config.go

+44-13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"time"
1212

13+
"github.com/influxdb/telegraf/internal"
1314
"github.com/influxdb/telegraf/outputs"
1415
"github.com/influxdb/telegraf/plugins"
1516

@@ -25,13 +26,22 @@ type Config struct {
2526
PluginFilters []string
2627
OutputFilters []string
2728

28-
Agent *ast.Table
29+
Agent *AgentConfig
2930
Plugins []*RunningPlugin
3031
Outputs []*RunningOutput
3132
}
3233

3334
func NewConfig() *Config {
3435
c := &Config{
36+
// Agent defaults:
37+
Agent: &AgentConfig{
38+
Interval: internal.Duration{10 * time.Second},
39+
RoundInterval: true,
40+
FlushInterval: internal.Duration{10 * time.Second},
41+
FlushRetries: 2,
42+
FlushJitter: internal.Duration{5 * time.Second},
43+
},
44+
3545
Tags: make(map[string]string),
3646
Plugins: make([]*RunningPlugin, 0),
3747
Outputs: make([]*RunningOutput, 0),
@@ -41,6 +51,34 @@ func NewConfig() *Config {
4151
return c
4252
}
4353

54+
type AgentConfig struct {
55+
// Interval at which to gather information
56+
Interval internal.Duration
57+
58+
// RoundInterval rounds collection interval to 'interval'.
59+
// ie, if Interval=10s then always collect on :00, :10, :20, etc.
60+
RoundInterval bool
61+
62+
// Interval at which to flush data
63+
FlushInterval internal.Duration
64+
65+
// FlushRetries is the number of times to retry each data flush
66+
FlushRetries int
67+
68+
// FlushJitter tells
69+
FlushJitter internal.Duration
70+
71+
// TODO(cam): Remove UTC and Precision parameters, they are no longer
72+
// valid for the agent config. Leaving them here for now for backwards-
73+
// compatability
74+
UTC bool `toml:"utc"`
75+
Precision string
76+
77+
// Option for running in debug mode
78+
Debug bool
79+
Hostname string
80+
}
81+
4482
// TagFilter is the name of a tag, and the values on which to filter
4583
type TagFilter struct {
4684
Name string
@@ -146,16 +184,6 @@ func (c *Config) OutputNames() []string {
146184
return name
147185
}
148186

149-
// ApplyAgent loads the toml config into the given Agent object, overriding
150-
// defaults (such as collection duration) with the values from the toml config.
151-
func (c *Config) ApplyAgent(a interface{}) error {
152-
if c.Agent != nil {
153-
return toml.UnmarshalTable(c.Agent, a)
154-
}
155-
156-
return nil
157-
}
158-
159187
// ListTags returns a string of tags specified in the config,
160188
// line-protocol style
161189
func (c *Config) ListTags() string {
@@ -346,7 +374,7 @@ func (c *Config) LoadDirectory(path string) error {
346374
continue
347375
}
348376
name := entry.Name()
349-
if name[len(name)-5:] != ".conf" {
377+
if len(name) < 6 || name[len(name)-5:] != ".conf" {
350378
continue
351379
}
352380
err := c.LoadConfig(filepath.Join(path, name))
@@ -377,7 +405,10 @@ func (c *Config) LoadConfig(path string) error {
377405

378406
switch name {
379407
case "agent":
380-
c.Agent = subTable
408+
if err = toml.UnmarshalTable(subTable, c.Agent); err != nil {
409+
log.Printf("Could not parse [agent] config\n")
410+
return err
411+
}
381412
case "tags":
382413
if err = toml.UnmarshalTable(subTable, c.Tags); err != nil {
383414
log.Printf("Could not parse [tags] config\n")

0 commit comments

Comments
 (0)