-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathrunning_input.go
269 lines (233 loc) · 6.27 KB
/
running_input.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package models
import (
"errors"
"fmt"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
logging "github.com/influxdata/telegraf/logger"
"github.com/influxdata/telegraf/selfstat"
)
var (
GlobalMetricsGathered = selfstat.Register("agent", "metrics_gathered", make(map[string]string))
GlobalGatherErrors = selfstat.Register("agent", "gather_errors", make(map[string]string))
GlobalGatherTimeouts = selfstat.Register("agent", "gather_timeouts", make(map[string]string))
)
type RunningInput struct {
Input telegraf.Input
Config *InputConfig
log telegraf.Logger
defaultTags map[string]string
startAcc telegraf.Accumulator
started bool
retries uint64
gatherStart time.Time
gatherEnd time.Time
MetricsGathered selfstat.Stat
GatherTime selfstat.Stat
GatherTimeouts selfstat.Stat
StartupErrors selfstat.Stat
}
func NewRunningInput(input telegraf.Input, config *InputConfig) *RunningInput {
tags := map[string]string{"input": config.Name}
if config.Alias != "" {
tags["alias"] = config.Alias
}
inputErrorsRegister := selfstat.Register("gather", "errors", tags)
logger := logging.New("inputs", config.Name, config.Alias)
logger.RegisterErrorCallback(func() {
inputErrorsRegister.Incr(1)
GlobalGatherErrors.Incr(1)
})
if err := logger.SetLogLevel(config.LogLevel); err != nil {
logger.Error(err)
}
SetLoggerOnPlugin(input, logger)
return &RunningInput{
Input: input,
Config: config,
MetricsGathered: selfstat.Register(
"gather",
"metrics_gathered",
tags,
),
GatherTime: selfstat.RegisterTiming(
"gather",
"gather_time_ns",
tags,
),
GatherTimeouts: selfstat.Register(
"gather",
"gather_timeouts",
tags,
),
StartupErrors: selfstat.Register(
"write",
"startup_errors",
tags,
),
log: logger,
}
}
// InputConfig is the common config for all inputs.
type InputConfig struct {
Name string
Alias string
ID string
Interval time.Duration
CollectionJitter time.Duration
CollectionOffset time.Duration
Precision time.Duration
TimeSource string
StartupErrorBehavior string
LogLevel string
NameOverride string
MeasurementPrefix string
MeasurementSuffix string
Tags map[string]string
Filter Filter
AlwaysIncludeLocalTags bool
AlwaysIncludeGlobalTags bool
}
func (r *RunningInput) metricFiltered(metric telegraf.Metric) {
metric.Drop()
}
func (r *RunningInput) LogName() string {
return logName("inputs", r.Config.Name, r.Config.Alias)
}
func (r *RunningInput) Init() error {
switch r.Config.StartupErrorBehavior {
case "", "error", "retry", "ignore":
default:
return fmt.Errorf("invalid 'startup_error_behavior' setting %q", r.Config.StartupErrorBehavior)
}
switch r.Config.TimeSource {
case "":
r.Config.TimeSource = "metric"
case "metric", "collection_start", "collection_end":
default:
return fmt.Errorf("invalid 'time_source' setting %q", r.Config.TimeSource)
}
if p, ok := r.Input.(telegraf.Initializer); ok {
return p.Init()
}
return nil
}
func (r *RunningInput) Start(acc telegraf.Accumulator) error {
plugin, ok := r.Input.(telegraf.ServiceInput)
if !ok {
return nil
}
// Try to start the plugin and exit early on success
r.startAcc = acc
err := plugin.Start(acc)
if err == nil {
r.started = true
return nil
}
r.StartupErrors.Incr(1)
// Check if the plugin reports a retry-able error, otherwise we exit.
var serr *internal.StartupError
if !errors.As(err, &serr) {
return err
}
// Handle the retry-able error depending on the configured behavior
switch r.Config.StartupErrorBehavior {
case "", "error": // fall-trough to return the actual error
case "retry":
if !serr.Retry {
return err
}
r.log.Infof("Startup failed: %v; retrying...", err)
return nil
case "ignore":
return &internal.FatalError{Err: serr}
default:
r.log.Errorf("Invalid 'startup_error_behavior' setting %q", r.Config.StartupErrorBehavior)
}
return err
}
func (r *RunningInput) Stop() {
if plugin, ok := r.Input.(telegraf.ServiceInput); ok {
plugin.Stop()
}
}
func (r *RunningInput) ID() string {
if p, ok := r.Input.(telegraf.PluginWithID); ok {
return p.ID()
}
return r.Config.ID
}
func (r *RunningInput) MakeMetric(metric telegraf.Metric) telegraf.Metric {
ok, err := r.Config.Filter.Select(metric)
if err != nil {
r.log.Errorf("filtering failed: %v", err)
} else if !ok {
r.metricFiltered(metric)
return nil
}
makeMetric(
metric,
r.Config.NameOverride,
r.Config.MeasurementPrefix,
r.Config.MeasurementSuffix,
r.Config.Tags,
r.defaultTags)
r.Config.Filter.Modify(metric)
if len(metric.FieldList()) == 0 {
r.metricFiltered(metric)
return nil
}
if r.Config.AlwaysIncludeLocalTags || r.Config.AlwaysIncludeGlobalTags {
var local, global map[string]string
if r.Config.AlwaysIncludeLocalTags {
local = r.Config.Tags
}
if r.Config.AlwaysIncludeGlobalTags {
global = r.defaultTags
}
makeMetric(metric, "", "", "", local, global)
}
switch r.Config.TimeSource {
case "collection_start":
metric.SetTime(r.gatherStart)
case "collection_end":
metric.SetTime(r.gatherEnd)
default:
}
r.MetricsGathered.Incr(1)
GlobalMetricsGathered.Incr(1)
return metric
}
func (r *RunningInput) Gather(acc telegraf.Accumulator) error {
// Try to connect if we are not yet started up
if plugin, ok := r.Input.(telegraf.ServiceInput); ok && !r.started {
r.retries++
if err := plugin.Start(r.startAcc); err != nil {
var serr *internal.StartupError
if !errors.As(err, &serr) || !serr.Retry || !serr.Partial {
r.StartupErrors.Incr(1)
return internal.ErrNotConnected
}
r.log.Debugf("Partially connected after %d attempts", r.retries)
} else {
r.started = true
r.log.Debugf("Successfully connected after %d attempts", r.retries)
}
}
r.gatherStart = time.Now()
err := r.Input.Gather(acc)
r.gatherEnd = time.Now()
r.GatherTime.Incr(r.gatherEnd.Sub(r.gatherStart).Nanoseconds())
return err
}
func (r *RunningInput) SetDefaultTags(tags map[string]string) {
r.defaultTags = tags
}
func (r *RunningInput) Log() telegraf.Logger {
return r.log
}
func (r *RunningInput) IncrGatherTimeouts() {
GlobalGatherTimeouts.Incr(1)
r.GatherTimeouts.Incr(1)
}