chore(go.d): pass context to init/check/collect/cleanup (#19180)
pass ctx to init/check/collect/cleanup
Ilya Mashchenko committed
Dec 10, 2024 at 23:04 UTC
b723a59d2aa176c5b8a20ac7a335f97d74e05f75
261 files changed
+2232
-1973
src/go/plugin/go.d/agent/agent_test.go
+4
-4
@@ -68,13 +68,13 @@ func prepareRegistry(mux *sync.Mutex, stats map[string]int, names ...string) mod
68
69
func prepareMockModule(name string, mux *sync.Mutex, stats map[string]int) module.Module {
70
return &module.MockModule{
71
- InitFunc: func() error {
71
+ InitFunc: func(context.Context) error {
72
mux.Lock()
73
defer mux.Unlock()
74
stats[name+"_init"]++
75
return nil
76
},
77
- CheckFunc: func() error {
77
+ CheckFunc: func(context.Context) error {
78
mux.Lock()
79
defer mux.Unlock()
80
stats[name+"_check"]++
@@ -88,13 +88,13 @@ func prepareMockModule(name string, mux *sync.Mutex, stats map[string]int) modul
88
&module.Chart{ID: "id", Title: "title", Units: "units", Dims: module.Dims{{ID: "id1"}}},
89
}
90
},
91
- CollectFunc: func() map[string]int64 {
91
+ CollectFunc: func(context.Context) map[string]int64 {
92
mux.Lock()
93
defer mux.Unlock()
94
stats[name+"_collect"]++
95
return map[string]int64{"id1": 1}
96
},
97
- CleanupFunc: func() {
97
+ CleanupFunc: func(context.Context) {
98
mux.Lock()
99
defer mux.Unlock()
100
stats[name+"_cleanup"]++
src/go/plugin/go.d/agent/jobmgr/dyncfg.go
+3
-3
@@ -265,13 +265,13 @@ func (m *Manager) dyncfgConfigTest(fn functions.Function) {
265
slog.String("job", cfg.Name()),
266
)
267
268
- defer job.Cleanup()
268
+ defer job.Cleanup(context.Background())
269
270
- if err := job.Init(); err != nil {
270
+ if err := job.Init(context.Background()); err != nil {
271
m.dyncfgRespf(fn, 422, "Job initialization failed: %v", err)
272
return
273
}
274
- if err := job.Check(); err != nil {
274
+ if err := job.Check(context.Background()); err != nil {
275
m.dyncfgRespf(fn, 422, "Job check failed: %v", err)
276
return
277
}
src/go/plugin/go.d/agent/jobmgr/sim_test.go
+2
-2
@@ -134,7 +134,7 @@ func prepareMockRegistry() module.Registry {
134
ChartsFunc: func() *module.Charts {
135
return &module.Charts{&module.Chart{ID: "id", Title: "title", Units: "units", Dims: module.Dims{{ID: "id1"}}}}
136
},
137
- CollectFunc: func() map[string]int64 { return map[string]int64{"id1": 1} },
137
+ CollectFunc: func(context.Context) map[string]int64 { return map[string]int64{"id1": 1} },
138
}
139
},
140
Config: func() any {
@@ -144,7 +144,7 @@ func prepareMockRegistry() module.Registry {
144
reg.Register("fail", module.Creator{
145
Create: func() module.Module {
146
return &module.MockModule{
147
- InitFunc: func() error { return errors.New("mock failed init") },
147
+ InitFunc: func(context.Context) error { return errors.New("mock failed init") },
148
}
149
},
150
})
src/go/plugin/go.d/agent/module/job.go
+6
-5
@@ -4,6 +4,7 @@ package module
4
5
import (
6
"bytes"
7
+ "context"
8
"errors"
9
"fmt"
10
"io"
@@ -224,7 +225,7 @@ func (j *Job) AutoDetection() (err error) {
225
}
226
}
227
if err != nil {
227
- j.module.Cleanup()
228
+ j.module.Cleanup(context.TODO())
229
}
230
}()
231
@@ -282,7 +283,7 @@ LOOP:
283
}
284
}
285
}
285
- j.module.Cleanup()
286
+ j.module.Cleanup(context.TODO())
287
j.Cleanup()
288
j.stop <- struct{}{}
289
}
@@ -342,7 +343,7 @@ func (j *Job) init() error {
343
return nil
344
}
345
345
- if err := j.module.Init(); err != nil {
346
+ if err := j.module.Init(context.TODO()); err != nil {
347
return err
348
}
349
@@ -352,7 +353,7 @@ func (j *Job) init() error {
353
}
354
355
func (j *Job) check() error {
355
- if err := j.module.Check(); err != nil {
356
+ if err := j.module.Check(context.TODO()); err != nil {
357
if j.AutoDetectTries != infTries {
358
j.AutoDetectTries--
359
}
@@ -405,7 +406,7 @@ func (j *Job) collect() (result map[string]int64) {
406
}
407
}
408
}()
408
- return j.module.Collect()
409
+ return j.module.Collect(context.TODO())
410
}
411
412
func (j *Job) processMetrics(metrics map[string]int64, startTime time.Time, sinceLastRun int) bool {
src/go/plugin/go.d/agent/module/job_test.go
+17
-16
@@ -3,6 +3,7 @@
3
package module
4
5
import (
6
+ "context"
7
"errors"
8
"fmt"
9
"io"
@@ -73,10 +74,10 @@ func TestJob_AutoDetectionEvery(t *testing.T) {
74
func TestJob_RetryAutoDetection(t *testing.T) {
75
job := newTestJob()
76
m := &MockModule{
76
- InitFunc: func() error {
77
+ InitFunc: func(context.Context) error {
78
return nil
79
},
79
- CheckFunc: func() error { return errors.New("check error") },
80
+ CheckFunc: func(context.Context) error { return errors.New("check error") },
81
ChartsFunc: func() *Charts {
82
return &Charts{}
83
},
@@ -104,11 +105,11 @@ func TestJob_AutoDetection(t *testing.T) {
105
job := newTestJob()
106
var v int
107
m := &MockModule{
107
- InitFunc: func() error {
108
+ InitFunc: func(context.Context) error {
109
v++
110
return nil
111
},
111
- CheckFunc: func() error {
112
+ CheckFunc: func(context.Context) error {
113
v++
114
return nil
115
},
@@ -126,7 +127,7 @@ func TestJob_AutoDetection(t *testing.T) {
127
func TestJob_AutoDetection_FailInit(t *testing.T) {
128
job := newTestJob()
129
m := &MockModule{
129
- InitFunc: func() error {
130
+ InitFunc: func(context.Context) error {
131
return errors.New("init error")
132
},
133
}
@@ -139,10 +140,10 @@ func TestJob_AutoDetection_FailInit(t *testing.T) {
140
func TestJob_AutoDetection_FailCheck(t *testing.T) {
141
job := newTestJob()
142
m := &MockModule{
142
- InitFunc: func() error {
143
+ InitFunc: func(context.Context) error {
144
return nil
145
},
145
- CheckFunc: func() error {
146
+ CheckFunc: func(context.Context) error {
147
return errors.New("check error")
148
},
149
}
@@ -155,10 +156,10 @@ func TestJob_AutoDetection_FailCheck(t *testing.T) {
156
func TestJob_AutoDetection_FailPostCheck(t *testing.T) {
157
job := newTestJob()
158
m := &MockModule{
158
- InitFunc: func() error {
159
+ InitFunc: func(context.Context) error {
160
return nil
161
},
161
- CheckFunc: func() error {
162
+ CheckFunc: func(context.Context) error {
163
return nil
164
},
165
ChartsFunc: func() *Charts {
@@ -174,7 +175,7 @@ func TestJob_AutoDetection_FailPostCheck(t *testing.T) {
175
func TestJob_AutoDetection_PanicInit(t *testing.T) {
176
job := newTestJob()
177
m := &MockModule{
177
- InitFunc: func() error {
178
+ InitFunc: func(context.Context) error {
179
panic("panic in Init")
180
},
181
}
@@ -187,10 +188,10 @@ func TestJob_AutoDetection_PanicInit(t *testing.T) {
188
func TestJob_AutoDetection_PanicCheck(t *testing.T) {
189
job := newTestJob()
190
m := &MockModule{
190
- InitFunc: func() error {
191
+ InitFunc: func(context.Context) error {
192
return nil
193
},
193
- CheckFunc: func() error {
194
+ CheckFunc: func(context.Context) error {
195
panic("panic in Check")
196
},
197
}
@@ -203,10 +204,10 @@ func TestJob_AutoDetection_PanicCheck(t *testing.T) {
204
func TestJob_AutoDetection_PanicPostCheck(t *testing.T) {
205
job := newTestJob()
206
m := &MockModule{
206
- InitFunc: func() error {
207
+ InitFunc: func(context.Context) error {
208
return nil
209
},
209
- CheckFunc: func() error {
210
+ CheckFunc: func(context.Context) error {
211
return nil
212
},
213
ChartsFunc: func() *Charts {
@@ -234,7 +235,7 @@ func TestJob_Start(t *testing.T) {
235
},
236
}
237
},
237
- CollectFunc: func() map[string]int64 {
238
+ CollectFunc: func(context.Context) map[string]int64 {
239
return map[string]int64{
240
"id1": 1,
241
"id2": 2,
@@ -261,7 +262,7 @@ func TestJob_Start(t *testing.T) {
262
263
func TestJob_MainLoop_Panic(t *testing.T) {
264
m := &MockModule{
264
- CollectFunc: func() map[string]int64 {
265
+ CollectFunc: func(context.Context) map[string]int64 {
266
panic("panic in Collect")
267
},
268
}
src/go/plugin/go.d/agent/module/mock.go
+16
-13
@@ -2,7 +2,10 @@
2
3
package module
4
5
-import "errors"
5
+import (
6
+ "context"
7
+ "errors"
8
+)
9
10
const MockConfigSchema = `
11
{
@@ -38,31 +41,31 @@ type MockModule struct {
41
42
FailOnInit bool
43
41
- InitFunc func() error
42
- CheckFunc func() error
44
+ InitFunc func(context.Context) error
45
+ CheckFunc func(context.Context) error
46
ChartsFunc func() *Charts
44
- CollectFunc func() map[string]int64
45
- CleanupFunc func()
47
+ CollectFunc func(context.Context) map[string]int64
48
+ CleanupFunc func(context.Context)
49
CleanupDone bool
50
}
51
52
// Init invokes InitFunc.
50
-func (m *MockModule) Init() error {
53
+func (m *MockModule) Init(ctx context.Context) error {
54
if m.FailOnInit {
55
return errors.New("mock init error")
56
}
57
if m.InitFunc == nil {
58
return nil
59
}
57
- return m.InitFunc()
60
+ return m.InitFunc(ctx)
61
}
62
63
// Check invokes CheckFunc.
61
-func (m *MockModule) Check() error {
64
+func (m *MockModule) Check(ctx context.Context) error {
65
if m.CheckFunc == nil {
66
return nil
67
}
65
- return m.CheckFunc()
68
+ return m.CheckFunc(ctx)
69
}
70
71
// Charts invokes ChartsFunc.
@@ -74,17 +77,17 @@ func (m *MockModule) Charts() *Charts {
77
}
78
79
// Collect invokes CollectDunc.
77
-func (m *MockModule) Collect() map[string]int64 {
80
+func (m *MockModule) Collect(ctx context.Context) map[string]int64 {
81
if m.CollectFunc == nil {
82
return nil
83
}
81
- return m.CollectFunc()
84
+ return m.CollectFunc(ctx)
85
}
86
87
// Cleanup sets CleanupDone to true.
85
-func (m *MockModule) Cleanup() {
88
+func (m *MockModule) Cleanup(ctx context.Context) {
89
if m.CleanupFunc != nil {
87
- m.CleanupFunc()
90
+ m.CleanupFunc(ctx)
91
}
92
m.CleanupDone = true
93
}
src/go/plugin/go.d/agent/module/mock_test.go
+16
-10
@@ -3,6 +3,7 @@
3
package module
4
5
import (
6
+ "context"
7
"testing"
8
9
"github.com/stretchr/testify/assert"
@@ -11,18 +12,20 @@ import (
12
13
func TestMockModule_Init(t *testing.T) {
14
m := &MockModule{}
15
+ ctx := context.Background()
16
15
- assert.NoError(t, m.Init())
16
- m.InitFunc = func() error { return nil }
17
- assert.NoError(t, m.Init())
17
+ assert.NoError(t, m.Init(ctx))
18
+ m.InitFunc = func(context.Context) error { return nil }
19
+ assert.NoError(t, m.Init(ctx))
20
}
21
22
func TestMockModule_Check(t *testing.T) {
23
m := &MockModule{}
24
+ ctx := context.Background()
25
23
- assert.NoError(t, m.Check())
24
- m.CheckFunc = func() error { return nil }
25
- assert.NoError(t, m.Check())
26
+ assert.NoError(t, m.Check(ctx))
27
+ m.CheckFunc = func(context.Context) error { return nil }
28
+ assert.NoError(t, m.Check(ctx))
29
}
30
31
func TestMockModule_Charts(t *testing.T) {
@@ -39,16 +42,19 @@ func TestMockModule_Collect(t *testing.T) {
42
d := map[string]int64{
43
"1": 1,
44
}
45
+ ctx := context.Background()
46
43
- assert.Nil(t, m.Collect())
44
- m.CollectFunc = func() map[string]int64 { return d }
45
- assert.Equal(t, d, m.Collect())
47
+ assert.Nil(t, m.Collect(ctx))
48
+ m.CollectFunc = func(ctx context.Context) map[string]int64 { return d }
49
+ assert.Equal(t, d, m.Collect(ctx))
50
}
51
52
func TestMockModule_Cleanup(t *testing.T) {
53
m := &MockModule{}
54
+ ctx := context.Background()
55
+
56
require.False(t, m.CleanupDone)
57
52
- m.Cleanup()
58
+ m.Cleanup(ctx)
59
assert.True(t, m.CleanupDone)
60
}
src/go/plugin/go.d/agent/module/module.go
+5
-4
@@ -3,6 +3,7 @@
3
package module
4
5
import (
6
+ "context"
7
"encoding/json"
8
"testing"
9
@@ -18,20 +19,20 @@ import (
19
type Module interface {
20
// Init does initialization.
21
// If it returns error, the job will be disabled.
21
- Init() error
22
+ Init(context.Context) error
23
24
// Check is called after Init.
25
// If it returns error, the job will be disabled.
25
- Check() error
26
+ Check(context.Context) error
27
28
// Charts returns the chart definition.
29
Charts() *Charts
30
31
// Collect collects metrics.
31
- Collect() map[string]int64
32
+ Collect(context.Context) map[string]int64
33
34
// Cleanup Cleanup
34
- Cleanup()
35
+ Cleanup(context.Context)
36
37
GetBase() *Base
38
src/go/plugin/go.d/collector/activemq/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package activemq
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -74,7 +75,7 @@ func (c *Collector) Configuration() any {
75
return c.Config
76
}
77
77
-func (c *Collector) Init() error {
78
+func (c *Collector) Init(context.Context) error {
79
if err := c.validateConfig(); err != nil {
80
return fmt.Errorf("config validation: %v", err)
81
}
@@ -101,7 +102,7 @@ func (c *Collector) Init() error {
102
return nil
103
}
104
104
-func (c *Collector) Check() error {
105
+func (c *Collector) Check(context.Context) error {
106
mx, err := c.collect()
107
if err != nil {
108
return err
@@ -117,13 +118,13 @@ func (c *Collector) Charts() *Charts {
118
return c.charts
119
}
120
120
-func (c *Collector) Cleanup() {
121
+func (c *Collector) Cleanup(context.Context) {
122
if c.apiClient != nil && c.apiClient.httpClient != nil {
123
c.apiClient.httpClient.CloseIdleConnections()
124
}
125
}
126
126
-func (c *Collector) Collect() map[string]int64 {
127
+func (c *Collector) Collect(context.Context) map[string]int64 {
128
mx, err := c.collect()
129
130
if err != nil {
src/go/plugin/go.d/collector/activemq/collector_test.go
+13
-12
@@ -3,6 +3,7 @@
3
package activemq
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -156,11 +157,11 @@ func TestCollector_Init(t *testing.T) {
157
158
// NG case
159
collr.Webadmin = ""
159
- assert.Error(t, collr.Init())
160
+ assert.Error(t, collr.Init(context.Background()))
161
162
// OK case
163
collr.Webadmin = "webadmin"
163
- assert.NoError(t, collr.Init())
164
+ assert.NoError(t, collr.Init(context.Background()))
165
assert.NotNil(t, collr.apiClient)
166
}
167
@@ -181,8 +182,8 @@ func TestCollector_Check(t *testing.T) {
182
collr.HTTPConfig.RequestConfig = web.RequestConfig{URL: ts.URL}
183
collr.Webadmin = "webadmin"
184
184
- require.NoError(t, collr.Init())
185
- require.NoError(t, collr.Check())
185
+ require.NoError(t, collr.Init(context.Background()))
186
+ require.NoError(t, collr.Check(context.Background()))
187
}
188
189
func TestCollector_Charts(t *testing.T) {
@@ -190,7 +191,7 @@ func TestCollector_Charts(t *testing.T) {
191
}
192
193
func TestCollector_Cleanup(t *testing.T) {
193
- New().Cleanup()
194
+ New().Cleanup(context.Background())
195
}
196
197
func TestCollector_Collect(t *testing.T) {
@@ -214,8 +215,8 @@ func TestCollector_Collect(t *testing.T) {
215
collr.HTTPConfig.RequestConfig = web.RequestConfig{URL: ts.URL}
216
collr.Webadmin = "webadmin"
217
217
- require.NoError(t, collr.Init())
218
- require.NoError(t, collr.Check())
218
+ require.NoError(t, collr.Init(context.Background()))
219
+ require.NoError(t, collr.Check(context.Background()))
220
221
cases := []struct {
222
expected map[string]int64
@@ -303,7 +304,7 @@ func TestCollector_Collect(t *testing.T) {
304
}
305
306
for _, c := range cases {
306
- require.Equal(t, c.expected, collr.Collect())
307
+ require.Equal(t, c.expected, collr.Collect(context.Background()))
308
assert.Len(t, collr.activeQueues, c.numQueues)
309
assert.Len(t, collr.activeTopics, c.numTopics)
310
assert.Len(t, *collr.charts, c.numCharts)
@@ -321,8 +322,8 @@ func TestCollector_404(t *testing.T) {
322
collr.Webadmin = "webadmin"
323
collr.HTTPConfig.RequestConfig = web.RequestConfig{URL: ts.URL}
324
324
- require.NoError(t, collr.Init())
325
- assert.Error(t, collr.Check())
325
+ require.NoError(t, collr.Init(context.Background()))
326
+ assert.Error(t, collr.Check(context.Background()))
327
}
328
329
func TestCollector_InvalidData(t *testing.T) {
@@ -335,6 +336,6 @@ func TestCollector_InvalidData(t *testing.T) {
336
collr.Webadmin = "webadmin"
337
collr.HTTPConfig.RequestConfig = web.RequestConfig{URL: ts.URL}
338
338
- require.NoError(t, collr.Init())
339
- assert.Error(t, collr.Check())
339
+ require.NoError(t, collr.Init(context.Background()))
340
+ assert.Error(t, collr.Check(context.Background()))
341
}
src/go/plugin/go.d/collector/adaptecraid/collector.go
+5
-4
@@ -5,6 +5,7 @@
5
package adaptecraid
6
7
import (
8
+ "context"
9
_ "embed"
10
"errors"
11
"fmt"
@@ -60,7 +61,7 @@ func (c *Collector) Configuration() any {
61
return c.Config
62
}
63
63
-func (c *Collector) Init() error {
64
+func (c *Collector) Init(context.Context) error {
65
arcconf, err := c.initArcconfCliExec()
66
if err != nil {
67
return fmt.Errorf("arcconf exec initialization: %v", err)
@@ -70,7 +71,7 @@ func (c *Collector) Init() error {
71
return nil
72
}
73
73
-func (c *Collector) Check() error {
74
+func (c *Collector) Check(context.Context) error {
75
mx, err := c.collect()
76
if err != nil {
77
return err
@@ -87,7 +88,7 @@ func (c *Collector) Charts() *module.Charts {
88
return c.charts
89
}
90
90
-func (c *Collector) Collect() map[string]int64 {
91
+func (c *Collector) Collect(context.Context) map[string]int64 {
92
mx, err := c.collect()
93
if err != nil {
94
c.Error(err)
@@ -100,4 +101,4 @@ func (c *Collector) Collect() map[string]int64 {
101
return mx
102
}
103
103
-func (c *Collector) Cleanup() {}
104
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/adaptecraid/collector_test.go
+9
-8
@@ -5,6 +5,7 @@
5
package adaptecraid
6
7
import (
8
+ "context"
9
"errors"
10
"os"
11
"testing"
@@ -59,9 +60,9 @@ func TestCollector_Init(t *testing.T) {
60
collr := New()
61
62
if test.wantFail {
62
- assert.Error(t, collr.Init())
63
+ assert.Error(t, collr.Init(context.Background()))
64
} else {
64
- assert.NoError(t, collr.Init())
65
+ assert.NoError(t, collr.Init(context.Background()))
66
}
67
})
68
}
@@ -80,7 +81,7 @@ func TestCollector_Cleanup(t *testing.T) {
81
prepare: func() *Collector {
82
collr := New()
83
collr.exec = prepareMockOkCurrent()
83
- _ = collr.Check()
84
+ _ = collr.Check(context.Background())
85
return collr
86
},
87
},
@@ -88,7 +89,7 @@ func TestCollector_Cleanup(t *testing.T) {
89
prepare: func() *Collector {
90
collr := New()
91
collr.exec = prepareMockOkCurrent()
91
- _ = collr.Collect()
92
+ _ = collr.Collect(context.Background())
93
return collr
94
},
95
},
@@ -98,7 +99,7 @@ func TestCollector_Cleanup(t *testing.T) {
99
t.Run(name, func(t *testing.T) {
100
collr := test.prepare()
101
101
- assert.NotPanics(t, collr.Cleanup)
102
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
103
})
104
}
105
}
@@ -141,9 +142,9 @@ func TestCollector_Check(t *testing.T) {
142
collr.exec = mock
143
144
if test.wantFail {
144
- assert.Error(t, collr.Check())
145
+ assert.Error(t, collr.Check(context.Background()))
146
} else {
146
- assert.NoError(t, collr.Check())
147
+ assert.NoError(t, collr.Check(context.Background()))
148
}
149
})
150
}
@@ -218,7 +219,7 @@ func TestCollector_Collect(t *testing.T) {
219
mock := test.prepareMock()
220
collr.exec = mock
221
221
- mx := collr.Collect()
222
+ mx := collr.Collect(context.Background())
223
224
assert.Equal(t, test.wantMetrics, mx)
225
assert.Len(t, *collr.Charts(), test.wantCharts)
src/go/plugin/go.d/collector/ap/collector.go
+5
-4
@@ -5,6 +5,7 @@
5
package ap
6
7
import (
8
+ "context"
9
_ "embed"
10
"errors"
11
"fmt"
@@ -60,7 +61,7 @@ func (c *Collector) Configuration() any {
61
return c.Config
62
}
63
63
-func (c *Collector) Init() error {
64
+func (c *Collector) Init(context.Context) error {
65
if err := c.validateConfig(); err != nil {
66
return fmt.Errorf("config validation: %s", err)
67
}
@@ -74,7 +75,7 @@ func (c *Collector) Init() error {
75
return nil
76
}
77
77
-func (c *Collector) Check() error {
78
+func (c *Collector) Check(context.Context) error {
79
mx, err := c.collect()
80
if err != nil {
81
return err
@@ -91,7 +92,7 @@ func (c *Collector) Charts() *module.Charts {
92
return c.charts
93
}
94
94
-func (c *Collector) Collect() map[string]int64 {
95
+func (c *Collector) Collect(context.Context) map[string]int64 {
96
mx, err := c.collect()
97
if err != nil {
98
c.Error(err)
@@ -104,4 +105,4 @@ func (c *Collector) Collect() map[string]int64 {
105
return mx
106
}
107
107
-func (c *Collector) Cleanup() {}
108
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/ap/collector_test.go
+9
-8
@@ -5,6 +5,7 @@
5
package ap
6
7
import (
8
+ "context"
9
"errors"
10
"os"
11
"testing"
@@ -66,9 +67,9 @@ func TestCollector_Init(t *testing.T) {
67
collr.Config = test.config
68
69
if test.wantFail {
69
- assert.Error(t, collr.Init())
70
+ assert.Error(t, collr.Init(context.Background()))
71
} else {
71
- assert.NoError(t, collr.Init())
72
+ assert.NoError(t, collr.Init(context.Background()))
73
}
74
})
75
}
@@ -87,7 +88,7 @@ func TestCollector_Cleanup(t *testing.T) {
88
prepare: func() *Collector {
89
collr := New()
90
collr.exec = prepareMockOk()
90
- _ = collr.Check()
91
+ _ = collr.Check(context.Background())
92
return collr
93
},
94
},
@@ -95,7 +96,7 @@ func TestCollector_Cleanup(t *testing.T) {
96
prepare: func() *Collector {
97
collr := New()
98
collr.exec = prepareMockOk()
98
- _ = collr.Collect()
99
+ _ = collr.Collect(context.Background())
100
return collr
101
},
102
},
@@ -105,7 +106,7 @@ func TestCollector_Cleanup(t *testing.T) {
106
t.Run(name, func(t *testing.T) {
107
collr := test.prepare()
108
108
- assert.NotPanics(t, collr.Cleanup)
109
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
110
})
111
}
112
}
@@ -147,9 +148,9 @@ func TestCollector_Check(t *testing.T) {
148
collr.exec = test.prepareMock()
149
150
if test.wantFail {
150
- assert.Error(t, collr.Check())
151
+ assert.Error(t, collr.Check(context.Background()))
152
} else {
152
- assert.NoError(t, collr.Check())
153
+ assert.NoError(t, collr.Check(context.Background()))
154
}
155
})
156
}
@@ -210,7 +211,7 @@ func TestCollector_Collect(t *testing.T) {
211
collr := New()
212
collr.exec = test.prepareMock()
213
213
- mx := collr.Collect()
214
+ mx := collr.Collect(context.Background())
215
216
assert.Equal(t, test.wantMetrics, mx)
217
src/go/plugin/go.d/collector/apache/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package apache
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -63,7 +64,7 @@ func (c *Collector) Configuration() any {
64
return c.Config
65
}
66
66
-func (c *Collector) Init() error {
67
+func (c *Collector) Init(context.Context) error {
68
if err := c.validateConfig(); err != nil {
69
return fmt.Errorf("config validation: %v", err)
70
}
@@ -80,7 +81,7 @@ func (c *Collector) Init() error {
81
return nil
82
}
83
83
-func (c *Collector) Check() error {
84
+func (c *Collector) Check(context.Context) error {
85
mx, err := c.collect()
86
if err != nil {
87
return err
@@ -96,7 +97,7 @@ func (c *Collector) Charts() *module.Charts {
97
return c.charts
98
}
99
99
-func (c *Collector) Collect() map[string]int64 {
100
+func (c *Collector) Collect(context.Context) map[string]int64 {
101
mx, err := c.collect()
102
if err != nil {
103
c.Error(err)
@@ -108,7 +109,7 @@ func (c *Collector) Collect() map[string]int64 {
109
return mx
110
}
111
111
-func (c *Collector) Cleanup() {
112
+func (c *Collector) Cleanup(context.Context) {
113
if c.httpClient != nil {
114
c.httpClient.CloseIdleConnections()
115
}
src/go/plugin/go.d/collector/apache/collector_test.go
+14
-13
@@ -3,6 +3,7 @@
3
package apache
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -76,9 +77,9 @@ func TestCollector_Init(t *testing.T) {
77
collr.Config = test.config
78
79
if test.wantFail {
79
- assert.Error(t, collr.Init())
80
+ assert.Error(t, collr.Init(context.Background()))
81
} else {
81
- assert.NoError(t, collr.Init())
82
+ assert.NoError(t, collr.Init(context.Background()))
83
}
84
})
85
}
@@ -125,9 +126,9 @@ func TestCollector_Check(t *testing.T) {
126
defer cleanup()
127
128
if test.wantFail {
128
- assert.Error(t, collr.Check())
129
+ assert.Error(t, collr.Check(context.Background()))
130
} else {
130
- assert.NoError(t, collr.Check())
131
+ assert.NoError(t, collr.Check(context.Background()))
132
}
133
})
134
}
@@ -247,9 +248,9 @@ func TestCollector_Collect(t *testing.T) {
248
collr, cleanup := test.prepare(t)
249
defer cleanup()
250
250
- _ = collr.Check()
251
+ _ = collr.Check(context.Background())
252
252
- collected := collr.Collect()
253
+ collected := collr.Collect(context.Background())
254
255
require.Equal(t, test.wantMetrics, collected)
256
assert.Equal(t, test.wantNumOfCharts, len(*collr.Charts()))
@@ -265,7 +266,7 @@ func caseMPMEventSimpleStatus(t *testing.T) (*Collector, func()) {
266
}))
267
collr := New()
268
collr.URL = srv.URL + "/server-status?auto"
268
- require.NoError(t, collr.Init())
269
+ require.NoError(t, collr.Init(context.Background()))
270
271
return collr, srv.Close
272
}
@@ -278,7 +279,7 @@ func caseMPMEventExtendedStatus(t *testing.T) (*Collector, func()) {
279
}))
280
collr := New()
281
collr.URL = srv.URL + "/server-status?auto"
281
- require.NoError(t, collr.Init())
282
+ require.NoError(t, collr.Init(context.Background()))
283
284
return collr, srv.Close
285
}
@@ -291,7 +292,7 @@ func caseMPMPreforkExtendedStatus(t *testing.T) (*Collector, func()) {
292
}))
293
collr := New()
294
collr.URL = srv.URL + "/server-status?auto"
294
- require.NoError(t, collr.Init())
295
+ require.NoError(t, collr.Init(context.Background()))
296
297
return collr, srv.Close
298
}
@@ -304,7 +305,7 @@ func caseLighttpdResponse(t *testing.T) (*Collector, func()) {
305
}))
306
collr := New()
307
collr.URL = srv.URL + "/server-status?auto"
307
- require.NoError(t, collr.Init())
308
+ require.NoError(t, collr.Init(context.Background()))
309
310
return collr, srv.Close
311
}
@@ -317,7 +318,7 @@ func caseInvalidDataResponse(t *testing.T) (*Collector, func()) {
318
}))
319
collr := New()
320
collr.URL = srv.URL + "/server-status?auto"
320
- require.NoError(t, collr.Init())
321
+ require.NoError(t, collr.Init(context.Background()))
322
323
return collr, srv.Close
324
}
@@ -326,7 +327,7 @@ func caseConnectionRefused(t *testing.T) (*Collector, func()) {
327
t.Helper()
328
collr := New()
329
collr.URL = "http://127.0.0.1:65001/server-status?auto"
329
- require.NoError(t, collr.Init())
330
+ require.NoError(t, collr.Init(context.Background()))
331
332
return collr, func() {}
333
}
@@ -339,7 +340,7 @@ func case404(t *testing.T) (*Collector, func()) {
340
}))
341
collr := New()
342
collr.URL = srv.URL + "/server-status?auto"
342
- require.NoError(t, collr.Init())
343
+ require.NoError(t, collr.Init(context.Background()))
344
345
return collr, srv.Close
346
}
src/go/plugin/go.d/collector/apcupsd/collect.go
+2
-1
@@ -3,6 +3,7 @@
3
package apcupsd
4
5
import (
6
+ "context"
7
"errors"
8
"fmt"
9
"strings"
@@ -22,7 +23,7 @@ func (c *Collector) collect() (map[string]int64, error) {
23
24
resp, err := c.conn.status()
25
if err != nil {
25
- c.Cleanup()
26
+ c.Cleanup(context.Background())
27
return nil, err
28
}
29
src/go/plugin/go.d/collector/apcupsd/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package apcupsd
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"time"
@@ -53,7 +54,7 @@ func (c *Collector) Configuration() any {
54
return c.Config
55
}
56
56
-func (c *Collector) Init() error {
57
+func (c *Collector) Init(context.Context) error {
58
if c.Address == "" {
59
return errors.New("config: 'address' not set")
60
}
@@ -61,7 +62,7 @@ func (c *Collector) Init() error {
62
return nil
63
}
64
64
-func (c *Collector) Check() error {
65
+func (c *Collector) Check(context.Context) error {
66
mx, err := c.collect()
67
if err != nil {
68
return err
@@ -76,7 +77,7 @@ func (c *Collector) Charts() *module.Charts {
77
return c.charts
78
}
79
79
-func (c *Collector) Collect() map[string]int64 {
80
+func (c *Collector) Collect(context.Context) map[string]int64 {
81
mx, err := c.collect()
82
if err != nil {
83
c.Error(err)
@@ -88,7 +89,7 @@ func (c *Collector) Collect() map[string]int64 {
89
return mx
90
}
91
91
-func (c *Collector) Cleanup() {
92
+func (c *Collector) Cleanup(context.Context) {
93
if c.conn != nil {
94
if err := c.conn.disconnect(); err != nil {
95
c.Warningf("error on disconnect: %v", err)
src/go/plugin/go.d/collector/apcupsd/collector_test.go
+13
-12
@@ -3,6 +3,7 @@
3
package apcupsd
4
5
import (
6
+ "context"
7
"errors"
8
"os"
9
"strings"
@@ -39,14 +40,14 @@ func TestCollector_ConfigurationSerialize(t *testing.T) {
40
41
func TestCollector_Cleanup(t *testing.T) {
42
collr := New()
42
- require.NotPanics(t, collr.Cleanup)
43
+ require.NotPanics(t, func() { collr.Cleanup(context.Background()) })
44
45
mock := prepareMockOk()
46
collr.newConn = func(Config) apcupsdConn { return mock }
47
47
- require.NoError(t, collr.Init())
48
- _ = collr.Collect()
49
- require.NotPanics(t, collr.Cleanup)
48
+ require.NoError(t, collr.Init(context.Background()))
49
+ _ = collr.Collect(context.Background())
50
+ require.NotPanics(t, func() { collr.Cleanup(context.Background()) })
51
assert.True(t, mock.calledDisconnect)
52
}
53
@@ -71,9 +72,9 @@ func TestCollector_Init(t *testing.T) {
72
collr.Config = test.config
73
74
if test.wantFail {
74
- assert.Error(t, collr.Init())
75
+ assert.Error(t, collr.Init(context.Background()))
76
} else {
76
- assert.NoError(t, collr.Init())
77
+ assert.NoError(t, collr.Init(context.Background()))
78
}
79
})
80
}
@@ -107,12 +108,12 @@ func TestCollector_Check(t *testing.T) {
108
collr := New()
109
collr.newConn = func(Config) apcupsdConn { return test.prepareMock() }
110
110
- require.NoError(t, collr.Init())
111
+ require.NoError(t, collr.Init(context.Background()))
112
113
if test.wantFail {
113
- assert.Error(t, collr.Check())
114
+ assert.Error(t, collr.Check(context.Background()))
115
} else {
115
- assert.NoError(t, collr.Check())
116
+ assert.NoError(t, collr.Check(context.Background()))
117
}
118
})
119
}
@@ -120,7 +121,7 @@ func TestCollector_Check(t *testing.T) {
121
122
func TestCollector_Charts(t *testing.T) {
123
collr := New()
123
- require.NoError(t, collr.Init())
124
+ require.NoError(t, collr.Init(context.Background()))
125
assert.NotNil(t, collr.Charts())
126
}
127
@@ -205,12 +206,12 @@ func TestCollector_Collect(t *testing.T) {
206
for name, test := range tests {
207
t.Run(name, func(t *testing.T) {
208
collr := New()
208
- require.NoError(t, collr.Init())
209
+ require.NoError(t, collr.Init(context.Background()))
210
211
mock := test.prepareMock()
212
collr.newConn = func(Config) apcupsdConn { return mock }
213
213
- mx := collr.Collect()
214
+ mx := collr.Collect(context.Background())
215
216
if _, ok := mx["battery_seconds_since_replacement"]; ok {
217
mx["battery_seconds_since_replacement"] = 86400
src/go/plugin/go.d/collector/beanstalk/collect.go
+2
-1
@@ -3,6 +3,7 @@
3
package beanstalk
4
5
import (
6
+ "context"
7
"fmt"
8
"slices"
9
"time"
@@ -22,7 +23,7 @@ func (c *Collector) collect() (map[string]int64, error) {
23
mx := make(map[string]int64)
24
25
if err := c.collectStats(mx); err != nil {
25
- c.Cleanup()
26
+ c.Cleanup(context.Background())
27
return nil, err
28
}
29
if err := c.collectTubesStats(mx); err != nil {
src/go/plugin/go.d/collector/beanstalk/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package beanstalk
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -68,7 +69,7 @@ func (c *Collector) Configuration() any {
69
return c.Config
70
}
71
71
-func (c *Collector) Init() error {
72
+func (c *Collector) Init(context.Context) error {
73
if err := c.validateConfig(); err != nil {
74
return fmt.Errorf("config validation: %v", err)
75
}
@@ -82,7 +83,7 @@ func (c *Collector) Init() error {
83
return nil
84
}
85
85
-func (c *Collector) Check() error {
86
+func (c *Collector) Check(context.Context) error {
87
mx, err := c.collect()
88
if err != nil {
89
return err
@@ -99,7 +100,7 @@ func (c *Collector) Charts() *module.Charts {
100
return c.charts
101
}
102
102
-func (c *Collector) Collect() map[string]int64 {
103
+func (c *Collector) Collect(context.Context) map[string]int64 {
104
mx, err := c.collect()
105
if err != nil {
106
c.Error(err)
@@ -112,7 +113,7 @@ func (c *Collector) Collect() map[string]int64 {
113
return mx
114
}
115
115
-func (c *Collector) Cleanup() {
116
+func (c *Collector) Cleanup(context.Context) {
117
if c.conn != nil {
118
if err := c.conn.disconnect(); err != nil {
119
c.Warningf("error on disconnect: %s", err)
src/go/plugin/go.d/collector/beanstalk/collector_test.go
+10
-9
@@ -4,6 +4,7 @@ package beanstalk
4
5
import (
6
"bufio"
7
+ "context"
8
"errors"
9
"fmt"
10
"net"
@@ -68,9 +69,9 @@ func TestCollector_Init(t *testing.T) {
69
collr.Config = test.config
70
71
if test.wantFail {
71
- assert.Error(t, collr.Init())
72
+ assert.Error(t, collr.Init(context.Background()))
73
} else {
73
- assert.NoError(t, collr.Init())
74
+ assert.NoError(t, collr.Init(context.Background()))
75
}
76
})
77
}
@@ -115,15 +116,15 @@ func TestCollector_Check(t *testing.T) {
116
t.Errorf("mock collr daemon start timed out")
117
}
118
118
- require.NoError(t, collr.Init())
119
+ require.NoError(t, collr.Init(context.Background()))
120
121
if test.wantFail {
121
- assert.Error(t, collr.Check())
122
+ assert.Error(t, collr.Check(context.Background()))
123
} else {
123
- assert.NoError(t, collr.Check())
124
+ assert.NoError(t, collr.Check(context.Background()))
125
}
126
126
- collr.Cleanup()
127
+ collr.Cleanup(context.Background())
128
129
select {
130
case <-daemon.stopped:
@@ -226,9 +227,9 @@ func TestCollector_Collect(t *testing.T) {
227
t.Errorf("mock collr daemon start timed out")
228
}
229
229
- require.NoError(t, collr.Init())
230
+ require.NoError(t, collr.Init(context.Background()))
231
231
- mx := collr.Collect()
232
+ mx := collr.Collect(context.Background())
233
234
require.Equal(t, test.wantMetrics, mx)
235
@@ -238,7 +239,7 @@ func TestCollector_Collect(t *testing.T) {
239
module.TestMetricsHasAllChartsDims(t, collr.Charts(), mx)
240
}
241
241
- collr.Cleanup()
242
+ collr.Cleanup(context.Background())
243
244
select {
245
case <-daemon.stopped:
src/go/plugin/go.d/collector/bind/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package bind
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -70,7 +71,7 @@ func (c *Collector) Configuration() any {
71
return c.Config
72
}
73
73
-func (c *Collector) Init() error {
74
+func (c *Collector) Init(context.Context) error {
75
if err := c.validateConfig(); err != nil {
76
return fmt.Errorf("config verification: %v", err)
77
}
@@ -98,7 +99,7 @@ func (c *Collector) Init() error {
99
return nil
100
}
101
101
-func (c *Collector) Check() error {
102
+func (c *Collector) Check(context.Context) error {
103
mx, err := c.collect()
104
if err != nil {
105
return err
@@ -114,7 +115,7 @@ func (c *Collector) Charts() *Charts {
115
return c.charts
116
}
117
117
-func (c *Collector) Collect() map[string]int64 {
118
+func (c *Collector) Collect(context.Context) map[string]int64 {
119
mx, err := c.collect()
120
121
if err != nil {
@@ -125,7 +126,7 @@ func (c *Collector) Collect() map[string]int64 {
126
return mx
127
}
128
128
-func (c *Collector) Cleanup() {
129
+func (c *Collector) Cleanup(context.Context) {
130
if c.httpClient != nil {
131
c.httpClient.CloseIdleConnections()
132
}
src/go/plugin/go.d/collector/bind/collector_test.go
+24
-19
@@ -3,6 +3,7 @@
3
package bind
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -38,18 +39,18 @@ func TestCollector_ConfigurationSerialize(t *testing.T) {
39
module.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML)
40
}
41
41
-func TestCollector_Cleanup(t *testing.T) { New().Cleanup() }
42
+func TestCollector_Cleanup(t *testing.T) { New().Cleanup(context.Background()) }
43
44
func TestCollector_Init(t *testing.T) {
45
// OK
46
collr := New()
46
- assert.NoError(t, collr.Init())
47
+ assert.NoError(t, collr.Init(context.Background()))
48
assert.NotNil(t, collr.bindAPIClient)
49
50
//NG
51
collr = New()
52
collr.URL = ""
52
- assert.Error(t, collr.Init())
53
+ assert.Error(t, collr.Init(context.Background()))
54
}
55
56
func TestCollector_Check(t *testing.T) {
@@ -65,16 +66,16 @@ func TestCollector_Check(t *testing.T) {
66
collr := New()
67
collr.URL = ts.URL + "/json/v1"
68
68
- require.NoError(t, collr.Init())
69
- require.NoError(t, collr.Check())
69
+ require.NoError(t, collr.Init(context.Background()))
70
+ require.NoError(t, collr.Check(context.Background()))
71
}
72
73
func TestCollector_CheckNG(t *testing.T) {
74
collr := New()
75
76
collr.URL = "http://127.0.0.1:38001/xml/v3"
76
- require.NoError(t, collr.Init())
77
- assert.Error(t, collr.Check())
77
+ require.NoError(t, collr.Init(context.Background()))
78
+ assert.Error(t, collr.Check(context.Background()))
79
}
80
81
func TestCollector_Charts(t *testing.T) {
@@ -95,8 +96,8 @@ func TestCollector_CollectJSON(t *testing.T) {
96
collr.URL = ts.URL + "/json/v1"
97
collr.PermitView = "*"
98
98
- require.NoError(t, collr.Init())
99
- require.NoError(t, collr.Check())
99
+ require.NoError(t, collr.Init(context.Background()))
100
+ require.NoError(t, collr.Check(context.Background()))
101
102
expected := map[string]int64{
103
"_default_Queryv4": 4503685324,
@@ -254,7 +255,7 @@ func TestCollector_CollectJSON(t *testing.T) {
255
"Others": 74006,
256
}
257
257
- assert.Equal(t, expected, collr.Collect())
258
+ assert.Equal(t, expected, collr.Collect(context.Background()))
259
assert.Len(t, *collr.charts, 17)
260
}
261
@@ -272,8 +273,8 @@ func TestCollector_CollectXML3(t *testing.T) {
273
collr.PermitView = "*"
274
collr.URL = ts.URL + "/xml/v3"
275
275
- require.NoError(t, collr.Init())
276
- require.NoError(t, collr.Check())
276
+ require.NoError(t, collr.Init(context.Background()))
277
+ require.NoError(t, collr.Check(context.Background()))
278
279
expected := map[string]int64{
280
"_bind_CookieClientOk": 0,
@@ -507,26 +508,30 @@ func TestCollector_CollectXML3(t *testing.T) {
508
"IQUERY": 199,
509
}
510
510
- assert.Equal(t, expected, collr.Collect())
511
+ assert.Equal(t, expected, collr.Collect(context.Background()))
512
assert.Len(t, *collr.charts, 20)
513
}
514
515
func TestCollector_InvalidData(t *testing.T) {
515
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte("hello and goodbye")) }))
516
+ ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
517
+ _, _ = w.Write([]byte("hello and goodbye"))
518
+ }))
519
defer ts.Close()
520
521
collr := New()
522
collr.URL = ts.URL + "/json/v1"
520
- require.NoError(t, collr.Init())
521
- assert.Error(t, collr.Check())
523
+ require.NoError(t, collr.Init(context.Background()))
524
+ assert.Error(t, collr.Check(context.Background()))
525
}
526
527
func TestCollector_404(t *testing.T) {
525
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(404) }))
528
+ ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
529
+ w.WriteHeader(404)
530
+ }))
531
defer ts.Close()
532
533
collr := New()
534
collr.URL = ts.URL + "/json/v1"
530
- require.NoError(t, collr.Init())
531
- assert.Error(t, collr.Check())
535
+ require.NoError(t, collr.Init(context.Background()))
536
+ assert.Error(t, collr.Check(context.Background()))
537
}
src/go/plugin/go.d/collector/boinc/collect.go
+2
-1
@@ -3,6 +3,7 @@
3
package boinc
4
5
import (
6
+ "context"
7
"fmt"
8
"net"
9
)
@@ -18,7 +19,7 @@ func (c *Collector) collect() (map[string]int64, error) {
19
20
results, err := c.conn.getResults()
21
if err != nil {
21
- c.Cleanup()
22
+ c.Cleanup(context.Background())
23
return nil, err
24
}
25
src/go/plugin/go.d/collector/boinc/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package boinc
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"time"
@@ -55,7 +56,7 @@ func (c *Collector) Configuration() any {
56
return c.Config
57
}
58
58
-func (c *Collector) Init() error {
59
+func (c *Collector) Init(context.Context) error {
60
if c.Address == "" {
61
return errors.New("config: 'address' not set")
62
}
@@ -63,7 +64,7 @@ func (c *Collector) Init() error {
64
return nil
65
}
66
66
-func (c *Collector) Check() error {
67
+func (c *Collector) Check(context.Context) error {
68
mx, err := c.collect()
69
if err != nil {
70
return err
@@ -80,7 +81,7 @@ func (c *Collector) Charts() *module.Charts {
81
return c.charts
82
}
83
83
-func (c *Collector) Collect() map[string]int64 {
84
+func (c *Collector) Collect(context.Context) map[string]int64 {
85
mx, err := c.collect()
86
if err != nil {
87
c.Error(err)
@@ -93,7 +94,7 @@ func (c *Collector) Collect() map[string]int64 {
94
return mx
95
}
96
96
-func (c *Collector) Cleanup() {
97
+func (c *Collector) Cleanup(context.Context) {
98
if c.conn != nil {
99
c.conn.disconnect()
100
c.conn = nil
src/go/plugin/go.d/collector/boinc/collector_test.go
+10
-9
@@ -3,6 +3,7 @@
3
package boinc
4
5
import (
6
+ "context"
7
"encoding/xml"
8
"errors"
9
"os"
@@ -63,9 +64,9 @@ func TestCollector_Init(t *testing.T) {
64
collr.Config = test.config
65
66
if test.wantFail {
66
- assert.Error(t, collr.Init())
67
+ assert.Error(t, collr.Init(context.Background()))
68
} else {
68
- assert.NoError(t, collr.Init())
69
+ assert.NoError(t, collr.Init(context.Background()))
70
}
71
})
72
}
@@ -84,7 +85,7 @@ func TestCollector_Cleanup(t *testing.T) {
85
prepare: func() *Collector {
86
collr := New()
87
collr.newConn = func(Config, *logger.Logger) boincConn { return prepareMockOk() }
87
- _ = collr.Check()
88
+ _ = collr.Check(context.Background())
89
return collr
90
},
91
},
@@ -92,7 +93,7 @@ func TestCollector_Cleanup(t *testing.T) {
93
prepare: func() *Collector {
94
collr := New()
95
collr.newConn = func(Config, *logger.Logger) boincConn { return prepareMockOk() }
95
- _ = collr.Collect()
96
+ _ = collr.Collect(context.Background())
97
return collr
98
},
99
},
@@ -102,7 +103,7 @@ func TestCollector_Cleanup(t *testing.T) {
103
t.Run(name, func(t *testing.T) {
104
collr := test.prepare()
105
105
- assert.NotPanics(t, collr.Cleanup)
106
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
107
})
108
}
109
}
@@ -133,9 +134,9 @@ func TestCollector_Check(t *testing.T) {
134
collr.newConn = func(Config, *logger.Logger) boincConn { return mock }
135
136
if test.wantFail {
136
- assert.Error(t, collr.Check())
137
+ assert.Error(t, collr.Check(context.Background()))
138
} else {
138
- assert.NoError(t, collr.Check())
139
+ assert.NoError(t, collr.Check(context.Background()))
140
}
141
})
142
}
@@ -216,7 +217,7 @@ func TestCollector_Collect(t *testing.T) {
217
mock := test.prepareMock()
218
collr.newConn = func(Config, *logger.Logger) boincConn { return mock }
219
219
- mx := collr.Collect()
220
+ mx := collr.Collect(context.Background())
221
222
require.Equal(t, test.wantMetrics, mx)
223
@@ -225,7 +226,7 @@ func TestCollector_Collect(t *testing.T) {
226
}
227
228
assert.Equal(t, test.disconnectBeforeCleanup, mock.disconnectCalled, "disconnect before cleanup")
228
- collr.Cleanup()
229
+ collr.Cleanup(context.Background())
230
assert.Equal(t, test.disconnectAfterCleanup, mock.disconnectCalled, "disconnect after cleanup")
231
})
232
}
src/go/plugin/go.d/collector/cassandra/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package cassandra
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -68,7 +69,7 @@ func (c *Collector) Configuration() any {
69
return c.Config
70
}
71
71
-func (c *Collector) Init() error {
72
+func (c *Collector) Init(context.Context) error {
73
if err := c.validateConfig(); err != nil {
74
return fmt.Errorf("error on validating config: %v", err)
75
}
@@ -82,7 +83,7 @@ func (c *Collector) Init() error {
83
return nil
84
}
85
85
-func (c *Collector) Check() error {
86
+func (c *Collector) Check(context.Context) error {
87
mx, err := c.collect()
88
if err != nil {
89
return err
@@ -98,7 +99,7 @@ func (c *Collector) Charts() *module.Charts {
99
return c.charts
100
}
101
101
-func (c *Collector) Collect() map[string]int64 {
102
+func (c *Collector) Collect(context.Context) map[string]int64 {
103
mx, err := c.collect()
104
if err != nil {
105
c.Error(err)
@@ -110,7 +111,7 @@ func (c *Collector) Collect() map[string]int64 {
111
return mx
112
}
113
113
-func (c *Collector) Cleanup() {
114
+func (c *Collector) Cleanup(context.Context) {
115
if c.prom != nil && c.prom.HTTPClient() != nil {
116
c.prom.HTTPClient().CloseIdleConnections()
117
}
src/go/plugin/go.d/collector/cassandra/collector_test.go
+8
-7
@@ -3,6 +3,7 @@
3
package cassandra
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -65,9 +66,9 @@ func TestCollector_Init(t *testing.T) {
66
collr.Config = test.config
67
68
if test.wantFail {
68
- assert.Error(t, collr.Init())
69
+ assert.Error(t, collr.Init(context.Background()))
70
} else {
70
- assert.NoError(t, collr.Init())
71
+ assert.NoError(t, collr.Init(context.Background()))
72
}
73
})
74
}
@@ -100,12 +101,12 @@ func TestCollector_Check(t *testing.T) {
101
collr, cleanup := test.prepare()
102
defer cleanup()
103
103
- require.NoError(t, collr.Init())
104
+ require.NoError(t, collr.Init(context.Background()))
105
106
if test.wantFail {
106
- assert.Error(t, collr.Check())
107
+ assert.Error(t, collr.Check(context.Background()))
108
} else {
108
- assert.NoError(t, collr.Check())
109
+ assert.NoError(t, collr.Check(context.Background()))
110
}
111
})
112
}
@@ -249,9 +250,9 @@ func TestCollector_Collect(t *testing.T) {
250
collr, cleanup := test.prepare()
251
defer cleanup()
252
252
- require.NoError(t, collr.Init())
253
+ require.NoError(t, collr.Init(context.Background()))
254
254
- mx := collr.Collect()
255
+ mx := collr.Collect(context.Background())
256
257
assert.Equal(t, test.wantCollected, mx)
258
})
src/go/plugin/go.d/collector/ceph/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package ceph
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -77,7 +78,7 @@ func (c *Collector) Configuration() any {
78
return c.Config
79
}
80
80
-func (c *Collector) Init() error {
81
+func (c *Collector) Init(context.Context) error {
82
if err := c.validateConfig(); err != nil {
83
return fmt.Errorf("invalid config: %v", err)
84
}
@@ -91,7 +92,7 @@ func (c *Collector) Init() error {
92
return nil
93
}
94
94
-func (c *Collector) Check() error {
95
+func (c *Collector) Check(context.Context) error {
96
mx, err := c.collect()
97
if err != nil {
98
return err
@@ -108,7 +109,7 @@ func (c *Collector) Charts() *module.Charts {
109
return c.charts
110
}
111
111
-func (c *Collector) Collect() map[string]int64 {
112
+func (c *Collector) Collect(context.Context) map[string]int64 {
113
mx, err := c.collect()
114
if err != nil {
115
c.Error(err)
@@ -121,7 +122,7 @@ func (c *Collector) Collect() map[string]int64 {
122
return mx
123
}
124
124
-func (c *Collector) Cleanup() {
125
+func (c *Collector) Cleanup(context.Context) {
126
if c.httpClient != nil {
127
if err := c.authLogout(); err != nil {
128
c.Warningf("failed to logout: %v", err)
src/go/plugin/go.d/collector/ceph/collector_test.go
+10
-9
@@ -4,6 +4,7 @@ package ceph
4
5
import (
6
"bytes"
7
+ "context"
8
"encoding/json"
9
"io"
10
"net/http"
@@ -72,9 +73,9 @@ func TestCollector_Init(t *testing.T) {
73
collr.Config = test.config
74
75
if test.wantFail {
75
- assert.Error(t, collr.Init())
76
+ assert.Error(t, collr.Init(context.Background()))
77
} else {
77
- assert.NoError(t, collr.Init())
78
+ assert.NoError(t, collr.Init(context.Background()))
79
}
80
})
81
}
@@ -105,9 +106,9 @@ func TestCollector_Check(t *testing.T) {
106
defer cleanup()
107
108
if test.wantFail {
108
- assert.Error(t, collr.Check())
109
+ assert.Error(t, collr.Check(context.Background()))
110
} else {
110
- assert.NoError(t, collr.Check())
111
+ assert.NoError(t, collr.Check(context.Background()))
112
}
113
})
114
}
@@ -227,9 +228,9 @@ func TestCollector_Collect(t *testing.T) {
228
collr, cleanup := test.prepare(t)
229
defer cleanup()
230
230
- _ = collr.Check()
231
+ _ = collr.Check(context.Background())
232
232
- mx := collr.Collect()
233
+ mx := collr.Collect(context.Background())
234
235
require.Equal(t, test.wantMetrics, mx)
236
@@ -299,7 +300,7 @@ func caseOk(t *testing.T) (*Collector, func()) {
300
collr.URL = srv.URL
301
collr.Username = "user"
302
collr.Password = "password"
302
- require.NoError(t, collr.Init())
303
+ require.NoError(t, collr.Init(context.Background()))
304
305
return collr, srv.Close
306
}
@@ -310,7 +311,7 @@ func caseConnectionRefused(t *testing.T) (*Collector, func()) {
311
collr.URL = "http://127.0.0.1:65001"
312
collr.Username = "user"
313
collr.Password = "password"
313
- require.NoError(t, collr.Init())
314
+ require.NoError(t, collr.Init(context.Background()))
315
316
return collr, func() {}
317
}
@@ -325,7 +326,7 @@ func case404(t *testing.T) (*Collector, func()) {
326
collr.URL = srv.URL
327
collr.Username = "user"
328
collr.Password = "password"
328
- require.NoError(t, collr.Init())
329
+ require.NoError(t, collr.Init(context.Background()))
330
331
return collr, srv.Close
332
}
src/go/plugin/go.d/collector/chrony/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package chrony
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -59,7 +60,7 @@ func (c *Collector) Configuration() any {
60
return c.Config
61
}
62
62
-func (c *Collector) Init() error {
63
+func (c *Collector) Init(context.Context) error {
64
if err := c.validateConfig(); err != nil {
65
return fmt.Errorf("config validation: %v", err)
66
}
@@ -72,7 +73,7 @@ func (c *Collector) Init() error {
73
return nil
74
}
75
75
-func (c *Collector) Check() error {
76
+func (c *Collector) Check(context.Context) error {
77
mx, err := c.collect()
78
if err != nil {
79
return err
@@ -88,7 +89,7 @@ func (c *Collector) Charts() *module.Charts {
89
return c.charts
90
}
91
91
-func (c *Collector) Collect() map[string]int64 {
92
+func (c *Collector) Collect(ctx context.Context) map[string]int64 {
93
mx, err := c.collect()
94
if err != nil {
95
c.Error(err)
@@ -100,7 +101,7 @@ func (c *Collector) Collect() map[string]int64 {
101
return mx
102
}
103
103
-func (c *Collector) Cleanup() {
104
+func (c *Collector) Cleanup(context.Context) {
105
if c.conn != nil {
106
c.conn.close()
107
c.conn = nil
src/go/plugin/go.d/collector/chrony/collector_test.go
+13
-12
@@ -3,6 +3,7 @@
3
package chrony
4
5
import (
6
+ "context"
7
"errors"
8
"net"
9
"os"
@@ -56,9 +57,9 @@ func TestCollector_Init(t *testing.T) {
57
collr.Config = test.config
58
59
if test.wantFail {
59
- assert.Error(t, collr.Init())
60
+ assert.Error(t, collr.Init(context.Background()))
61
} else {
61
- assert.NoError(t, collr.Init())
62
+ assert.NoError(t, collr.Init(context.Background()))
63
}
64
})
65
}
@@ -95,12 +96,12 @@ func TestCollector_Check(t *testing.T) {
96
t.Run(name, func(t *testing.T) {
97
collr := test.prepare()
98
98
- require.NoError(t, collr.Init())
99
+ require.NoError(t, collr.Init(context.Background()))
100
101
if test.wantFail {
101
- assert.Error(t, collr.Check())
102
+ assert.Error(t, collr.Check(context.Background()))
103
} else {
103
- assert.NoError(t, collr.Check())
104
+ assert.NoError(t, collr.Check(context.Background()))
105
}
106
})
107
}
@@ -121,15 +122,15 @@ func TestCollector_Cleanup(t *testing.T) {
122
},
123
"after Init": {
124
wantClose: false,
124
- prepare: func(c *Collector) { _ = c.Init() },
125
+ prepare: func(c *Collector) { _ = c.Init(context.Background()) },
126
},
127
"after Check": {
128
wantClose: true,
128
- prepare: func(c *Collector) { _ = c.Init(); _ = c.Check() },
129
+ prepare: func(c *Collector) { _ = c.Init(context.Background()); _ = c.Check(context.Background()) },
130
},
131
"after Collect": {
132
wantClose: true,
132
- prepare: func(c *Collector) { _ = c.Init(); _ = c.Collect() },
133
+ prepare: func(c *Collector) { _ = c.Init(context.Background()); _ = c.Collect(context.Background()) },
134
},
135
}
136
@@ -139,7 +140,7 @@ func TestCollector_Cleanup(t *testing.T) {
140
collr := prepareChronyWithMock(m)
141
test.prepare(collr)
142
142
- require.NotPanics(t, collr.Cleanup)
143
+ require.NotPanics(t, func() { collr.Cleanup(context.Background()) })
144
145
if test.wantClose {
146
assert.True(t, m.closeCalled)
@@ -222,11 +223,11 @@ func TestCollector_Collect(t *testing.T) {
223
t.Run(name, func(t *testing.T) {
224
collr := test.prepare()
225
225
- require.NoError(t, collr.Init())
226
+ require.NoError(t, collr.Init(context.Background()))
227
collr.exec = &mockChronyc{}
227
- _ = collr.Check()
228
+ _ = collr.Check(context.Background())
229
229
- mx := collr.Collect()
230
+ mx := collr.Collect(context.Background())
231
copyRefMeasurementTime(mx, test.expected)
232
233
assert.Equal(t, test.expected, mx)
src/go/plugin/go.d/collector/clickhouse/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package clickhouse
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -68,7 +69,7 @@ func (c *Collector) Configuration() any {
69
return c.Config
70
}
71
71
-func (c *Collector) Init() error {
72
+func (c *Collector) Init(context.Context) error {
73
if err := c.validateConfig(); err != nil {
74
return fmt.Errorf("config validation: %v", err)
75
}
@@ -85,7 +86,7 @@ func (c *Collector) Init() error {
86
return nil
87
}
88
88
-func (c *Collector) Check() error {
89
+func (c *Collector) Check(context.Context) error {
90
mx, err := c.collect()
91
if err != nil {
92
return err
@@ -102,7 +103,7 @@ func (c *Collector) Charts() *module.Charts {
103
return c.charts
104
}
105
105
-func (c *Collector) Collect() map[string]int64 {
106
+func (c *Collector) Collect(context.Context) map[string]int64 {
107
mx, err := c.collect()
108
if err != nil {
109
c.Error(err)
@@ -115,7 +116,7 @@ func (c *Collector) Collect() map[string]int64 {
116
return mx
117
}
118
118
-func (c *Collector) Cleanup() {
119
+func (c *Collector) Cleanup(context.Context) {
120
if c.httpClient != nil {
121
c.httpClient.CloseIdleConnections()
122
}
src/go/plugin/go.d/collector/clickhouse/collector_test.go
+9
-8
@@ -3,6 +3,7 @@
3
package clickhouse
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -71,9 +72,9 @@ func TestCollector_Init(t *testing.T) {
72
collr.Config = test.config
73
74
if test.wantFail {
74
- assert.Error(t, collr.Init())
75
+ assert.Error(t, collr.Init(context.Background()))
76
} else {
76
- assert.NoError(t, collr.Init())
77
+ assert.NoError(t, collr.Init(context.Background()))
78
}
79
})
80
}
@@ -108,9 +109,9 @@ func TestCollector_Check(t *testing.T) {
109
defer cleanup()
110
111
if test.wantFail {
111
- assert.Error(t, collr.Check())
112
+ assert.Error(t, collr.Check(context.Background()))
113
} else {
113
- assert.NoError(t, collr.Check())
114
+ assert.NoError(t, collr.Check(context.Background()))
115
}
116
})
117
}
@@ -240,7 +241,7 @@ func TestCollector_Collect(t *testing.T) {
241
collr, cleanup := test.prepare(t)
242
defer cleanup()
243
243
- mx := collr.Collect()
244
+ mx := collr.Collect(context.Background())
245
246
require.Equal(t, test.wantMetrics, mx)
247
@@ -275,7 +276,7 @@ func prepareCaseOk(t *testing.T) (*Collector, func()) {
276
277
collr := New()
278
collr.URL = srv.URL
278
- require.NoError(t, collr.Init())
279
+ require.NoError(t, collr.Init(context.Background()))
280
281
return collr, srv.Close
282
}
@@ -289,7 +290,7 @@ func prepareCaseUnexpectedResponse(t *testing.T) (*Collector, func()) {
290
291
collr := New()
292
collr.URL = srv.URL
292
- require.NoError(t, collr.Init())
293
+ require.NoError(t, collr.Init(context.Background()))
294
295
return collr, srv.Close
296
}
@@ -298,7 +299,7 @@ func prepareCaseConnectionRefused(t *testing.T) (*Collector, func()) {
299
t.Helper()
300
collr := New()
301
collr.URL = "http://127.0.0.1:65001/stat"
301
- require.NoError(t, collr.Init())
302
+ require.NoError(t, collr.Init(context.Background()))
303
304
return collr, func() {}
305
}
src/go/plugin/go.d/collector/cockroachdb/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package cockroachdb
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -66,7 +67,7 @@ func (c *Collector) Configuration() any {
67
return c.Config
68
}
69
69
-func (c *Collector) Init() error {
70
+func (c *Collector) Init(context.Context) error {
71
if err := c.validateConfig(); err != nil {
72
return fmt.Errorf("error on validating config: %v", err)
73
}
@@ -85,7 +86,7 @@ func (c *Collector) Init() error {
86
return nil
87
}
88
88
-func (c *Collector) Check() error {
89
+func (c *Collector) Check(context.Context) error {
90
mx, err := c.collect()
91
if err != nil {
92
return err
@@ -101,7 +102,7 @@ func (c *Collector) Charts() *Charts {
102
return c.charts
103
}
104
104
-func (c *Collector) Collect() map[string]int64 {
105
+func (c *Collector) Collect(context.Context) map[string]int64 {
106
mx, err := c.collect()
107
if err != nil {
108
c.Error(err)
@@ -113,7 +114,7 @@ func (c *Collector) Collect() map[string]int64 {
114
return mx
115
}
116
116
-func (c *Collector) Cleanup() {
117
+func (c *Collector) Cleanup(context.Context) {
118
if c.prom != nil && c.prom.HTTPClient() != nil {
119
c.prom.HTTPClient().CloseIdleConnections()
120
}
src/go/plugin/go.d/collector/cockroachdb/collector_test.go
+18
-17
@@ -3,6 +3,7 @@
3
package cockroachdb
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -44,36 +45,36 @@ func TestNew(t *testing.T) {
45
func TestCollector_Init(t *testing.T) {
46
collr := prepareCockroachDB()
47
47
- assert.NoError(t, collr.Init())
48
+ assert.NoError(t, collr.Init(context.Background()))
49
}
50
51
func TestCollector_Init_ReturnsFalseIfConfigURLIsNotSet(t *testing.T) {
52
collr := prepareCockroachDB()
53
collr.URL = ""
54
54
- assert.Error(t, collr.Init())
55
+ assert.Error(t, collr.Init(context.Background()))
56
}
57
58
func TestCollector_Init_ReturnsFalseIfClientWrongTLSCA(t *testing.T) {
59
collr := prepareCockroachDB()
60
collr.ClientConfig.TLSConfig.TLSCA = "testdata/tls"
61
61
- assert.Error(t, collr.Init())
62
+ assert.Error(t, collr.Init(context.Background()))
63
}
64
65
func TestCollector_Check(t *testing.T) {
66
collr, srv := prepareClientServer(t)
67
defer srv.Close()
68
68
- assert.NoError(t, collr.Check())
69
+ assert.NoError(t, collr.Check(context.Background()))
70
}
71
72
func TestCollector_Check_ReturnsFalseIfConnectionRefused(t *testing.T) {
73
collr := New()
74
collr.URL = "http://127.0.0.1:38001/metrics"
74
- require.NoError(t, collr.Init())
75
+ require.NoError(t, collr.Init(context.Background()))
76
76
- assert.Error(t, collr.Check())
77
+ assert.Error(t, collr.Check(context.Background()))
78
}
79
80
func TestCollector_Charts(t *testing.T) {
@@ -81,7 +82,7 @@ func TestCollector_Charts(t *testing.T) {
82
}
83
84
func TestCollector_Cleanup(t *testing.T) {
84
- assert.NotPanics(t, New().Cleanup)
85
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
86
}
87
88
func TestCollector_Collect(t *testing.T) {
@@ -221,7 +222,7 @@ func TestCollector_Collect(t *testing.T) {
222
"valcount": 124081,
223
}
224
224
- mx := collr.Collect()
225
+ mx := collr.Collect(context.Background())
226
227
assert.Equal(t, expected, mx)
228
@@ -232,28 +233,28 @@ func TestCollector_Collect_ReturnsNilIfNotCockroachDBMetrics(t *testing.T) {
233
collr, srv := prepareClientServerNotCockroachDBMetricResponse(t)
234
defer srv.Close()
235
235
- assert.Nil(t, collr.Collect())
236
+ assert.Nil(t, collr.Collect(context.Background()))
237
}
238
239
func TestCollector_Collect_ReturnsNilIfConnectionRefused(t *testing.T) {
240
collr := prepareCockroachDB()
240
- require.NoError(t, collr.Init())
241
+ require.NoError(t, collr.Init(context.Background()))
242
242
- assert.Nil(t, collr.Collect())
243
+ assert.Nil(t, collr.Collect(context.Background()))
244
}
245
246
func TestCollector_Collect_ReturnsNilIfReceiveInvalidResponse(t *testing.T) {
247
collr, ts := prepareClientServerInvalidDataResponse(t)
248
defer ts.Close()
249
249
- assert.Nil(t, collr.Collect())
250
+ assert.Nil(t, collr.Collect(context.Background()))
251
}
252
253
func TestCollector_Collect_ReturnsNilIfReceiveResponse404(t *testing.T) {
254
collr, ts := prepareClientServerResponse404(t)
255
defer ts.Close()
256
256
- assert.Nil(t, collr.Collect())
257
+ assert.Nil(t, collr.Collect(context.Background()))
258
}
259
260
func prepareCockroachDB() *Collector {
@@ -271,7 +272,7 @@ func prepareClientServer(t *testing.T) (*Collector, *httptest.Server) {
272
273
collr := New()
274
collr.URL = ts.URL
274
- require.NoError(t, collr.Init())
275
+ require.NoError(t, collr.Init(context.Background()))
276
277
return collr, ts
278
}
@@ -285,7 +286,7 @@ func prepareClientServerNotCockroachDBMetricResponse(t *testing.T) (*Collector,
286
287
collr := New()
288
collr.URL = ts.URL
288
- require.NoError(t, collr.Init())
289
+ require.NoError(t, collr.Init(context.Background()))
290
291
return collr, ts
292
}
@@ -299,7 +300,7 @@ func prepareClientServerInvalidDataResponse(t *testing.T) (*Collector, *httptest
300
301
collr := New()
302
collr.URL = ts.URL
302
- require.NoError(t, collr.Init())
303
+ require.NoError(t, collr.Init(context.Background()))
304
305
return collr, ts
306
}
@@ -313,6 +314,6 @@ func prepareClientServerResponse404(t *testing.T) (*Collector, *httptest.Server)
314
315
collr := New()
316
collr.URL = ts.URL
316
- require.NoError(t, collr.Init())
317
+ require.NoError(t, collr.Init(context.Background()))
318
return collr, ts
319
}
src/go/plugin/go.d/collector/consul/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package consul
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -79,7 +80,7 @@ func (c *Collector) Configuration() any {
80
return c.Config
81
}
82
82
-func (c *Collector) Init() error {
83
+func (c *Collector) Init(context.Context) error {
84
if err := c.validateConfig(); err != nil {
85
return fmt.Errorf("config validation: %v", err)
86
}
@@ -99,7 +100,7 @@ func (c *Collector) Init() error {
100
return nil
101
}
102
102
-func (c *Collector) Check() error {
103
+func (c *Collector) Check(context.Context) error {
104
mx, err := c.collect()
105
if err != nil {
106
return err
@@ -115,7 +116,7 @@ func (c *Collector) Charts() *module.Charts {
116
return c.charts
117
}
118
118
-func (c *Collector) Collect() map[string]int64 {
119
+func (c *Collector) Collect(context.Context) map[string]int64 {
120
mx, err := c.collect()
121
if err != nil {
122
c.Error(err)
@@ -127,7 +128,7 @@ func (c *Collector) Collect() map[string]int64 {
128
return mx
129
}
130
130
-func (c *Collector) Cleanup() {
131
+func (c *Collector) Cleanup(context.Context) {
132
if c.httpClient != nil {
133
c.httpClient.CloseIdleConnections()
134
}
src/go/plugin/go.d/collector/consul/collector_test.go
+14
-13
@@ -3,6 +3,7 @@
3
package consul
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -88,9 +89,9 @@ func TestCollector_Init(t *testing.T) {
89
collr.Config = test.config
90
91
if test.wantFail {
91
- assert.Error(t, collr.Init())
92
+ assert.Error(t, collr.Init(context.Background()))
93
} else {
93
- assert.NoError(t, collr.Init())
94
+ assert.NoError(t, collr.Init(context.Background()))
95
}
96
})
97
}
@@ -141,9 +142,9 @@ func TestCollector_Check(t *testing.T) {
142
defer cleanup()
143
144
if test.wantFail {
144
- assert.Error(t, collr.Check())
145
+ assert.Error(t, collr.Check(context.Background()))
146
} else {
146
- assert.NoError(t, collr.Check())
147
+ assert.NoError(t, collr.Check(context.Background()))
148
}
149
})
150
}
@@ -535,7 +536,7 @@ func TestCollector_Collect(t *testing.T) {
536
collr, cleanup := test.prepare(t)
537
defer cleanup()
538
538
- mx := collr.Collect()
539
+ mx := collr.Collect(context.Background())
540
541
delete(mx, "autopilot_server_stable_time")
542
delete(test.wantMetrics, "autopilot_server_stable_time")
@@ -571,7 +572,7 @@ func caseConsulV1143CloudServerResponse(t *testing.T) (*Collector, func()) {
572
collr := New()
573
collr.URL = srv.URL
574
574
- require.NoError(t, collr.Init())
575
+ require.NoError(t, collr.Init(context.Background()))
576
577
return collr, srv.Close
578
}
@@ -599,7 +600,7 @@ func caseConsulV1132ServerResponse(t *testing.T) (*Collector, func()) {
600
collr := New()
601
collr.URL = srv.URL
602
602
- require.NoError(t, collr.Init())
603
+ require.NoError(t, collr.Init(context.Background()))
604
605
return collr, srv.Close
606
}
@@ -627,7 +628,7 @@ func caseConsulV1132ServerWithHostnameResponse(t *testing.T) (*Collector, func()
628
collr := New()
629
collr.URL = srv.URL
630
630
- require.NoError(t, collr.Init())
631
+ require.NoError(t, collr.Init(context.Background()))
632
633
return collr, srv.Close
634
}
@@ -653,7 +654,7 @@ func caseConsulV1132ServerWithDisabledPrometheus(t *testing.T) (*Collector, func
654
collr := New()
655
collr.URL = srv.URL
656
656
- require.NoError(t, collr.Init())
657
+ require.NoError(t, collr.Init(context.Background()))
658
659
return collr, srv.Close
660
}
@@ -677,7 +678,7 @@ func caseConsulV1132ClientResponse(t *testing.T) (*Collector, func()) {
678
collr := New()
679
collr.URL = srv.URL
680
680
- require.NoError(t, collr.Init())
681
+ require.NoError(t, collr.Init(context.Background()))
682
683
return collr, srv.Close
684
}
@@ -692,7 +693,7 @@ func caseInvalidDataResponse(t *testing.T) (*Collector, func()) {
693
collr := New()
694
collr.URL = srv.URL
695
695
- require.NoError(t, collr.Init())
696
+ require.NoError(t, collr.Init(context.Background()))
697
698
return collr, srv.Close
699
}
@@ -701,7 +702,7 @@ func caseConnectionRefused(t *testing.T) (*Collector, func()) {
702
t.Helper()
703
collr := New()
704
collr.URL = "http://127.0.0.1:65535/"
704
- require.NoError(t, collr.Init())
705
+ require.NoError(t, collr.Init(context.Background()))
706
707
return collr, func() {}
708
}
@@ -715,7 +716,7 @@ func case404(t *testing.T) (*Collector, func()) {
716
717
collr := New()
718
collr.URL = srv.URL
718
- require.NoError(t, collr.Init())
719
+ require.NoError(t, collr.Init(context.Background()))
720
721
return collr, srv.Close
722
}
src/go/plugin/go.d/collector/coredns/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package coredns
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -74,7 +75,7 @@ func (c *Collector) Configuration() any {
75
return c.Config
76
}
77
77
-func (c *Collector) Init() error {
78
+func (c *Collector) Init(context.Context) error {
79
if err := c.validateConfig(); err != nil {
80
return fmt.Errorf("config validation: %v", err)
81
}
@@ -104,7 +105,7 @@ func (c *Collector) Init() error {
105
return nil
106
}
107
107
-func (c *Collector) Check() error {
108
+func (c *Collector) Check(context.Context) error {
109
mx, err := c.collect()
110
if err != nil {
111
return err
@@ -120,7 +121,7 @@ func (c *Collector) Charts() *Charts {
121
return c.charts
122
}
123
123
-func (c *Collector) Collect() map[string]int64 {
124
+func (c *Collector) Collect(context.Context) map[string]int64 {
125
mx, err := c.collect()
126
127
if err != nil {
@@ -131,7 +132,7 @@ func (c *Collector) Collect() map[string]int64 {
132
return mx
133
}
134
134
-func (c *Collector) Cleanup() {
135
+func (c *Collector) Cleanup(context.Context) {
136
if c.prom != nil && c.prom.HTTPClient() != nil {
137
c.prom.HTTPClient().CloseIdleConnections()
138
}
src/go/plugin/go.d/collector/coredns/collector_test.go
+21
-20
@@ -3,6 +3,7 @@
3
package coredns
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -50,17 +51,17 @@ func TestCollector_Charts(t *testing.T) {
51
}
52
53
func TestCollector_Cleanup(t *testing.T) {
53
- New().Cleanup()
54
+ New().Cleanup(context.Background())
55
}
56
57
func TestCollector_Init(t *testing.T) {
57
- assert.NoError(t, New().Init())
58
+ assert.NoError(t, New().Init(context.Background()))
59
}
60
61
func TestCollector_InitNG(t *testing.T) {
62
collr := New()
63
collr.URL = ""
63
- assert.Error(t, collr.Init())
64
+ assert.Error(t, collr.Init(context.Background()))
65
}
66
67
func TestCollector_Check(t *testing.T) {
@@ -83,8 +84,8 @@ func TestCollector_Check(t *testing.T) {
84
85
collr := New()
86
collr.URL = ts.URL + "/metrics"
86
- require.NoError(t, collr.Init())
87
- assert.NoError(t, collr.Check())
87
+ require.NoError(t, collr.Init(context.Background()))
88
+ assert.NoError(t, collr.Check(context.Background()))
89
})
90
}
91
}
@@ -92,8 +93,8 @@ func TestCollector_Check(t *testing.T) {
93
func TestCollector_CheckNG(t *testing.T) {
94
collr := New()
95
collr.URL = "http://127.0.0.1:38001/metrics"
95
- require.NoError(t, collr.Init())
96
- assert.Error(t, collr.Check())
96
+ require.NoError(t, collr.Init(context.Background()))
97
+ assert.Error(t, collr.Check(context.Background()))
98
}
99
100
func TestCollector_Collect(t *testing.T) {
@@ -118,8 +119,8 @@ func TestCollector_Collect(t *testing.T) {
119
collr.URL = ts.URL + "/metrics"
120
collr.PerServerStats.Includes = []string{"glob:*"}
121
collr.PerZoneStats.Includes = []string{"glob:*"}
121
- require.NoError(t, collr.Init())
122
- require.NoError(t, collr.Check())
122
+ require.NoError(t, collr.Init(context.Background()))
123
+ require.NoError(t, collr.Check(context.Background()))
124
125
expected := map[string]int64{
126
"coredns.io._request_per_ip_family_v4": 19,
@@ -441,7 +442,7 @@ func TestCollector_Collect(t *testing.T) {
442
"ya.ru._response_total": 21,
443
}
444
444
- assert.Equal(t, expected, collr.Collect())
445
+ assert.Equal(t, expected, collr.Collect(context.Background()))
446
})
447
}
448
}
@@ -467,8 +468,8 @@ func TestCollector_CollectNoLoad(t *testing.T) {
468
collr.URL = ts.URL + "/metrics"
469
collr.PerServerStats.Includes = []string{"glob:*"}
470
collr.PerZoneStats.Includes = []string{"glob:*"}
470
- require.NoError(t, collr.Init())
471
- require.NoError(t, collr.Check())
471
+ require.NoError(t, collr.Init(context.Background()))
472
+ require.NoError(t, collr.Check(context.Background()))
473
474
expected := map[string]int64{
475
"no_matching_zone_dropped_total": 0,
@@ -520,7 +521,7 @@ func TestCollector_CollectNoLoad(t *testing.T) {
521
"response_total": 0,
522
}
523
523
- assert.Equal(t, expected, collr.Collect())
524
+ assert.Equal(t, expected, collr.Collect(context.Background()))
525
})
526
}
527
@@ -536,8 +537,8 @@ func TestCollector_InvalidData(t *testing.T) {
537
538
collr := New()
539
collr.URL = ts.URL + "/metrics"
539
- require.NoError(t, collr.Init())
540
- assert.Error(t, collr.Check())
540
+ require.NoError(t, collr.Init(context.Background()))
541
+ assert.Error(t, collr.Check(context.Background()))
542
}
543
544
func TestCollector_404(t *testing.T) {
@@ -550,8 +551,8 @@ func TestCollector_404(t *testing.T) {
551
552
collr := New()
553
collr.URL = ts.URL + "/metrics"
553
- require.NoError(t, collr.Init())
554
- assert.Error(t, collr.Check())
554
+ require.NoError(t, collr.Init(context.Background()))
555
+ assert.Error(t, collr.Check(context.Background()))
556
}
557
558
func TestCollector_CollectNoVersion(t *testing.T) {
@@ -566,8 +567,8 @@ func TestCollector_CollectNoVersion(t *testing.T) {
567
collr.URL = ts.URL + "/metrics"
568
collr.PerServerStats.Includes = []string{"glob:*"}
569
collr.PerZoneStats.Includes = []string{"glob:*"}
569
- require.NoError(t, collr.Init())
570
- require.Error(t, collr.Check())
570
+ require.NoError(t, collr.Init(context.Background()))
571
+ require.Error(t, collr.Check(context.Background()))
572
572
- assert.Nil(t, collr.Collect())
573
+ assert.Nil(t, collr.Collect(context.Background()))
574
}
src/go/plugin/go.d/collector/couchbase/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package couchbase
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -63,7 +64,7 @@ func (c *Collector) Configuration() any {
64
return c.Config
65
}
66
66
-func (c *Collector) Init() error {
67
+func (c *Collector) Init(context.Context) error {
68
err := c.validateConfig()
69
if err != nil {
70
return fmt.Errorf("check configuration: %v", err)
@@ -84,7 +85,7 @@ func (c *Collector) Init() error {
85
return nil
86
}
87
87
-func (c *Collector) Check() error {
88
+func (c *Collector) Check(context.Context) error {
89
mx, err := c.collect()
90
if err != nil {
91
return err
@@ -100,7 +101,7 @@ func (c *Collector) Charts() *Charts {
101
return c.charts
102
}
103
103
-func (c *Collector) Collect() map[string]int64 {
104
+func (c *Collector) Collect(context.Context) map[string]int64 {
105
mx, err := c.collect()
106
if err != nil {
107
c.Error(err)
@@ -112,7 +113,7 @@ func (c *Collector) Collect() map[string]int64 {
113
return mx
114
}
115
115
-func (c *Collector) Cleanup() {
116
+func (c *Collector) Cleanup(context.Context) {
117
if c.httpClient == nil {
118
return
119
}
src/go/plugin/go.d/collector/couchbase/collector_test.go
+10
-9
@@ -3,6 +3,7 @@
3
package couchbase
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -72,9 +73,9 @@ func TestCollector_Init(t *testing.T) {
73
collr.Config = test.config
74
75
if test.wantFail {
75
- assert.Error(t, collr.Init())
76
+ assert.Error(t, collr.Init(context.Background()))
77
} else {
77
- assert.NoError(t, collr.Init())
78
+ assert.NoError(t, collr.Init(context.Background()))
79
}
80
})
81
}
@@ -108,9 +109,9 @@ func TestCollector_Check(t *testing.T) {
109
defer cleanup()
110
111
if test.wantFail {
111
- assert.Error(t, collr.Check())
112
+ assert.Error(t, collr.Check(context.Background()))
113
} else {
113
- assert.NoError(t, collr.Check())
114
+ assert.NoError(t, collr.Check(context.Background()))
115
}
116
})
117
}
@@ -166,7 +167,7 @@ func TestCollector_Collect(t *testing.T) {
167
collr, cleanup := test.prepare(t)
168
defer cleanup()
169
169
- mx := collr.Collect()
170
+ mx := collr.Collect(context.Background())
171
172
assert.Equal(t, test.wantCollected, mx)
173
module.TestMetricsHasAllChartsDims(t, collr.Charts(), mx)
@@ -183,7 +184,7 @@ func prepareCouchbaseV660(t *testing.T) (collr *Collector, cleanup func()) {
184
185
collr = New()
186
collr.URL = srv.URL
186
- require.NoError(t, collr.Init())
187
+ require.NoError(t, collr.Init(context.Background()))
188
189
return collr, srv.Close
190
}
@@ -196,7 +197,7 @@ func prepareCouchbaseInvalidData(t *testing.T) (*Collector, func()) {
197
}))
198
collr := New()
199
collr.URL = srv.URL
199
- require.NoError(t, collr.Init())
200
+ require.NoError(t, collr.Init(context.Background()))
201
202
return collr, srv.Close
203
}
@@ -209,7 +210,7 @@ func prepareCouchbase404(t *testing.T) (*Collector, func()) {
210
}))
211
collr := New()
212
collr.URL = srv.URL
212
- require.NoError(t, collr.Init())
213
+ require.NoError(t, collr.Init(context.Background()))
214
215
return collr, srv.Close
216
}
@@ -218,7 +219,7 @@ func prepareCouchbaseConnectionRefused(t *testing.T) (*Collector, func()) {
219
t.Helper()
220
collr := New()
221
collr.URL = "http://127.0.0.1:38001"
221
- require.NoError(t, collr.Init())
222
+ require.NoError(t, collr.Init(context.Background()))
223
224
return collr, func() {}
225
}
src/go/plugin/go.d/collector/couchdb/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package couchdb
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -67,7 +68,7 @@ func (c *Collector) Configuration() any {
68
return c.Config
69
}
70
70
-func (c *Collector) Init() error {
71
+func (c *Collector) Init(context.Context) error {
72
err := c.validateConfig()
73
if err != nil {
74
return fmt.Errorf("check configuration: %v", err)
@@ -90,7 +91,7 @@ func (c *Collector) Init() error {
91
return nil
92
}
93
93
-func (c *Collector) Check() error {
94
+func (c *Collector) Check(context.Context) error {
95
if err := c.pingCouchDB(); err != nil {
96
return err
97
}
@@ -111,7 +112,7 @@ func (c *Collector) Charts() *Charts {
112
return c.charts
113
}
114
114
-func (c *Collector) Collect() map[string]int64 {
115
+func (c *Collector) Collect(context.Context) map[string]int64 {
116
mx, err := c.collect()
117
if err != nil {
118
c.Error(err)
@@ -123,7 +124,7 @@ func (c *Collector) Collect() map[string]int64 {
124
return mx
125
}
126
126
-func (c *Collector) Cleanup() {
127
+func (c *Collector) Cleanup(context.Context) {
128
if c.httpClient == nil {
129
return
130
}
src/go/plugin/go.d/collector/couchdb/collector_test.go
+11
-10
@@ -3,6 +3,7 @@
3
package couchdb
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -84,9 +85,9 @@ func TestCollector_Init(t *testing.T) {
85
collr.Config = test.config
86
87
if test.wantFail {
87
- assert.Error(t, collr.Init())
88
+ assert.Error(t, collr.Init(context.Background()))
89
} else {
89
- assert.NoError(t, collr.Init())
90
+ assert.NoError(t, collr.Init(context.Background()))
91
assert.Equal(t, test.wantNumOfCharts, len(*collr.Charts()))
92
}
93
})
@@ -110,9 +111,9 @@ func TestCollector_Check(t *testing.T) {
111
defer cleanup()
112
113
if test.wantFail {
113
- assert.Error(t, collr.Check())
114
+ assert.Error(t, collr.Check(context.Background()))
115
} else {
115
- assert.NoError(t, collr.Check())
116
+ assert.NoError(t, collr.Check(context.Background()))
117
}
118
})
119
}
@@ -123,7 +124,7 @@ func TestCollector_Charts(t *testing.T) {
124
}
125
126
func TestCollector_Cleanup(t *testing.T) {
126
- assert.NotPanics(t, New().Cleanup)
127
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
128
}
129
130
func TestCollector_Collect(t *testing.T) {
@@ -359,7 +360,7 @@ func TestCollector_Collect(t *testing.T) {
360
361
var mx map[string]int64
362
for i := 0; i < 10; i++ {
362
- mx = collr.Collect()
363
+ mx = collr.Collect(context.Background())
364
}
365
366
assert.Equal(t, test.wantCollected, mx)
@@ -376,7 +377,7 @@ func prepareCouchDB(t *testing.T, createCDB func() *Collector) (collr *Collector
377
srv := prepareCouchDBEndpoint()
378
collr.URL = srv.URL
379
379
- require.NoError(t, collr.Init())
380
+ require.NoError(t, collr.Init(context.Background()))
381
382
return collr, srv.Close
383
}
@@ -393,7 +394,7 @@ func prepareCouchDBInvalidData(t *testing.T) (*Collector, func()) {
394
}))
395
collr := New()
396
collr.URL = srv.URL
396
- require.NoError(t, collr.Init())
397
+ require.NoError(t, collr.Init(context.Background()))
398
399
return collr, srv.Close
400
}
@@ -406,7 +407,7 @@ func prepareCouchDB404(t *testing.T) (*Collector, func()) {
407
}))
408
collr := New()
409
collr.URL = srv.URL
409
- require.NoError(t, collr.Init())
410
+ require.NoError(t, collr.Init(context.Background()))
411
412
return collr, srv.Close
413
}
@@ -415,7 +416,7 @@ func prepareCouchDBConnectionRefused(t *testing.T) (*Collector, func()) {
416
t.Helper()
417
collr := New()
418
collr.URL = "http://127.0.0.1:38001"
418
- require.NoError(t, collr.Init())
419
+ require.NoError(t, collr.Init(context.Background()))
420
421
return collr, func() {}
422
}
src/go/plugin/go.d/collector/dmcache/collector.go
+5
-4
@@ -5,6 +5,7 @@
5
package dmcache
6
7
import (
8
+ "context"
9
_ "embed"
10
"errors"
11
"fmt"
@@ -58,7 +59,7 @@ func (c *Collector) Configuration() any {
59
return c.Config
60
}
61
61
-func (c *Collector) Init() error {
62
+func (c *Collector) Init(context.Context) error {
63
dmsetup, err := c.initDmsetupCLI()
64
if err != nil {
65
return fmt.Errorf("dmsetup exec initialization: %v", err)
@@ -68,7 +69,7 @@ func (c *Collector) Init() error {
69
return nil
70
}
71
71
-func (c *Collector) Check() error {
72
+func (c *Collector) Check(context.Context) error {
73
mx, err := c.collect()
74
if err != nil {
75
return err
@@ -85,7 +86,7 @@ func (c *Collector) Charts() *module.Charts {
86
return c.charts
87
}
88
88
-func (c *Collector) Collect() map[string]int64 {
89
+func (c *Collector) Collect(context.Context) map[string]int64 {
90
mx, err := c.collect()
91
if err != nil {
92
c.Error(err)
@@ -98,4 +99,4 @@ func (c *Collector) Collect() map[string]int64 {
99
return mx
100
}
101
101
-func (c *Collector) Cleanup() {}
102
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/dmcache/collector_test.go
+9
-8
@@ -5,6 +5,7 @@
5
package dmcache
6
7
import (
8
+ "context"
9
"errors"
10
"os"
11
"testing"
@@ -51,9 +52,9 @@ func TestCollector_Init(t *testing.T) {
52
collr.Config = test.config
53
54
if test.wantFail {
54
- assert.Error(t, collr.Init())
55
+ assert.Error(t, collr.Init(context.Background()))
56
} else {
56
- assert.NoError(t, collr.Init())
57
+ assert.NoError(t, collr.Init(context.Background()))
58
}
59
})
60
}
@@ -72,7 +73,7 @@ func TestCollector_Cleanup(t *testing.T) {
73
prepare: func() *Collector {
74
collr := New()
75
collr.exec = prepareMockOK()
75
- _ = collr.Check()
76
+ _ = collr.Check(context.Background())
77
return collr
78
},
79
},
@@ -80,7 +81,7 @@ func TestCollector_Cleanup(t *testing.T) {
81
prepare: func() *Collector {
82
collr := New()
83
collr.exec = prepareMockOK()
83
- _ = collr.Collect()
84
+ _ = collr.Collect(context.Background())
85
return collr
86
},
87
},
@@ -90,7 +91,7 @@ func TestCollector_Cleanup(t *testing.T) {
91
t.Run(name, func(t *testing.T) {
92
collr := test.prepare()
93
93
- assert.NotPanics(t, collr.Cleanup)
94
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
95
})
96
}
97
}
@@ -129,9 +130,9 @@ func TestCollector_Check(t *testing.T) {
130
collr.exec = mock
131
132
if test.wantFail {
132
- assert.Error(t, collr.Check())
133
+ assert.Error(t, collr.Check(context.Background()))
134
} else {
134
- assert.NoError(t, collr.Check())
135
+ assert.NoError(t, collr.Check(context.Background()))
136
}
137
})
138
}
@@ -191,7 +192,7 @@ func TestLVM_Collect(t *testing.T) {
192
mock := test.prepareMock()
193
collr.exec = mock
194
194
- mx := collr.Collect()
195
+ mx := collr.Collect(context.Background())
196
197
assert.Equal(t, test.wantMetrics, mx)
198
src/go/plugin/go.d/collector/dnsdist/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package dnsdist
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -61,7 +62,7 @@ func (c *Collector) Configuration() any {
62
return c.Config
63
}
64
64
-func (c *Collector) Init() error {
65
+func (c *Collector) Init(context.Context) error {
66
err := c.validateConfig()
67
if err != nil {
68
return fmt.Errorf("config validation: %v", err)
@@ -82,7 +83,7 @@ func (c *Collector) Init() error {
83
return nil
84
}
85
85
-func (c *Collector) Check() error {
86
+func (c *Collector) Check(context.Context) error {
87
mx, err := c.collect()
88
if err != nil {
89
return err
@@ -98,7 +99,7 @@ func (c *Collector) Charts() *module.Charts {
99
return c.charts
100
}
101
101
-func (c *Collector) Collect() map[string]int64 {
102
+func (c *Collector) Collect(context.Context) map[string]int64 {
103
ms, err := c.collect()
104
if err != nil {
105
c.Error(err)
@@ -111,7 +112,7 @@ func (c *Collector) Collect() map[string]int64 {
112
return ms
113
}
114
114
-func (c *Collector) Cleanup() {
115
+func (c *Collector) Cleanup(context.Context) {
116
if c.httpClient == nil {
117
return
118
}
src/go/plugin/go.d/collector/dnsdist/collector_test.go
+10
-9
@@ -3,6 +3,7 @@
3
package dnsdist
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -74,9 +75,9 @@ func TestCollector_Init(t *testing.T) {
75
collr.Config = test.config
76
77
if test.wantFail {
77
- assert.Error(t, collr.Init())
78
+ assert.Error(t, collr.Init(context.Background()))
79
} else {
79
- assert.NoError(t, collr.Init())
80
+ assert.NoError(t, collr.Init(context.Background()))
81
}
82
})
83
}
@@ -84,12 +85,12 @@ func TestCollector_Init(t *testing.T) {
85
86
func TestCollector_Charts(t *testing.T) {
87
collr := New()
87
- require.NoError(t, collr.Init())
88
+ require.NoError(t, collr.Init(context.Background()))
89
assert.NotNil(t, collr.Charts())
90
}
91
92
func TestCollector_Cleanup(t *testing.T) {
92
- assert.NotPanics(t, New().Cleanup)
93
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
94
}
95
96
func TestCollector_Check(t *testing.T) {
@@ -119,12 +120,12 @@ func TestCollector_Check(t *testing.T) {
120
t.Run(name, func(t *testing.T) {
121
collr, cleanup := test.prepare()
122
defer cleanup()
122
- require.NoError(t, collr.Init())
123
+ require.NoError(t, collr.Init(context.Background()))
124
125
if test.wantFail {
125
- assert.Error(t, collr.Check())
126
+ assert.Error(t, collr.Check(context.Background()))
127
} else {
127
- assert.NoError(t, collr.Check())
128
+ assert.NoError(t, collr.Check(context.Background()))
129
}
130
})
131
}
@@ -187,9 +188,9 @@ func TestCollector_Collect(t *testing.T) {
188
t.Run(name, func(t *testing.T) {
189
collr, cleanup := test.prepare()
190
defer cleanup()
190
- require.NoError(t, collr.Init())
191
+ require.NoError(t, collr.Init(context.Background()))
192
192
- mx := collr.Collect()
193
+ mx := collr.Collect(context.Background())
194
195
assert.Equal(t, test.wantCollected, mx)
196
if len(test.wantCollected) > 0 {
src/go/plugin/go.d/collector/dnsmasq/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package dnsmasq
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -68,7 +69,7 @@ func (c *Collector) Configuration() any {
69
return c.Config
70
}
71
71
-func (c *Collector) Init() error {
72
+func (c *Collector) Init(context.Context) error {
73
err := c.validateConfig()
74
if err != nil {
75
return fmt.Errorf("config validation: %v", err)
@@ -89,7 +90,7 @@ func (c *Collector) Init() error {
90
return nil
91
}
92
92
-func (c *Collector) Check() error {
93
+func (c *Collector) Check(context.Context) error {
94
mx, err := c.collect()
95
if err != nil {
96
return err
@@ -105,7 +106,7 @@ func (c *Collector) Charts() *module.Charts {
106
return c.charts
107
}
108
108
-func (c *Collector) Collect() map[string]int64 {
109
+func (c *Collector) Collect(context.Context) map[string]int64 {
110
ms, err := c.collect()
111
if err != nil {
112
c.Error(err)
@@ -117,4 +118,4 @@ func (c *Collector) Collect() map[string]int64 {
118
return ms
119
}
120
120
-func (c *Collector) Cleanup() {}
121
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/dnsmasq/collector_test.go
+10
-9
@@ -3,6 +3,7 @@
3
package dnsmasq
4
5
import (
6
+ "context"
7
"errors"
8
"fmt"
9
"os"
@@ -71,9 +72,9 @@ func TestCollector_Init(t *testing.T) {
72
collr.Config = test.config
73
74
if test.wantFail {
74
- assert.Error(t, collr.Init())
75
+ assert.Error(t, collr.Init(context.Background()))
76
} else {
76
- assert.NoError(t, collr.Init())
77
+ assert.NoError(t, collr.Init(context.Background()))
78
}
79
})
80
}
@@ -100,12 +101,12 @@ func TestCollector_Check(t *testing.T) {
101
for name, test := range tests {
102
t.Run(name, func(t *testing.T) {
103
collr := test.prepare()
103
- require.NoError(t, collr.Init())
104
+ require.NoError(t, collr.Init(context.Background()))
105
106
if test.wantFail {
106
- assert.Error(t, collr.Check())
107
+ assert.Error(t, collr.Check(context.Background()))
108
} else {
108
- assert.NoError(t, collr.Check())
109
+ assert.NoError(t, collr.Check(context.Background()))
110
}
111
})
112
}
@@ -113,12 +114,12 @@ func TestCollector_Check(t *testing.T) {
114
115
func TestCollector_Charts(t *testing.T) {
116
collr := New()
116
- require.NoError(t, collr.Init())
117
+ require.NoError(t, collr.Init(context.Background()))
118
assert.NotNil(t, collr.Charts())
119
}
120
121
func TestCollector_Cleanup(t *testing.T) {
121
- assert.NotPanics(t, New().Cleanup)
122
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
123
}
124
125
func TestCollector_Collect(t *testing.T) {
@@ -150,9 +151,9 @@ func TestCollector_Collect(t *testing.T) {
151
for name, test := range tests {
152
t.Run(name, func(t *testing.T) {
153
collr := test.prepare()
153
- require.NoError(t, collr.Init())
154
+ require.NoError(t, collr.Init(context.Background()))
155
155
- mx := collr.Collect()
156
+ mx := collr.Collect(context.Background())
157
158
assert.Equal(t, test.wantCollected, mx)
159
if len(test.wantCollected) > 0 {
src/go/plugin/go.d/collector/dnsmasq_dhcp/collector.go
+5
-4
@@ -5,6 +5,7 @@
5
package dnsmasq_dhcp
6
7
import (
8
+ "context"
9
_ "embed"
10
"errors"
11
"fmt"
@@ -68,7 +69,7 @@ func (c *Collector) Configuration() any {
69
return c.Config
70
}
71
71
-func (c *Collector) Init() error {
72
+func (c *Collector) Init(context.Context) error {
73
if err := c.validateConfig(); err != nil {
74
return fmt.Errorf("config validation: %v", err)
75
}
@@ -79,7 +80,7 @@ func (c *Collector) Init() error {
80
return nil
81
}
82
82
-func (c *Collector) Check() error {
83
+func (c *Collector) Check(context.Context) error {
84
mx, err := c.collect()
85
if err != nil {
86
return err
@@ -95,7 +96,7 @@ func (c *Collector) Charts() *module.Charts {
96
return c.charts
97
}
98
98
-func (c *Collector) Collect() map[string]int64 {
99
+func (c *Collector) Collect(context.Context) map[string]int64 {
100
mx, err := c.collect()
101
if err != nil {
102
c.Error(err)
@@ -108,4 +109,4 @@ func (c *Collector) Collect() map[string]int64 {
109
return mx
110
}
111
111
-func (c *Collector) Cleanup() {}
112
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/dnsmasq_dhcp/collector_test.go
+15
-14
@@ -5,6 +5,7 @@
5
package dnsmasq_dhcp
6
7
import (
8
+ "context"
9
"os"
10
"testing"
11
@@ -44,14 +45,14 @@ func TestCollector_Init(t *testing.T) {
45
collr.ConfPath = testConfPath
46
collr.ConfDir = testConfDir
47
47
- assert.NoError(t, collr.Init())
48
+ assert.NoError(t, collr.Init(context.Background()))
49
}
50
51
func TestCollector_InitEmptyLeasesPath(t *testing.T) {
52
collr := New()
53
collr.LeasesPath = ""
54
54
- assert.Error(t, collr.Init())
55
+ assert.Error(t, collr.Init(context.Background()))
56
}
57
58
func TestCollector_InitInvalidLeasesPath(t *testing.T) {
@@ -59,7 +60,7 @@ func TestCollector_InitInvalidLeasesPath(t *testing.T) {
60
collr.LeasesPath = testLeasesPath
61
collr.LeasesPath += "!"
62
62
- assert.Error(t, collr.Init())
63
+ assert.Error(t, collr.Init(context.Background()))
64
}
65
66
func TestCollector_InitZeroDHCPRanges(t *testing.T) {
@@ -68,7 +69,7 @@ func TestCollector_InitZeroDHCPRanges(t *testing.T) {
69
collr.ConfPath = "testdata/dnsmasq3.conf"
70
collr.ConfDir = ""
71
71
- assert.NoError(t, collr.Init())
72
+ assert.NoError(t, collr.Init(context.Background()))
73
}
74
75
func TestCollector_Check(t *testing.T) {
@@ -77,8 +78,8 @@ func TestCollector_Check(t *testing.T) {
78
collr.ConfPath = testConfPath
79
collr.ConfDir = testConfDir
80
80
- require.NoError(t, collr.Init())
81
- assert.NoError(t, collr.Check())
81
+ require.NoError(t, collr.Init(context.Background()))
82
+ assert.NoError(t, collr.Check(context.Background()))
83
}
84
85
func TestCollector_Charts(t *testing.T) {
@@ -87,13 +88,13 @@ func TestCollector_Charts(t *testing.T) {
88
collr.ConfPath = testConfPath
89
collr.ConfDir = testConfDir
90
90
- require.NoError(t, collr.Init())
91
+ require.NoError(t, collr.Init(context.Background()))
92
93
assert.NotNil(t, collr.Charts())
94
}
95
96
func TestCollector_Cleanup(t *testing.T) {
96
- assert.NotPanics(t, New().Cleanup)
97
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
98
}
99
100
func TestCollector_Collect(t *testing.T) {
@@ -102,8 +103,8 @@ func TestCollector_Collect(t *testing.T) {
103
collr.ConfPath = testConfPath
104
collr.ConfDir = testConfDir
105
105
- require.NoError(t, collr.Init())
106
- require.NoError(t, collr.Check())
106
+ require.NoError(t, collr.Init(context.Background()))
107
+ require.NoError(t, collr.Check(context.Background()))
108
109
expected := map[string]int64{
110
"dhcp_range_1230::1-1230::64_allocated_leases": 7,
@@ -134,7 +135,7 @@ func TestCollector_Collect(t *testing.T) {
135
"ipv6_dhcp_ranges": 5,
136
}
137
137
- assert.Equal(t, expected, collr.Collect())
138
+ assert.Equal(t, expected, collr.Collect(context.Background()))
139
}
140
141
func TestCollector_CollectFailedToOpenLeasesPath(t *testing.T) {
@@ -143,11 +144,11 @@ func TestCollector_CollectFailedToOpenLeasesPath(t *testing.T) {
144
collr.ConfPath = testConfPath
145
collr.ConfDir = testConfDir
146
146
- require.NoError(t, collr.Init())
147
- require.NoError(t, collr.Check())
147
+ require.NoError(t, collr.Init(context.Background()))
148
+ require.NoError(t, collr.Check(context.Background()))
149
150
collr.LeasesPath = ""
150
- assert.Nil(t, collr.Collect())
151
+ assert.Nil(t, collr.Collect(context.Background()))
152
}
153
154
func TestCollector_parseDHCPRangeValue(t *testing.T) {
src/go/plugin/go.d/collector/dnsquery/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package dnsquery
4
5
import (
6
+ "context"
7
_ "embed"
8
"fmt"
9
"time"
@@ -76,7 +77,7 @@ func (c *Collector) Configuration() any {
77
return c.Config
78
}
79
79
-func (c *Collector) Init() error {
80
+func (c *Collector) Init(context.Context) error {
81
if err := c.verifyConfig(); err != nil {
82
return fmt.Errorf("config validation: %v", err)
83
}
@@ -96,7 +97,7 @@ func (c *Collector) Init() error {
97
return nil
98
}
99
99
-func (c *Collector) Check() error {
100
+func (c *Collector) Check(context.Context) error {
101
return nil
102
}
103
@@ -104,7 +105,7 @@ func (c *Collector) Charts() *module.Charts {
105
return c.charts
106
}
107
107
-func (c *Collector) Collect() map[string]int64 {
108
+func (c *Collector) Collect(context.Context) map[string]int64 {
109
mx, err := c.collect()
110
if err != nil {
111
c.Error(err)
@@ -116,4 +117,4 @@ func (c *Collector) Collect() map[string]int64 {
117
return mx
118
}
119
119
-func (c *Collector) Cleanup() {}
120
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/dnsquery/collector_test.go
+9
-8
@@ -3,6 +3,7 @@
3
package dnsquery
4
5
import (
6
+ "context"
7
"errors"
8
"os"
9
"testing"
@@ -117,9 +118,9 @@ func TestCollector_Init(t *testing.T) {
118
collr.Config = test.config
119
120
if test.wantFail {
120
- assert.Error(t, collr.Init())
121
+ assert.Error(t, collr.Init(context.Background()))
122
} else {
122
- assert.NoError(t, collr.Init())
123
+ assert.NoError(t, collr.Init(context.Background()))
124
}
125
})
126
}
@@ -144,12 +145,12 @@ func TestCollector_Check(t *testing.T) {
145
t.Run(name, func(t *testing.T) {
146
collr := test.prepare()
147
147
- require.NoError(t, collr.Init())
148
+ require.NoError(t, collr.Init(context.Background()))
149
150
if test.wantFail {
150
- assert.Error(t, collr.Check())
151
+ assert.Error(t, collr.Check(context.Background()))
152
} else {
152
- assert.NoError(t, collr.Check())
153
+ assert.NoError(t, collr.Check(context.Background()))
154
}
155
})
156
}
@@ -160,7 +161,7 @@ func TestCollector_Charts(t *testing.T) {
161
162
collr.Domains = []string{"google.com"}
163
collr.Servers = []string{"192.0.2.0", "192.0.2.1"}
163
- require.NoError(t, collr.Init())
164
+ require.NoError(t, collr.Init(context.Background()))
165
166
assert.NotNil(t, collr.Charts())
167
assert.Len(t, *collr.Charts(), len(dnsChartsTmpl)*len(collr.Servers))
@@ -201,9 +202,9 @@ func TestCollector_Collect(t *testing.T) {
202
t.Run(name, func(t *testing.T) {
203
collr := test.prepare()
204
204
- require.NoError(t, collr.Init())
205
+ require.NoError(t, collr.Init(context.Background()))
206
206
- mx := collr.Collect()
207
+ mx := collr.Collect(context.Background())
208
209
require.Equal(t, test.wantMetrics, mx)
210
})
src/go/plugin/go.d/collector/docker/collector.go
+4
-4
@@ -79,7 +79,7 @@ func (c *Collector) Configuration() any {
79
return c.Config
80
}
81
82
-func (c *Collector) Init() error {
82
+func (c *Collector) Init(context.Context) error {
83
if addr := dockerhost.FromEnv(); addr != "" && c.Address == docker.DefaultDockerHost {
84
c.Infof("using docker host from environment: %s ", addr)
85
c.Address = addr
@@ -87,7 +87,7 @@ func (c *Collector) Init() error {
87
return nil
88
}
89
90
-func (c *Collector) Check() error {
90
+func (c *Collector) Check(context.Context) error {
91
mx, err := c.collect()
92
if err != nil {
93
return err
@@ -103,7 +103,7 @@ func (c *Collector) Charts() *module.Charts {
103
return c.charts
104
}
105
106
-func (c *Collector) Collect() map[string]int64 {
106
+func (c *Collector) Collect(context.Context) map[string]int64 {
107
mx, err := c.collect()
108
if err != nil {
109
c.Error(err)
@@ -115,7 +115,7 @@ func (c *Collector) Collect() map[string]int64 {
115
return mx
116
}
117
118
-func (c *Collector) Cleanup() {
118
+func (c *Collector) Cleanup(context.Context) {
119
if c.client == nil {
120
return
121
}
src/go/plugin/go.d/collector/docker/collector_test.go
+11
-11
@@ -59,9 +59,9 @@ func TestCollector_Init(t *testing.T) {
59
collr.Config = test.config
60
61
if test.wantFail {
62
- assert.Error(t, collr.Init())
62
+ assert.Error(t, collr.Init(context.Background()))
63
} else {
64
- assert.NoError(t, collr.Init())
64
+ assert.NoError(t, collr.Init(context.Background()))
65
}
66
})
67
}
@@ -82,15 +82,15 @@ func TestCollector_Cleanup(t *testing.T) {
82
},
83
"after Init": {
84
wantClose: false,
85
- prepare: func(c *Collector) { _ = c.Init() },
85
+ prepare: func(c *Collector) { _ = c.Init(context.Background()) },
86
},
87
"after Check": {
88
wantClose: true,
89
- prepare: func(c *Collector) { _ = c.Init(); _ = c.Check() },
89
+ prepare: func(c *Collector) { _ = c.Init(context.Background()); _ = c.Check(context.Background()) },
90
},
91
"after Collect": {
92
wantClose: true,
93
- prepare: func(c *Collector) { _ = c.Init(); c.Collect() },
93
+ prepare: func(c *Collector) { _ = c.Init(context.Background()); c.Collect(context.Background()) },
94
},
95
}
96
@@ -102,7 +102,7 @@ func TestCollector_Cleanup(t *testing.T) {
102
103
test.prepare(collr)
104
105
- require.NotPanics(t, collr.Cleanup)
105
+ require.NotPanics(t, func() { collr.Cleanup(context.Background()) })
106
107
if test.wantClose {
108
assert.True(t, m.closeCalled)
@@ -160,12 +160,12 @@ func TestCollector_Check(t *testing.T) {
160
t.Run(name, func(t *testing.T) {
161
collr := test.prepare()
162
163
- require.NoError(t, collr.Init())
163
+ require.NoError(t, collr.Init(context.Background()))
164
165
if test.wantFail {
166
- assert.Error(t, collr.Check())
166
+ assert.Error(t, collr.Check(context.Background()))
167
} else {
168
- assert.NoError(t, collr.Check())
168
+ assert.NoError(t, collr.Check(context.Background()))
169
}
170
})
171
}
@@ -678,9 +678,9 @@ func TestCollector_Collect(t *testing.T) {
678
t.Run(name, func(t *testing.T) {
679
collr := test.prepare()
680
681
- require.NoError(t, collr.Init())
681
+ require.NoError(t, collr.Init(context.Background()))
682
683
- mx := collr.Collect()
683
+ mx := collr.Collect(context.Background())
684
685
require.Equal(t, test.expected, mx)
686
src/go/plugin/go.d/collector/docker_engine/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package docker_engine
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -59,7 +60,7 @@ func (c *Collector) Configuration() any {
60
return c.Config
61
}
62
62
-func (c *Collector) Init() error {
63
+func (c *Collector) Init(context.Context) error {
64
if err := c.validateConfig(); err != nil {
65
return fmt.Errorf("config validation: %v", err)
66
}
@@ -73,7 +74,7 @@ func (c *Collector) Init() error {
74
return nil
75
}
76
76
-func (c *Collector) Check() error {
77
+func (c *Collector) Check(context.Context) error {
78
mx, err := c.collect()
79
if err != nil {
80
return err
@@ -104,7 +105,7 @@ func (c *Collector) Charts() *Charts {
105
return cs
106
}
107
107
-func (c *Collector) Collect() map[string]int64 {
108
+func (c *Collector) Collect(context.Context) map[string]int64 {
109
mx, err := c.collect()
110
if err != nil {
111
c.Error(err)
@@ -117,7 +118,7 @@ func (c *Collector) Collect() map[string]int64 {
118
return mx
119
}
120
120
-func (c *Collector) Cleanup() {
121
+func (c *Collector) Cleanup(context.Context) {
122
if c.prom != nil && c.prom.HTTPClient() != nil {
123
c.prom.HTTPClient().CloseIdleConnections()
124
}
src/go/plugin/go.d/collector/docker_engine/collector_test.go
+17
-16
@@ -3,6 +3,7 @@
3
package docker_engine
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -44,7 +45,7 @@ func TestCollector_ConfigurationSerialize(t *testing.T) {
45
}
46
47
func TestCollector_Cleanup(t *testing.T) {
47
- assert.NotPanics(t, New().Cleanup)
48
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
49
}
50
51
func TestCollector_Init(t *testing.T) {
@@ -73,9 +74,9 @@ func TestCollector_Init(t *testing.T) {
74
collr.Config = test.config
75
76
if test.wantFail {
76
- assert.Error(t, collr.Init())
77
+ assert.Error(t, collr.Init(context.Background()))
78
} else {
78
- assert.NoError(t, collr.Init())
79
+ assert.NoError(t, collr.Init(context.Background()))
80
}
81
})
82
}
@@ -101,9 +102,9 @@ func TestCollector_Check(t *testing.T) {
102
defer srv.Close()
103
104
if test.wantFail {
104
- assert.Error(t, collr.Check())
105
+ assert.Error(t, collr.Check(context.Background()))
106
} else {
106
- assert.NoError(t, collr.Check())
107
+ assert.NoError(t, collr.Check(context.Background()))
108
}
109
})
110
}
@@ -124,7 +125,7 @@ func TestCollector_Charts(t *testing.T) {
125
collr, srv := test.prepare(t)
126
defer srv.Close()
127
127
- require.NoError(t, collr.Check())
128
+ require.NoError(t, collr.Check(context.Background()))
129
assert.Len(t, *collr.Charts(), test.wantNumCharts)
130
})
131
}
@@ -145,7 +146,7 @@ func TestCollector_Collect_ReturnsNilOnErrors(t *testing.T) {
146
collr, srv := test.prepare(t)
147
defer srv.Close()
148
148
- assert.Nil(t, collr.Collect())
149
+ assert.Nil(t, collr.Collect(context.Background()))
150
})
151
}
152
}
@@ -251,9 +252,9 @@ func TestCollector_Collect(t *testing.T) {
252
defer srv.Close()
253
254
for i := 0; i < 10; i++ {
254
- _ = pulsar.Collect()
255
+ _ = pulsar.Collect(context.Background())
256
}
256
- mx := pulsar.Collect()
257
+ mx := pulsar.Collect(context.Background())
258
259
require.NotNil(t, mx)
260
require.Equal(t, test.expected, mx)
@@ -271,7 +272,7 @@ func prepareClientServerV17050CE(t *testing.T) (*Collector, *httptest.Server) {
272
273
collr := New()
274
collr.URL = srv.URL
274
- require.NoError(t, collr.Init())
275
+ require.NoError(t, collr.Init(context.Background()))
276
277
return collr, srv
278
}
@@ -285,7 +286,7 @@ func prepareClientServerV18093CE(t *testing.T) (*Collector, *httptest.Server) {
286
287
collr := New()
288
collr.URL = srv.URL
288
- require.NoError(t, collr.Init())
289
+ require.NoError(t, collr.Init(context.Background()))
290
291
return collr, srv
292
}
@@ -299,7 +300,7 @@ func prepareClientServerV18093CESwarm(t *testing.T) (*Collector, *httptest.Serve
300
301
collr := New()
302
collr.URL = srv.URL
302
- require.NoError(t, collr.Init())
303
+ require.NoError(t, collr.Init(context.Background()))
304
305
return collr, srv
306
}
@@ -313,7 +314,7 @@ func prepareClientServerNonDockerEngine(t *testing.T) (*Collector, *httptest.Ser
314
315
collr := New()
316
collr.URL = srv.URL
316
- require.NoError(t, collr.Init())
317
+ require.NoError(t, collr.Init(context.Background()))
318
319
return collr, srv
320
}
@@ -327,7 +328,7 @@ func prepareClientServerInvalidData(t *testing.T) (*Collector, *httptest.Server)
328
329
collr := New()
330
collr.URL = srv.URL
330
- require.NoError(t, collr.Init())
331
+ require.NoError(t, collr.Init(context.Background()))
332
333
return collr, srv
334
}
@@ -341,7 +342,7 @@ func prepareClientServer404(t *testing.T) (*Collector, *httptest.Server) {
342
343
collr := New()
344
collr.URL = srv.URL
344
- require.NoError(t, collr.Init())
345
+ require.NoError(t, collr.Init(context.Background()))
346
347
return collr, srv
348
}
@@ -352,7 +353,7 @@ func prepareClientServerConnectionRefused(t *testing.T) (*Collector, *httptest.S
353
354
collr := New()
355
collr.URL = "http://127.0.0.1:38001/metrics"
355
- require.NoError(t, collr.Init())
356
+ require.NoError(t, collr.Init(context.Background()))
357
358
return collr, srv
359
}
src/go/plugin/go.d/collector/dockerhub/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package dockerhub
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -59,7 +60,7 @@ func (c *Collector) Configuration() any {
60
return c.Config
61
}
62
62
-func (c *Collector) Init() error {
63
+func (c *Collector) Init(context.Context) error {
64
if err := c.validateConfig(); err != nil {
65
return fmt.Errorf("config validation: %v", err)
66
}
@@ -73,7 +74,7 @@ func (c *Collector) Init() error {
74
return nil
75
}
76
76
-func (c *Collector) Check() error {
77
+func (c *Collector) Check(context.Context) error {
78
mx, err := c.collect()
79
if err != nil {
80
return err
@@ -91,7 +92,7 @@ func (c *Collector) Charts() *Charts {
92
return cs
93
}
94
94
-func (c *Collector) Collect() map[string]int64 {
95
+func (c *Collector) Collect(context.Context) map[string]int64 {
96
mx, err := c.collect()
97
98
if err != nil {
@@ -102,7 +103,7 @@ func (c *Collector) Collect() map[string]int64 {
103
return mx
104
}
105
105
-func (c *Collector) Cleanup() {
106
+func (c *Collector) Cleanup(context.Context) {
107
if c.client != nil && c.client.httpClient != nil {
108
c.client.httpClient.CloseIdleConnections()
109
}
src/go/plugin/go.d/collector/dockerhub/collector_test.go
+15
-14
@@ -3,6 +3,7 @@
3
package dockerhub
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -42,17 +43,17 @@ func TestCollector_ConfigurationSerialize(t *testing.T) {
43
44
func TestCollector_Charts(t *testing.T) { assert.NotNil(t, New().Charts()) }
45
45
-func TestCollector_Cleanup(t *testing.T) { New().Cleanup() }
46
+func TestCollector_Cleanup(t *testing.T) { New().Cleanup(context.Background()) }
47
48
func TestCollector_Init(t *testing.T) {
49
collr := New()
50
collr.Repositories = []string{"name/repo"}
50
- assert.NoError(t, collr.Init())
51
+ assert.NoError(t, collr.Init(context.Background()))
52
assert.NotNil(t, collr.client)
53
}
54
55
func TestCollector_InitNG(t *testing.T) {
55
- assert.Error(t, New().Init())
56
+ assert.Error(t, New().Init(context.Background()))
57
}
58
59
func TestCollector_Check(t *testing.T) {
@@ -73,16 +74,16 @@ func TestCollector_Check(t *testing.T) {
74
collr := New()
75
collr.URL = ts.URL
76
collr.Repositories = []string{"name1/repo1", "name2/repo2", "name3/repo3"}
76
- require.NoError(t, collr.Init())
77
- assert.NoError(t, collr.Check())
77
+ require.NoError(t, collr.Init(context.Background()))
78
+ assert.NoError(t, collr.Check(context.Background()))
79
}
80
81
func TestCollector_CheckNG(t *testing.T) {
82
collr := New()
83
collr.URL = "http://127.0.0.1:38001/metrics"
84
collr.Repositories = []string{"name1/repo1", "name2/repo2", "name3/repo3"}
84
- require.NoError(t, collr.Init())
85
- assert.Error(t, collr.Check())
85
+ require.NoError(t, collr.Init(context.Background()))
86
+ assert.Error(t, collr.Check(context.Background()))
87
}
88
89
func TestCollector_Collect(t *testing.T) {
@@ -103,8 +104,8 @@ func TestCollector_Collect(t *testing.T) {
104
collr := New()
105
collr.URL = ts.URL
106
collr.Repositories = []string{"name1/repo1", "name2/repo2", "name3/repo3"}
106
- require.NoError(t, collr.Init())
107
- require.NoError(t, collr.Check())
107
+ require.NoError(t, collr.Init(context.Background()))
108
+ require.NoError(t, collr.Check(context.Background()))
109
110
expected := map[string]int64{
111
"star_count_user1/name1": 45,
@@ -119,7 +120,7 @@ func TestCollector_Collect(t *testing.T) {
120
"pull_sum": 55620576,
121
}
122
122
- collected := collr.Collect()
123
+ collected := collr.Collect(context.Background())
124
125
for k := range collected {
126
if strings.HasPrefix(k, "last") {
@@ -140,8 +141,8 @@ func TestCollector_InvalidData(t *testing.T) {
141
collr := New()
142
collr.URL = ts.URL
143
collr.Repositories = []string{"name1/repo1", "name2/repo2", "name3/repo3"}
143
- require.NoError(t, collr.Init())
144
- assert.Error(t, collr.Check())
144
+ require.NoError(t, collr.Init(context.Background()))
145
+ assert.Error(t, collr.Check(context.Background()))
146
}
147
148
func TestCollector_404(t *testing.T) {
@@ -154,6 +155,6 @@ func TestCollector_404(t *testing.T) {
155
156
collr := New()
157
collr.Repositories = []string{"name1/repo1", "name2/repo2", "name3/repo3"}
157
- require.NoError(t, collr.Init())
158
- assert.Error(t, collr.Check())
158
+ require.NoError(t, collr.Init(context.Background()))
159
+ assert.Error(t, collr.Check(context.Background()))
160
}
src/go/plugin/go.d/collector/dovecot/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package dovecot
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"time"
@@ -53,7 +54,7 @@ func (c *Collector) Configuration() any {
54
return c.Config
55
}
56
56
-func (c *Collector) Init() error {
57
+func (c *Collector) Init(context.Context) error {
58
if c.Address == "" {
59
return errors.New("config: 'address' not set")
60
}
@@ -61,7 +62,7 @@ func (c *Collector) Init() error {
62
return nil
63
}
64
64
-func (c *Collector) Check() error {
65
+func (c *Collector) Check(context.Context) error {
66
mx, err := c.collect()
67
if err != nil {
68
return err
@@ -78,7 +79,7 @@ func (c *Collector) Charts() *module.Charts {
79
return c.charts
80
}
81
81
-func (c *Collector) Collect() map[string]int64 {
82
+func (c *Collector) Collect(context.Context) map[string]int64 {
83
mx, err := c.collect()
84
if err != nil {
85
c.Error(err)
@@ -91,7 +92,7 @@ func (c *Collector) Collect() map[string]int64 {
92
return mx
93
}
94
94
-func (c *Collector) Cleanup() {
95
+func (c *Collector) Cleanup(context.Context) {
96
if c.conn != nil {
97
c.conn.disconnect()
98
c.conn = nil
src/go/plugin/go.d/collector/dovecot/collector_test.go
+10
-9
@@ -3,6 +3,7 @@
3
package dovecot
4
5
import (
6
+ "context"
7
"errors"
8
"os"
9
"testing"
@@ -59,9 +60,9 @@ func TestCollector_Init(t *testing.T) {
60
collr.Config = test.config
61
62
if test.wantFail {
62
- assert.Error(t, collr.Init())
63
+ assert.Error(t, collr.Init(context.Background()))
64
} else {
64
- assert.NoError(t, collr.Init())
65
+ assert.NoError(t, collr.Init(context.Background()))
66
}
67
})
68
}
@@ -80,7 +81,7 @@ func TestCollector_Cleanup(t *testing.T) {
81
prepare: func() *Collector {
82
collr := New()
83
collr.newConn = func(Config) dovecotConn { return prepareMockOk() }
83
- _ = collr.Check()
84
+ _ = collr.Check(context.Background())
85
return collr
86
},
87
},
@@ -88,7 +89,7 @@ func TestCollector_Cleanup(t *testing.T) {
89
prepare: func() *Collector {
90
collr := New()
91
collr.newConn = func(Config) dovecotConn { return prepareMockOk() }
91
- _ = collr.Collect()
92
+ _ = collr.Collect(context.Background())
93
return collr
94
},
95
},
@@ -98,7 +99,7 @@ func TestCollector_Cleanup(t *testing.T) {
99
t.Run(name, func(t *testing.T) {
100
collr := test.prepare()
101
101
- assert.NotPanics(t, collr.Cleanup)
102
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
103
})
104
}
105
}
@@ -137,9 +138,9 @@ func TestCollector_Check(t *testing.T) {
138
collr.newConn = func(Config) dovecotConn { return mock }
139
140
if test.wantFail {
140
- assert.Error(t, collr.Check())
141
+ assert.Error(t, collr.Check(context.Background()))
142
} else {
142
- assert.NoError(t, collr.Check())
143
+ assert.NoError(t, collr.Check(context.Background()))
144
}
145
})
146
}
@@ -212,7 +213,7 @@ func TestCollector_Collect(t *testing.T) {
213
mock := test.prepareMock()
214
collr.newConn = func(Config) dovecotConn { return mock }
215
215
- mx := collr.Collect()
216
+ mx := collr.Collect(context.Background())
217
218
require.Equal(t, test.wantMetrics, mx)
219
@@ -221,7 +222,7 @@ func TestCollector_Collect(t *testing.T) {
222
}
223
224
assert.Equal(t, test.disconnectBeforeCleanup, mock.disconnectCalled, "disconnect before cleanup")
224
- collr.Cleanup()
225
+ collr.Cleanup(context.Background())
226
assert.Equal(t, test.disconnectAfterCleanup, mock.disconnectCalled, "disconnect after cleanup")
227
})
228
}
src/go/plugin/go.d/collector/elasticsearch/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package elasticsearch
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -85,7 +86,7 @@ func (c *Collector) Configuration() any {
86
return c.Config
87
}
88
88
-func (c *Collector) Init() error {
89
+func (c *Collector) Init(context.Context) error {
90
err := c.validateConfig()
91
if err != nil {
92
return fmt.Errorf("check configuration: %v", err)
@@ -100,7 +101,7 @@ func (c *Collector) Init() error {
101
return nil
102
}
103
103
-func (c *Collector) Check() error {
104
+func (c *Collector) Check(context.Context) error {
105
mx, err := c.collect()
106
if err != nil {
107
return err
@@ -116,7 +117,7 @@ func (c *Collector) Charts() *module.Charts {
117
return c.charts
118
}
119
119
-func (c *Collector) Collect() map[string]int64 {
120
+func (c *Collector) Collect(context.Context) map[string]int64 {
121
mx, err := c.collect()
122
if err != nil {
123
c.Error(err)
@@ -128,7 +129,7 @@ func (c *Collector) Collect() map[string]int64 {
129
return mx
130
}
131
131
-func (c *Collector) Cleanup() {
132
+func (c *Collector) Cleanup(context.Context) {
133
if c.httpClient != nil {
134
c.httpClient.CloseIdleConnections()
135
}
src/go/plugin/go.d/collector/elasticsearch/collector_test.go
+11
-10
@@ -3,6 +3,7 @@
3
package elasticsearch
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -113,9 +114,9 @@ func TestCollector_Init(t *testing.T) {
114
collr.Config = test.config
115
116
if test.wantFail {
116
- assert.Error(t, collr.Init())
117
+ assert.Error(t, collr.Init(context.Background()))
118
} else {
118
- assert.NoError(t, collr.Init())
119
+ assert.NoError(t, collr.Init(context.Background()))
120
}
121
})
122
}
@@ -138,9 +139,9 @@ func TestCollector_Check(t *testing.T) {
139
defer cleanup()
140
141
if test.wantFail {
141
- assert.Error(t, collr.Check())
142
+ assert.Error(t, collr.Check(context.Background()))
143
} else {
143
- assert.NoError(t, collr.Check())
144
+ assert.NoError(t, collr.Check(context.Background()))
145
}
146
})
147
}
@@ -151,7 +152,7 @@ func TestCollector_Charts(t *testing.T) {
152
}
153
154
func TestCollector_Cleanup(t *testing.T) {
154
- assert.NotPanics(t, New().Cleanup)
155
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
156
}
157
158
func TestCollector_Collect(t *testing.T) {
@@ -633,7 +634,7 @@ func TestCollector_Collect(t *testing.T) {
634
635
var mx map[string]int64
636
for i := 0; i < 10; i++ {
636
- mx = collr.Collect()
637
+ mx = collr.Collect(context.Background())
638
}
639
640
assert.Equal(t, test.wantCollected, mx)
@@ -649,7 +650,7 @@ func prepareElasticsearch(t *testing.T, createES func() *Collector) (collr *Coll
650
651
collr = createES()
652
collr.URL = srv.URL
652
- require.NoError(t, collr.Init())
653
+ require.NoError(t, collr.Init(context.Background()))
654
655
return collr, srv.Close
656
}
@@ -666,7 +667,7 @@ func prepareElasticsearchInvalidData(t *testing.T) (*Collector, func()) {
667
}))
668
collr := New()
669
collr.URL = srv.URL
669
- require.NoError(t, collr.Init())
670
+ require.NoError(t, collr.Init(context.Background()))
671
672
return collr, srv.Close
673
}
@@ -679,7 +680,7 @@ func prepareElasticsearch404(t *testing.T) (*Collector, func()) {
680
}))
681
collr := New()
682
collr.URL = srv.URL
682
- require.NoError(t, collr.Init())
683
+ require.NoError(t, collr.Init(context.Background()))
684
685
return collr, srv.Close
686
}
@@ -688,7 +689,7 @@ func prepareElasticsearchConnectionRefused(t *testing.T) (*Collector, func()) {
689
t.Helper()
690
collr := New()
691
collr.URL = "http://127.0.0.1:38001"
691
- require.NoError(t, collr.Init())
692
+ require.NoError(t, collr.Init(context.Background()))
693
694
return collr, func() {}
695
}
src/go/plugin/go.d/collector/envoy/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package envoy
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -74,7 +75,7 @@ func (c *Collector) Configuration() any {
75
return c.Config
76
}
77
77
-func (c *Collector) Init() error {
78
+func (c *Collector) Init(context.Context) error {
79
if err := c.validateConfig(); err != nil {
80
return fmt.Errorf("config validation: %v", err)
81
}
@@ -88,7 +89,7 @@ func (c *Collector) Init() error {
89
return nil
90
}
91
91
-func (c *Collector) Check() error {
92
+func (c *Collector) Check(context.Context) error {
93
mx, err := c.collect()
94
if err != nil {
95
return err
@@ -104,7 +105,7 @@ func (c *Collector) Charts() *module.Charts {
105
return c.charts
106
}
107
107
-func (c *Collector) Collect() map[string]int64 {
108
+func (c *Collector) Collect(context.Context) map[string]int64 {
109
mx, err := c.collect()
110
if err != nil {
111
c.Error(err)
@@ -116,7 +117,7 @@ func (c *Collector) Collect() map[string]int64 {
117
return mx
118
}
119
119
-func (c *Collector) Cleanup() {
120
+func (c *Collector) Cleanup(context.Context) {
121
if c.prom == nil || c.prom.HTTPClient() == nil {
122
return
123
}
src/go/plugin/go.d/collector/envoy/collector_test.go
+13
-12
@@ -3,6 +3,7 @@
3
package envoy
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -63,9 +64,9 @@ func TestCollector_Init(t *testing.T) {
64
collr.Config = test.config
65
66
if test.wantFail {
66
- assert.Error(t, collr.Init())
67
+ assert.Error(t, collr.Init(context.Background()))
68
} else {
68
- assert.NoError(t, collr.Init())
69
+ assert.NoError(t, collr.Init(context.Background()))
70
}
71
})
72
}
@@ -74,10 +75,10 @@ func TestCollector_Init(t *testing.T) {
75
76
func TestCollector_Cleanup(t *testing.T) {
77
collr := New()
77
- assert.NotPanics(t, collr.Cleanup)
78
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
79
79
- require.NoError(t, collr.Init())
80
- assert.NotPanics(t, collr.Cleanup)
80
+ require.NoError(t, collr.Init(context.Background()))
81
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
82
}
83
84
func TestCollector_Charts(t *testing.T) {
@@ -86,8 +87,8 @@ func TestCollector_Charts(t *testing.T) {
87
88
require.Empty(t, *collr.Charts())
89
89
- require.NoError(t, collr.Init())
90
- _ = collr.Collect()
90
+ require.NoError(t, collr.Init(context.Background()))
91
+ _ = collr.Collect(context.Background())
92
require.NotEmpty(t, *collr.Charts())
93
}
94
@@ -119,12 +120,12 @@ func TestCollector_Check(t *testing.T) {
120
collr, cleanup := test.prepare()
121
defer cleanup()
122
122
- require.NoError(t, collr.Init())
123
+ require.NoError(t, collr.Init(context.Background()))
124
125
if test.wantFail {
125
- assert.Error(t, collr.Check())
126
+ assert.Error(t, collr.Check(context.Background()))
127
} else {
127
- assert.NoError(t, collr.Check())
128
+ assert.NoError(t, collr.Check(context.Background()))
129
}
130
})
131
}
@@ -499,9 +500,9 @@ func TestCollector_Collect(t *testing.T) {
500
collr, cleanup := test.prepare()
501
defer cleanup()
502
502
- require.NoError(t, collr.Init())
503
+ require.NoError(t, collr.Init(context.Background()))
504
504
- mx := collr.Collect()
505
+ mx := collr.Collect(context.Background())
506
507
require.Equal(t, test.wantMetrics, mx)
508
module.TestMetricsHasAllChartsDims(t, collr.Charts(), mx)
src/go/plugin/go.d/collector/exim/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package exim
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -53,7 +54,7 @@ func (c *Collector) Configuration() any {
54
return c.Config
55
}
56
56
-func (c *Collector) Init() error {
57
+func (c *Collector) Init(context.Context) error {
58
exim, err := c.initEximExec()
59
if err != nil {
60
return fmt.Errorf("exim exec initialization: %v", err)
@@ -63,7 +64,7 @@ func (c *Collector) Init() error {
64
return nil
65
}
66
66
-func (c *Collector) Check() error {
67
+func (c *Collector) Check(context.Context) error {
68
mx, err := c.collect()
69
if err != nil {
70
return err
@@ -80,7 +81,7 @@ func (c *Collector) Charts() *module.Charts {
81
return c.charts
82
}
83
83
-func (c *Collector) Collect() map[string]int64 {
84
+func (c *Collector) Collect(context.Context) map[string]int64 {
85
mx, err := c.collect()
86
if err != nil {
87
c.Error(err)
@@ -93,4 +94,4 @@ func (c *Collector) Collect() map[string]int64 {
94
return mx
95
}
96
96
-func (c *Collector) Cleanup() {}
97
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/exim/collector_test.go
+9
-8
@@ -3,6 +3,7 @@
3
package exim
4
5
import (
6
+ "context"
7
"errors"
8
"os"
9
"testing"
@@ -49,9 +50,9 @@ func TestCollector_Init(t *testing.T) {
50
collr.Config = test.config
51
52
if test.wantFail {
52
- assert.Error(t, collr.Init())
53
+ assert.Error(t, collr.Init(context.Background()))
54
} else {
54
- assert.NoError(t, collr.Init())
55
+ assert.NoError(t, collr.Init(context.Background()))
56
}
57
})
58
}
@@ -70,7 +71,7 @@ func TestCollector_Cleanup(t *testing.T) {
71
prepare: func() *Collector {
72
collr := New()
73
collr.exec = prepareMockOK()
73
- _ = collr.Check()
74
+ _ = collr.Check(context.Background())
75
return collr
76
},
77
},
@@ -78,7 +79,7 @@ func TestCollector_Cleanup(t *testing.T) {
79
prepare: func() *Collector {
80
collr := New()
81
collr.exec = prepareMockOK()
81
- _ = collr.Collect()
82
+ _ = collr.Collect(context.Background())
83
return collr
84
},
85
},
@@ -88,7 +89,7 @@ func TestCollector_Cleanup(t *testing.T) {
89
t.Run(name, func(t *testing.T) {
90
collr := test.prepare()
91
91
- assert.NotPanics(t, collr.Cleanup)
92
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
93
})
94
}
95
}
@@ -127,9 +128,9 @@ func TestCollector_Check(t *testing.T) {
128
collr.exec = mock
129
130
if test.wantFail {
130
- assert.Error(t, collr.Check())
131
+ assert.Error(t, collr.Check(context.Background()))
132
} else {
132
- assert.NoError(t, collr.Check())
133
+ assert.NoError(t, collr.Check(context.Background()))
134
}
135
})
136
}
@@ -166,7 +167,7 @@ func TestCollector_Collect(t *testing.T) {
167
mock := test.prepareMock()
168
collr.exec = mock
169
169
- mx := collr.Collect()
170
+ mx := collr.Collect(context.Background())
171
172
assert.Equal(t, test.wantMetrics, mx)
173
src/go/plugin/go.d/collector/fail2ban/collector.go
+5
-4
@@ -5,6 +5,7 @@
5
package fail2ban
6
7
import (
8
+ "context"
9
_ "embed"
10
"errors"
11
"fmt"
@@ -64,7 +65,7 @@ func (c *Collector) Configuration() any {
65
return c.Config
66
}
67
67
-func (c *Collector) Init() error {
68
+func (c *Collector) Init(context.Context) error {
69
f2bClientExec, err := c.initFail2banClientCliExec()
70
if err != nil {
71
return fmt.Errorf("fail2ban-client exec initialization: %v", err)
@@ -74,7 +75,7 @@ func (c *Collector) Init() error {
75
return nil
76
}
77
77
-func (c *Collector) Check() error {
78
+func (c *Collector) Check(context.Context) error {
79
mx, err := c.collect()
80
if err != nil {
81
return err
@@ -91,7 +92,7 @@ func (c *Collector) Charts() *module.Charts {
92
return c.charts
93
}
94
94
-func (c *Collector) Collect() map[string]int64 {
95
+func (c *Collector) Collect(context.Context) map[string]int64 {
96
mx, err := c.collect()
97
if err != nil {
98
c.Error(err)
@@ -104,4 +105,4 @@ func (c *Collector) Collect() map[string]int64 {
105
return mx
106
}
107
107
-func (c *Collector) Cleanup() {}
108
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/fail2ban/collector_test.go
+9
-8
@@ -5,6 +5,7 @@
5
package fail2ban
6
7
import (
8
+ "context"
9
"errors"
10
"os"
11
"testing"
@@ -57,9 +58,9 @@ func TestCollector_Init(t *testing.T) {
58
collr.Config = test.config
59
60
if test.wantFail {
60
- assert.Error(t, collr.Init())
61
+ assert.Error(t, collr.Init(context.Background()))
62
} else {
62
- assert.NoError(t, collr.Init())
63
+ assert.NoError(t, collr.Init(context.Background()))
64
}
65
})
66
}
@@ -78,7 +79,7 @@ func TestCollector_Cleanup(t *testing.T) {
79
prepare: func() *Collector {
80
collr := New()
81
collr.exec = prepareMockOk()
81
- _ = collr.Check()
82
+ _ = collr.Check(context.Background())
83
return collr
84
},
85
},
@@ -86,7 +87,7 @@ func TestCollector_Cleanup(t *testing.T) {
87
prepare: func() *Collector {
88
collr := New()
89
collr.exec = prepareMockOk()
89
- _ = collr.Collect()
90
+ _ = collr.Collect(context.Background())
91
return collr
92
},
93
},
@@ -96,7 +97,7 @@ func TestCollector_Cleanup(t *testing.T) {
97
t.Run(name, func(t *testing.T) {
98
collr := test.prepare()
99
99
- assert.NotPanics(t, collr.Cleanup)
100
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
101
})
102
}
103
}
@@ -131,9 +132,9 @@ func TestCollector_Check(t *testing.T) {
132
collr.exec = mock
133
134
if test.wantFail {
134
- assert.Error(t, collr.Check())
135
+ assert.Error(t, collr.Check(context.Background()))
136
} else {
136
- assert.NoError(t, collr.Check())
137
+ assert.NoError(t, collr.Check(context.Background()))
138
}
139
})
140
}
@@ -169,7 +170,7 @@ func TestCollector_Collect(t *testing.T) {
170
mock := test.prepareMock()
171
collr.exec = mock
172
172
- mx := collr.Collect()
173
+ mx := collr.Collect(context.Background())
174
175
assert.Equal(t, test.wantMetrics, mx)
176
src/go/plugin/go.d/collector/filecheck/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package filecheck
4
5
import (
6
+ "context"
7
_ "embed"
8
"fmt"
9
"time"
@@ -78,7 +79,7 @@ func (c *Collector) Configuration() any {
79
return c.Config
80
}
81
81
-func (c *Collector) Init() error {
82
+func (c *Collector) Init(context.Context) error {
83
err := c.validateConfig()
84
if err != nil {
85
return fmt.Errorf("config validation: %v", err)
@@ -102,7 +103,7 @@ func (c *Collector) Init() error {
103
return nil
104
}
105
105
-func (c *Collector) Check() error {
106
+func (c *Collector) Check(context.Context) error {
107
return nil
108
}
109
@@ -110,7 +111,7 @@ func (c *Collector) Charts() *module.Charts {
111
return c.charts
112
}
113
113
-func (c *Collector) Collect() map[string]int64 {
114
+func (c *Collector) Collect(context.Context) map[string]int64 {
115
mx, err := c.collect()
116
if err != nil {
117
c.Error(err)
@@ -123,4 +124,4 @@ func (c *Collector) Collect() map[string]int64 {
124
return mx
125
}
126
126
-func (c *Collector) Cleanup() {}
127
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/filecheck/collector_test.go
+8
-7
@@ -3,6 +3,7 @@
3
package filecheck
4
5
import (
6
+ "context"
7
"os"
8
"strings"
9
"testing"
@@ -32,7 +33,7 @@ func TestCollector_ConfigurationSerialize(t *testing.T) {
33
}
34
35
func TestCollector_Cleanup(t *testing.T) {
35
- assert.NotPanics(t, New().Cleanup)
36
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
37
}
38
39
func TestCollector_Init(t *testing.T) {
@@ -100,9 +101,9 @@ func TestCollector_Init(t *testing.T) {
101
collr.Config = test.config
102
103
if test.wantFail {
103
- assert.Error(t, collr.Init())
104
+ assert.Error(t, collr.Init(context.Background()))
105
} else {
105
- require.NoError(t, collr.Init())
106
+ require.NoError(t, collr.Init(context.Background()))
107
}
108
})
109
}
@@ -124,9 +125,9 @@ func TestCollector_Check(t *testing.T) {
125
for name, test := range tests {
126
t.Run(name, func(t *testing.T) {
127
collr := test.prepare()
127
- require.NoError(t, collr.Init())
128
+ require.NoError(t, collr.Init(context.Background()))
129
129
- assert.NoError(t, collr.Check())
130
+ assert.NoError(t, collr.Check(context.Background()))
131
})
132
}
133
}
@@ -239,9 +240,9 @@ func TestCollector_Collect(t *testing.T) {
240
for name, test := range tests {
241
t.Run(name, func(t *testing.T) {
242
collr := test.prepare()
242
- require.NoError(t, collr.Init())
243
+ require.NoError(t, collr.Init(context.Background()))
244
244
- mx := collr.Collect()
245
+ mx := collr.Collect(context.Background())
246
247
copyModTime(test.wantCollected, mx)
248
src/go/plugin/go.d/collector/fluentd/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package fluentd
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -63,7 +64,7 @@ func (c *Collector) Configuration() any {
64
return c.Config
65
}
66
66
-func (c *Collector) Init() error {
67
+func (c *Collector) Init(context.Context) error {
68
if err := c.validateConfig(); err != nil {
69
return fmt.Errorf("invalid config: %v", err)
70
}
@@ -86,7 +87,7 @@ func (c *Collector) Init() error {
87
return nil
88
}
89
89
-func (c *Collector) Check() error {
90
+func (c *Collector) Check(context.Context) error {
91
mx, err := c.collect()
92
if err != nil {
93
return err
@@ -102,7 +103,7 @@ func (c *Collector) Charts() *Charts {
103
return c.charts
104
}
105
105
-func (c *Collector) Collect() map[string]int64 {
106
+func (c *Collector) Collect(context.Context) map[string]int64 {
107
mx, err := c.collect()
108
109
if err != nil {
@@ -113,7 +114,7 @@ func (c *Collector) Collect() map[string]int64 {
114
return mx
115
}
116
116
-func (c *Collector) Cleanup() {
117
+func (c *Collector) Cleanup(context.Context) {
118
if c.apiClient != nil && c.apiClient.httpClient != nil {
119
c.apiClient.httpClient.CloseIdleConnections()
120
}
src/go/plugin/go.d/collector/fluentd/collector_test.go
+15
-14
@@ -3,6 +3,7 @@
3
package fluentd
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -38,13 +39,13 @@ func TestCollector_ConfigurationSerialize(t *testing.T) {
39
func TestCollector_Init(t *testing.T) {
40
// OK
41
collr := New()
41
- assert.NoError(t, collr.Init())
42
+ assert.NoError(t, collr.Init(context.Background()))
43
assert.NotNil(t, collr.apiClient)
44
45
//NG
46
collr = New()
47
collr.URL = ""
47
- assert.Error(t, collr.Init())
48
+ assert.Error(t, collr.Init(context.Background()))
49
}
50
51
func TestCollector_Check(t *testing.T) {
@@ -57,14 +58,14 @@ func TestCollector_Check(t *testing.T) {
58
// OK
59
collr := New()
60
collr.URL = ts.URL
60
- require.NoError(t, collr.Init())
61
- require.NoError(t, collr.Check())
61
+ require.NoError(t, collr.Init(context.Background()))
62
+ require.NoError(t, collr.Check(context.Background()))
63
64
// NG
65
collr = New()
66
collr.URL = "http://127.0.0.1:38001/api/plugins.json"
66
- require.NoError(t, collr.Init())
67
- require.Error(t, collr.Check())
67
+ require.NoError(t, collr.Init(context.Background()))
68
+ require.Error(t, collr.Check(context.Background()))
69
}
70
71
func TestCollector_Charts(t *testing.T) {
@@ -72,7 +73,7 @@ func TestCollector_Charts(t *testing.T) {
73
}
74
75
func TestCollector_Cleanup(t *testing.T) {
75
- New().Cleanup()
76
+ New().Cleanup(context.Background())
77
}
78
79
func TestCollector_Collect(t *testing.T) {
@@ -85,8 +86,8 @@ func TestCollector_Collect(t *testing.T) {
86
collr := New()
87
collr.URL = ts.URL
88
88
- require.NoError(t, collr.Init())
89
- require.NoError(t, collr.Check())
89
+ require.NoError(t, collr.Init(context.Background()))
90
+ require.NoError(t, collr.Check(context.Background()))
91
92
expected := map[string]int64{
93
"output_stdout_stdout_output_retry_count": 0,
@@ -94,7 +95,7 @@ func TestCollector_Collect(t *testing.T) {
95
"output_td_tdlog_output_buffer_queue_length": 0,
96
"output_td_tdlog_output_buffer_total_queued_size": 0,
97
}
97
- assert.Equal(t, expected, collr.Collect())
98
+ assert.Equal(t, expected, collr.Collect(context.Background()))
99
assert.Len(t, collr.charts.Get("retry_count").Dims, 2)
100
assert.Len(t, collr.charts.Get("buffer_queue_length").Dims, 1)
101
assert.Len(t, collr.charts.Get("buffer_total_queued_size").Dims, 1)
@@ -109,8 +110,8 @@ func TestCollector_InvalidData(t *testing.T) {
110
111
collr := New()
112
collr.URL = ts.URL
112
- require.NoError(t, collr.Init())
113
- assert.Error(t, collr.Check())
113
+ require.NoError(t, collr.Init(context.Background()))
114
+ assert.Error(t, collr.Check(context.Background()))
115
}
116
117
func TestCollector_404(t *testing.T) {
@@ -122,6 +123,6 @@ func TestCollector_404(t *testing.T) {
123
124
collr := New()
125
collr.URL = ts.URL
125
- require.NoError(t, collr.Init())
126
- assert.Error(t, collr.Check())
126
+ require.NoError(t, collr.Init(context.Background()))
127
+ assert.Error(t, collr.Check(context.Background()))
128
}
src/go/plugin/go.d/collector/freeradius/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package freeradius
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -59,7 +60,7 @@ func (c *Collector) Configuration() any {
60
return c.Config
61
}
62
62
-func (c *Collector) Init() error {
63
+func (c *Collector) Init(context.Context) error {
64
if err := c.validateConfig(); err != nil {
65
return fmt.Errorf("config validation: %v", err)
66
}
@@ -74,7 +75,7 @@ func (c *Collector) Init() error {
75
return nil
76
}
77
77
-func (c *Collector) Check() error {
78
+func (c *Collector) Check(context.Context) error {
79
mx, err := c.collect()
80
if err != nil {
81
return err
@@ -90,7 +91,7 @@ func (c *Collector) Charts() *Charts {
91
return charts.Copy()
92
}
93
93
-func (c *Collector) Collect() map[string]int64 {
94
+func (c *Collector) Collect(context.Context) map[string]int64 {
95
mx, err := c.collect()
96
if err != nil {
97
c.Error(err)
@@ -102,4 +103,4 @@ func (c *Collector) Collect() map[string]int64 {
103
return mx
104
}
105
105
-func (c *Collector) Cleanup() {}
106
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/freeradius/collector_test.go
+10
-9
@@ -3,6 +3,7 @@
3
package freeradius
4
5
import (
6
+ "context"
7
"errors"
8
"os"
9
"testing"
@@ -35,42 +36,42 @@ func TestCollector_ConfigurationSerialize(t *testing.T) {
36
func TestCollector_Init(t *testing.T) {
37
collr := New()
38
38
- assert.NoError(t, collr.Init())
39
+ assert.NoError(t, collr.Init(context.Background()))
40
}
41
42
func TestCollector_Init_ReturnsFalseIfAddressNotSet(t *testing.T) {
43
collr := New()
44
collr.Address = ""
45
45
- assert.Error(t, collr.Init())
46
+ assert.Error(t, collr.Init(context.Background()))
47
}
48
49
func TestCollector_Init_ReturnsFalseIfPortNotSet(t *testing.T) {
50
collr := New()
51
collr.Port = 0
52
52
- assert.Error(t, collr.Init())
53
+ assert.Error(t, collr.Init(context.Background()))
54
}
55
56
func TestCollector_Init_ReturnsFalseIfSecretNotSet(t *testing.T) {
57
collr := New()
58
collr.Secret = ""
59
59
- assert.Error(t, collr.Init())
60
+ assert.Error(t, collr.Init(context.Background()))
61
}
62
63
func TestCollector_Check(t *testing.T) {
64
collr := New()
65
collr.client = newOKMockClient()
66
66
- assert.NoError(t, collr.Check())
67
+ assert.NoError(t, collr.Check(context.Background()))
68
}
69
70
func TestCollector_Check_ReturnsFalseIfClientStatusReturnsError(t *testing.T) {
71
collr := New()
72
collr.client = newErrorMockClient()
73
73
- assert.Error(t, collr.Check())
74
+ assert.Error(t, collr.Check(context.Background()))
75
}
76
77
func TestCollector_Charts(t *testing.T) {
@@ -117,7 +118,7 @@ func TestCollector_Collect(t *testing.T) {
118
"proxy-acct-dropped-requests": 33,
119
"proxy-acct-unknown-types": 34,
120
}
120
- mx := collr.Collect()
121
+ mx := collr.Collect(context.Background())
122
123
assert.Equal(t, expected, mx)
124
module.TestMetricsHasAllChartsDims(t, collr.Charts(), mx)
@@ -127,11 +128,11 @@ func TestCollector_Collect_ReturnsNilIfClientStatusReturnsError(t *testing.T) {
128
collr := New()
129
collr.client = newErrorMockClient()
130
130
- assert.Nil(t, collr.Collect())
131
+ assert.Nil(t, collr.Collect(context.Background()))
132
}
133
134
func TestCollector_Cleanup(t *testing.T) {
134
- New().Cleanup()
135
+ New().Cleanup(context.Background())
136
}
137
138
func newOKMockClient() *mockClient {
src/go/plugin/go.d/collector/gearman/collect.go
+3
-2
@@ -5,6 +5,7 @@ package gearman
5
import (
6
"bufio"
7
"bytes"
8
+ "context"
9
"errors"
10
"fmt"
11
"strconv"
@@ -22,13 +23,13 @@ func (c *Collector) collect() (map[string]int64, error) {
23
24
status, err := c.conn.queryStatus()
25
if err != nil {
25
- c.Cleanup()
26
+ c.Cleanup(context.Background())
27
return nil, fmt.Errorf("couldn't query status: %v", err)
28
}
29
30
prioStatus, err := c.conn.queryPriorityStatus()
31
if err != nil {
31
- c.Cleanup()
32
+ c.Cleanup(context.Background())
33
return nil, fmt.Errorf("couldn't query priority status: %v", err)
34
}
35
src/go/plugin/go.d/collector/gearman/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package gearman
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"time"
@@ -58,7 +59,7 @@ func (c *Collector) Configuration() any {
59
return c.Config
60
}
61
61
-func (c *Collector) Init() error {
62
+func (c *Collector) Init(context.Context) error {
63
if c.Address == "" {
64
return errors.New("config: 'address' not set")
65
}
@@ -66,7 +67,7 @@ func (c *Collector) Init() error {
67
return nil
68
}
69
69
-func (c *Collector) Check() error {
70
+func (c *Collector) Check(context.Context) error {
71
mx, err := c.collect()
72
if err != nil {
73
return err
@@ -83,7 +84,7 @@ func (c *Collector) Charts() *module.Charts {
84
return c.charts
85
}
86
86
-func (c *Collector) Collect() map[string]int64 {
87
+func (c *Collector) Collect(context.Context) map[string]int64 {
88
mx, err := c.collect()
89
if err != nil {
90
c.Error(err)
@@ -96,7 +97,7 @@ func (c *Collector) Collect() map[string]int64 {
97
return mx
98
}
99
99
-func (c *Collector) Cleanup() {
100
+func (c *Collector) Cleanup(context.Context) {
101
if c.conn != nil {
102
c.conn.disconnect()
103
c.conn = nil
src/go/plugin/go.d/collector/gearman/collector_test.go
+10
-9
@@ -3,6 +3,7 @@
3
package gearman
4
5
import (
6
+ "context"
7
"errors"
8
"os"
9
"testing"
@@ -62,9 +63,9 @@ func TestCollector_Init(t *testing.T) {
63
collr.Config = test.config
64
65
if test.wantFail {
65
- assert.Error(t, collr.Init())
66
+ assert.Error(t, collr.Init(context.Background()))
67
} else {
67
- assert.NoError(t, collr.Init())
68
+ assert.NoError(t, collr.Init(context.Background()))
69
}
70
})
71
}
@@ -83,7 +84,7 @@ func TestCollector_Cleanup(t *testing.T) {
84
prepare: func() *Collector {
85
collr := New()
86
collr.newConn = func(Config) gearmanConn { return prepareMockOk() }
86
- _ = collr.Check()
87
+ _ = collr.Check(context.Background())
88
return collr
89
},
90
},
@@ -91,7 +92,7 @@ func TestCollector_Cleanup(t *testing.T) {
92
prepare: func() *Collector {
93
collr := New()
94
collr.newConn = func(Config) gearmanConn { return prepareMockOk() }
94
- _ = collr.Collect()
95
+ _ = collr.Collect(context.Background())
96
return collr
97
},
98
},
@@ -101,7 +102,7 @@ func TestCollector_Cleanup(t *testing.T) {
102
t.Run(name, func(t *testing.T) {
103
collr := test.prepare()
104
104
- assert.NotPanics(t, collr.Cleanup)
105
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
106
})
107
}
108
}
@@ -140,9 +141,9 @@ func TestCollector_Check(t *testing.T) {
141
collr.newConn = func(Config) gearmanConn { return mock }
142
143
if test.wantFail {
143
- assert.Error(t, collr.Check())
144
+ assert.Error(t, collr.Check(context.Background()))
145
} else {
145
- assert.NoError(t, collr.Check())
146
+ assert.NoError(t, collr.Check(context.Background()))
147
}
148
})
149
}
@@ -238,7 +239,7 @@ func TestCollector_Collect(t *testing.T) {
239
mock := test.prepareMock()
240
collr.newConn = func(Config) gearmanConn { return mock }
241
241
- mx := collr.Collect()
242
+ mx := collr.Collect(context.Background())
243
244
require.Equal(t, test.wantMetrics, mx, "want metrics")
245
@@ -248,7 +249,7 @@ func TestCollector_Collect(t *testing.T) {
249
}
250
251
assert.Equal(t, test.disconnectBeforeCleanup, mock.disconnectCalled, "disconnect before cleanup")
251
- collr.Cleanup()
252
+ collr.Cleanup(context.Background())
253
assert.Equal(t, test.disconnectAfterCleanup, mock.disconnectCalled, "disconnect after cleanup")
254
})
255
}
src/go/plugin/go.d/collector/geth/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package geth
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -59,7 +60,7 @@ func (c *Collector) Configuration() any {
60
return c.Config
61
}
62
62
-func (c *Collector) Init() error {
63
+func (c *Collector) Init(context.Context) error {
64
if err := c.validateConfig(); err != nil {
65
return fmt.Errorf("error on validating config: %v", err)
66
}
@@ -73,7 +74,7 @@ func (c *Collector) Init() error {
74
return nil
75
}
76
76
-func (c *Collector) Check() error {
77
+func (c *Collector) Check(context.Context) error {
78
mx, err := c.collect()
79
if err != nil {
80
return err
@@ -88,7 +89,7 @@ func (c *Collector) Charts() *Charts {
89
return c.charts
90
}
91
91
-func (c *Collector) Collect() map[string]int64 {
92
+func (c *Collector) Collect(context.Context) map[string]int64 {
93
mx, err := c.collect()
94
if err != nil {
95
c.Error(err)
@@ -100,7 +101,7 @@ func (c *Collector) Collect() map[string]int64 {
101
return mx
102
}
103
103
-func (c *Collector) Cleanup() {
104
+func (c *Collector) Cleanup(context.Context) {
105
if c.prom != nil && c.prom.HTTPClient() != nil {
106
c.prom.HTTPClient().CloseIdleConnections()
107
}
src/go/plugin/go.d/collector/haproxy/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package haproxy
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -65,7 +66,7 @@ func (c *Collector) Configuration() any {
66
return c.Config
67
}
68
68
-func (c *Collector) Init() error {
69
+func (c *Collector) Init(context.Context) error {
70
if err := c.validateConfig(); err != nil {
71
return fmt.Errorf("config validation: %v", err)
72
}
@@ -79,7 +80,7 @@ func (c *Collector) Init() error {
80
return nil
81
}
82
82
-func (c *Collector) Check() error {
83
+func (c *Collector) Check(context.Context) error {
84
mx, err := c.collect()
85
if err != nil {
86
return err
@@ -94,7 +95,7 @@ func (c *Collector) Charts() *module.Charts {
95
return c.charts
96
}
97
97
-func (c *Collector) Collect() map[string]int64 {
98
+func (c *Collector) Collect(context.Context) map[string]int64 {
99
mx, err := c.collect()
100
if err != nil {
101
c.Error(err)
@@ -107,7 +108,7 @@ func (c *Collector) Collect() map[string]int64 {
108
return mx
109
}
110
110
-func (c *Collector) Cleanup() {
111
+func (c *Collector) Cleanup(context.Context) {
112
if c.prom != nil && c.prom.HTTPClient() != nil {
113
c.prom.HTTPClient().CloseIdleConnections()
114
}
src/go/plugin/go.d/collector/haproxy/collector_test.go
+11
-10
@@ -3,6 +3,7 @@
3
package haproxy
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -68,9 +69,9 @@ func TestCollector_Init(t *testing.T) {
69
collr.Config = test.config
70
71
if test.wantFail {
71
- assert.Error(t, collr.Init())
72
+ assert.Error(t, collr.Init(context.Background()))
73
} else {
73
- assert.NoError(t, collr.Init())
74
+ assert.NoError(t, collr.Init(context.Background()))
75
}
76
})
77
}
@@ -81,7 +82,7 @@ func TestCollector_Charts(t *testing.T) {
82
}
83
84
func TestCollector_Cleanup(t *testing.T) {
84
- assert.NotPanics(t, New().Cleanup)
85
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
86
}
87
88
func TestCollector_Check(t *testing.T) {
@@ -113,9 +114,9 @@ func TestCollector_Check(t *testing.T) {
114
defer cleanup()
115
116
if test.wantFail {
116
- assert.Error(t, collr.Check())
117
+ assert.Error(t, collr.Check(context.Background()))
118
} else {
118
- assert.NoError(t, collr.Check())
119
+ assert.NoError(t, collr.Check(context.Background()))
120
}
121
})
122
}
@@ -173,7 +174,7 @@ func TestCollector_Collect(t *testing.T) {
174
collr, cleanup := test.prepare(t)
175
defer cleanup()
176
176
- mx := collr.Collect()
177
+ mx := collr.Collect(context.Background())
178
179
assert.Equal(t, test.wantCollected, mx)
180
if len(test.wantCollected) > 0 {
@@ -191,7 +192,7 @@ func prepareCaseHaproxyV231Metrics(t *testing.T) (*Collector, func()) {
192
}))
193
collr := New()
194
collr.URL = srv.URL
194
- require.NoError(t, collr.Init())
195
+ require.NoError(t, collr.Init(context.Background()))
196
197
return collr, srv.Close
198
}
@@ -219,7 +220,7 @@ application_backend_http_responses_total{proxy="infra-vernemq-ws",code="other"}
220
}))
221
collr := New()
222
collr.URL = srv.URL
222
- require.NoError(t, collr.Init())
223
+ require.NoError(t, collr.Init(context.Background()))
224
225
return collr, srv.Close
226
}
@@ -232,7 +233,7 @@ func prepareCase404Response(t *testing.T) (*Collector, func()) {
233
}))
234
collr := New()
235
collr.URL = srv.URL
235
- require.NoError(t, collr.Init())
236
+ require.NoError(t, collr.Init(context.Background()))
237
238
return collr, srv.Close
239
}
@@ -241,7 +242,7 @@ func prepareCaseConnectionRefused(t *testing.T) (*Collector, func()) {
242
t.Helper()
243
collr := New()
244
collr.URL = "http://127.0.0.1:38001"
244
- require.NoError(t, collr.Init())
245
+ require.NoError(t, collr.Init(context.Background()))
246
247
return collr, func() {}
248
}
src/go/plugin/go.d/collector/hddtemp/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package hddtemp
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -57,7 +58,7 @@ func (c *Collector) Configuration() any {
58
return c.Config
59
}
60
60
-func (c *Collector) Init() error {
61
+func (c *Collector) Init(context.Context) error {
62
if c.Address == "" {
63
return fmt.Errorf("config: 'address' not set")
64
}
@@ -67,7 +68,7 @@ func (c *Collector) Init() error {
68
return nil
69
}
70
70
-func (c *Collector) Check() error {
71
+func (c *Collector) Check(context.Context) error {
72
mx, err := c.collect()
73
if err != nil {
74
return err
@@ -82,7 +83,7 @@ func (c *Collector) Charts() *module.Charts {
83
return c.charts
84
}
85
85
-func (c *Collector) Collect() map[string]int64 {
86
+func (c *Collector) Collect(context.Context) map[string]int64 {
87
mx, err := c.collect()
88
if err != nil {
89
c.Error(err)
@@ -94,4 +95,4 @@ func (c *Collector) Collect() map[string]int64 {
95
return mx
96
}
97
97
-func (c *Collector) Cleanup() {}
98
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/hddtemp/collector_test.go
+9
-8
@@ -3,6 +3,7 @@
3
package hddtemp
4
5
import (
6
+ "context"
7
"errors"
8
"os"
9
"testing"
@@ -62,9 +63,9 @@ func TestCollector_Init(t *testing.T) {
63
collr.Config = test.config
64
65
if test.wantFail {
65
- assert.Error(t, collr.Init())
66
+ assert.Error(t, collr.Init(context.Background()))
67
} else {
67
- assert.NoError(t, collr.Init())
68
+ assert.NoError(t, collr.Init(context.Background()))
69
}
70
})
71
}
@@ -83,7 +84,7 @@ func TestCollector_Cleanup(t *testing.T) {
84
prepare: func() *Collector {
85
collr := New()
86
collr.conn = prepareMockAllDisksOk()
86
- _ = collr.Check()
87
+ _ = collr.Check(context.Background())
88
return collr
89
},
90
},
@@ -91,7 +92,7 @@ func TestCollector_Cleanup(t *testing.T) {
92
prepare: func() *Collector {
93
collr := New()
94
collr.conn = prepareMockAllDisksOk()
94
- _ = collr.Collect()
95
+ _ = collr.Collect(context.Background())
96
return collr
97
},
98
},
@@ -101,7 +102,7 @@ func TestCollector_Cleanup(t *testing.T) {
102
t.Run(name, func(t *testing.T) {
103
collr := test.prepare()
104
104
- assert.NotPanics(t, collr.Cleanup)
105
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
106
})
107
}
108
}
@@ -139,9 +140,9 @@ func TestCollector_Check(t *testing.T) {
140
collr.conn = test.prepareMock()
141
142
if test.wantFail {
142
- assert.Error(t, collr.Check())
143
+ assert.Error(t, collr.Check(context.Background()))
144
} else {
144
- assert.NoError(t, collr.Check())
145
+ assert.NoError(t, collr.Check(context.Background()))
146
}
147
})
148
}
@@ -229,7 +230,7 @@ func TestCollector_Collect(t *testing.T) {
230
collr := New()
231
collr.conn = test.prepareMock()
232
232
- mx := collr.Collect()
233
+ mx := collr.Collect(context.Background())
234
235
assert.Equal(t, test.wantMetrics, mx)
236
src/go/plugin/go.d/collector/hdfs/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package hdfs
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -60,7 +61,7 @@ func (c *Collector) Configuration() any {
61
return c.Config
62
}
63
63
-func (c *Collector) Init() error {
64
+func (c *Collector) Init(context.Context) error {
65
if c.URL == "" {
66
return errors.New("URL is required but not set")
67
}
@@ -74,7 +75,7 @@ func (c *Collector) Init() error {
75
return nil
76
}
77
77
-func (c *Collector) Check() error {
78
+func (c *Collector) Check(context.Context) error {
79
typ, err := c.determineNodeType()
80
if err != nil {
81
return fmt.Errorf("error on node type determination : %v", err)
@@ -104,7 +105,7 @@ func (c *Collector) Charts() *Charts {
105
}
106
}
107
107
-func (c *Collector) Collect() map[string]int64 {
108
+func (c *Collector) Collect(context.Context) map[string]int64 {
109
mx, err := c.collect()
110
if err != nil {
111
c.Error(err)
@@ -114,7 +115,7 @@ func (c *Collector) Collect() map[string]int64 {
115
return mx
116
}
117
117
-func (c *Collector) Cleanup() {
118
+func (c *Collector) Cleanup(context.Context) {
119
if c.httpClient != nil {
120
c.httpClient.CloseIdleConnections()
121
}
src/go/plugin/go.d/collector/hdfs/collector_test.go
+28
-27
@@ -3,6 +3,7 @@
3
package hdfs
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -42,14 +43,14 @@ func TestCollector_ConfigurationSerialize(t *testing.T) {
43
func TestCollector_Init(t *testing.T) {
44
collr := New()
45
45
- assert.NoError(t, collr.Init())
46
+ assert.NoError(t, collr.Init(context.Background()))
47
}
48
49
func TestCollector_InitErrorOnCreatingClientWrongTLSCA(t *testing.T) {
50
collr := New()
51
collr.ClientConfig.TLSConfig.TLSCA = "testdata/tls"
52
52
- assert.Error(t, collr.Init())
53
+ assert.Error(t, collr.Init(context.Background()))
54
}
55
56
func TestCollector_Check(t *testing.T) {
@@ -62,9 +63,9 @@ func TestCollector_Check(t *testing.T) {
63
64
collr := New()
65
collr.URL = ts.URL
65
- require.NoError(t, collr.Init())
66
+ require.NoError(t, collr.Init(context.Background()))
67
67
- assert.NoError(t, collr.Check())
68
+ assert.NoError(t, collr.Check(context.Background()))
69
assert.NotZero(t, collr.nodeType)
70
}
71
@@ -78,9 +79,9 @@ func TestCollector_CheckDataNode(t *testing.T) {
79
80
collr := New()
81
collr.URL = ts.URL
81
- require.NoError(t, collr.Init())
82
+ require.NoError(t, collr.Init(context.Background()))
83
83
- assert.NoError(t, collr.Check())
84
+ assert.NoError(t, collr.Check(context.Background()))
85
assert.Equal(t, dataNodeType, collr.nodeType)
86
}
87
@@ -94,9 +95,9 @@ func TestCollector_CheckNameNode(t *testing.T) {
95
96
collr := New()
97
collr.URL = ts.URL
97
- require.NoError(t, collr.Init())
98
+ require.NoError(t, collr.Init(context.Background()))
99
99
- assert.NoError(t, collr.Check())
100
+ assert.NoError(t, collr.Check(context.Background()))
101
assert.Equal(t, nameNodeType, collr.nodeType)
102
}
103
@@ -110,17 +111,17 @@ func TestCollector_CheckErrorOnNodeTypeDetermination(t *testing.T) {
111
112
collr := New()
113
collr.URL = ts.URL
113
- require.NoError(t, collr.Init())
114
+ require.NoError(t, collr.Init(context.Background()))
115
115
- assert.Error(t, collr.Check())
116
+ assert.Error(t, collr.Check(context.Background()))
117
}
118
119
func TestCollector_CheckNoResponse(t *testing.T) {
120
collr := New()
121
collr.URL = "http://127.0.0.1:38001/jmx"
121
- require.NoError(t, collr.Init())
122
+ require.NoError(t, collr.Init(context.Background()))
123
123
- assert.Error(t, collr.Check())
124
+ assert.Error(t, collr.Check(context.Background()))
125
}
126
127
func TestCollector_Charts(t *testing.T) {
@@ -148,7 +149,7 @@ func TestCollector_ChartsNameNode(t *testing.T) {
149
}
150
151
func TestCollector_Cleanup(t *testing.T) {
151
- New().Cleanup()
152
+ New().Cleanup(context.Background())
153
}
154
155
func TestCollector_CollectDataNode(t *testing.T) {
@@ -161,8 +162,8 @@ func TestCollector_CollectDataNode(t *testing.T) {
162
163
collr := New()
164
collr.URL = ts.URL
164
- require.NoError(t, collr.Init())
165
- require.NoError(t, collr.Check())
165
+ require.NoError(t, collr.Init(context.Background()))
166
+ require.NoError(t, collr.Check(context.Background()))
167
168
expected := map[string]int64{
169
"dna_bytes_read": 80689178,
@@ -200,7 +201,7 @@ func TestCollector_CollectDataNode(t *testing.T) {
201
"rpc_sent_bytes": 187,
202
}
203
203
- assert.Equal(t, expected, collr.Collect())
204
+ assert.Equal(t, expected, collr.Collect(context.Background()))
205
}
206
207
func TestCollector_CollectNameNode(t *testing.T) {
@@ -213,8 +214,8 @@ func TestCollector_CollectNameNode(t *testing.T) {
214
215
collr := New()
216
collr.URL = ts.URL
216
- require.NoError(t, collr.Init())
217
- require.NoError(t, collr.Check())
217
+ require.NoError(t, collr.Init(context.Background()))
218
+ require.NoError(t, collr.Check(context.Background()))
219
220
expected := map[string]int64{
221
"fsns_blocks_total": 15,
@@ -259,7 +260,7 @@ func TestCollector_CollectNameNode(t *testing.T) {
260
"rpc_sent_bytes": 25067414,
261
}
262
262
- assert.Equal(t, expected, collr.Collect())
263
+ assert.Equal(t, expected, collr.Collect(context.Background()))
264
}
265
266
func TestCollector_CollectUnknownNode(t *testing.T) {
@@ -272,17 +273,17 @@ func TestCollector_CollectUnknownNode(t *testing.T) {
273
274
collr := New()
275
collr.URL = ts.URL
275
- require.NoError(t, collr.Init())
276
+ require.NoError(t, collr.Init(context.Background()))
277
277
- assert.Panics(t, func() { _ = collr.Collect() })
278
+ assert.Panics(t, func() { _ = collr.Collect(context.Background()) })
279
}
280
281
func TestCollector_CollectNoResponse(t *testing.T) {
282
collr := New()
283
collr.URL = "http://127.0.0.1:38001/jmx"
283
- require.NoError(t, collr.Init())
284
+ require.NoError(t, collr.Init(context.Background()))
285
285
- assert.Nil(t, collr.Collect())
286
+ assert.Nil(t, collr.Collect(context.Background()))
287
}
288
289
func TestCollector_CollectReceiveInvalidResponse(t *testing.T) {
@@ -295,9 +296,9 @@ func TestCollector_CollectReceiveInvalidResponse(t *testing.T) {
296
297
collr := New()
298
collr.URL = ts.URL
298
- require.NoError(t, collr.Init())
299
+ require.NoError(t, collr.Init(context.Background()))
300
300
- assert.Nil(t, collr.Collect())
301
+ assert.Nil(t, collr.Collect(context.Background()))
302
}
303
304
func TestCollector_CollectReceive404(t *testing.T) {
@@ -310,7 +311,7 @@ func TestCollector_CollectReceive404(t *testing.T) {
311
312
collr := New()
313
collr.URL = ts.URL
313
- require.NoError(t, collr.Init())
314
+ require.NoError(t, collr.Init(context.Background()))
315
315
- assert.Nil(t, collr.Collect())
316
+ assert.Nil(t, collr.Collect(context.Background()))
317
}
src/go/plugin/go.d/collector/hpssa/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package hpssa
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -62,7 +63,7 @@ func (c *Collector) Configuration() any {
63
return c.Config
64
}
65
65
-func (c *Collector) Init() error {
66
+func (c *Collector) Init(context.Context) error {
67
ssacli, err := c.initSsacliBinary()
68
if err != nil {
69
return fmt.Errorf("ssacli exec initialization: %v", err)
@@ -72,7 +73,7 @@ func (c *Collector) Init() error {
73
return nil
74
}
75
75
-func (c *Collector) Check() error {
76
+func (c *Collector) Check(context.Context) error {
77
mx, err := c.collect()
78
if err != nil {
79
return err
@@ -89,7 +90,7 @@ func (c *Collector) Charts() *module.Charts {
90
return c.charts
91
}
92
92
-func (c *Collector) Collect() map[string]int64 {
93
+func (c *Collector) Collect(context.Context) map[string]int64 {
94
mx, err := c.collect()
95
if err != nil {
96
c.Error(err)
@@ -102,4 +103,4 @@ func (c *Collector) Collect() map[string]int64 {
103
return mx
104
}
105
105
-func (c *Collector) Cleanup() {}
106
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/hpssa/collector_test.go
+9
-8
@@ -3,6 +3,7 @@
3
package hpssa
4
5
import (
6
+ "context"
7
"errors"
8
"os"
9
"testing"
@@ -53,9 +54,9 @@ func TestCollector_Init(t *testing.T) {
54
collr := New()
55
56
if test.wantFail {
56
- assert.Error(t, collr.Init())
57
+ assert.Error(t, collr.Init(context.Background()))
58
} else {
58
- assert.NoError(t, collr.Init())
59
+ assert.NoError(t, collr.Init(context.Background()))
60
}
61
})
62
}
@@ -74,7 +75,7 @@ func TestCollector_Cleanup(t *testing.T) {
75
prepare: func() *Collector {
76
collr := New()
77
collr.exec = prepareMockOkP212andP410i()
77
- _ = collr.Check()
78
+ _ = collr.Check(context.Background())
79
return collr
80
},
81
},
@@ -82,7 +83,7 @@ func TestCollector_Cleanup(t *testing.T) {
83
prepare: func() *Collector {
84
collr := New()
85
collr.exec = prepareMockOkP212andP410i()
85
- _ = collr.Collect()
86
+ _ = collr.Collect(context.Background())
87
return collr
88
},
89
},
@@ -92,7 +93,7 @@ func TestCollector_Cleanup(t *testing.T) {
93
t.Run(name, func(t *testing.T) {
94
collr := test.prepare()
95
95
- assert.NotPanics(t, collr.Cleanup)
96
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
97
})
98
}
99
}
@@ -139,9 +140,9 @@ func TestCollector_Check(t *testing.T) {
140
collr.exec = mock
141
142
if test.wantFail {
142
- assert.Error(t, collr.Check())
143
+ assert.Error(t, collr.Check(context.Background()))
144
} else {
144
- assert.NoError(t, collr.Check())
145
+ assert.NoError(t, collr.Check(context.Background()))
146
}
147
})
148
}
@@ -385,7 +386,7 @@ func TestCollector_Collect(t *testing.T) {
386
mock := test.prepareMock()
387
collr.exec = mock
388
388
- mx := collr.Collect()
389
+ mx := collr.Collect(context.Background())
390
391
assert.Equal(t, test.wantMetrics, mx)
392
src/go/plugin/go.d/collector/httpcheck/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package httpcheck
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -80,7 +81,7 @@ func (c *Collector) Configuration() any {
81
return c.Config
82
}
83
83
-func (c *Collector) Init() error {
84
+func (c *Collector) Init(context.Context) error {
85
if err := c.validateConfig(); err != nil {
86
return fmt.Errorf("config validation: %v", err)
87
}
@@ -119,7 +120,7 @@ func (c *Collector) Init() error {
120
return nil
121
}
122
122
-func (c *Collector) Check() error {
123
+func (c *Collector) Check(context.Context) error {
124
mx, err := c.collect()
125
if err != nil {
126
return err
@@ -134,7 +135,7 @@ func (c *Collector) Charts() *module.Charts {
135
return c.charts
136
}
137
137
-func (c *Collector) Collect() map[string]int64 {
138
+func (c *Collector) Collect(context.Context) map[string]int64 {
139
mx, err := c.collect()
140
if err != nil {
141
c.Error(err)
@@ -146,7 +147,7 @@ func (c *Collector) Collect() map[string]int64 {
147
return mx
148
}
149
149
-func (c *Collector) Cleanup() {
150
+func (c *Collector) Cleanup(context.Context) {
151
if c.httpClient != nil {
152
c.httpClient.CloseIdleConnections()
153
}
src/go/plugin/go.d/collector/httpcheck/collector_test.go
+12
-11
@@ -3,6 +3,7 @@
3
package httpcheck
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -77,9 +78,9 @@ func TestCollector_Init(t *testing.T) {
78
collr.Config = test.config
79
80
if test.wantFail {
80
- assert.Error(t, collr.Init())
81
+ assert.Error(t, collr.Init(context.Background()))
82
} else {
82
- assert.NoError(t, collr.Init())
83
+ assert.NoError(t, collr.Init(context.Background()))
84
}
85
})
86
}
@@ -101,7 +102,7 @@ func TestCollector_Charts(t *testing.T) {
102
prepare: func(t *testing.T) *Collector {
103
collr := New()
104
collr.URL = "http://127.0.0.1:38001"
104
- require.NoError(t, collr.Init())
105
+ require.NoError(t, collr.Init(context.Background()))
106
107
return collr
108
},
@@ -123,11 +124,11 @@ func TestCollector_Charts(t *testing.T) {
124
125
func TestCollector_Cleanup(t *testing.T) {
126
collr := New()
126
- assert.NotPanics(t, collr.Cleanup)
127
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
128
129
collr.URL = "http://127.0.0.1:38001"
129
- require.NoError(t, collr.Init())
130
- assert.NotPanics(t, collr.Cleanup)
130
+ require.NoError(t, collr.Init(context.Background()))
131
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
132
}
133
134
func TestCollector_Check(t *testing.T) {
@@ -150,12 +151,12 @@ func TestCollector_Check(t *testing.T) {
151
collr, cleanup := test.prepare()
152
defer cleanup()
153
153
- require.NoError(t, collr.Init())
154
+ require.NoError(t, collr.Init(context.Background()))
155
156
if test.wantFail {
156
- assert.Error(t, collr.Check())
157
+ assert.Error(t, collr.Check(context.Background()))
158
} else {
158
- assert.NoError(t, collr.Check())
159
+ assert.NoError(t, collr.Check(context.Background()))
160
}
161
})
162
}
@@ -459,12 +460,12 @@ func TestCollector_Collect(t *testing.T) {
460
test.update(collr)
461
}
462
462
- require.NoError(t, collr.Init())
463
+ require.NoError(t, collr.Init(context.Background()))
464
465
var mx map[string]int64
466
467
for i := 0; i < 2; i++ {
467
- mx = collr.Collect()
468
+ mx = collr.Collect(context.Background())
469
time.Sleep(time.Duration(collr.UpdateEvery) * time.Second)
470
}
471
src/go/plugin/go.d/collector/icecast/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package icecast
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -63,7 +64,7 @@ func (c *Collector) Configuration() any {
64
return c.Config
65
}
66
66
-func (c *Collector) Init() error {
67
+func (c *Collector) Init(context.Context) error {
68
if c.URL == "" {
69
return errors.New("url not set")
70
}
@@ -80,7 +81,7 @@ func (c *Collector) Init() error {
81
return nil
82
}
83
83
-func (c *Collector) Check() error {
84
+func (c *Collector) Check(context.Context) error {
85
mx, err := c.collect()
86
if err != nil {
87
return err
@@ -97,7 +98,7 @@ func (c *Collector) Charts() *module.Charts {
98
return c.charts
99
}
100
100
-func (c *Collector) Collect() map[string]int64 {
101
+func (c *Collector) Collect(context.Context) map[string]int64 {
102
mx, err := c.collect()
103
if err != nil {
104
c.Error(err)
@@ -110,7 +111,7 @@ func (c *Collector) Collect() map[string]int64 {
111
return mx
112
}
113
113
-func (c *Collector) Cleanup() {
114
+func (c *Collector) Cleanup(context.Context) {
115
if c.httpClient != nil {
116
c.httpClient.CloseIdleConnections()
117
}
src/go/plugin/go.d/collector/icecast/collector_test.go
+12
-11
@@ -3,6 +3,7 @@
3
package icecast
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -65,9 +66,9 @@ func TestCollector_Init(t *testing.T) {
66
collr.Config = test.config
67
68
if test.wantFail {
68
- assert.Error(t, collr.Init())
69
+ assert.Error(t, collr.Init(context.Background()))
70
} else {
70
- assert.NoError(t, collr.Init())
71
+ assert.NoError(t, collr.Init(context.Background()))
72
}
73
})
74
}
@@ -114,9 +115,9 @@ func TestCollector_Check(t *testing.T) {
115
defer cleanup()
116
117
if test.wantFail {
117
- assert.Error(t, collr.Check())
118
+ assert.Error(t, collr.Check(context.Background()))
119
} else {
119
- assert.NoError(t, collr.Check())
120
+ assert.NoError(t, collr.Check(context.Background()))
121
}
122
})
123
}
@@ -162,7 +163,7 @@ func TestCollector_Collect(t *testing.T) {
163
collr, cleanup := test.prepare(t)
164
defer cleanup()
165
165
- mx := collr.Collect()
166
+ mx := collr.Collect(context.Background())
167
168
require.Equal(t, test.wantMetrics, mx)
169
if len(test.wantMetrics) > 0 {
@@ -187,7 +188,7 @@ func prepareCaseMultipleSources(t *testing.T) (*Collector, func()) {
188
189
collr := New()
190
collr.URL = srv.URL
190
- require.NoError(t, collr.Init())
191
+ require.NoError(t, collr.Init(context.Background()))
192
193
return collr, srv.Close
194
}
@@ -206,7 +207,7 @@ func prepareCaseSingleSource(t *testing.T) (*Collector, func()) {
207
208
collr := New()
209
collr.URL = srv.URL
209
- require.NoError(t, collr.Init())
210
+ require.NoError(t, collr.Init(context.Background()))
211
212
return collr, srv.Close
213
}
@@ -225,7 +226,7 @@ func prepareCaseNoSources(t *testing.T) (*Collector, func()) {
226
227
collr := New()
228
collr.URL = srv.URL
228
- require.NoError(t, collr.Init())
229
+ require.NoError(t, collr.Init(context.Background()))
230
231
return collr, srv.Close
232
}
@@ -256,7 +257,7 @@ func prepareCaseUnexpectedJsonResponse(t *testing.T) (*Collector, func()) {
257
258
collr := New()
259
collr.URL = srv.URL
259
- require.NoError(t, collr.Init())
260
+ require.NoError(t, collr.Init(context.Background()))
261
262
return collr, srv.Close
263
}
@@ -270,7 +271,7 @@ func prepareCaseInvalidFormatResponse(t *testing.T) (*Collector, func()) {
271
272
collr := New()
273
collr.URL = srv.URL
273
- require.NoError(t, collr.Init())
274
+ require.NoError(t, collr.Init(context.Background()))
275
276
return collr, srv.Close
277
}
@@ -279,7 +280,7 @@ func prepareCaseConnectionRefused(t *testing.T) (*Collector, func()) {
280
t.Helper()
281
collr := New()
282
collr.URL = "http://127.0.0.1:65001"
282
- require.NoError(t, collr.Init())
283
+ require.NoError(t, collr.Init(context.Background()))
284
285
return collr, func() {}
286
}
src/go/plugin/go.d/collector/intelgpu/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package intelgpu
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -49,7 +50,7 @@ func (c *Collector) Configuration() any {
50
return c.Config
51
}
52
52
-func (c *Collector) Init() error {
53
+func (c *Collector) Init(context.Context) error {
54
topExec, err := c.initIntelGPUTopExec()
55
56
if err != nil {
@@ -61,7 +62,7 @@ func (c *Collector) Init() error {
62
return nil
63
}
64
64
-func (c *Collector) Check() error {
65
+func (c *Collector) Check(context.Context) error {
66
mx, err := c.collect()
67
if err != nil {
68
return err
@@ -78,7 +79,7 @@ func (c *Collector) Charts() *module.Charts {
79
return c.charts
80
}
81
81
-func (c *Collector) Collect() map[string]int64 {
82
+func (c *Collector) Collect(context.Context) map[string]int64 {
83
mx, err := c.collect()
84
if err != nil {
85
c.Error(err)
@@ -91,7 +92,7 @@ func (c *Collector) Collect() map[string]int64 {
92
return mx
93
}
94
94
-func (c *Collector) Cleanup() {
95
+func (c *Collector) Cleanup(context.Context) {
96
if c.exec != nil {
97
if err := c.exec.stop(); err != nil {
98
c.Error(err)
src/go/plugin/go.d/collector/intelgpu/collector_test.go
+9
-8
@@ -3,6 +3,7 @@
3
package intelgpu
4
5
import (
6
+ "context"
7
"errors"
8
"os"
9
"testing"
@@ -55,9 +56,9 @@ func TestCollector_Init(t *testing.T) {
56
test.prepare(collr)
57
58
if test.wantFail {
58
- assert.Error(t, collr.Init())
59
+ assert.Error(t, collr.Init(context.Background()))
60
} else {
60
- assert.NoError(t, collr.Init())
61
+ assert.NoError(t, collr.Init(context.Background()))
62
}
63
})
64
}
@@ -85,9 +86,9 @@ func TestCollector_Check(t *testing.T) {
86
collr.exec = mock
87
88
if test.wantFail {
88
- assert.Error(t, collr.Check())
89
+ assert.Error(t, collr.Check(context.Background()))
90
} else {
90
- assert.NoError(t, collr.Check())
91
+ assert.NoError(t, collr.Check(context.Background()))
92
}
93
})
94
}
@@ -123,7 +124,7 @@ func TestCollector_Collect(t *testing.T) {
124
mock := test.prepareMock()
125
collr.exec = mock
126
126
- mx := collr.Collect()
127
+ mx := collr.Collect(context.Background())
128
129
assert.Equal(t, test.wantMetrics, mx)
130
if len(test.wantMetrics) > 0 {
@@ -146,7 +147,7 @@ func TestCollector_Cleanup(t *testing.T) {
147
prepare: func() *Collector {
148
collr := New()
149
collr.exec = prepareMockOK()
149
- _ = collr.Check()
150
+ _ = collr.Check(context.Background())
151
return collr
152
},
153
},
@@ -154,7 +155,7 @@ func TestCollector_Cleanup(t *testing.T) {
155
prepare: func() *Collector {
156
collr := New()
157
collr.exec = prepareMockOK()
157
- _ = collr.Collect()
158
+ _ = collr.Collect(context.Background())
159
return collr
160
},
161
},
@@ -166,7 +167,7 @@ func TestCollector_Cleanup(t *testing.T) {
167
168
mock, ok := collr.exec.(*mockIntelGpuTop)
169
169
- assert.NotPanics(t, collr.Cleanup)
170
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
171
172
if ok {
173
assert.True(t, mock.stopCalled)
src/go/plugin/go.d/collector/ipfs/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package ipfs
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -64,7 +65,7 @@ func (c *Collector) Configuration() any {
65
return c.Config
66
}
67
67
-func (c *Collector) Init() error {
68
+func (c *Collector) Init(context.Context) error {
69
if c.URL == "" {
70
return errors.New("url not set")
71
}
@@ -90,7 +91,7 @@ func (c *Collector) Init() error {
91
return nil
92
}
93
93
-func (c *Collector) Check() error {
94
+func (c *Collector) Check(context.Context) error {
95
mx, err := c.collect()
96
if err != nil {
97
return err
@@ -107,7 +108,7 @@ func (c *Collector) Charts() *module.Charts {
108
return c.charts
109
}
110
110
-func (c *Collector) Collect() map[string]int64 {
111
+func (c *Collector) Collect(context.Context) map[string]int64 {
112
mx, err := c.collect()
113
if err != nil {
114
c.Error(err)
@@ -120,7 +121,7 @@ func (c *Collector) Collect() map[string]int64 {
121
return mx
122
}
123
123
-func (c *Collector) Cleanup() {
124
+func (c *Collector) Cleanup(context.Context) {
125
if c.httpClient != nil {
126
c.httpClient.CloseIdleConnections()
127
}
src/go/plugin/go.d/collector/ipfs/collector_test.go
+10
-9
@@ -3,6 +3,7 @@
3
package ipfs
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -67,9 +68,9 @@ func TestCollector_Init(t *testing.T) {
68
collr.Config = test.config
69
70
if test.wantFail {
70
- assert.Error(t, collr.Init())
71
+ assert.Error(t, collr.Init(context.Background()))
72
} else {
72
- assert.NoError(t, collr.Init())
73
+ assert.NoError(t, collr.Init(context.Background()))
74
}
75
})
76
}
@@ -112,9 +113,9 @@ func TestCollector_Check(t *testing.T) {
113
defer cleanup()
114
115
if test.wantFail {
115
- assert.Error(t, collr.Check())
116
+ assert.Error(t, collr.Check(context.Background()))
117
} else {
117
- assert.NoError(t, collr.Check())
118
+ assert.NoError(t, collr.Check(context.Background()))
119
}
120
})
121
}
@@ -162,7 +163,7 @@ func TestCollector_Collect(t *testing.T) {
163
collr, cleanup := test.prepare(t)
164
defer cleanup()
165
165
- mx := collr.Collect()
166
+ mx := collr.Collect(context.Background())
167
168
require.Equal(t, test.wantMetrics, mx)
169
@@ -193,7 +194,7 @@ func prepareCaseOkDefault(t *testing.T) (*Collector, func()) {
194
195
collr := New()
196
collr.URL = srv.URL
196
- require.NoError(t, collr.Init())
197
+ require.NoError(t, collr.Init(context.Background()))
198
199
return collr, srv.Close
200
}
@@ -234,7 +235,7 @@ func prepareCaseUnexpectedJsonResponse(t *testing.T) (*Collector, func()) {
235
236
collr := New()
237
collr.URL = srv.URL
237
- require.NoError(t, collr.Init())
238
+ require.NoError(t, collr.Init(context.Background()))
239
240
return collr, srv.Close
241
}
@@ -248,7 +249,7 @@ func prepareCaseInvalidFormatResponse(t *testing.T) (*Collector, func()) {
249
250
collr := New()
251
collr.URL = srv.URL
251
- require.NoError(t, collr.Init())
252
+ require.NoError(t, collr.Init(context.Background()))
253
254
return collr, srv.Close
255
}
@@ -257,7 +258,7 @@ func prepareCaseConnectionRefused(t *testing.T) (*Collector, func()) {
258
t.Helper()
259
collr := New()
260
collr.URL = "http://127.0.0.1:65001"
260
- require.NoError(t, collr.Init())
261
+ require.NoError(t, collr.Init(context.Background()))
262
263
return collr, func() {}
264
}
src/go/plugin/go.d/collector/isc_dhcpd/collector.go
+5
-4
@@ -5,6 +5,7 @@
5
package isc_dhcpd
6
7
import (
8
+ "context"
9
_ "embed"
10
"errors"
11
"fmt"
@@ -65,7 +66,7 @@ func (c *Collector) Configuration() any {
66
return c.Config
67
}
68
68
-func (c *Collector) Init() error {
69
+func (c *Collector) Init(context.Context) error {
70
err := c.validateConfig()
71
if err != nil {
72
return fmt.Errorf("config validation: %v", err)
@@ -89,7 +90,7 @@ func (c *Collector) Init() error {
90
return nil
91
}
92
92
-func (c *Collector) Check() error {
93
+func (c *Collector) Check(context.Context) error {
94
mx, err := c.collect()
95
if err != nil {
96
return err
@@ -104,7 +105,7 @@ func (c *Collector) Charts() *module.Charts {
105
return c.charts
106
}
107
107
-func (c *Collector) Collect() map[string]int64 {
108
+func (c *Collector) Collect(context.Context) map[string]int64 {
109
mx, err := c.collect()
110
if err != nil {
111
c.Error(err)
@@ -117,4 +118,4 @@ func (c *Collector) Collect() map[string]int64 {
118
return mx
119
}
120
120
-func (c *Collector) Cleanup() {}
121
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/isc_dhcpd/collector_test.go
+10
-9
@@ -5,6 +5,7 @@
5
package isc_dhcpd
6
7
import (
8
+ "context"
9
"os"
10
"testing"
11
@@ -33,7 +34,7 @@ func TestCollector_ConfigurationSerialize(t *testing.T) {
34
}
35
36
func TestCollector_Cleanup(t *testing.T) {
36
- assert.NotPanics(t, New().Cleanup)
37
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
38
}
39
40
func TestCollector_Init(t *testing.T) {
@@ -84,9 +85,9 @@ func TestCollector_Init(t *testing.T) {
85
collr.Config = test.config
86
87
if test.wantFail {
87
- assert.Error(t, collr.Init())
88
+ assert.Error(t, collr.Init(context.Background()))
89
} else {
89
- assert.NoError(t, collr.Init())
90
+ assert.NoError(t, collr.Init(context.Background()))
91
}
92
})
93
}
@@ -108,12 +109,12 @@ func TestCollector_Check(t *testing.T) {
109
for name, test := range tests {
110
t.Run(name, func(t *testing.T) {
111
collr := test.prepare()
111
- require.NoError(t, collr.Init())
112
+ require.NoError(t, collr.Init(context.Background()))
113
114
if test.wantFail {
114
- assert.Error(t, collr.Check())
115
+ assert.Error(t, collr.Check(context.Background()))
116
} else {
116
- assert.NoError(t, collr.Check())
117
+ assert.NoError(t, collr.Check(context.Background()))
118
}
119
})
120
}
@@ -125,7 +126,7 @@ func TestCollector_Charts(t *testing.T) {
126
collr.Pools = []PoolConfig{
127
{Name: "name", Networks: "192.0.2.0/24"},
128
}
128
- require.NoError(t, collr.Init())
129
+ require.NoError(t, collr.Init(context.Background()))
130
131
assert.NotNil(t, collr.Charts())
132
}
@@ -226,9 +227,9 @@ func TestCollector_Collect(t *testing.T) {
227
for name, test := range tests {
228
t.Run(name, func(t *testing.T) {
229
collr := test.prepare()
229
- require.NoError(t, collr.Init())
230
+ require.NoError(t, collr.Init(context.Background()))
231
231
- mx := collr.Collect()
232
+ mx := collr.Collect(context.Background())
233
234
assert.Equal(t, test.wantCollected, mx)
235
if len(mx) > 0 {
src/go/plugin/go.d/collector/k8s_kubelet/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package k8s_kubelet
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -70,7 +71,7 @@ func (c *Collector) Configuration() any {
71
return c.Config
72
}
73
73
-func (c *Collector) Init() error {
74
+func (c *Collector) Init(context.Context) error {
75
if err := c.validateConfig(); err != nil {
76
return fmt.Errorf("config validation: %v", err)
77
}
@@ -88,7 +89,7 @@ func (c *Collector) Init() error {
89
return nil
90
}
91
91
-func (c *Collector) Check() error {
92
+func (c *Collector) Check(context.Context) error {
93
mx, err := c.collect()
94
if err != nil {
95
return err
@@ -103,7 +104,7 @@ func (c *Collector) Charts() *Charts {
104
return c.charts
105
}
106
106
-func (c *Collector) Collect() map[string]int64 {
107
+func (c *Collector) Collect(context.Context) map[string]int64 {
108
mx, err := c.collect()
109
110
if err != nil {
@@ -114,7 +115,7 @@ func (c *Collector) Collect() map[string]int64 {
115
return mx
116
}
117
117
-func (c *Collector) Cleanup() {
118
+func (c *Collector) Cleanup(context.Context) {
119
if c.prom != nil && c.prom.HTTPClient() != nil {
120
c.prom.HTTPClient().CloseIdleConnections()
121
}
src/go/plugin/go.d/collector/k8s_kubelet/collector_test.go
+16
-15
@@ -3,6 +3,7 @@
3
package k8s_kubelet
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -42,18 +43,18 @@ func TestCollector_Charts(t *testing.T) {
43
}
44
45
func TestCollector_Cleanup(t *testing.T) {
45
- New().Cleanup()
46
+ New().Cleanup(context.Background())
47
}
48
49
func TestCollector_Init(t *testing.T) {
49
- assert.NoError(t, New().Init())
50
+ assert.NoError(t, New().Init(context.Background()))
51
}
52
53
func TestCollector_Init_ReadServiceAccountToken(t *testing.T) {
54
collr := New()
55
collr.TokenPath = "testdata/token.txt"
56
56
- assert.NoError(t, collr.Init())
57
+ assert.NoError(t, collr.Init(context.Background()))
58
assert.Equal(t, "Bearer "+string(dataServiceAccountToken), collr.RequestConfig.Headers["Authorization"])
59
}
60
@@ -61,7 +62,7 @@ func TestCollector_InitErrorOnCreatingClientWrongTLSCA(t *testing.T) {
62
collr := New()
63
collr.ClientConfig.TLSConfig.TLSCA = "testdata/tls"
64
64
- assert.Error(t, collr.Init())
65
+ assert.Error(t, collr.Init(context.Background()))
66
}
67
68
func TestCollector_Check(t *testing.T) {
@@ -74,15 +75,15 @@ func TestCollector_Check(t *testing.T) {
75
76
collr := New()
77
collr.URL = ts.URL + "/metrics"
77
- require.NoError(t, collr.Init())
78
- assert.NoError(t, collr.Check())
78
+ require.NoError(t, collr.Init(context.Background()))
79
+ assert.NoError(t, collr.Check(context.Background()))
80
}
81
82
func TestCollector_Check_ConnectionRefused(t *testing.T) {
83
collr := New()
84
collr.URL = "http://127.0.0.1:38001/metrics"
84
- require.NoError(t, collr.Init())
85
- assert.Error(t, collr.Check())
85
+ require.NoError(t, collr.Init(context.Background()))
86
+ assert.Error(t, collr.Check(context.Background()))
87
}
88
89
func TestCollector_Collect(t *testing.T) {
@@ -95,8 +96,8 @@ func TestCollector_Collect(t *testing.T) {
96
97
collr := New()
98
collr.URL = ts.URL + "/metrics"
98
- require.NoError(t, collr.Init())
99
- require.NoError(t, collr.Check())
99
+ require.NoError(t, collr.Init(context.Background()))
100
+ require.NoError(t, collr.Check(context.Background()))
101
102
expected := map[string]int64{
103
"apiserver_audit_requests_rejected_total": 0,
@@ -181,7 +182,7 @@ func TestCollector_Collect(t *testing.T) {
182
"volume_manager_plugin_kubernetes.io/secret_state_desired": 4,
183
}
184
184
- assert.Equal(t, expected, collr.Collect())
185
+ assert.Equal(t, expected, collr.Collect(context.Background()))
186
}
187
188
func TestCollector_Collect_ReceiveInvalidResponse(t *testing.T) {
@@ -194,8 +195,8 @@ func TestCollector_Collect_ReceiveInvalidResponse(t *testing.T) {
195
196
collr := New()
197
collr.URL = ts.URL + "/metrics"
197
- require.NoError(t, collr.Init())
198
- assert.Error(t, collr.Check())
198
+ require.NoError(t, collr.Init(context.Background()))
199
+ assert.Error(t, collr.Check(context.Background()))
200
}
201
202
func TestCollector_Collect_Receive404(t *testing.T) {
@@ -208,6 +209,6 @@ func TestCollector_Collect_Receive404(t *testing.T) {
209
210
collr := New()
211
collr.URL = ts.URL + "/metrics"
211
- require.NoError(t, collr.Init())
212
- assert.Error(t, collr.Check())
212
+ require.NoError(t, collr.Init(context.Background()))
213
+ assert.Error(t, collr.Check(context.Background()))
214
}
src/go/plugin/go.d/collector/k8s_kubeproxy/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package k8s_kubeproxy
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -63,7 +64,7 @@ func (c *Collector) Configuration() any {
64
return c.Config
65
}
66
66
-func (c *Collector) Init() error {
67
+func (c *Collector) Init(context.Context) error {
68
if err := c.validateConfig(); err != nil {
69
return fmt.Errorf("config validation: %v", err)
70
}
@@ -77,7 +78,7 @@ func (c *Collector) Init() error {
78
return nil
79
}
80
80
-func (c *Collector) Check() error {
81
+func (c *Collector) Check(context.Context) error {
82
mx, err := c.collect()
83
if err != nil {
84
return err
@@ -92,7 +93,7 @@ func (c *Collector) Charts() *Charts {
93
return c.charts
94
}
95
95
-func (c *Collector) Collect() map[string]int64 {
96
+func (c *Collector) Collect(context.Context) map[string]int64 {
97
mx, err := c.collect()
98
99
if err != nil {
@@ -103,7 +104,7 @@ func (c *Collector) Collect() map[string]int64 {
104
return mx
105
}
106
106
-func (c *Collector) Cleanup() {
107
+func (c *Collector) Cleanup(context.Context) {
108
if c.prom != nil && c.prom.HTTPClient() != nil {
109
c.prom.HTTPClient().CloseIdleConnections()
110
}
src/go/plugin/go.d/collector/k8s_kubeproxy/collector_test.go
+15
-14
@@ -3,6 +3,7 @@
3
package k8s_kubeproxy
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -40,17 +41,17 @@ func TestCollector_Charts(t *testing.T) {
41
}
42
43
func TestCollector_Cleanup(t *testing.T) {
43
- New().Cleanup()
44
+ New().Cleanup(context.Background())
45
}
46
47
func TestCollector_Init(t *testing.T) {
47
- assert.NoError(t, New().Init())
48
+ assert.NoError(t, New().Init(context.Background()))
49
}
50
51
func TestCollector_InitNG(t *testing.T) {
52
collr := New()
53
collr.URL = ""
53
- assert.Error(t, collr.Init())
54
+ assert.Error(t, collr.Init(context.Background()))
55
}
56
57
func TestCollector_Check(t *testing.T) {
@@ -63,15 +64,15 @@ func TestCollector_Check(t *testing.T) {
64
65
collr := New()
66
collr.URL = ts.URL + "/metrics"
66
- require.NoError(t, collr.Init())
67
- assert.NoError(t, collr.Check())
67
+ require.NoError(t, collr.Init(context.Background()))
68
+ assert.NoError(t, collr.Check(context.Background()))
69
}
70
71
func TestCollector_CheckNG(t *testing.T) {
72
collr := New()
73
collr.URL = "http://127.0.0.1:38001/metrics"
73
- require.NoError(t, collr.Init())
74
- assert.Error(t, collr.Check())
74
+ require.NoError(t, collr.Init(context.Background()))
75
+ assert.Error(t, collr.Check(context.Background()))
76
}
77
78
func TestCollector_Collect(t *testing.T) {
@@ -84,8 +85,8 @@ func TestCollector_Collect(t *testing.T) {
85
86
collr := New()
87
collr.URL = ts.URL + "/metrics"
87
- require.NoError(t, collr.Init())
88
- require.NoError(t, collr.Check())
88
+ require.NoError(t, collr.Init(context.Background()))
89
+ require.NoError(t, collr.Check(context.Background()))
90
91
expected := map[string]int64{
92
"sync_proxy_rules_count": 2669,
@@ -114,7 +115,7 @@ func TestCollector_Collect(t *testing.T) {
115
"http_request_duration_099": 9464,
116
}
117
117
- assert.Equal(t, expected, collr.Collect())
118
+ assert.Equal(t, expected, collr.Collect(context.Background()))
119
}
120
121
func TestCollector_InvalidData(t *testing.T) {
@@ -127,8 +128,8 @@ func TestCollector_InvalidData(t *testing.T) {
128
129
collr := New()
130
collr.URL = ts.URL + "/metrics"
130
- require.NoError(t, collr.Init())
131
- assert.Error(t, collr.Check())
131
+ require.NoError(t, collr.Init(context.Background()))
132
+ assert.Error(t, collr.Check(context.Background()))
133
}
134
135
func TestCollector_404(t *testing.T) {
@@ -141,6 +142,6 @@ func TestCollector_404(t *testing.T) {
142
143
collr := New()
144
collr.URL = ts.URL + "/metrics"
144
- require.NoError(t, collr.Init())
145
- assert.Error(t, collr.Check())
145
+ require.NoError(t, collr.Init(context.Background()))
146
+ assert.Error(t, collr.Check(context.Background()))
147
}
src/go/plugin/go.d/collector/k8s_state/collector.go
+4
-4
@@ -70,7 +70,7 @@ func (c *Collector) Configuration() any {
70
return c.Config
71
}
72
73
-func (c *Collector) Init() error {
73
+func (c *Collector) Init(context.Context) error {
74
client, err := c.initClient()
75
if err != nil {
76
return fmt.Errorf("init k8s client: %v", err)
@@ -84,7 +84,7 @@ func (c *Collector) Init() error {
84
return nil
85
}
86
87
-func (c *Collector) Check() error {
87
+func (c *Collector) Check(context.Context) error {
88
if c.client == nil || c.discoverer == nil {
89
return errors.New("not initialized")
90
}
@@ -103,7 +103,7 @@ func (c *Collector) Charts() *module.Charts {
103
return c.charts
104
}
105
106
-func (c *Collector) Collect() map[string]int64 {
106
+func (c *Collector) Collect(context.Context) map[string]int64 {
107
ms, err := c.collect()
108
if err != nil {
109
c.Error(err)
@@ -115,7 +115,7 @@ func (c *Collector) Collect() map[string]int64 {
115
return ms
116
}
117
118
-func (c *Collector) Cleanup() {
118
+func (c *Collector) Cleanup(context.Context) {
119
if c.ctxCancel == nil {
120
return
121
}
src/go/plugin/go.d/collector/k8s_state/collector_test.go
+21
-21
@@ -70,9 +70,9 @@ func TestCollector_Init(t *testing.T) {
70
collr := test.prepare()
71
72
if test.wantFail {
73
- assert.Error(t, collr.Init())
73
+ assert.Error(t, collr.Init(context.Background()))
74
} else {
75
- assert.NoError(t, collr.Init())
75
+ assert.NoError(t, collr.Init(context.Background()))
76
}
77
})
78
}
@@ -105,12 +105,12 @@ func TestCollector_Check(t *testing.T) {
105
for name, test := range tests {
106
t.Run(name, func(t *testing.T) {
107
collr := test.prepare()
108
- require.NoError(t, collr.Init())
108
+ require.NoError(t, collr.Init(context.Background()))
109
110
if test.wantFail {
111
- assert.Error(t, collr.Check())
111
+ assert.Error(t, collr.Check(context.Background()))
112
} else {
113
- assert.NoError(t, collr.Check())
113
+ assert.NoError(t, collr.Check(context.Background()))
114
}
115
})
116
}
@@ -162,14 +162,14 @@ func TestCollector_Cleanup(t *testing.T) {
162
collr := test.prepare()
163
164
if test.doInit {
165
- _ = collr.Init()
165
+ _ = collr.Init(context.Background())
166
}
167
if test.doCollect {
168
- _ = collr.Collect()
168
+ _ = collr.Collect(context.Background())
169
time.Sleep(collr.initDelay)
170
}
171
172
- assert.NotPanics(t, collr.Cleanup)
172
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
173
time.Sleep(time.Second)
174
if test.doCollect {
175
assert.True(t, collr.discoverer.stopped())
@@ -197,7 +197,7 @@ func TestCollector_Collect(t *testing.T) {
197
)
198
199
step1 := func(t *testing.T, collr *Collector) {
200
- mx := collr.Collect()
200
+ mx := collr.Collect(context.Background())
201
expected := map[string]int64{
202
"discovery_node_discoverer_state": 1,
203
"discovery_pod_discoverer_state": 1,
@@ -265,7 +265,7 @@ func TestCollector_Collect(t *testing.T) {
265
)
266
267
step1 := func(t *testing.T, collr *Collector) {
268
- mx := collr.Collect()
268
+ mx := collr.Collect(context.Background())
269
expected := map[string]int64{
270
"discovery_node_discoverer_state": 1,
271
"discovery_pod_discoverer_state": 1,
@@ -366,7 +366,7 @@ func TestCollector_Collect(t *testing.T) {
366
)
367
368
step1 := func(t *testing.T, collr *Collector) {
369
- mx := collr.Collect()
369
+ mx := collr.Collect(context.Background())
370
expected := map[string]int64{
371
"discovery_node_discoverer_state": 1,
372
"discovery_pod_discoverer_state": 1,
@@ -505,12 +505,12 @@ func TestCollector_Collect(t *testing.T) {
505
pod,
506
)
507
step1 := func(t *testing.T, collr *Collector) {
508
- _ = collr.Collect()
508
+ _ = collr.Collect(context.Background())
509
_ = client.CoreV1().Pods(pod.Namespace).Delete(ctx, pod.Name, metav1.DeleteOptions{})
510
}
511
512
step2 := func(t *testing.T, collr *Collector) {
513
- mx := collr.Collect()
513
+ mx := collr.Collect(context.Background())
514
expected := map[string]int64{
515
"discovery_node_discoverer_state": 1,
516
"discovery_pod_discoverer_state": 1,
@@ -586,7 +586,7 @@ func TestCollector_Collect(t *testing.T) {
586
podUpdated := newPod(node.Name, "pod01") // with set Spec.NodeName
587
588
step1 := func(t *testing.T, collr *Collector) {
589
- _ = collr.Collect()
589
+ _ = collr.Collect(context.Background())
590
for _, c := range *collr.Charts() {
591
if strings.HasPrefix(c.ID, "pod_") {
592
ok := isLabelValueSet(c, labelKeyNodeName)
@@ -597,7 +597,7 @@ func TestCollector_Collect(t *testing.T) {
597
step2 := func(t *testing.T, collr *Collector) {
598
_, _ = client.CoreV1().Pods(podOrig.Namespace).Update(ctx, podUpdated, metav1.UpdateOptions{})
599
time.Sleep(time.Millisecond * 50)
600
- _ = collr.Collect()
600
+ _ = collr.Collect(context.Background())
601
602
for _, c := range *collr.Charts() {
603
if strings.HasPrefix(c.ID, "pod_") {
@@ -624,12 +624,12 @@ func TestCollector_Collect(t *testing.T) {
624
pod1,
625
)
626
step1 := func(t *testing.T, collr *Collector) {
627
- _ = collr.Collect()
627
+ _ = collr.Collect(context.Background())
628
_, _ = client.CoreV1().Pods(pod1.Namespace).Create(ctx, pod2, metav1.CreateOptions{})
629
}
630
631
step2 := func(t *testing.T, collr *Collector) {
632
- mx := collr.Collect()
632
+ mx := collr.Collect(context.Background())
633
expected := map[string]int64{
634
"discovery_node_discoverer_state": 1,
635
"discovery_pod_discoverer_state": 1,
@@ -840,13 +840,13 @@ func TestCollector_Collect(t *testing.T) {
840
collr := New()
841
collr.newKubeClient = func() (kubernetes.Interface, error) { return test.client, nil }
842
843
- require.NoError(t, collr.Init())
844
- require.NoError(t, collr.Check())
845
- defer collr.Cleanup()
843
+ require.NoError(t, collr.Init(context.Background()))
844
+ require.NoError(t, collr.Check(context.Background()))
845
+ defer collr.Cleanup(context.Background())
846
847
for i, executeStep := range test.steps {
848
if i == 0 {
849
- _ = collr.Collect()
849
+ _ = collr.Collect(context.Background())
850
time.Sleep(collr.initDelay)
851
} else {
852
time.Sleep(time.Second)
src/go/plugin/go.d/collector/lighttpd/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package lighttpd
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -59,7 +60,7 @@ func (c *Collector) Configuration() any {
60
return c.Config
61
}
62
62
-func (c *Collector) Init() error {
63
+func (c *Collector) Init(context.Context) error {
64
if c.URL == "" {
65
return errors.New("URL is required but not set")
66
}
@@ -79,7 +80,7 @@ func (c *Collector) Init() error {
80
return nil
81
}
82
82
-func (c *Collector) Check() error {
83
+func (c *Collector) Check(context.Context) error {
84
mx, err := c.collect()
85
if err != nil {
86
return err
@@ -96,7 +97,7 @@ func (c *Collector) Charts() *Charts {
97
return c.charts
98
}
99
99
-func (c *Collector) Collect() map[string]int64 {
100
+func (c *Collector) Collect(context.Context) map[string]int64 {
101
mx, err := c.collect()
102
if err != nil {
103
c.Error(err)
@@ -106,7 +107,7 @@ func (c *Collector) Collect() map[string]int64 {
107
return mx
108
}
109
109
-func (c *Collector) Cleanup() {
110
+func (c *Collector) Cleanup(context.Context) {
111
if c.httpClient != nil {
112
c.httpClient.CloseIdleConnections()
113
}
src/go/plugin/go.d/collector/lighttpd/collector_test.go
+17
-16
@@ -3,6 +3,7 @@
3
package lighttpd
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -35,19 +36,19 @@ func TestCollector_ConfigurationSerialize(t *testing.T) {
36
module.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML)
37
}
38
38
-func TestCollector_Cleanup(t *testing.T) { New().Cleanup() }
39
+func TestCollector_Cleanup(t *testing.T) { New().Cleanup(context.Background()) }
40
41
func TestCollector_Init(t *testing.T) {
42
collr := New()
43
43
- require.NoError(t, collr.Init())
44
+ require.NoError(t, collr.Init(context.Background()))
45
}
46
47
func TestCollector_InitNG(t *testing.T) {
48
collr := New()
49
50
collr.URL = ""
50
- assert.Error(t, collr.Init())
51
+ assert.Error(t, collr.Init(context.Background()))
52
}
53
54
func TestCollector_Check(t *testing.T) {
@@ -60,16 +61,16 @@ func TestCollector_Check(t *testing.T) {
61
62
collr := New()
63
collr.URL = ts.URL + "/server-status?auto"
63
- require.NoError(t, collr.Init())
64
- assert.NoError(t, collr.Check())
64
+ require.NoError(t, collr.Init(context.Background()))
65
+ assert.NoError(t, collr.Check(context.Background()))
66
}
67
68
func TestCollector_CheckNG(t *testing.T) {
69
collr := New()
70
71
collr.URL = "http://127.0.0.1:38001/server-status?auto"
71
- require.NoError(t, collr.Init())
72
- assert.Error(t, collr.Check())
72
+ require.NoError(t, collr.Init(context.Background()))
73
+ assert.Error(t, collr.Check(context.Background()))
74
}
75
76
func TestCollector_Charts(t *testing.T) { assert.NotNil(t, New().Charts()) }
@@ -84,8 +85,8 @@ func TestCollector_Collect(t *testing.T) {
85
86
collr := New()
87
collr.URL = ts.URL + "/server-status?auto"
87
- require.NoError(t, collr.Init())
88
- require.NoError(t, collr.Check())
88
+ require.NoError(t, collr.Init(context.Background()))
89
+ require.NoError(t, collr.Check(context.Background()))
90
91
expected := map[string]int64{
92
"scoreboard_waiting": 125,
@@ -108,7 +109,7 @@ func TestCollector_Collect(t *testing.T) {
109
"total_accesses": 12,
110
}
111
111
- assert.Equal(t, expected, collr.Collect())
112
+ assert.Equal(t, expected, collr.Collect(context.Background()))
113
}
114
115
func TestCollector_InvalidData(t *testing.T) {
@@ -121,8 +122,8 @@ func TestCollector_InvalidData(t *testing.T) {
122
123
collr := New()
124
collr.URL = ts.URL + "/server-status?auto"
124
- require.NoError(t, collr.Init())
125
- assert.Error(t, collr.Check())
125
+ require.NoError(t, collr.Init(context.Background()))
126
+ assert.Error(t, collr.Check(context.Background()))
127
}
128
129
func TestCollector_ApacheData(t *testing.T) {
@@ -135,8 +136,8 @@ func TestCollector_ApacheData(t *testing.T) {
136
137
collr := New()
138
collr.URL = ts.URL + "/server-status?auto"
138
- require.NoError(t, collr.Init())
139
- require.Error(t, collr.Check())
139
+ require.NoError(t, collr.Init(context.Background()))
140
+ require.Error(t, collr.Check(context.Background()))
141
}
142
143
func TestCollector_404(t *testing.T) {
@@ -149,6 +150,6 @@ func TestCollector_404(t *testing.T) {
150
151
collr := New()
152
collr.URL = ts.URL + "/server-status?auto"
152
- require.NoError(t, collr.Init())
153
- assert.Error(t, collr.Check())
153
+ require.NoError(t, collr.Init(context.Background()))
154
+ assert.Error(t, collr.Check(context.Background()))
155
}
src/go/plugin/go.d/collector/litespeed/collector.go
+5
-5
@@ -5,6 +5,7 @@
5
package litespeed
6
7
import (
8
+ "context"
9
_ "embed"
10
"errors"
11
@@ -54,14 +55,14 @@ func (c *Collector) Configuration() any {
55
return c.Config
56
}
57
57
-func (c *Collector) Init() error {
58
+func (c *Collector) Init(context.Context) error {
59
if c.ReportsDir == "" {
60
return errors.New("reports_dir is required")
61
}
62
return nil
63
}
64
64
-func (c *Collector) Check() error {
65
+func (c *Collector) Check(context.Context) error {
66
mx, err := c.collect()
67
if err != nil {
68
return err
@@ -78,9 +79,8 @@ func (c *Collector) Charts() *module.Charts {
79
return c.charts
80
}
81
81
-func (c *Collector) Collect() map[string]int64 {
82
+func (c *Collector) Collect(context.Context) map[string]int64 {
83
mx, err := c.collect()
83
-
84
if err != nil {
85
c.Error(err)
86
return nil
@@ -89,4 +89,4 @@ func (c *Collector) Collect() map[string]int64 {
89
return mx
90
}
91
92
-func (c *Collector) Cleanup() {}
92
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/litespeed/collector_test.go
+6
-5
@@ -5,6 +5,7 @@
5
package litespeed
6
7
import (
8
+ "context"
9
"os"
10
"testing"
11
@@ -55,9 +56,9 @@ func TestCollector_Init(t *testing.T) {
56
collr.Config = test.config
57
58
if test.wantFail {
58
- assert.Error(t, collr.Init())
59
+ assert.Error(t, collr.Init(context.Background()))
60
} else {
60
- assert.NoError(t, collr.Init())
61
+ assert.NoError(t, collr.Init(context.Background()))
62
}
63
})
64
}
@@ -87,9 +88,9 @@ func TestCollector_Check(t *testing.T) {
88
collr := test.prepareLitespeed()
89
90
if test.wantFail {
90
- assert.Error(t, collr.Check())
91
+ assert.Error(t, collr.Check(context.Background()))
92
} else {
92
- assert.NoError(t, collr.Check())
93
+ assert.NoError(t, collr.Check(context.Background()))
94
}
95
})
96
}
@@ -127,7 +128,7 @@ func TestCollector_Collect(t *testing.T) {
128
t.Run(name, func(t *testing.T) {
129
collr := test.prepareLitespeed()
130
130
- mx := collr.Collect()
131
+ mx := collr.Collect(context.Background())
132
133
assert.Equal(t, test.wantMetrics, mx)
134
src/go/plugin/go.d/collector/logind/collector.go
+5
-4
@@ -5,6 +5,7 @@
5
package logind
6
7
import (
8
+ "context"
9
_ "embed"
10
"errors"
11
"time"
@@ -58,11 +59,11 @@ func (c *Collector) Configuration() any {
59
return c.Config
60
}
61
61
-func (c *Collector) Init() error {
62
+func (c *Collector) Init(context.Context) error {
63
return nil
64
}
65
65
-func (c *Collector) Check() error {
66
+func (c *Collector) Check(context.Context) error {
67
mx, err := c.collect()
68
if err != nil {
69
return err
@@ -77,7 +78,7 @@ func (c *Collector) Charts() *module.Charts {
78
return c.charts
79
}
80
80
-func (c *Collector) Collect() map[string]int64 {
81
+func (c *Collector) Collect(context.Context) map[string]int64 {
82
mx, err := c.collect()
83
if err != nil {
84
c.Error(err)
@@ -89,7 +90,7 @@ func (c *Collector) Collect() map[string]int64 {
90
return mx
91
}
92
92
-func (c *Collector) Cleanup() {
93
+func (c *Collector) Cleanup(context.Context) {
94
if c.conn != nil {
95
c.conn.Close()
96
}
src/go/plugin/go.d/collector/logind/collector_test.go
+12
-11
@@ -5,6 +5,7 @@
5
package logind
6
7
import (
8
+ "context"
9
"errors"
10
"os"
11
"testing"
@@ -52,9 +53,9 @@ func TestCollector_Init(t *testing.T) {
53
collr.Config = test.config
54
55
if test.wantFail {
55
- assert.Error(t, collr.Init())
56
+ assert.Error(t, collr.Init(context.Background()))
57
} else {
57
- assert.NoError(t, collr.Init())
58
+ assert.NoError(t, collr.Init(context.Background()))
59
}
60
})
61
}
@@ -75,15 +76,15 @@ func TestCollector_Cleanup(t *testing.T) {
76
},
77
"after Init": {
78
wantClose: false,
78
- prepare: func(l *Collector) { _ = l.Init() },
79
+ prepare: func(l *Collector) { _ = l.Init(context.Background()) },
80
},
81
"after Check": {
82
wantClose: true,
82
- prepare: func(l *Collector) { _ = l.Init(); _ = l.Check() },
83
+ prepare: func(l *Collector) { _ = l.Init(context.Background()); _ = l.Check(context.Background()) },
84
},
85
"after Collect": {
86
wantClose: true,
86
- prepare: func(l *Collector) { _ = l.Init(); l.Collect() },
87
+ prepare: func(l *Collector) { _ = l.Init(context.Background()); l.Collect(context.Background()) },
88
},
89
}
90
@@ -94,7 +95,7 @@ func TestCollector_Cleanup(t *testing.T) {
95
collr.newLogindConn = func(Config) (logindConnection, error) { return m, nil }
96
test.prepare(collr)
97
97
- require.NotPanics(t, collr.Cleanup)
98
+ require.NotPanics(t, func() { collr.Cleanup(context.Background()) })
99
100
if test.wantClose {
101
assert.True(t, m.closeCalled)
@@ -139,13 +140,13 @@ func TestCollector_Check(t *testing.T) {
140
for name, test := range tests {
141
t.Run(name, func(t *testing.T) {
142
collr := New()
142
- require.NoError(t, collr.Init())
143
+ require.NoError(t, collr.Init(context.Background()))
144
collr.conn = test.prepare()
145
146
if test.wantFail {
146
- assert.Error(t, collr.Check())
147
+ assert.Error(t, collr.Check(context.Background()))
148
} else {
148
- assert.NoError(t, collr.Check())
149
+ assert.NoError(t, collr.Check(context.Background()))
150
}
151
})
152
}
@@ -213,10 +214,10 @@ func TestCollector_Collect(t *testing.T) {
214
for name, test := range tests {
215
t.Run(name, func(t *testing.T) {
216
collr := New()
216
- require.NoError(t, collr.Init())
217
+ require.NoError(t, collr.Init(context.Background()))
218
collr.conn = test.prepare()
219
219
- mx := collr.Collect()
220
+ mx := collr.Collect(context.Background())
221
222
assert.Equal(t, test.expected, mx)
223
})
src/go/plugin/go.d/collector/logstash/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package logstash
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -62,7 +63,7 @@ func (c *Collector) Configuration() any {
63
return c.Config
64
}
65
65
-func (c *Collector) Init() error {
66
+func (c *Collector) Init(context.Context) error {
67
if c.URL == "" {
68
return errors.New("config: 'url' cannot be empty")
69
}
@@ -79,7 +80,7 @@ func (c *Collector) Init() error {
80
return nil
81
}
82
82
-func (c *Collector) Check() error {
83
+func (c *Collector) Check(context.Context) error {
84
mx, err := c.collect()
85
if err != nil {
86
return err
@@ -94,7 +95,7 @@ func (c *Collector) Charts() *module.Charts {
95
return c.charts
96
}
97
97
-func (c *Collector) Collect() map[string]int64 {
98
+func (c *Collector) Collect(context.Context) map[string]int64 {
99
mx, err := c.collect()
100
if err != nil {
101
c.Error(err)
@@ -106,7 +107,7 @@ func (c *Collector) Collect() map[string]int64 {
107
return mx
108
}
109
109
-func (c *Collector) Cleanup() {
110
+func (c *Collector) Cleanup(context.Context) {
111
if c.httpClient != nil {
112
c.httpClient.CloseIdleConnections()
113
}
src/go/plugin/go.d/collector/logstash/collector_test.go
+11
-10
@@ -3,6 +3,7 @@
3
package logstash
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -62,9 +63,9 @@ func TestCollector_Init(t *testing.T) {
63
collr.Config = test.config
64
65
if test.wantFail {
65
- assert.Error(t, collr.Init())
66
+ assert.Error(t, collr.Init(context.Background()))
67
} else {
67
- assert.NoError(t, collr.Init())
68
+ assert.NoError(t, collr.Init(context.Background()))
69
}
70
})
71
}
@@ -75,7 +76,7 @@ func TestCollector_Charts(t *testing.T) {
76
}
77
78
func TestCollector_Cleanup(t *testing.T) {
78
- assert.NotPanics(t, New().Cleanup)
79
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
80
}
81
82
func TestCollector_Check(t *testing.T) {
@@ -107,9 +108,9 @@ func TestCollector_Check(t *testing.T) {
108
defer cleanup()
109
110
if test.wantFail {
110
- assert.Error(t, collr.Check())
111
+ assert.Error(t, collr.Check(context.Background()))
112
} else {
112
- assert.NoError(t, collr.Check())
113
+ assert.NoError(t, collr.Check(context.Background()))
114
}
115
})
116
}
@@ -175,7 +176,7 @@ func TestCollector_Collect(t *testing.T) {
176
collr, cleanup := test.prepare(t)
177
defer cleanup()
178
178
- mx := collr.Collect()
179
+ mx := collr.Collect(context.Background())
180
181
require.Equal(t, test.wantMetrics, mx)
182
if len(test.wantMetrics) > 0 {
@@ -199,7 +200,7 @@ func caseValidResponse(t *testing.T) (*Collector, func()) {
200
}))
201
collr := New()
202
collr.URL = srv.URL
202
- require.NoError(t, collr.Init())
203
+ require.NoError(t, collr.Init(context.Background()))
204
205
return collr, srv.Close
206
}
@@ -212,7 +213,7 @@ func caseInvalidDataResponse(t *testing.T) (*Collector, func()) {
213
}))
214
collr := New()
215
collr.URL = srv.URL
215
- require.NoError(t, collr.Init())
216
+ require.NoError(t, collr.Init(context.Background()))
217
218
return collr, srv.Close
219
}
@@ -221,7 +222,7 @@ func caseConnectionRefused(t *testing.T) (*Collector, func()) {
222
t.Helper()
223
collr := New()
224
collr.URL = "http://127.0.0.1:65001"
224
- require.NoError(t, collr.Init())
225
+ require.NoError(t, collr.Init(context.Background()))
226
227
return collr, func() {}
228
}
@@ -234,7 +235,7 @@ func case404(t *testing.T) (*Collector, func()) {
235
}))
236
collr := New()
237
collr.URL = srv.URL
237
- require.NoError(t, collr.Init())
238
+ require.NoError(t, collr.Init(context.Background()))
239
240
return collr, srv.Close
241
}
src/go/plugin/go.d/collector/lvm/collector.go
+5
-4
@@ -5,6 +5,7 @@
5
package lvm
6
7
import (
8
+ "context"
9
_ "embed"
10
"errors"
11
"fmt"
@@ -58,7 +59,7 @@ func (c *Collector) Configuration() any {
59
return c.Config
60
}
61
61
-func (c *Collector) Init() error {
62
+func (c *Collector) Init(context.Context) error {
63
lvmExec, err := c.initLVMCLIExec()
64
if err != nil {
65
return fmt.Errorf("init lvm exec: %v", err)
@@ -68,7 +69,7 @@ func (c *Collector) Init() error {
69
return nil
70
}
71
71
-func (c *Collector) Check() error {
72
+func (c *Collector) Check(context.Context) error {
73
mx, err := c.collect()
74
if err != nil {
75
return err
@@ -85,7 +86,7 @@ func (c *Collector) Charts() *module.Charts {
86
return c.charts
87
}
88
88
-func (c *Collector) Collect() map[string]int64 {
89
+func (c *Collector) Collect(context.Context) map[string]int64 {
90
mx, err := c.collect()
91
if err != nil {
92
c.Error(err)
@@ -98,4 +99,4 @@ func (c *Collector) Collect() map[string]int64 {
99
return mx
100
}
101
101
-func (c *Collector) Cleanup() {}
102
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/lvm/collector_test.go
+9
-8
@@ -5,6 +5,7 @@
5
package lvm
6
7
import (
8
+ "context"
9
"errors"
10
"os"
11
"testing"
@@ -57,9 +58,9 @@ func TestCollector_Init(t *testing.T) {
58
collr.Config = test.config
59
60
if test.wantFail {
60
- assert.Error(t, collr.Init())
61
+ assert.Error(t, collr.Init(context.Background()))
62
} else {
62
- assert.NoError(t, collr.Init())
63
+ assert.NoError(t, collr.Init(context.Background()))
64
}
65
})
66
}
@@ -78,7 +79,7 @@ func TestCollector_Cleanup(t *testing.T) {
79
prepare: func() *Collector {
80
collr := New()
81
collr.exec = prepareMockOK()
81
- _ = collr.Check()
82
+ _ = collr.Check(context.Background())
83
return collr
84
},
85
},
@@ -86,7 +87,7 @@ func TestCollector_Cleanup(t *testing.T) {
87
prepare: func() *Collector {
88
collr := New()
89
collr.exec = prepareMockOK()
89
- _ = collr.Collect()
90
+ _ = collr.Collect(context.Background())
91
return collr
92
},
93
},
@@ -96,7 +97,7 @@ func TestCollector_Cleanup(t *testing.T) {
97
t.Run(name, func(t *testing.T) {
98
collr := test.prepare()
99
99
- assert.NotPanics(t, collr.Cleanup)
100
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
101
})
102
}
103
}
@@ -139,9 +140,9 @@ func TestCollector_Check(t *testing.T) {
140
collr.exec = mock
141
142
if test.wantFail {
142
- assert.Error(t, collr.Check())
143
+ assert.Error(t, collr.Check(context.Background()))
144
} else {
144
- assert.NoError(t, collr.Check())
145
+ assert.NoError(t, collr.Check(context.Background()))
146
}
147
})
148
}
@@ -183,7 +184,7 @@ func TestCollector_Collect(t *testing.T) {
184
mock := test.prepareMock()
185
collr.exec = mock
186
186
- mx := collr.Collect()
187
+ mx := collr.Collect(context.Background())
188
189
assert.Equal(t, test.wantMetrics, mx)
190
if len(test.wantMetrics) > 0 {
src/go/plugin/go.d/collector/maxscale/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package maxscale
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -64,7 +65,7 @@ func (c *Collector) Configuration() any {
65
return c.Config
66
}
67
67
-func (c *Collector) Init() error {
68
+func (c *Collector) Init(context.Context) error {
69
if c.URL == "" {
70
return errors.New("URL required but not set")
71
}
@@ -81,7 +82,7 @@ func (c *Collector) Init() error {
82
return nil
83
}
84
84
-func (c *Collector) Check() error {
85
+func (c *Collector) Check(context.Context) error {
86
mx, err := c.collect()
87
if err != nil {
88
return err
@@ -98,7 +99,7 @@ func (c *Collector) Charts() *module.Charts {
99
return c.charts
100
}
101
101
-func (c *Collector) Collect() map[string]int64 {
102
+func (c *Collector) Collect(context.Context) map[string]int64 {
103
mx, err := c.collect()
104
if err != nil {
105
c.Error(err)
@@ -108,7 +109,7 @@ func (c *Collector) Collect() map[string]int64 {
109
return mx
110
}
111
111
-func (c *Collector) Cleanup() {
112
+func (c *Collector) Cleanup(context.Context) {
113
if c.httpClient != nil {
114
c.httpClient.CloseIdleConnections()
115
}
src/go/plugin/go.d/collector/maxscale/collector_test.go
+12
-11
@@ -3,6 +3,7 @@
3
package maxscale
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -65,9 +66,9 @@ func TestCollector_Init(t *testing.T) {
66
collr.Config = test.config
67
68
if test.wantFail {
68
- assert.Error(t, collr.Init())
69
+ assert.Error(t, collr.Init(context.Background()))
70
} else {
70
- assert.NoError(t, collr.Init())
71
+ assert.NoError(t, collr.Init(context.Background()))
72
}
73
})
74
}
@@ -106,9 +107,9 @@ func TestCollector_Check(t *testing.T) {
107
defer cleanup()
108
109
if test.wantFail {
109
- assert.Error(t, collr.Check())
110
+ assert.Error(t, collr.Check(context.Background()))
111
} else {
111
- assert.NoError(t, collr.Check())
112
+ assert.NoError(t, collr.Check(context.Background()))
113
}
114
})
115
}
@@ -181,9 +182,9 @@ func TestCollector_Collect(t *testing.T) {
182
collr, cleanup := test.prepare(t)
183
defer cleanup()
184
184
- _ = collr.Check()
185
+ _ = collr.Check(context.Background())
186
186
- mx := collr.Collect()
187
+ mx := collr.Collect(context.Background())
188
189
require.Equal(t, test.wantMetrics, mx)
190
@@ -213,7 +214,7 @@ func caseOk(t *testing.T) (*Collector, func()) {
214
}))
215
collr := New()
216
collr.URL = srv.URL
216
- require.NoError(t, collr.Init())
217
+ require.NoError(t, collr.Init(context.Background()))
218
219
return collr, srv.Close
220
}
@@ -243,7 +244,7 @@ func caseUnexpectedJsonResponse(t *testing.T) (*Collector, func()) {
244
}))
245
collr := New()
246
collr.URL = srv.URL
246
- require.NoError(t, collr.Init())
247
+ require.NoError(t, collr.Init(context.Background()))
248
249
return collr, srv.Close
250
}
@@ -256,7 +257,7 @@ func caseInvalidDataResponse(t *testing.T) (*Collector, func()) {
257
}))
258
collr := New()
259
collr.URL = srv.URL
259
- require.NoError(t, collr.Init())
260
+ require.NoError(t, collr.Init(context.Background()))
261
262
return collr, srv.Close
263
}
@@ -265,7 +266,7 @@ func caseConnectionRefused(t *testing.T) (*Collector, func()) {
266
t.Helper()
267
collr := New()
268
collr.URL = "http://127.0.0.1:65001"
268
- require.NoError(t, collr.Init())
269
+ require.NoError(t, collr.Init(context.Background()))
270
271
return collr, func() {}
272
}
@@ -278,7 +279,7 @@ func case404(t *testing.T) (*Collector, func()) {
279
}))
280
collr := New()
281
collr.URL = srv.URL
281
- require.NoError(t, collr.Init())
282
+ require.NoError(t, collr.Init(context.Background()))
283
284
return collr, srv.Close
285
}
src/go/plugin/go.d/collector/megacli/collector.go
+5
-4
@@ -5,6 +5,7 @@
5
package megacli
6
7
import (
8
+ "context"
9
_ "embed"
10
"errors"
11
"fmt"
@@ -62,7 +63,7 @@ func (c *Collector) Configuration() any {
63
return c.Config
64
}
65
65
-func (c *Collector) Init() error {
66
+func (c *Collector) Init(context.Context) error {
67
lvmExec, err := c.initMegaCliExec()
68
if err != nil {
69
return fmt.Errorf("init megacli exec: %v", err)
@@ -72,7 +73,7 @@ func (c *Collector) Init() error {
73
return nil
74
}
75
75
-func (c *Collector) Check() error {
76
+func (c *Collector) Check(context.Context) error {
77
mx, err := c.collect()
78
if err != nil {
79
return err
@@ -89,7 +90,7 @@ func (c *Collector) Charts() *module.Charts {
90
return c.charts
91
}
92
92
-func (c *Collector) Collect() map[string]int64 {
93
+func (c *Collector) Collect(context.Context) map[string]int64 {
94
mx, err := c.collect()
95
if err != nil {
96
c.Error(err)
@@ -102,4 +103,4 @@ func (c *Collector) Collect() map[string]int64 {
103
return mx
104
}
105
105
-func (c *Collector) Cleanup() {}
106
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/megacli/collector_test.go
+9
-8
@@ -5,6 +5,7 @@
5
package megacli
6
7
import (
8
+ "context"
9
"errors"
10
"os"
11
"testing"
@@ -57,9 +58,9 @@ func TestCollector_Init(t *testing.T) {
58
collr := New()
59
60
if test.wantFail {
60
- assert.Error(t, collr.Init())
61
+ assert.Error(t, collr.Init(context.Background()))
62
} else {
62
- assert.NoError(t, collr.Init())
63
+ assert.NoError(t, collr.Init(context.Background()))
64
}
65
})
66
}
@@ -78,7 +79,7 @@ func TestCollector_Cleanup(t *testing.T) {
79
prepare: func() *Collector {
80
collr := New()
81
collr.exec = prepareMockOK()
81
- _ = collr.Check()
82
+ _ = collr.Check(context.Background())
83
return collr
84
},
85
},
@@ -86,7 +87,7 @@ func TestCollector_Cleanup(t *testing.T) {
87
prepare: func() *Collector {
88
collr := New()
89
collr.exec = prepareMockOK()
89
- _ = collr.Collect()
90
+ _ = collr.Collect(context.Background())
91
return collr
92
},
93
},
@@ -96,7 +97,7 @@ func TestCollector_Cleanup(t *testing.T) {
97
t.Run(name, func(t *testing.T) {
98
collr := test.prepare()
99
99
- assert.NotPanics(t, collr.Cleanup)
100
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
101
})
102
}
103
}
@@ -139,9 +140,9 @@ func TestCollector_Check(t *testing.T) {
140
collr.exec = mock
141
142
if test.wantFail {
142
- assert.Error(t, collr.Check())
143
+ assert.Error(t, collr.Check(context.Background()))
144
} else {
144
- assert.NoError(t, collr.Check())
145
+ assert.NoError(t, collr.Check(context.Background()))
146
}
147
})
148
}
@@ -235,7 +236,7 @@ func TestCollector_Collect(t *testing.T) {
236
mock := test.prepareMock()
237
collr.exec = mock
238
238
- mx := collr.Collect()
239
+ mx := collr.Collect(context.Background())
240
241
assert.Equal(t, test.wantMetrics, mx)
242
assert.Len(t, *collr.Charts(), test.wantCharts)
src/go/plugin/go.d/collector/memcached/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package memcached
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"time"
@@ -53,7 +54,7 @@ func (c *Collector) Configuration() any {
54
return c.Config
55
}
56
56
-func (c *Collector) Init() error {
57
+func (c *Collector) Init(context.Context) error {
58
if c.Address == "" {
59
return errors.New("config: 'address' not set")
60
}
@@ -61,7 +62,7 @@ func (c *Collector) Init() error {
62
return nil
63
}
64
64
-func (c *Collector) Check() error {
65
+func (c *Collector) Check(context.Context) error {
66
mx, err := c.collect()
67
if err != nil {
68
return err
@@ -78,7 +79,7 @@ func (c *Collector) Charts() *module.Charts {
79
return c.charts
80
}
81
81
-func (c *Collector) Collect() map[string]int64 {
82
+func (c *Collector) Collect(context.Context) map[string]int64 {
83
mx, err := c.collect()
84
if err != nil {
85
c.Error(err)
@@ -91,7 +92,7 @@ func (c *Collector) Collect() map[string]int64 {
92
return mx
93
}
94
94
-func (c *Collector) Cleanup() {
95
+func (c *Collector) Cleanup(context.Context) {
96
if c.conn != nil {
97
c.conn.disconnect()
98
c.conn = nil
src/go/plugin/go.d/collector/memcached/collector_test.go
+10
-9
@@ -3,6 +3,7 @@
3
package memcached
4
5
import (
6
+ "context"
7
"errors"
8
"os"
9
"testing"
@@ -60,9 +61,9 @@ func TestCollector_Init(t *testing.T) {
61
collr.Config = test.config
62
63
if test.wantFail {
63
- assert.Error(t, collr.Init())
64
+ assert.Error(t, collr.Init(context.Background()))
65
} else {
65
- assert.NoError(t, collr.Init())
66
+ assert.NoError(t, collr.Init(context.Background()))
67
}
68
})
69
}
@@ -81,7 +82,7 @@ func TestCollector_Cleanup(t *testing.T) {
82
prepare: func() *Collector {
83
collr := New()
84
collr.newMemcachedConn = func(config Config) memcachedConn { return prepareMockOk() }
84
- _ = collr.Check()
85
+ _ = collr.Check(context.Background())
86
return collr
87
},
88
},
@@ -89,7 +90,7 @@ func TestCollector_Cleanup(t *testing.T) {
90
prepare: func() *Collector {
91
collr := New()
92
collr.newMemcachedConn = func(config Config) memcachedConn { return prepareMockOk() }
92
- _ = collr.Collect()
93
+ _ = collr.Collect(context.Background())
94
return collr
95
},
96
},
@@ -99,7 +100,7 @@ func TestCollector_Cleanup(t *testing.T) {
100
t.Run(name, func(t *testing.T) {
101
collr := test.prepare()
102
102
- assert.NotPanics(t, collr.Cleanup)
103
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
104
})
105
}
106
}
@@ -138,9 +139,9 @@ func TestCollector_Check(t *testing.T) {
139
collr.newMemcachedConn = func(config Config) memcachedConn { return mock }
140
141
if test.wantFail {
141
- assert.Error(t, collr.Check())
142
+ assert.Error(t, collr.Check(context.Background()))
143
} else {
143
- assert.NoError(t, collr.Check())
144
+ assert.NoError(t, collr.Check(context.Background()))
145
}
146
})
147
}
@@ -221,7 +222,7 @@ func TestCollector_Collect(t *testing.T) {
222
mock := test.prepareMock()
223
collr.newMemcachedConn = func(config Config) memcachedConn { return mock }
224
224
- mx := collr.Collect()
225
+ mx := collr.Collect(context.Background())
226
227
require.Equal(t, test.wantMetrics, mx)
228
@@ -230,7 +231,7 @@ func TestCollector_Collect(t *testing.T) {
231
}
232
233
assert.Equal(t, test.disconnectBeforeCleanup, mock.disconnectCalled, "disconnect before cleanup")
233
- collr.Cleanup()
234
+ collr.Cleanup(context.Background())
235
assert.Equal(t, test.disconnectAfterCleanup, mock.disconnectCalled, "disconnect after cleanup")
236
})
237
}
src/go/plugin/go.d/collector/mongodb/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package mongo
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -75,7 +76,7 @@ func (c *Collector) Configuration() any {
76
return c.Config
77
}
78
78
-func (c *Collector) Init() error {
79
+func (c *Collector) Init(context.Context) error {
80
if err := c.verifyConfig(); err != nil {
81
return fmt.Errorf("config validation: %v", err)
82
}
@@ -87,7 +88,7 @@ func (c *Collector) Init() error {
88
return nil
89
}
90
90
-func (c *Collector) Check() error {
91
+func (c *Collector) Check(context.Context) error {
92
mx, err := c.collect()
93
if err != nil {
94
return err
@@ -102,7 +103,7 @@ func (c *Collector) Charts() *module.Charts {
103
return c.charts
104
}
105
105
-func (c *Collector) Collect() map[string]int64 {
106
+func (c *Collector) Collect(context.Context) map[string]int64 {
107
mx, err := c.collect()
108
if err != nil {
109
c.Error(err)
@@ -116,7 +117,7 @@ func (c *Collector) Collect() map[string]int64 {
117
return mx
118
}
119
119
-func (c *Collector) Cleanup() {
120
+func (c *Collector) Cleanup(context.Context) {
121
if c.conn == nil {
122
return
123
}
src/go/plugin/go.d/collector/mongodb/collector_test.go
+11
-10
@@ -3,6 +3,7 @@
3
package mongo
4
5
import (
6
+ "context"
7
"encoding/json"
8
"errors"
9
"os"
@@ -75,9 +76,9 @@ func TestCollector_Init(t *testing.T) {
76
collr.Config = test.config
77
78
if test.wantFail {
78
- assert.Error(t, collr.Init())
79
+ assert.Error(t, collr.Init(context.Background()))
80
} else {
80
- assert.NoError(t, collr.Init())
81
+ assert.NoError(t, collr.Init(context.Background()))
82
}
83
})
84
}
@@ -114,7 +115,7 @@ func TestCollector_Cleanup(t *testing.T) {
115
t.Run(name, func(t *testing.T) {
116
collr := test.prepare(t)
117
117
- require.NotPanics(t, collr.Cleanup)
118
+ require.NotPanics(t, func() { collr.Cleanup(context.Background()) })
119
if test.wantClose {
120
collr, ok := collr.conn.(*mockMongoClient)
121
require.True(t, ok)
@@ -146,15 +147,15 @@ func TestCollector_Check(t *testing.T) {
147
for name, test := range tests {
148
t.Run(name, func(t *testing.T) {
149
collr := prepareMongo()
149
- defer collr.Cleanup()
150
+ defer collr.Cleanup(context.Background())
151
collr.conn = test.prepare()
152
152
- require.NoError(t, collr.Init())
153
+ require.NoError(t, collr.Init(context.Background()))
154
155
if test.wantFail {
155
- assert.Error(t, collr.Check())
156
+ assert.Error(t, collr.Check(context.Background()))
157
} else {
157
- assert.NoError(t, collr.Check())
158
+ assert.NoError(t, collr.Check(context.Background()))
159
}
160
})
161
}
@@ -597,12 +598,12 @@ func TestCollector_Collect(t *testing.T) {
598
for name, test := range tests {
599
t.Run(name, func(t *testing.T) {
600
collr := prepareMongo()
600
- defer collr.Cleanup()
601
+ defer collr.Cleanup(context.Background())
602
collr.conn = test.prepare()
603
603
- require.NoError(t, collr.Init())
604
+ require.NoError(t, collr.Init(context.Background()))
605
605
- mx := collr.Collect()
606
+ mx := collr.Collect(context.Background())
607
608
assert.Equal(t, test.wantCollected, mx)
609
})
src/go/plugin/go.d/collector/monit/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package monit
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -64,7 +65,7 @@ func (c *Collector) Configuration() any {
65
return c.Config
66
}
67
67
-func (c *Collector) Init() error {
68
+func (c *Collector) Init(context.Context) error {
69
if c.URL == "" {
70
return fmt.Errorf("config: monit url is required but not set")
71
}
@@ -81,7 +82,7 @@ func (c *Collector) Init() error {
82
return nil
83
}
84
84
-func (c *Collector) Check() error {
85
+func (c *Collector) Check(context.Context) error {
86
mx, err := c.collect()
87
if err != nil {
88
return err
@@ -97,7 +98,7 @@ func (c *Collector) Charts() *module.Charts {
98
return c.charts
99
}
100
100
-func (c *Collector) Collect() map[string]int64 {
101
+func (c *Collector) Collect(context.Context) map[string]int64 {
102
mx, err := c.collect()
103
if err != nil {
104
c.Error(err)
@@ -109,7 +110,7 @@ func (c *Collector) Collect() map[string]int64 {
110
return mx
111
}
112
112
-func (c *Collector) Cleanup() {
113
+func (c *Collector) Cleanup(context.Context) {
114
if c.httpClient != nil {
115
c.httpClient.CloseIdleConnections()
116
}
src/go/plugin/go.d/collector/monit/collector_test.go
+12
-11
@@ -3,6 +3,7 @@
3
package monit
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -62,9 +63,9 @@ func TestCollector_Init(t *testing.T) {
63
collr.Config = test.config
64
65
if test.wantFail {
65
- assert.Error(t, collr.Init())
66
+ assert.Error(t, collr.Init(context.Background()))
67
} else {
67
- assert.NoError(t, collr.Init())
68
+ assert.NoError(t, collr.Init(context.Background()))
69
}
70
})
71
}
@@ -103,9 +104,9 @@ func TestCollector_Check(t *testing.T) {
104
defer cleanup()
105
106
if test.wantFail {
106
- assert.Error(t, collr.Check())
107
+ assert.Error(t, collr.Check(context.Background()))
108
} else {
108
- assert.NoError(t, collr.Check())
109
+ assert.NoError(t, collr.Check(context.Background()))
110
}
111
})
112
}
@@ -255,9 +256,9 @@ func TestCollector_Collect(t *testing.T) {
256
collr, cleanup := test.prepare(t)
257
defer cleanup()
258
258
- _ = collr.Check()
259
+ _ = collr.Check(context.Background())
260
260
- mx := collr.Collect()
261
+ mx := collr.Collect(context.Background())
262
263
require.Equal(t, test.wantMetrics, mx)
264
@@ -281,7 +282,7 @@ func caseOk(t *testing.T) (*Collector, func()) {
282
}))
283
collr := New()
284
collr.URL = srv.URL
284
- require.NoError(t, collr.Init())
285
+ require.NoError(t, collr.Init(context.Background()))
286
287
return collr, srv.Close
288
}
@@ -330,7 +331,7 @@ func caseUnexpectedXMLResponse(t *testing.T) (*Collector, func()) {
331
}))
332
collr := New()
333
collr.URL = srv.URL
333
- require.NoError(t, collr.Init())
334
+ require.NoError(t, collr.Init(context.Background()))
335
336
return collr, srv.Close
337
}
@@ -343,7 +344,7 @@ func caseInvalidDataResponse(t *testing.T) (*Collector, func()) {
344
}))
345
collr := New()
346
collr.URL = srv.URL
346
- require.NoError(t, collr.Init())
347
+ require.NoError(t, collr.Init(context.Background()))
348
349
return collr, srv.Close
350
}
@@ -352,7 +353,7 @@ func caseConnectionRefused(t *testing.T) (*Collector, func()) {
353
t.Helper()
354
collr := New()
355
collr.URL = "http://127.0.0.1:65001"
355
- require.NoError(t, collr.Init())
356
+ require.NoError(t, collr.Init(context.Background()))
357
358
return collr, func() {}
359
}
@@ -365,7 +366,7 @@ func case404(t *testing.T) (*Collector, func()) {
366
}))
367
collr := New()
368
collr.URL = srv.URL
368
- require.NoError(t, collr.Init())
369
+ require.NoError(t, collr.Init(context.Background()))
370
371
return collr, srv.Close
372
}
src/go/plugin/go.d/collector/mysql/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package mysql
4
5
import (
6
+ "context"
7
"database/sql"
8
_ "embed"
9
"errors"
@@ -102,7 +103,7 @@ func (c *Collector) Configuration() any {
103
return c.Config
104
}
105
105
-func (c *Collector) Init() error {
106
+func (c *Collector) Init(context.Context) error {
107
if c.MyCNF != "" {
108
dsn, err := dsnFromFile(c.MyCNF)
109
if err != nil {
@@ -128,7 +129,7 @@ func (c *Collector) Init() error {
129
return nil
130
}
131
131
-func (c *Collector) Check() error {
132
+func (c *Collector) Check(context.Context) error {
133
mx, err := c.collect()
134
if err != nil {
135
return err
@@ -143,7 +144,7 @@ func (c *Collector) Charts() *module.Charts {
144
return c.charts
145
}
146
146
-func (c *Collector) Collect() map[string]int64 {
147
+func (c *Collector) Collect(context.Context) map[string]int64 {
148
mx, err := c.collect()
149
if err != nil {
150
c.Error(err)
@@ -155,7 +156,7 @@ func (c *Collector) Collect() map[string]int64 {
156
return mx
157
}
158
158
-func (c *Collector) Cleanup() {
159
+func (c *Collector) Cleanup(context.Context) {
160
if c.db == nil {
161
return
162
}
src/go/plugin/go.d/collector/mysql/collector_test.go
+16
-15
@@ -5,6 +5,7 @@ package mysql
5
import (
6
"bufio"
7
"bytes"
8
+ "context"
9
"database/sql/driver"
10
"errors"
11
"fmt"
@@ -117,9 +118,9 @@ func TestCollector_Init(t *testing.T) {
118
collr.Config = test.config
119
120
if test.wantFail {
120
- assert.Error(t, collr.Init())
121
+ assert.Error(t, collr.Init(context.Background()))
122
} else {
122
- assert.NoError(t, collr.Init())
123
+ assert.NoError(t, collr.Init(context.Background()))
124
}
125
})
126
}
@@ -148,7 +149,7 @@ func TestCollector_Cleanup(t *testing.T) {
149
collr, cleanup := prepare(t)
150
defer cleanup()
151
151
- assert.NotPanics(t, collr.Cleanup)
152
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
153
assert.Nil(t, collr.db)
154
})
155
}
@@ -257,14 +258,14 @@ func TestCollector_Check(t *testing.T) {
258
collr.db = db
259
defer func() { _ = db.Close() }()
260
260
- require.NoError(t, collr.Init())
261
+ require.NoError(t, collr.Init(context.Background()))
262
263
test.prepareMock(t, mock)
264
265
if test.wantFail {
265
- assert.Error(t, collr.Check())
266
+ assert.Error(t, collr.Check(context.Background()))
267
} else {
267
- assert.NoError(t, collr.Check())
268
+ assert.NoError(t, collr.Check(context.Background()))
269
}
270
assert.NoError(t, mock.ExpectationsWereMet())
271
})
@@ -290,7 +291,7 @@ func TestCollector_Collect(t *testing.T) {
291
mockExpect(t, m, queryShowProcessList, dataMariaVer5564ProcessList)
292
},
293
check: func(t *testing.T, collr *Collector) {
293
- mx := collr.Collect()
294
+ mx := collr.Collect(context.Background())
295
296
expected := map[string]int64{
297
"aborted_connects": 0,
@@ -425,7 +426,7 @@ func TestCollector_Collect(t *testing.T) {
426
mockExpect(t, m, queryShowProcessList, dataMariaVer1084ProcessList)
427
},
428
check: func(t *testing.T, collr *Collector) {
428
- mx := collr.Collect()
429
+ mx := collr.Collect(context.Background())
430
431
expected := map[string]int64{
432
@@ -607,7 +608,7 @@ func TestCollector_Collect(t *testing.T) {
608
mockExpect(t, m, queryShowProcessList, dataMariaVer1084ProcessList)
609
},
610
check: func(t *testing.T, collr *Collector) {
610
- mx := collr.Collect()
611
+ mx := collr.Collect(context.Background())
612
613
expected := map[string]int64{
614
"aborted_connects": 2,
@@ -791,7 +792,7 @@ func TestCollector_Collect(t *testing.T) {
792
mockExpect(t, m, queryShowProcessList, dataMariaVer1084ProcessList)
793
},
794
check: func(t *testing.T, collr *Collector) {
794
- mx := collr.Collect()
795
+ mx := collr.Collect(context.Background())
796
797
expected := map[string]int64{
798
"aborted_connects": 2,
@@ -978,7 +979,7 @@ func TestCollector_Collect(t *testing.T) {
979
mockExpect(t, m, queryShowProcessList, dataMariaVer1084ProcessList)
980
},
981
check: func(t *testing.T, collr *Collector) {
981
- mx := collr.Collect()
982
+ mx := collr.Collect(context.Background())
983
984
expected := map[string]int64{
985
"aborted_connects": 2,
@@ -1159,7 +1160,7 @@ func TestCollector_Collect(t *testing.T) {
1160
mockExpect(t, m, queryShowProcessList, dataMariaGaleraClusterVer1084ProcessList)
1161
},
1162
check: func(t *testing.T, collr *Collector) {
1162
- mx := collr.Collect()
1163
+ mx := collr.Collect(context.Background())
1164
1165
expected := map[string]int64{
1166
"aborted_connects": 0,
@@ -1355,7 +1356,7 @@ func TestCollector_Collect(t *testing.T) {
1356
mockExpect(t, m, queryShowProcessListPS, dataMySQLVer8030ProcessList)
1357
},
1358
check: func(t *testing.T, collr *Collector) {
1358
- mx := collr.Collect()
1359
+ mx := collr.Collect(context.Background())
1360
1361
expected := map[string]int64{
1362
"aborted_connects": 0,
@@ -1494,7 +1495,7 @@ func TestCollector_Collect(t *testing.T) {
1495
mockExpect(t, m, queryShowProcessListPS, dataPerconaV8029ProcessList)
1496
},
1497
check: func(t *testing.T, collr *Collector) {
1497
- mx := collr.Collect()
1498
+ mx := collr.Collect(context.Background())
1499
1500
expected := map[string]int64{
1501
"aborted_connects": 1,
@@ -1653,7 +1654,7 @@ func TestCollector_Collect(t *testing.T) {
1654
collr.db = db
1655
defer func() { _ = db.Close() }()
1656
1656
- require.NoError(t, collr.Init())
1657
+ require.NoError(t, collr.Init(context.Background()))
1658
1659
for i, step := range test {
1660
t.Run(fmt.Sprintf("step[%d]", i), func(t *testing.T) {
src/go/plugin/go.d/collector/nginx/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package nginx
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -59,7 +60,7 @@ func (c *Collector) Configuration() any {
60
return c.Config
61
}
62
62
-func (c *Collector) Init() error {
63
+func (c *Collector) Init(context.Context) error {
64
if c.URL == "" {
65
return errors.New("nginx URL required but not set")
66
}
@@ -76,7 +77,7 @@ func (c *Collector) Init() error {
77
return nil
78
}
79
79
-func (c *Collector) Check() error {
80
+func (c *Collector) Check(context.Context) error {
81
mx, err := c.collect()
82
if err != nil {
83
return err
@@ -93,7 +94,7 @@ func (c *Collector) Charts() *Charts {
94
return c.charts
95
}
96
96
-func (c *Collector) Collect() map[string]int64 {
97
+func (c *Collector) Collect(context.Context) map[string]int64 {
98
mx, err := c.collect()
99
if err != nil {
100
c.Error(err)
@@ -103,7 +104,7 @@ func (c *Collector) Collect() map[string]int64 {
104
return mx
105
}
106
106
-func (c *Collector) Cleanup() {
107
+func (c *Collector) Cleanup(context.Context) {
108
if c.httpClient != nil {
109
c.httpClient.CloseIdleConnections()
110
}
src/go/plugin/go.d/collector/nginx/collector_test.go
+17
-16
@@ -3,6 +3,7 @@
3
package nginx
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -38,13 +39,13 @@ func TestCollector_ConfigurationSerialize(t *testing.T) {
39
}
40
41
func TestCollector_Cleanup(t *testing.T) {
41
- New().Cleanup()
42
+ New().Cleanup(context.Background())
43
}
44
45
func TestCollector_Init(t *testing.T) {
46
collr := New()
47
47
- require.NoError(t, collr.Init())
48
+ require.NoError(t, collr.Init(context.Background()))
49
}
50
51
func TestCollector_Check(t *testing.T) {
@@ -57,16 +58,16 @@ func TestCollector_Check(t *testing.T) {
58
59
collr := New()
60
collr.URL = ts.URL
60
- require.NoError(t, collr.Init())
61
- assert.NoError(t, collr.Check())
61
+ require.NoError(t, collr.Init(context.Background()))
62
+ assert.NoError(t, collr.Check(context.Background()))
63
}
64
65
func TestCollector_CheckNG(t *testing.T) {
66
collr := New()
67
68
collr.URL = "http://127.0.0.1:38001/us"
68
- require.NoError(t, collr.Init())
69
- assert.Error(t, collr.Check())
69
+ require.NoError(t, collr.Init(context.Background()))
70
+ assert.Error(t, collr.Check(context.Background()))
71
}
72
73
func TestCollector_Charts(t *testing.T) {
@@ -83,8 +84,8 @@ func TestCollector_Collect(t *testing.T) {
84
85
collr := New()
86
collr.URL = ts.URL
86
- require.NoError(t, collr.Init())
87
- require.NoError(t, collr.Check())
87
+ require.NoError(t, collr.Init(context.Background()))
88
+ require.NoError(t, collr.Check(context.Background()))
89
90
expected := map[string]int64{
91
"accepts": 36,
@@ -96,7 +97,7 @@ func TestCollector_Collect(t *testing.T) {
97
"writing": 1,
98
}
99
99
- assert.Equal(t, expected, collr.Collect())
100
+ assert.Equal(t, expected, collr.Collect(context.Background()))
101
}
102
103
func TestCollector_CollectTengine(t *testing.T) {
@@ -109,8 +110,8 @@ func TestCollector_CollectTengine(t *testing.T) {
110
111
collr := New()
112
collr.URL = ts.URL
112
- require.NoError(t, collr.Init())
113
- require.NoError(t, collr.Check())
113
+ require.NoError(t, collr.Init(context.Background()))
114
+ require.NoError(t, collr.Check(context.Background()))
115
116
expected := map[string]int64{
117
"accepts": 1140,
@@ -123,7 +124,7 @@ func TestCollector_CollectTengine(t *testing.T) {
124
"writing": 1,
125
}
126
126
- assert.Equal(t, expected, collr.Collect())
127
+ assert.Equal(t, expected, collr.Collect(context.Background()))
128
}
129
130
func TestCollector_InvalidData(t *testing.T) {
@@ -136,8 +137,8 @@ func TestCollector_InvalidData(t *testing.T) {
137
138
collr := New()
139
collr.URL = ts.URL
139
- require.NoError(t, collr.Init())
140
- assert.Error(t, collr.Check())
140
+ require.NoError(t, collr.Init(context.Background()))
141
+ assert.Error(t, collr.Check(context.Background()))
142
}
143
144
func TestCollector_404(t *testing.T) {
@@ -150,6 +151,6 @@ func TestCollector_404(t *testing.T) {
151
152
collr := New()
153
collr.URL = ts.URL
153
- require.NoError(t, collr.Init())
154
- assert.Error(t, collr.Check())
154
+ require.NoError(t, collr.Init(context.Background()))
155
+ assert.Error(t, collr.Check(context.Background()))
156
}
src/go/plugin/go.d/collector/nginxplus/collector.go
+5
-5
@@ -3,6 +3,7 @@
3
package nginxplus
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -79,7 +80,7 @@ func (c *Collector) Configuration() any {
80
return c.Config
81
}
82
82
-func (c *Collector) Init() error {
83
+func (c *Collector) Init(context.Context) error {
84
if c.URL == "" {
85
return errors.New("config: 'url' can not be empty'")
86
}
@@ -93,7 +94,7 @@ func (c *Collector) Init() error {
94
return nil
95
}
96
96
-func (c *Collector) Check() error {
97
+func (c *Collector) Check(context.Context) error {
98
mx, err := c.collect()
99
if err != nil {
100
return err
@@ -108,9 +109,8 @@ func (c *Collector) Charts() *module.Charts {
109
return c.charts
110
}
111
111
-func (c *Collector) Collect() map[string]int64 {
112
+func (c *Collector) Collect(context.Context) map[string]int64 {
113
mx, err := c.collect()
113
-
114
if err != nil {
115
c.Error(err)
116
return nil
@@ -119,7 +119,7 @@ func (c *Collector) Collect() map[string]int64 {
119
return mx
120
}
121
122
-func (c *Collector) Cleanup() {
122
+func (c *Collector) Cleanup(context.Context) {
123
if c.httpClient != nil {
124
c.httpClient.CloseIdleConnections()
125
}
src/go/plugin/go.d/collector/nginxplus/collector_test.go
+10
-9
@@ -3,6 +3,7 @@
3
package nginxplus
4
5
import (
6
+ "context"
7
"fmt"
8
"net/http"
9
"net/http/httptest"
@@ -90,9 +91,9 @@ func TestCollector_Init(t *testing.T) {
91
collr.Config = test.config
92
93
if test.wantFail {
93
- assert.Error(t, collr.Init())
94
+ assert.Error(t, collr.Init(context.Background()))
95
} else {
95
- assert.NoError(t, collr.Init())
96
+ assert.NoError(t, collr.Init(context.Background()))
97
}
98
})
99
}
@@ -127,9 +128,9 @@ func TestCollector_Check(t *testing.T) {
128
defer cleanup()
129
130
if test.wantFail {
130
- assert.Error(t, collr.Check())
131
+ assert.Error(t, collr.Check(context.Background()))
132
} else {
132
- assert.NoError(t, collr.Check())
133
+ assert.NoError(t, collr.Check(context.Background()))
134
}
135
})
136
}
@@ -458,7 +459,7 @@ func TestCollector_Collect(t *testing.T) {
459
collr, cleanup := test.prepare(t)
460
defer cleanup()
461
461
- mx := collr.Collect()
462
+ mx := collr.Collect(context.Background())
463
464
require.Equal(t, test.wantMetrics, mx)
465
if len(test.wantMetrics) > 0 {
@@ -512,7 +513,7 @@ func caseAPI8AllRequestsOK(t *testing.T) (*Collector, func()) {
513
}))
514
collr := New()
515
collr.URL = srv.URL
515
- require.NoError(t, collr.Init())
516
+ require.NoError(t, collr.Init(context.Background()))
517
518
return collr, srv.Close
519
}
@@ -554,7 +555,7 @@ func caseAPI8AllRequestsExceptStreamOK(t *testing.T) (*Collector, func()) {
555
}))
556
collr := New()
557
collr.URL = srv.URL
557
- require.NoError(t, collr.Init())
558
+ require.NoError(t, collr.Init(context.Background()))
559
560
return collr, srv.Close
561
}
@@ -567,7 +568,7 @@ func caseInvalidDataResponse(t *testing.T) (*Collector, func()) {
568
}))
569
collr := New()
570
collr.URL = srv.URL
570
- require.NoError(t, collr.Init())
571
+ require.NoError(t, collr.Init(context.Background()))
572
573
return collr, srv.Close
574
}
@@ -576,7 +577,7 @@ func caseConnectionRefused(t *testing.T) (*Collector, func()) {
577
t.Helper()
578
collr := New()
579
collr.URL = "http://127.0.0.1:65001"
579
- require.NoError(t, collr.Init())
580
+ require.NoError(t, collr.Init(context.Background()))
581
582
return collr, func() {}
583
}
src/go/plugin/go.d/collector/nginxunit/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package nginxunit
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -59,7 +60,7 @@ func (c *Collector) Configuration() any {
60
return c.Config
61
}
62
62
-func (c *Collector) Init() error {
63
+func (c *Collector) Init(context.Context) error {
64
if c.URL == "" {
65
return errors.New("URL required but not set")
66
}
@@ -76,7 +77,7 @@ func (c *Collector) Init() error {
77
return nil
78
}
79
79
-func (c *Collector) Check() error {
80
+func (c *Collector) Check(context.Context) error {
81
mx, err := c.collect()
82
if err != nil {
83
return err
@@ -93,7 +94,7 @@ func (c *Collector) Charts() *module.Charts {
94
return c.charts
95
}
96
96
-func (c *Collector) Collect() map[string]int64 {
97
+func (c *Collector) Collect(context.Context) map[string]int64 {
98
mx, err := c.collect()
99
if err != nil {
100
c.Error(err)
@@ -103,7 +104,7 @@ func (c *Collector) Collect() map[string]int64 {
104
return mx
105
}
106
106
-func (c *Collector) Cleanup() {
107
+func (c *Collector) Cleanup(context.Context) {
108
if c.httpClient != nil {
109
c.httpClient.CloseIdleConnections()
110
}
src/go/plugin/go.d/collector/nginxunit/collector_test.go
+12
-11
@@ -3,6 +3,7 @@
3
package nginxunit
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -62,9 +63,9 @@ func TestCollector_Init(t *testing.T) {
63
collr.Config = test.config
64
65
if test.wantFail {
65
- assert.Error(t, collr.Init())
66
+ assert.Error(t, collr.Init(context.Background()))
67
} else {
67
- assert.NoError(t, collr.Init())
68
+ assert.NoError(t, collr.Init(context.Background()))
69
}
70
})
71
}
@@ -103,9 +104,9 @@ func TestCollector_Check(t *testing.T) {
104
defer cleanup()
105
106
if test.wantFail {
106
- assert.Error(t, collr.Check())
107
+ assert.Error(t, collr.Check(context.Background()))
108
} else {
108
- assert.NoError(t, collr.Check())
109
+ assert.NoError(t, collr.Check(context.Background()))
110
}
111
})
112
}
@@ -155,9 +156,9 @@ func TestCollector_Collect(t *testing.T) {
156
collr, cleanup := test.prepare(t)
157
defer cleanup()
158
158
- _ = collr.Check()
159
+ _ = collr.Check(context.Background())
160
160
- mx := collr.Collect()
161
+ mx := collr.Collect(context.Background())
162
163
require.Equal(t, test.wantMetrics, mx)
164
@@ -183,7 +184,7 @@ func caseOk(t *testing.T) (*Collector, func()) {
184
}))
185
collr := New()
186
collr.URL = srv.URL
186
- require.NoError(t, collr.Init())
187
+ require.NoError(t, collr.Init(context.Background()))
188
189
return collr, srv.Close
190
}
@@ -213,7 +214,7 @@ func caseUnexpectedJsonResponse(t *testing.T) (*Collector, func()) {
214
}))
215
collr := New()
216
collr.URL = srv.URL
216
- require.NoError(t, collr.Init())
217
+ require.NoError(t, collr.Init(context.Background()))
218
219
return collr, srv.Close
220
}
@@ -226,7 +227,7 @@ func caseInvalidDataResponse(t *testing.T) (*Collector, func()) {
227
}))
228
collr := New()
229
collr.URL = srv.URL
229
- require.NoError(t, collr.Init())
230
+ require.NoError(t, collr.Init(context.Background()))
231
232
return collr, srv.Close
233
}
@@ -235,7 +236,7 @@ func caseConnectionRefused(t *testing.T) (*Collector, func()) {
236
t.Helper()
237
collr := New()
238
collr.URL = "http://127.0.0.1:65001"
238
- require.NoError(t, collr.Init())
239
+ require.NoError(t, collr.Init(context.Background()))
240
241
return collr, func() {}
242
}
@@ -248,7 +249,7 @@ func case404(t *testing.T) (*Collector, func()) {
249
}))
250
collr := New()
251
collr.URL = srv.URL
251
- require.NoError(t, collr.Init())
252
+ require.NoError(t, collr.Init(context.Background()))
253
254
return collr, srv.Close
255
}
src/go/plugin/go.d/collector/nginxvts/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package nginxvts
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -61,14 +62,14 @@ func (c *Collector) Configuration() any {
62
return c.Config
63
}
64
64
-func (c *Collector) Cleanup() {
65
+func (c *Collector) Cleanup(context.Context) {
66
if c.httpClient == nil {
67
return
68
}
69
c.httpClient.CloseIdleConnections()
70
}
71
71
-func (c *Collector) Init() error {
72
+func (c *Collector) Init(context.Context) error {
73
err := c.validateConfig()
74
if err != nil {
75
return fmt.Errorf("config: %v", err)
@@ -89,7 +90,7 @@ func (c *Collector) Init() error {
90
return nil
91
}
92
92
-func (c *Collector) Check() error {
93
+func (c *Collector) Check(context.Context) error {
94
mx, err := c.collect()
95
if err != nil {
96
return err
@@ -104,7 +105,7 @@ func (c *Collector) Charts() *module.Charts {
105
return c.charts
106
}
107
107
-func (c *Collector) Collect() map[string]int64 {
108
+func (c *Collector) Collect(context.Context) map[string]int64 {
109
mx, err := c.collect()
110
if err != nil {
111
c.Error(err)
src/go/plugin/go.d/collector/nginxvts/collector_test.go
+11
-10
@@ -3,6 +3,7 @@
3
package nginxvts
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -75,9 +76,9 @@ func TestCollector_Init(t *testing.T) {
76
collr.Config = test.config
77
78
if test.wantFail {
78
- assert.Error(t, collr.Init())
79
+ assert.Error(t, collr.Init(context.Background()))
80
} else {
80
- assert.NoError(t, collr.Init())
81
+ assert.NoError(t, collr.Init(context.Background()))
82
assert.Equal(t, test.wantNumOfCharts, len(*collr.Charts()))
83
}
84
})
@@ -101,9 +102,9 @@ func TestCollector_Check(t *testing.T) {
102
defer cleanup()
103
104
if test.wantFail {
104
- assert.Error(t, collr.Check())
105
+ assert.Error(t, collr.Check(context.Background()))
106
} else {
106
- assert.NoError(t, collr.Check())
107
+ assert.NoError(t, collr.Check(context.Background()))
108
}
109
})
110
}
@@ -114,7 +115,7 @@ func TestCollector_Charts(t *testing.T) {
115
}
116
117
func TestCollector_Cleanup(t *testing.T) {
117
- assert.NotPanics(t, New().Cleanup)
118
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
119
}
120
121
func TestCollector_Collect(t *testing.T) {
@@ -169,7 +170,7 @@ func TestCollector_Collect(t *testing.T) {
170
collr, cleanup := test.prepare(t)
171
defer cleanup()
172
172
- mx := collr.Collect()
173
+ mx := collr.Collect(context.Background())
174
175
assert.Equal(t, test.wantCollected, mx)
176
if test.checkCharts {
@@ -185,7 +186,7 @@ func prepareNginxVTS(t *testing.T, createNginxVTS func() *Collector) (collr *Col
186
srv := prepareNginxVTSEndpoint()
187
collr.URL = srv.URL
188
188
- require.NoError(t, collr.Init())
189
+ require.NoError(t, collr.Init(context.Background()))
190
191
return collr, srv.Close
192
}
@@ -202,7 +203,7 @@ func prepareNginxVTSInvalidData(t *testing.T) (*Collector, func()) {
203
}))
204
collr := New()
205
collr.URL = srv.URL
205
- require.NoError(t, collr.Init())
206
+ require.NoError(t, collr.Init(context.Background()))
207
208
return collr, srv.Close
209
}
@@ -215,7 +216,7 @@ func prepareNginxVTS404(t *testing.T) (*Collector, func()) {
216
}))
217
collr := New()
218
collr.URL = srv.URL
218
- require.NoError(t, collr.Init())
219
+ require.NoError(t, collr.Init(context.Background()))
220
221
return collr, srv.Close
222
}
@@ -224,7 +225,7 @@ func prepareNginxVTSConnectionRefused(t *testing.T) (*Collector, func()) {
225
t.Helper()
226
collr := New()
227
collr.URL = "http://127.0.0.1:18080"
227
- require.NoError(t, collr.Init())
228
+ require.NoError(t, collr.Init(context.Background()))
229
230
return collr, func() {}
231
}
src/go/plugin/go.d/collector/nsd/collector.go
+5
-4
@@ -5,6 +5,7 @@
5
package nsd
6
7
import (
8
+ "context"
9
_ "embed"
10
"errors"
11
"fmt"
@@ -55,7 +56,7 @@ func (c *Collector) Configuration() any {
56
return c.Config
57
}
58
58
-func (c *Collector) Init() error {
59
+func (c *Collector) Init(context.Context) error {
60
nsdControl, err := c.initNsdControlExec()
61
if err != nil {
62
return fmt.Errorf("nsd-control exec initialization: %v", err)
@@ -65,7 +66,7 @@ func (c *Collector) Init() error {
66
return nil
67
}
68
68
-func (c *Collector) Check() error {
69
+func (c *Collector) Check(context.Context) error {
70
mx, err := c.collect()
71
if err != nil {
72
return err
@@ -82,7 +83,7 @@ func (c *Collector) Charts() *module.Charts {
83
return c.charts
84
}
85
85
-func (c *Collector) Collect() map[string]int64 {
86
+func (c *Collector) Collect(context.Context) map[string]int64 {
87
mx, err := c.collect()
88
if err != nil {
89
c.Error(err)
@@ -95,4 +96,4 @@ func (c *Collector) Collect() map[string]int64 {
96
return mx
97
}
98
98
-func (c *Collector) Cleanup() {}
99
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/nsd/collector_test.go
+9
-8
@@ -5,6 +5,7 @@
5
package nsd
6
7
import (
8
+ "context"
9
"errors"
10
"os"
11
"testing"
@@ -54,9 +55,9 @@ func TestCollector_Init(t *testing.T) {
55
collr.Config = test.config
56
57
if test.wantFail {
57
- assert.Error(t, collr.Init())
58
+ assert.Error(t, collr.Init(context.Background()))
59
} else {
59
- assert.NoError(t, collr.Init())
60
+ assert.NoError(t, collr.Init(context.Background()))
61
}
62
})
63
}
@@ -75,7 +76,7 @@ func TestCollector_Cleanup(t *testing.T) {
76
prepare: func() *Collector {
77
collr := New()
78
collr.exec = prepareMockOK()
78
- _ = collr.Check()
79
+ _ = collr.Check(context.Background())
80
return collr
81
},
82
},
@@ -83,7 +84,7 @@ func TestCollector_Cleanup(t *testing.T) {
84
prepare: func() *Collector {
85
collr := New()
86
collr.exec = prepareMockOK()
86
- _ = collr.Collect()
87
+ _ = collr.Collect(context.Background())
88
return collr
89
},
90
},
@@ -93,7 +94,7 @@ func TestCollector_Cleanup(t *testing.T) {
94
t.Run(name, func(t *testing.T) {
95
collr := test.prepare()
96
96
- assert.NotPanics(t, collr.Cleanup)
97
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
98
})
99
}
100
}
@@ -132,9 +133,9 @@ func TestCollector_Check(t *testing.T) {
133
collr.exec = mock
134
135
if test.wantFail {
135
- assert.Error(t, collr.Check())
136
+ assert.Error(t, collr.Check(context.Background()))
137
} else {
137
- assert.NoError(t, collr.Check())
138
+ assert.NoError(t, collr.Check(context.Background()))
139
}
140
})
141
}
@@ -288,7 +289,7 @@ func TestCollector_Collect(t *testing.T) {
289
mock := test.prepareMock()
290
collr.exec = mock
291
291
- mx := collr.Collect()
292
+ mx := collr.Collect(context.Background())
293
294
assert.Equal(t, test.wantMetrics, mx)
295
src/go/plugin/go.d/collector/ntpd/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package ntpd
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -65,7 +66,7 @@ func (c *Collector) Configuration() any {
66
return c.Config
67
}
68
68
-func (c *Collector) Init() error {
69
+func (c *Collector) Init(context.Context) error {
70
if c.Address == "" {
71
return errors.New("config: 'address' can not be empty")
72
}
@@ -81,7 +82,7 @@ func (c *Collector) Init() error {
82
return nil
83
}
84
84
-func (c *Collector) Check() error {
85
+func (c *Collector) Check(context.Context) error {
86
mx, err := c.collect()
87
if err != nil {
88
return err
@@ -96,7 +97,7 @@ func (c *Collector) Charts() *module.Charts {
97
return c.charts
98
}
99
99
-func (c *Collector) Collect() map[string]int64 {
100
+func (c *Collector) Collect(context.Context) map[string]int64 {
101
mx, err := c.collect()
102
if err != nil {
103
c.Error(err)
@@ -108,7 +109,7 @@ func (c *Collector) Collect() map[string]int64 {
109
return mx
110
}
111
111
-func (c *Collector) Cleanup() {
112
+func (c *Collector) Cleanup(context.Context) {
113
if c.client != nil {
114
c.client.close()
115
c.client = nil
src/go/plugin/go.d/collector/ntpd/collector_test.go
+14
-12
@@ -3,6 +3,7 @@
3
package ntpd
4
5
import (
6
+ "context"
7
"errors"
8
"fmt"
9
"os"
@@ -54,9 +55,9 @@ func TestCollector_Init(t *testing.T) {
55
collr.Config = test.config
56
57
if test.wantFail {
57
- assert.Error(t, collr.Init())
58
+ assert.Error(t, collr.Init(context.Background()))
59
} else {
59
- assert.NoError(t, collr.Init())
60
+ assert.NoError(t, collr.Init(context.Background()))
61
}
62
})
63
}
@@ -77,15 +78,15 @@ func TestCollector_Cleanup(t *testing.T) {
78
},
79
"after Init": {
80
wantClose: false,
80
- prepare: func(n *Collector) { _ = n.Init() },
81
+ prepare: func(n *Collector) { _ = n.Init(context.Background()) },
82
},
83
"after Check": {
84
wantClose: true,
84
- prepare: func(n *Collector) { _ = n.Init(); _ = n.Check() },
85
+ prepare: func(n *Collector) { _ = n.Init(context.Background()); _ = n.Check(context.Background()) },
86
},
87
"after Collect": {
88
wantClose: true,
88
- prepare: func(n *Collector) { _ = n.Init(); n.Collect() },
89
+ prepare: func(n *Collector) { _ = n.Init(context.Background()); n.Collect(context.Background()) },
90
},
91
}
92
@@ -95,7 +96,7 @@ func TestCollector_Cleanup(t *testing.T) {
96
collr := prepareNTPdWithMock(m, true)
97
test.prepare(collr)
98
98
- require.NotPanics(t, collr.Cleanup)
99
+ require.NotPanics(t, func() { collr.Cleanup(context.Background()) })
100
101
if test.wantClose {
102
assert.True(t, m.closeCalled)
@@ -137,12 +138,12 @@ func TestCollector_Check(t *testing.T) {
138
t.Run(name, func(t *testing.T) {
139
collr := test.prepare()
140
140
- require.NoError(t, collr.Init())
141
+ require.NoError(t, collr.Init(context.Background()))
142
143
if test.wantFail {
143
- assert.Error(t, collr.Check())
144
+ assert.Error(t, collr.Check(context.Background()))
145
} else {
145
- assert.NoError(t, collr.Check())
146
+ assert.NoError(t, collr.Check(context.Background()))
147
}
148
})
149
}
@@ -258,10 +259,11 @@ func TestCollector_Collect(t *testing.T) {
259
t.Run(name, func(t *testing.T) {
260
collr := test.prepare()
261
261
- require.NoError(t, collr.Init())
262
- _ = collr.Check()
262
+ require.NoError(t, collr.Init(context.Background()))
263
264
- mx := collr.Collect()
264
+ _ = collr.Check(context.Background())
265
+
266
+ mx := collr.Collect(context.Background())
267
268
assert.Equal(t, test.expected, mx)
269
assert.Equal(t, test.expectedCharts, len(*collr.Charts()))
src/go/plugin/go.d/collector/nvidia_smi/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package nvidia_smi
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"runtime"
@@ -66,7 +67,7 @@ func (c *Collector) Configuration() any {
67
return c.Config
68
}
69
69
-func (c *Collector) Init() error {
70
+func (c *Collector) Init(context.Context) error {
71
if c.exec == nil {
72
if runtime.GOOS == "windows" && c.LoopMode {
73
c.LoopMode = false
@@ -81,7 +82,7 @@ func (c *Collector) Init() error {
82
return nil
83
}
84
84
-func (c *Collector) Check() error {
85
+func (c *Collector) Check(context.Context) error {
86
mx, err := c.collect()
87
if err != nil {
88
return err
@@ -96,7 +97,7 @@ func (c *Collector) Charts() *module.Charts {
97
return c.charts
98
}
99
99
-func (c *Collector) Collect() map[string]int64 {
100
+func (c *Collector) Collect(context.Context) map[string]int64 {
101
mx, err := c.collect()
102
if err != nil {
103
c.Error(err)
@@ -108,7 +109,7 @@ func (c *Collector) Collect() map[string]int64 {
109
return mx
110
}
111
111
-func (c *Collector) Cleanup() {
112
+func (c *Collector) Cleanup(context.Context) {
113
if c.exec != nil {
114
if err := c.exec.stop(); err != nil {
115
c.Errorf("cleanup: %v", err)
src/go/plugin/go.d/collector/nvidia_smi/collector_test.go
+11
-10
@@ -3,6 +3,7 @@
3
package nvidia_smi
4
5
import (
6
+ "context"
7
"errors"
8
"fmt"
9
"os"
@@ -64,9 +65,9 @@ func TestCollector_Init(t *testing.T) {
65
test.prepare(collr)
66
67
if test.wantFail {
67
- assert.Error(t, collr.Init())
68
+ assert.Error(t, collr.Init(context.Background()))
69
} else {
69
- assert.NoError(t, collr.Init())
70
+ assert.NoError(t, collr.Init(context.Background()))
71
}
72
})
73
}
@@ -110,9 +111,9 @@ func TestCollector_Check(t *testing.T) {
111
test.prepare(collr)
112
113
if test.wantFail {
113
- assert.Error(t, collr.Check())
114
+ assert.Error(t, collr.Check(context.Background()))
115
} else {
115
- assert.NoError(t, collr.Check())
116
+ assert.NoError(t, collr.Check(context.Background()))
117
}
118
})
119
}
@@ -128,7 +129,7 @@ func TestCollector_Collect(t *testing.T) {
129
{
130
prepare: prepareCaseMIGA100,
131
check: func(t *testing.T, collr *Collector) {
131
- mx := collr.Collect()
132
+ mx := collr.Collect(context.Background())
133
134
expected := map[string]int64{
135
"gpu_GPU-27b94a00-ed54-5c24-b1fd-1054085de32a_bar1_memory_usage_free": 68718428160,
@@ -188,7 +189,7 @@ func TestCollector_Collect(t *testing.T) {
189
{
190
prepare: prepareCaseRTX4090Driver535,
191
check: func(t *testing.T, collr *Collector) {
191
- mx := collr.Collect()
192
+ mx := collr.Collect(context.Background())
193
194
expected := map[string]int64{
195
"gpu_GPU-71d1acc2-662d-2166-bf9f-65272d2fc437_bar1_memory_usage_free": 267386880,
@@ -238,7 +239,7 @@ func TestCollector_Collect(t *testing.T) {
239
{
240
prepare: prepareCaseRTX3060,
241
check: func(t *testing.T, collr *Collector) {
241
- mx := collr.Collect()
242
+ mx := collr.Collect(context.Background())
243
244
expected := map[string]int64{
245
"gpu_GPU-473d8d0f-d462-185c-6b36-6fc23e23e571_bar1_memory_usage_free": 8586788864,
@@ -287,7 +288,7 @@ func TestCollector_Collect(t *testing.T) {
288
{
289
prepare: prepareCaseTeslaP100,
290
check: func(t *testing.T, collr *Collector) {
290
- mx := collr.Collect()
291
+ mx := collr.Collect(context.Background())
292
293
expected := map[string]int64{
294
"gpu_GPU-d3da8716-eaab-75db-efc1-60e88e1cd55e_bar1_memory_usage_free": 17177772032,
@@ -335,7 +336,7 @@ func TestCollector_Collect(t *testing.T) {
336
{
337
prepare: prepareCaseRTX2080Win,
338
check: func(t *testing.T, collr *Collector) {
338
- mx := collr.Collect()
339
+ mx := collr.Collect(context.Background())
340
341
expected := map[string]int64{
342
"gpu_GPU-fbd55ed4-1eec-4423-0a47-ad594b4333e3_bar1_memory_usage_free": 266338304,
@@ -384,7 +385,7 @@ func TestCollector_Collect(t *testing.T) {
385
{
386
prepare: prepareCaseErrOnQueryGPUInfo,
387
check: func(t *testing.T, collr *Collector) {
387
- mx := collr.Collect()
388
+ mx := collr.Collect(context.Background())
389
390
assert.Equal(t, map[string]int64(nil), mx)
391
},
src/go/plugin/go.d/collector/nvme/collector.go
+5
-4
@@ -5,6 +5,7 @@
5
package nvme
6
7
import (
8
+ "context"
9
_ "embed"
10
"errors"
11
"fmt"
@@ -64,7 +65,7 @@ func (c *Collector) Configuration() any {
65
return c.Config
66
}
67
67
-func (c *Collector) Init() error {
68
+func (c *Collector) Init(context.Context) error {
69
nvmeExec, err := c.initNVMeCLIExec()
70
if err != nil {
71
return fmt.Errorf("init nvme-cli exec: %v", err)
@@ -74,7 +75,7 @@ func (c *Collector) Init() error {
75
return nil
76
}
77
77
-func (c *Collector) Check() error {
78
+func (c *Collector) Check(context.Context) error {
79
mx, err := c.collect()
80
if err != nil {
81
return err
@@ -89,7 +90,7 @@ func (c *Collector) Charts() *module.Charts {
90
return c.charts
91
}
92
92
-func (c *Collector) Collect() map[string]int64 {
93
+func (c *Collector) Collect(context.Context) map[string]int64 {
94
mx, err := c.collect()
95
if err != nil {
96
c.Error(err)
@@ -101,4 +102,4 @@ func (c *Collector) Collect() map[string]int64 {
102
return mx
103
}
104
104
-func (c *Collector) Cleanup() {}
105
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/nvme/collector_test.go
+12
-11
@@ -5,6 +5,7 @@
5
package nvme
6
7
import (
8
+ "context"
9
"encoding/json"
10
"errors"
11
"fmt"
@@ -61,9 +62,9 @@ func TestCollector_Init(t *testing.T) {
62
collr := New()
63
64
if test.wantFail {
64
- assert.Error(t, collr.Init())
65
+ assert.Error(t, collr.Init(context.Background()))
66
} else {
66
- assert.NoError(t, collr.Init())
67
+ assert.NoError(t, collr.Init(context.Background()))
68
}
69
})
70
}
@@ -74,7 +75,7 @@ func TestCollector_Charts(t *testing.T) {
75
}
76
77
func TestCollector_Cleanup(t *testing.T) {
77
- assert.NotPanics(t, New().Cleanup)
78
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
79
}
80
81
func TestCollector_Check(t *testing.T) {
@@ -107,9 +108,9 @@ func TestCollector_Check(t *testing.T) {
108
test.prepare(collr)
109
110
if test.wantFail {
110
- assert.Error(t, collr.Check())
111
+ assert.Error(t, collr.Check(context.Background()))
112
} else {
112
- assert.NoError(t, collr.Check())
113
+ assert.NoError(t, collr.Check(context.Background()))
114
}
115
})
116
}
@@ -126,7 +127,7 @@ func TestCollector_Collect(t *testing.T) {
127
{
128
prepare: prepareCaseOK,
129
check: func(t *testing.T, collr *Collector) {
129
- mx := collr.Collect()
130
+ mx := collr.Collect(context.Background())
131
132
expected := map[string]int64{
133
"device_nvme0n1_available_spare": 100,
@@ -189,7 +190,7 @@ func TestCollector_Collect(t *testing.T) {
190
{
191
prepare: prepareCaseStringValuesOK,
192
check: func(t *testing.T, collr *Collector) {
192
- mx := collr.Collect()
193
+ mx := collr.Collect(context.Background())
194
195
expected := map[string]int64{
196
"device_nvme0n1_available_spare": 100,
@@ -252,7 +253,7 @@ func TestCollector_Collect(t *testing.T) {
253
{
254
prepare: prepareCaseFloatValuesOK,
255
check: func(t *testing.T, collr *Collector) {
255
- mx := collr.Collect()
256
+ mx := collr.Collect(context.Background())
257
258
expected := map[string]int64{
259
"device_nvme0n1_available_spare": 100,
@@ -315,7 +316,7 @@ func TestCollector_Collect(t *testing.T) {
316
{
317
prepare: prepareCaseEmptyList,
318
check: func(t *testing.T, collr *Collector) {
318
- mx := collr.Collect()
319
+ mx := collr.Collect(context.Background())
320
321
assert.Equal(t, (map[string]int64)(nil), mx)
322
},
@@ -325,7 +326,7 @@ func TestCollector_Collect(t *testing.T) {
326
{
327
prepare: prepareCaseErrOnList,
328
check: func(t *testing.T, collr *Collector) {
328
- mx := collr.Collect()
329
+ mx := collr.Collect(context.Background())
330
331
assert.Equal(t, (map[string]int64)(nil), mx)
332
},
@@ -335,7 +336,7 @@ func TestCollector_Collect(t *testing.T) {
336
{
337
prepare: prepareCaseErrOnSmartLog,
338
check: func(t *testing.T, collr *Collector) {
338
- mx := collr.Collect()
339
+ mx := collr.Collect(context.Background())
340
341
assert.Equal(t, (map[string]int64)(nil), mx)
342
},
src/go/plugin/go.d/collector/openldap/collect.go
+3
-2
@@ -3,6 +3,7 @@
3
package openldap
4
5
import (
6
+ "context"
7
"github.com/go-ldap/ldap/v3"
8
)
9
@@ -18,11 +19,11 @@ func (c *Collector) collect() (map[string]int64, error) {
19
mx := make(map[string]int64)
20
21
if err := c.collectMonitorCounters(mx); err != nil {
21
- c.Cleanup()
22
+ c.Cleanup(context.Background())
23
return nil, err
24
}
25
if err := c.collectOperations(mx); err != nil {
25
- c.Cleanup()
26
+ c.Cleanup(context.Background())
27
return nil, err
28
}
29
src/go/plugin/go.d/collector/openldap/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package openldap
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"time"
@@ -63,7 +64,7 @@ func (c *Collector) Configuration() any {
64
return c.Config
65
}
66
66
-func (c *Collector) Init() error {
67
+func (c *Collector) Init(context.Context) error {
68
if c.URL == "" {
69
return errors.New("empty LDAP server url")
70
}
@@ -74,7 +75,7 @@ func (c *Collector) Init() error {
75
return nil
76
}
77
77
-func (c *Collector) Check() error {
78
+func (c *Collector) Check(context.Context) error {
79
mx, err := c.collect()
80
if err != nil {
81
return err
@@ -91,7 +92,7 @@ func (c *Collector) Charts() *module.Charts {
92
return c.charts
93
}
94
94
-func (c *Collector) Collect() map[string]int64 {
95
+func (c *Collector) Collect(context.Context) map[string]int64 {
96
mx, err := c.collect()
97
if err != nil {
98
c.Error(err)
@@ -104,7 +105,7 @@ func (c *Collector) Collect() map[string]int64 {
105
return mx
106
}
107
107
-func (c *Collector) Cleanup() {
108
+func (c *Collector) Cleanup(context.Context) {
109
if c.conn != nil {
110
if err := c.conn.disconnect(); err != nil {
111
c.Warningf("error disconnecting ldap client: %v", err)
src/go/plugin/go.d/collector/openldap/collector_test.go
+10
-9
@@ -3,6 +3,7 @@
3
package openldap
4
5
import (
6
+ "context"
7
"errors"
8
"fmt"
9
"os"
@@ -58,9 +59,9 @@ func TestCollector_Init(t *testing.T) {
59
collr.Config = test.config
60
61
if test.wantFail {
61
- assert.Error(t, collr.Init())
62
+ assert.Error(t, collr.Init(context.Background()))
63
} else {
63
- assert.NoError(t, collr.Init())
64
+ assert.NoError(t, collr.Init(context.Background()))
65
}
66
})
67
}
@@ -79,7 +80,7 @@ func TestCollector_Cleanup(t *testing.T) {
80
prepare: func() *Collector {
81
collr := New()
82
collr.newConn = func(Config) ldapConn { return prepareMockOk() }
82
- _ = collr.Check()
83
+ _ = collr.Check(context.Background())
84
return collr
85
},
86
},
@@ -87,7 +88,7 @@ func TestCollector_Cleanup(t *testing.T) {
88
prepare: func() *Collector {
89
collr := New()
90
collr.newConn = func(Config) ldapConn { return prepareMockOk() }
90
- _ = collr.Collect()
91
+ _ = collr.Collect(context.Background())
92
return collr
93
},
94
},
@@ -97,7 +98,7 @@ func TestCollector_Cleanup(t *testing.T) {
98
t.Run(name, func(t *testing.T) {
99
collr := test.prepare()
100
100
- assert.NotPanics(t, collr.Cleanup)
101
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
102
})
103
}
104
}
@@ -132,9 +133,9 @@ func TestCollector_Check(t *testing.T) {
133
collr.newConn = func(Config) ldapConn { return mock }
134
135
if test.wantFail {
135
- assert.Error(t, collr.Check())
136
+ assert.Error(t, collr.Check(context.Background()))
137
} else {
137
- assert.NoError(t, collr.Check())
138
+ assert.NoError(t, collr.Check(context.Background()))
139
}
140
})
141
}
@@ -195,7 +196,7 @@ func TestCollector_Collect(t *testing.T) {
196
mock := test.prepareMock()
197
collr.newConn = func(Config) ldapConn { return mock }
198
198
- mx := collr.Collect()
199
+ mx := collr.Collect(context.Background())
200
201
require.Equal(t, test.wantMetrics, mx)
202
@@ -204,7 +205,7 @@ func TestCollector_Collect(t *testing.T) {
205
}
206
207
assert.Equal(t, test.disconnectBeforeCleanup, mock.disconnectCalled, "disconnect before cleanup")
207
- collr.Cleanup()
208
+ collr.Cleanup(context.Background())
209
assert.Equal(t, test.disconnectAfterCleanup, mock.disconnectCalled, "disconnect after cleanup")
210
})
211
}
src/go/plugin/go.d/collector/openvpn/collector.go
+6
-5
@@ -3,6 +3,7 @@
3
package openvpn
4
5
import (
6
+ "context"
7
_ "embed"
8
"time"
9
@@ -67,7 +68,7 @@ func (c *Collector) Configuration() any {
68
return c.Config
69
}
70
70
-func (c *Collector) Init() error {
71
+func (c *Collector) Init(context.Context) error {
72
if err := c.validateConfig(); err != nil {
73
return err
74
}
@@ -85,7 +86,7 @@ func (c *Collector) Init() error {
86
return nil
87
}
88
88
-func (c *Collector) Check() error {
89
+func (c *Collector) Check(ctx context.Context) error {
90
if err := c.client.Connect(); err != nil {
91
return err
92
}
@@ -93,7 +94,7 @@ func (c *Collector) Check() error {
94
95
ver, err := c.client.Version()
96
if err != nil {
96
- c.Cleanup()
97
+ c.Cleanup(ctx)
98
return err
99
}
100
@@ -104,7 +105,7 @@ func (c *Collector) Check() error {
105
106
func (c *Collector) Charts() *Charts { return c.charts }
107
107
-func (c *Collector) Collect() map[string]int64 {
108
+func (c *Collector) Collect(context.Context) map[string]int64 {
109
mx, err := c.collect()
110
if err != nil {
111
c.Error(err)
@@ -116,7 +117,7 @@ func (c *Collector) Collect() map[string]int64 {
117
return mx
118
}
119
119
-func (c *Collector) Cleanup() {
120
+func (c *Collector) Cleanup(context.Context) {
121
if c.client == nil {
122
return
123
}
src/go/plugin/go.d/collector/openvpn/collector_test.go
+14
-13
@@ -3,6 +3,7 @@
3
package openvpn
4
5
import (
6
+ "context"
7
"os"
8
"testing"
9
@@ -34,15 +35,15 @@ func TestCollector_ConfigurationSerialize(t *testing.T) {
35
}
36
37
func TestCollector_Init(t *testing.T) {
37
- assert.NoError(t, New().Init())
38
+ assert.NoError(t, New().Init(context.Background()))
39
}
40
41
func TestCollector_Check(t *testing.T) {
42
collr := New()
43
43
- require.NoError(t, collr.Init())
44
+ require.NoError(t, collr.Init(context.Background()))
45
collr.client = prepareMockOpenVPNClient()
45
- require.NoError(t, collr.Check())
46
+ require.NoError(t, collr.Check(context.Background()))
47
}
48
49
func TestCollector_Charts(t *testing.T) {
@@ -52,20 +53,20 @@ func TestCollector_Charts(t *testing.T) {
53
func TestCollector_Cleanup(t *testing.T) {
54
collr := New()
55
55
- assert.NotPanics(t, collr.Cleanup)
56
- require.NoError(t, collr.Init())
56
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
57
+ require.NoError(t, collr.Init(context.Background()))
58
collr.client = prepareMockOpenVPNClient()
58
- require.NoError(t, collr.Check())
59
- collr.Cleanup()
59
+ require.NoError(t, collr.Check(context.Background()))
60
+ collr.Cleanup(context.Background())
61
}
62
63
func TestCollector_Collect(t *testing.T) {
64
collr := New()
65
65
- require.NoError(t, collr.Init())
66
+ require.NoError(t, collr.Init(context.Background()))
67
collr.perUserMatcher = matcher.TRUE()
68
collr.client = prepareMockOpenVPNClient()
68
- require.NoError(t, collr.Check())
69
+ require.NoError(t, collr.Check(context.Background()))
70
71
expected := map[string]int64{
72
"bytes_in": 1,
@@ -75,7 +76,7 @@ func TestCollector_Collect(t *testing.T) {
76
"name_bytes_sent": 2,
77
}
78
78
- mx := collr.Collect()
79
+ mx := collr.Collect(context.Background())
80
require.NotNil(t, mx)
81
delete(mx, "name_connection_time")
82
assert.Equal(t, expected, mx)
@@ -84,12 +85,12 @@ func TestCollector_Collect(t *testing.T) {
85
func TestCollector_Collect_UNDEFUsername(t *testing.T) {
86
collr := New()
87
87
- require.NoError(t, collr.Init())
88
+ require.NoError(t, collr.Init(context.Background()))
89
collr.perUserMatcher = matcher.TRUE()
90
cl := prepareMockOpenVPNClient()
91
cl.users = testUsersUNDEF
92
collr.client = cl
92
- require.NoError(t, collr.Check())
93
+ require.NoError(t, collr.Check(context.Background()))
94
95
expected := map[string]int64{
96
"bytes_in": 1,
@@ -99,7 +100,7 @@ func TestCollector_Collect_UNDEFUsername(t *testing.T) {
100
"common_name_bytes_sent": 2,
101
}
102
102
- mx := collr.Collect()
103
+ mx := collr.Collect(context.Background())
104
require.NotNil(t, mx)
105
106
delete(mx, "common_name_connection_time")
src/go/plugin/go.d/collector/openvpn_status_log/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package openvpn_status_log
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -52,7 +53,7 @@ func (c *Collector) Configuration() any {
53
return c.Config
54
}
55
55
-func (c *Collector) Init() error {
56
+func (c *Collector) Init(context.Context) error {
57
if err := c.validateConfig(); err != nil {
58
return fmt.Errorf("error on validating config: %v", err)
59
}
@@ -68,7 +69,7 @@ func (c *Collector) Init() error {
69
return nil
70
}
71
71
-func (c *Collector) Check() error {
72
+func (c *Collector) Check(context.Context) error {
73
mx, err := c.collect()
74
if err != nil {
75
return err
@@ -83,7 +84,7 @@ func (c *Collector) Charts() *module.Charts {
84
return c.charts
85
}
86
86
-func (c *Collector) Collect() map[string]int64 {
87
+func (c *Collector) Collect(context.Context) map[string]int64 {
88
mx, err := c.collect()
89
if err != nil {
90
c.Error(err)
@@ -95,4 +96,4 @@ func (c *Collector) Collect() map[string]int64 {
96
return mx
97
}
98
98
-func (c *Collector) Cleanup() {}
99
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/openvpn_status_log/collector_test.go
+12
-11
@@ -3,6 +3,7 @@
3
package openvpn_status_log
4
5
import (
6
+ "context"
7
"os"
8
"strings"
9
"testing"
@@ -66,9 +67,9 @@ func TestCollector_Init(t *testing.T) {
67
collr.Config = test.config
68
69
if test.wantFail {
69
- assert.Error(t, collr.Init())
70
+ assert.Error(t, collr.Init(context.Background()))
71
} else {
71
- assert.NoError(t, collr.Init())
72
+ assert.NoError(t, collr.Init(context.Background()))
73
}
74
})
75
}
@@ -93,12 +94,12 @@ func TestCollector_Check(t *testing.T) {
94
t.Run(name, func(t *testing.T) {
95
collr := test.prepare()
96
96
- require.NoError(t, collr.Init())
97
+ require.NoError(t, collr.Init(context.Background()))
98
99
if test.wantFail {
99
- assert.Error(t, collr.Check())
100
+ assert.Error(t, collr.Check(context.Background()))
101
} else {
101
- assert.NoError(t, collr.Check())
102
+ assert.NoError(t, collr.Check(context.Background()))
103
}
104
})
105
}
@@ -131,9 +132,9 @@ func TestCollector_Charts(t *testing.T) {
132
t.Run(name, func(t *testing.T) {
133
collr := test.prepare()
134
134
- require.NoError(t, collr.Init())
135
- _ = collr.Check()
136
- _ = collr.Collect()
135
+ require.NoError(t, collr.Init(context.Background()))
136
+ _ = collr.Check(context.Background())
137
+ _ = collr.Collect(context.Background())
138
139
assert.Equal(t, test.wantNumCharts, len(*collr.Charts()))
140
})
@@ -257,10 +258,10 @@ func TestCollector_Collect(t *testing.T) {
258
t.Run(name, func(t *testing.T) {
259
collr := test.prepare()
260
260
- require.NoError(t, collr.Init())
261
- _ = collr.Check()
261
+ require.NoError(t, collr.Init(context.Background()))
262
+ _ = collr.Check(context.Background())
263
263
- collected := collr.Collect()
264
+ collected := collr.Collect(context.Background())
265
266
copyConnTime(collected, test.expected)
267
assert.Equal(t, test.expected, collected)
src/go/plugin/go.d/collector/oracledb/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package oracledb
4
5
import (
6
+ "context"
7
"database/sql"
8
_ "embed"
9
"errors"
@@ -59,7 +60,7 @@ func (c *Collector) Configuration() any {
60
return c.Config
61
}
62
62
-func (c *Collector) Init() error {
63
+func (c *Collector) Init(context.Context) error {
64
dsn, err := c.validateDSN()
65
if err != nil {
66
return fmt.Errorf("invalid oracle DSN: %w", err)
@@ -70,7 +71,7 @@ func (c *Collector) Init() error {
71
return nil
72
}
73
73
-func (c *Collector) Check() error {
74
+func (c *Collector) Check(context.Context) error {
75
mx, err := c.collect()
76
if err != nil {
77
return fmt.Errorf("failed to collect metrics [%s]: %w", c.publicDSN, err)
@@ -86,7 +87,7 @@ func (c *Collector) Charts() *module.Charts {
87
return c.charts
88
}
89
89
-func (c *Collector) Collect() map[string]int64 {
90
+func (c *Collector) Collect(context.Context) map[string]int64 {
91
mx, err := c.collect()
92
if err != nil {
93
c.Error(fmt.Sprintf("failed to collect metrics [%s]: %s", c.publicDSN, err))
@@ -99,7 +100,7 @@ func (c *Collector) Collect() map[string]int64 {
100
return mx
101
}
102
102
-func (c *Collector) Cleanup() {
103
+func (c *Collector) Cleanup(context.Context) {
104
if c.db != nil {
105
if err := c.db.Close(); err != nil {
106
c.Errorf("cleanup: error on closing connection [%s]: %v", c.publicDSN, err)
src/go/plugin/go.d/collector/oracledb/collector_test.go
+9
-8
@@ -5,6 +5,7 @@ package oracledb
5
import (
6
"bufio"
7
"bytes"
8
+ "context"
9
"database/sql/driver"
10
"errors"
11
"fmt"
@@ -71,9 +72,9 @@ func TestCollector_Init(t *testing.T) {
72
collr.Config = test.config
73
74
if test.wantFail {
74
- assert.Error(t, collr.Init())
75
+ assert.Error(t, collr.Init(context.Background()))
76
} else {
76
- assert.NoError(t, collr.Init())
77
+ assert.NoError(t, collr.Init(context.Background()))
78
}
79
})
80
}
@@ -102,7 +103,7 @@ func TestCollector_Cleanup(t *testing.T) {
103
collr, cleanup := prepare(t)
104
defer cleanup()
105
105
- assert.NotPanics(t, collr.Cleanup)
106
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
107
assert.Nil(t, collr.db)
108
})
109
}
@@ -142,14 +143,14 @@ func TestCollector_Check(t *testing.T) {
143
collr.db = db
144
defer func() { _ = db.Close() }()
145
145
- require.NoError(t, collr.Init())
146
+ require.NoError(t, collr.Init(context.Background()))
147
148
test.prepareMock(t, mock)
149
150
if test.wantFail {
150
- assert.Error(t, collr.Check())
151
+ assert.Error(t, collr.Check(context.Background()))
152
} else {
152
- assert.NoError(t, collr.Check())
153
+ assert.NoError(t, collr.Check(context.Background()))
154
}
155
assert.NoError(t, mock.ExpectationsWereMet())
156
})
@@ -243,11 +244,11 @@ func TestCollector_Collect(t *testing.T) {
244
collr.db = db
245
defer func() { _ = db.Close() }()
246
246
- require.NoError(t, collr.Init())
247
+ require.NoError(t, collr.Init(context.Background()))
248
249
test.prepareMock(t, mock)
250
250
- mx := collr.Collect()
251
+ mx := collr.Collect(context.Background())
252
253
require.Equal(t, test.wantMetrics, mx)
254
if len(test.wantMetrics) > 0 {
src/go/plugin/go.d/collector/pgbouncer/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package pgbouncer
4
5
import (
6
+ "context"
7
"database/sql"
8
_ "embed"
9
"errors"
@@ -67,7 +68,7 @@ func (c *Collector) Configuration() any {
68
return c.Config
69
}
70
70
-func (c *Collector) Init() error {
71
+func (c *Collector) Init(context.Context) error {
72
err := c.validateConfig()
73
if err != nil {
74
return fmt.Errorf("config validation: %v", err)
@@ -76,7 +77,7 @@ func (c *Collector) Init() error {
77
return nil
78
}
79
79
-func (c *Collector) Check() error {
80
+func (c *Collector) Check(context.Context) error {
81
mx, err := c.collect()
82
if err != nil {
83
return err
@@ -91,7 +92,7 @@ func (c *Collector) Charts() *module.Charts {
92
return c.charts
93
}
94
94
-func (c *Collector) Collect() map[string]int64 {
95
+func (c *Collector) Collect(context.Context) map[string]int64 {
96
mx, err := c.collect()
97
if err != nil {
98
c.Error(err)
@@ -103,7 +104,7 @@ func (c *Collector) Collect() map[string]int64 {
104
return mx
105
}
106
106
-func (c *Collector) Cleanup() {
107
+func (c *Collector) Cleanup(context.Context) {
108
if c.db == nil {
109
return
110
}
src/go/plugin/go.d/collector/pgbouncer/collector_test.go
+11
-10
@@ -5,6 +5,7 @@ package pgbouncer
5
import (
6
"bufio"
7
"bytes"
8
+ "context"
9
"database/sql/driver"
10
"errors"
11
"fmt"
@@ -71,9 +72,9 @@ func TestCollector_Init(t *testing.T) {
72
collr.Config = test.config
73
74
if test.wantFail {
74
- assert.Error(t, collr.Init())
75
+ assert.Error(t, collr.Init(context.Background()))
76
} else {
76
- assert.NoError(t, collr.Init())
77
+ assert.NoError(t, collr.Init(context.Background()))
78
}
79
})
80
}
@@ -129,14 +130,14 @@ func TestCollector_Check(t *testing.T) {
130
collr.db = db
131
defer func() { _ = db.Close() }()
132
132
- require.NoError(t, collr.Init())
133
+ require.NoError(t, collr.Init(context.Background()))
134
135
test.prepareMock(t, mock)
136
137
if test.wantFail {
137
- assert.Error(t, collr.Check())
138
+ assert.Error(t, collr.Check(context.Background()))
139
} else {
139
- assert.NoError(t, collr.Check())
140
+ assert.NoError(t, collr.Check(context.Background()))
141
}
142
assert.NoError(t, mock.ExpectationsWereMet())
143
})
@@ -159,7 +160,7 @@ func TestCollector_Collect(t *testing.T) {
160
mockExpect(t, m, queryShowPools, dataVer1170Pools)
161
},
162
check: func(t *testing.T, collr *Collector) {
162
- mx := collr.Collect()
163
+ mx := collr.Collect(context.Background())
164
165
expected := map[string]int64{
166
"cl_conns_utilization": 47,
@@ -251,7 +252,7 @@ func TestCollector_Collect(t *testing.T) {
252
mockExpectErr(m, queryShowVersion)
253
},
254
check: func(t *testing.T, collr *Collector) {
254
- mx := collr.Collect()
255
+ mx := collr.Collect(context.Background())
256
var expected map[string]int64
257
assert.Equal(t, expected, mx)
258
},
@@ -263,7 +264,7 @@ func TestCollector_Collect(t *testing.T) {
264
mockExpect(t, m, queryShowVersion, dataVer170Version)
265
},
266
check: func(t *testing.T, collr *Collector) {
266
- mx := collr.Collect()
267
+ mx := collr.Collect(context.Background())
268
var expected map[string]int64
269
assert.Equal(t, expected, mx)
270
},
@@ -276,7 +277,7 @@ func TestCollector_Collect(t *testing.T) {
277
mockExpectErr(m, queryShowConfig)
278
},
279
check: func(t *testing.T, collr *Collector) {
279
- mx := collr.Collect()
280
+ mx := collr.Collect(context.Background())
281
var expected map[string]int64
282
assert.Equal(t, expected, mx)
283
},
@@ -294,7 +295,7 @@ func TestCollector_Collect(t *testing.T) {
295
collr.db = db
296
defer func() { _ = db.Close() }()
297
297
- require.NoError(t, collr.Init())
298
+ require.NoError(t, collr.Init(context.Background()))
299
300
for i, step := range test {
301
t.Run(fmt.Sprintf("step[%d]", i), func(t *testing.T) {
src/go/plugin/go.d/collector/phpdaemon/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package phpdaemon
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -61,7 +62,7 @@ func (c *Collector) Configuration() any {
62
return c.Config
63
}
64
64
-func (c *Collector) Init() error {
65
+func (c *Collector) Init(context.Context) error {
66
if c.URL == "" {
67
return errors.New("phpDaemon URL is required but not set")
68
}
@@ -78,7 +79,7 @@ func (c *Collector) Init() error {
79
return nil
80
}
81
81
-func (c *Collector) Check() error {
82
+func (c *Collector) Check(context.Context) error {
83
mx, err := c.collect()
84
if err != nil {
85
return err
@@ -95,7 +96,7 @@ func (c *Collector) Charts() *Charts {
96
return c.charts
97
}
98
98
-func (c *Collector) Collect() map[string]int64 {
99
+func (c *Collector) Collect(context.Context) map[string]int64 {
100
mx, err := c.collect()
101
if err != nil {
102
c.Error(err)
@@ -105,7 +106,7 @@ func (c *Collector) Collect() map[string]int64 {
106
return mx
107
}
108
108
-func (c *Collector) Cleanup() {
109
+func (c *Collector) Cleanup(context.Context) {
110
if c.httpClient != nil {
111
c.httpClient.CloseIdleConnections()
112
}
src/go/plugin/go.d/collector/phpdaemon/collector_test.go
+16
-15
@@ -3,6 +3,7 @@
3
package phpdaemon
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -38,7 +39,7 @@ func TestCollector_ConfigurationSerialize(t *testing.T) {
39
func TestCollector_Init(t *testing.T) {
40
collr := New()
41
41
- require.NoError(t, collr.Init())
42
+ require.NoError(t, collr.Init(context.Background()))
43
}
44
45
func TestCollector_Check(t *testing.T) {
@@ -51,15 +52,15 @@ func TestCollector_Check(t *testing.T) {
52
53
collr := New()
54
collr.URL = ts.URL
54
- require.NoError(t, collr.Init())
55
- assert.NoError(t, collr.Check())
55
+ require.NoError(t, collr.Init(context.Background()))
56
+ assert.NoError(t, collr.Check(context.Background()))
57
}
58
59
func TestCollector_CheckNG(t *testing.T) {
60
collr := New()
61
collr.URL = "http://127.0.0.1:38001"
61
- require.NoError(t, collr.Init())
62
- assert.Error(t, collr.Check())
62
+ require.NoError(t, collr.Init(context.Background()))
63
+ assert.Error(t, collr.Check(context.Background()))
64
}
65
66
func TestCollector_Charts(t *testing.T) {
@@ -76,13 +77,13 @@ func TestCollector_Charts(t *testing.T) {
77
defer ts.Close()
78
79
collr.URL = ts.URL
79
- require.NoError(t, collr.Init())
80
- assert.NoError(t, collr.Check())
80
+ require.NoError(t, collr.Init(context.Background()))
81
+ assert.NoError(t, collr.Check(context.Background()))
82
assert.True(t, collr.charts.Has(uptimeChart.ID))
83
}
84
85
func TestCollector_Cleanup(t *testing.T) {
85
- assert.NotPanics(t, New().Cleanup)
86
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
87
}
88
89
func TestCollector_Collect(t *testing.T) {
@@ -95,8 +96,8 @@ func TestCollector_Collect(t *testing.T) {
96
97
collr := New()
98
collr.URL = ts.URL
98
- require.NoError(t, collr.Init())
99
- assert.NoError(t, collr.Check())
99
+ require.NoError(t, collr.Init(context.Background()))
100
+ assert.NoError(t, collr.Check(context.Background()))
101
102
expected := map[string]int64{
103
"alive": 350,
@@ -110,7 +111,7 @@ func TestCollector_Collect(t *testing.T) {
111
"uptime": 15765,
112
}
113
113
- assert.Equal(t, expected, collr.Collect())
114
+ assert.Equal(t, expected, collr.Collect(context.Background()))
115
116
}
117
@@ -124,8 +125,8 @@ func TestCollector_InvalidData(t *testing.T) {
125
126
collr := New()
127
collr.URL = ts.URL
127
- require.NoError(t, collr.Init())
128
- assert.Error(t, collr.Check())
128
+ require.NoError(t, collr.Init(context.Background()))
129
+ assert.Error(t, collr.Check(context.Background()))
130
}
131
132
func TestCollector_404(t *testing.T) {
@@ -138,6 +139,6 @@ func TestCollector_404(t *testing.T) {
139
140
collr := New()
141
collr.URL = ts.URL
141
- require.NoError(t, collr.Init())
142
- assert.Error(t, collr.Check())
142
+ require.NoError(t, collr.Init(context.Background()))
143
+ assert.Error(t, collr.Check(context.Background()))
144
}
src/go/plugin/go.d/collector/phpfpm/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package phpfpm
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -59,7 +60,7 @@ func (c *Collector) Configuration() any {
60
return c.Config
61
}
62
62
-func (c *Collector) Init() error {
63
+func (c *Collector) Init(context.Context) error {
64
cli, err := c.initClient()
65
if err != nil {
66
return fmt.Errorf("init client: %v", err)
@@ -69,7 +70,7 @@ func (c *Collector) Init() error {
70
return nil
71
}
72
72
-func (c *Collector) Check() error {
73
+func (c *Collector) Check(context.Context) error {
74
mx, err := c.collect()
75
if err != nil {
76
return err
@@ -84,7 +85,7 @@ func (c *Collector) Charts() *Charts {
85
return charts.Copy()
86
}
87
87
-func (c *Collector) Collect() map[string]int64 {
88
+func (c *Collector) Collect(context.Context) map[string]int64 {
89
mx, err := c.collect()
90
if err != nil {
91
c.Error(err)
@@ -96,4 +97,4 @@ func (c *Collector) Collect() map[string]int64 {
97
return mx
98
}
99
99
-func (c *Collector) Cleanup() {}
100
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/phpfpm/collector_test.go
+23
-22
@@ -3,6 +3,7 @@
3
package phpfpm
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -46,7 +47,7 @@ func TestCollector_ConfigurationSerialize(t *testing.T) {
47
func TestCollector_Init(t *testing.T) {
48
collr := New()
49
49
- require.NoError(t, collr.Init())
50
+ require.NoError(t, collr.Init(context.Background()))
51
assert.NotNil(t, collr.client)
52
}
53
@@ -60,17 +61,17 @@ func TestCollector_Check(t *testing.T) {
61
62
collr := New()
63
collr.URL = ts.URL
63
- require.NoError(t, collr.Init())
64
+ require.NoError(t, collr.Init(context.Background()))
65
65
- assert.NoError(t, collr.Check())
66
+ assert.NoError(t, collr.Check(context.Background()))
67
}
68
69
func TestCollector_CheckReturnsFalseOnFailure(t *testing.T) {
70
collr := New()
71
collr.URL = "http://127.0.0.1:38001/us"
71
- require.NoError(t, collr.Init())
72
+ require.NoError(t, collr.Init(context.Background()))
73
73
- assert.Error(t, collr.Check())
74
+ assert.Error(t, collr.Check(context.Background()))
75
}
76
77
func TestCollector_Charts(t *testing.T) {
@@ -89,9 +90,9 @@ func TestCollector_CollectJSON(t *testing.T) {
90
91
collr := New()
92
collr.URL = ts.URL + "/?json"
92
- require.NoError(t, collr.Init())
93
+ require.NoError(t, collr.Init(context.Background()))
94
94
- got := collr.Collect()
95
+ got := collr.Collect(context.Background())
96
97
want := map[string]int64{
98
"active": 1,
@@ -114,9 +115,9 @@ func TestCollector_CollectJSONFull(t *testing.T) {
115
116
collr := New()
117
collr.URL = ts.URL + "/?json"
117
- require.NoError(t, collr.Init())
118
+ require.NoError(t, collr.Init(context.Background()))
119
119
- got := collr.Collect()
120
+ got := collr.Collect(context.Background())
121
122
want := map[string]int64{
123
"active": 1,
@@ -148,9 +149,9 @@ func TestCollector_CollectNoIdleProcessesJSONFull(t *testing.T) {
149
150
collr := New()
151
collr.URL = ts.URL + "/?json"
151
- require.NoError(t, collr.Init())
152
+ require.NoError(t, collr.Init(context.Background()))
153
153
- got := collr.Collect()
154
+ got := collr.Collect(context.Background())
155
156
want := map[string]int64{
157
"active": 1,
@@ -173,9 +174,9 @@ func TestCollector_CollectText(t *testing.T) {
174
175
collr := New()
176
collr.URL = ts.URL
176
- require.NoError(t, collr.Init())
177
+ require.NoError(t, collr.Init(context.Background()))
178
178
- got := collr.Collect()
179
+ got := collr.Collect(context.Background())
180
181
want := map[string]int64{
182
"active": 1,
@@ -198,9 +199,9 @@ func TestCollector_CollectTextFull(t *testing.T) {
199
200
collr := New()
201
collr.URL = ts.URL
201
- require.NoError(t, collr.Init())
202
+ require.NoError(t, collr.Init(context.Background()))
203
203
- got := collr.Collect()
204
+ got := collr.Collect(context.Background())
205
206
want := map[string]int64{
207
"active": 1,
@@ -232,9 +233,9 @@ func TestCollector_CollectReturnsNothingWhenInvalidData(t *testing.T) {
233
234
collr := New()
235
collr.URL = ts.URL
235
- require.NoError(t, collr.Init())
236
+ require.NoError(t, collr.Init(context.Background()))
237
237
- assert.Len(t, collr.Collect(), 0)
238
+ assert.Len(t, collr.Collect(context.Background()), 0)
239
}
240
241
func TestCollector_CollectReturnsNothingWhenEmptyData(t *testing.T) {
@@ -247,9 +248,9 @@ func TestCollector_CollectReturnsNothingWhenEmptyData(t *testing.T) {
248
249
collr := New()
250
collr.URL = ts.URL
250
- require.NoError(t, collr.Init())
251
+ require.NoError(t, collr.Init(context.Background()))
252
252
- assert.Len(t, collr.Collect(), 0)
253
+ assert.Len(t, collr.Collect(context.Background()), 0)
254
}
255
256
func TestCollector_CollectReturnsNothingWhenBadStatusCode(t *testing.T) {
@@ -262,11 +263,11 @@ func TestCollector_CollectReturnsNothingWhenBadStatusCode(t *testing.T) {
263
264
collr := New()
265
collr.URL = ts.URL
265
- require.NoError(t, collr.Init())
266
+ require.NoError(t, collr.Init(context.Background()))
267
267
- assert.Len(t, collr.Collect(), 0)
268
+ assert.Len(t, collr.Collect(context.Background()), 0)
269
}
270
271
func TestCollector_Cleanup(t *testing.T) {
271
- New().Cleanup()
272
+ New().Cleanup(context.Background())
273
}
src/go/plugin/go.d/collector/pihole/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package pihole
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -72,7 +73,7 @@ func (c *Collector) Configuration() any {
73
return c.Config
74
}
75
75
-func (c *Collector) Init() error {
76
+func (c *Collector) Init(context.Context) error {
77
if err := c.validateConfig(); err != nil {
78
return fmt.Errorf("config validation: %v", err)
79
}
@@ -93,7 +94,7 @@ func (c *Collector) Init() error {
94
return nil
95
}
96
96
-func (c *Collector) Check() error {
97
+func (c *Collector) Check(context.Context) error {
98
mx, err := c.collect()
99
if err != nil {
100
return err
@@ -108,7 +109,7 @@ func (c *Collector) Charts() *module.Charts {
109
return c.charts
110
}
111
111
-func (c *Collector) Collect() map[string]int64 {
112
+func (c *Collector) Collect(context.Context) map[string]int64 {
113
mx, err := c.collect()
114
if err != nil {
115
c.Error(err)
@@ -121,7 +122,7 @@ func (c *Collector) Collect() map[string]int64 {
122
return mx
123
}
124
124
-func (c *Collector) Cleanup() {
125
+func (c *Collector) Cleanup(context.Context) {
126
if c.httpClient != nil {
127
c.httpClient.CloseIdleConnections()
128
}
src/go/plugin/go.d/collector/pihole/collector_test.go
+9
-8
@@ -3,6 +3,7 @@
3
package pihole
4
5
import (
6
+ "context"
7
"fmt"
8
"net/http"
9
"net/http/httptest"
@@ -73,9 +74,9 @@ func TestCollector_Init(t *testing.T) {
74
collr.Config = test.config
75
76
if test.wantFail {
76
- assert.Error(t, collr.Init())
77
+ assert.Error(t, collr.Init(context.Background()))
78
} else {
78
- assert.NoError(t, collr.Init())
79
+ assert.NoError(t, collr.Init(context.Background()))
80
}
81
})
82
}
@@ -106,9 +107,9 @@ func TestCollector_Check(t *testing.T) {
107
defer cleanup()
108
109
if test.wantFail {
109
- assert.Error(t, collr.Check())
110
+ assert.Error(t, collr.Check(context.Background()))
111
} else {
111
- assert.NoError(t, collr.Check())
112
+ assert.NoError(t, collr.Check(context.Background()))
113
}
114
})
115
}
@@ -168,7 +169,7 @@ func TestCollector_Collect(t *testing.T) {
169
collr, cleanup := test.prepare(t)
170
defer cleanup()
171
171
- mx := collr.Collect()
172
+ mx := collr.Collect(context.Background())
173
174
copyBlockListLastUpdate(mx, test.wantMetrics)
175
require.Equal(t, test.wantMetrics, mx)
@@ -185,7 +186,7 @@ func caseSuccessWithWebPassword(t *testing.T) (*Collector, func()) {
186
collr.SetupVarsPath = pathSetupVarsOK
187
collr.URL = srv.URL
188
188
- require.NoError(t, collr.Init())
189
+ require.NoError(t, collr.Init(context.Background()))
190
191
return collr, srv.Close
192
}
@@ -196,7 +197,7 @@ func caseFailNoWebPassword(t *testing.T) (*Collector, func()) {
197
collr.SetupVarsPath = pathSetupVarsWrong
198
collr.URL = srv.URL
199
199
- require.NoError(t, collr.Init())
200
+ require.NoError(t, collr.Init(context.Background()))
201
202
return collr, srv.Close
203
}
@@ -207,7 +208,7 @@ func caseFailUnsupportedVersion(t *testing.T) (*Collector, func()) {
208
collr.SetupVarsPath = pathSetupVarsOK
209
collr.URL = srv.URL
210
210
- require.NoError(t, collr.Init())
211
+ require.NoError(t, collr.Init(context.Background()))
212
213
return collr, srv.Close
214
}
src/go/plugin/go.d/collector/pika/collector.go
+4
-4
@@ -71,7 +71,7 @@ func (c *Collector) Configuration() any {
71
return c.Config
72
}
73
74
-func (c *Collector) Init() error {
74
+func (c *Collector) Init(context.Context) error {
75
err := c.validateConfig()
76
if err != nil {
77
return fmt.Errorf("config validation: %v", err)
@@ -92,7 +92,7 @@ func (c *Collector) Init() error {
92
return nil
93
}
94
95
-func (c *Collector) Check() error {
95
+func (c *Collector) Check(context.Context) error {
96
mx, err := c.collect()
97
if err != nil {
98
return err
@@ -107,7 +107,7 @@ func (c *Collector) Charts() *module.Charts {
107
return c.charts
108
}
109
110
-func (c *Collector) Collect() map[string]int64 {
110
+func (c *Collector) Collect(context.Context) map[string]int64 {
111
ms, err := c.collect()
112
if err != nil {
113
c.Error(err)
@@ -119,7 +119,7 @@ func (c *Collector) Collect() map[string]int64 {
119
return ms
120
}
121
122
-func (c *Collector) Cleanup() {
122
+func (c *Collector) Cleanup(context.Context) {
123
if c.pdb == nil {
124
return
125
}
src/go/plugin/go.d/collector/pika/collector_test.go
+12
-12
@@ -70,9 +70,9 @@ func TestCollector_Init(t *testing.T) {
70
collr.Config = test.config
71
72
if test.wantFail {
73
- assert.Error(t, collr.Init())
73
+ assert.Error(t, collr.Init(context.Background()))
74
} else {
75
- assert.NoError(t, collr.Init())
75
+ assert.NoError(t, collr.Init(context.Background()))
76
}
77
})
78
}
@@ -101,9 +101,9 @@ func TestCollector_Check(t *testing.T) {
101
collr := test.prepare(t)
102
103
if test.wantFail {
104
- assert.Error(t, collr.Check())
104
+ assert.Error(t, collr.Check(context.Background()))
105
} else {
106
- assert.NoError(t, collr.Check())
106
+ assert.NoError(t, collr.Check(context.Background()))
107
}
108
})
109
}
@@ -111,20 +111,20 @@ func TestCollector_Check(t *testing.T) {
111
112
func TestCollector_Charts(t *testing.T) {
113
collr := New()
114
- require.NoError(t, collr.Init())
114
+ require.NoError(t, collr.Init(context.Background()))
115
116
assert.NotNil(t, collr.Charts())
117
}
118
119
func TestCollector_Cleanup(t *testing.T) {
120
collr := New()
121
- assert.NotPanics(t, collr.Cleanup)
121
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
122
123
- require.NoError(t, collr.Init())
123
+ require.NoError(t, collr.Init(context.Background()))
124
m := &mockRedisClient{}
125
collr.pdb = m
126
127
- collr.Cleanup()
127
+ collr.Cleanup(context.Background())
128
129
assert.True(t, m.calledClose)
130
}
@@ -187,7 +187,7 @@ func TestCollector_Collect(t *testing.T) {
187
t.Run(name, func(t *testing.T) {
188
collr := test.prepare(t)
189
190
- mx := collr.Collect()
190
+ mx := collr.Collect(context.Background())
191
192
assert.Equal(t, test.wantCollected, mx)
193
if len(test.wantCollected) > 0 {
@@ -201,7 +201,7 @@ func TestCollector_Collect(t *testing.T) {
201
202
func preparePikaV340(t *testing.T) *Collector {
203
collr := New()
204
- require.NoError(t, collr.Init())
204
+ require.NoError(t, collr.Init(context.Background()))
205
collr.pdb = &mockRedisClient{
206
result: dataVer340InfoAll,
207
}
@@ -210,7 +210,7 @@ func preparePikaV340(t *testing.T) *Collector {
210
211
func preparePikaErrorOnInfo(t *testing.T) *Collector {
212
collr := New()
213
- require.NoError(t, collr.Init())
213
+ require.NoError(t, collr.Init(context.Background()))
214
collr.pdb = &mockRedisClient{
215
errOnInfo: true,
216
}
@@ -219,7 +219,7 @@ func preparePikaErrorOnInfo(t *testing.T) *Collector {
219
220
func preparePikaWithRedisMetrics(t *testing.T) *Collector {
221
collr := New()
222
- require.NoError(t, collr.Init())
222
+ require.NoError(t, collr.Init(context.Background()))
223
collr.pdb = &mockRedisClient{
224
result: dataRedisInfoAll,
225
}
src/go/plugin/go.d/collector/ping/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package ping
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -68,7 +69,7 @@ func (c *Collector) Configuration() any {
69
return c.Config
70
}
71
71
-func (c *Collector) Init() error {
72
+func (c *Collector) Init(context.Context) error {
73
err := c.validateConfig()
74
if err != nil {
75
return fmt.Errorf("config validation: %v", err)
@@ -83,7 +84,7 @@ func (c *Collector) Init() error {
84
return nil
85
}
86
86
-func (c *Collector) Check() error {
87
+func (c *Collector) Check(context.Context) error {
88
mx, err := c.collect()
89
if err != nil {
90
return err
@@ -99,7 +100,7 @@ func (c *Collector) Charts() *module.Charts {
100
return c.charts
101
}
102
102
-func (c *Collector) Collect() map[string]int64 {
103
+func (c *Collector) Collect(context.Context) map[string]int64 {
104
mx, err := c.collect()
105
if err != nil {
106
c.Error(err)
@@ -111,4 +112,4 @@ func (c *Collector) Collect() map[string]int64 {
112
return mx
113
}
114
114
-func (c *Collector) Cleanup() {}
115
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/ping/collector_test.go
+9
-8
@@ -3,6 +3,7 @@
3
package ping
4
5
import (
6
+ "context"
7
"errors"
8
"os"
9
"testing"
@@ -59,9 +60,9 @@ func TestCollector_Init(t *testing.T) {
60
collr.UpdateEvery = 1
61
62
if test.wantFail {
62
- assert.Error(t, collr.Init())
63
+ assert.Error(t, collr.Init(context.Background()))
64
} else {
64
- assert.NoError(t, collr.Init())
65
+ assert.NoError(t, collr.Init(context.Background()))
66
}
67
})
68
}
@@ -72,7 +73,7 @@ func TestCollector_Charts(t *testing.T) {
73
}
74
75
func TestCollector_Cleanup(t *testing.T) {
75
- assert.NotPanics(t, New().Cleanup)
76
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
77
}
78
79
func TestCollector_Check(t *testing.T) {
@@ -95,9 +96,9 @@ func TestCollector_Check(t *testing.T) {
96
collr := test.prepare(t)
97
98
if test.wantFail {
98
- assert.Error(t, collr.Check())
99
+ assert.Error(t, collr.Check(context.Background()))
100
} else {
100
- assert.NoError(t, collr.Check())
101
+ assert.NoError(t, collr.Check(context.Background()))
102
}
103
})
104
}
@@ -147,7 +148,7 @@ func TestCollector_Collect(t *testing.T) {
148
t.Run(name, func(t *testing.T) {
149
collr := test.prepare(t)
150
150
- mx := collr.Collect()
151
+ mx := collr.Collect(context.Background())
152
153
require.Equal(t, test.wantMetrics, mx)
154
@@ -165,7 +166,7 @@ func casePingSuccess(t *testing.T) *Collector {
166
collr.newProber = func(_ pingProberConfig, _ *logger.Logger) prober {
167
return &mockProber{}
168
}
168
- require.NoError(t, collr.Init())
169
+ require.NoError(t, collr.Init(context.Background()))
170
return collr
171
}
172
@@ -176,7 +177,7 @@ func casePingError(t *testing.T) *Collector {
177
collr.newProber = func(_ pingProberConfig, _ *logger.Logger) prober {
178
return &mockProber{errOnPing: true}
179
}
179
- require.NoError(t, collr.Init())
180
+ require.NoError(t, collr.Init(context.Background()))
181
return collr
182
}
183
src/go/plugin/go.d/collector/portcheck/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package portcheck
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -73,7 +74,7 @@ func (c *Collector) Configuration() any {
74
return c.Config
75
}
76
76
-func (c *Collector) Init() error {
77
+func (c *Collector) Init(context.Context) error {
78
if err := c.validateConfig(); err != nil {
79
return fmt.Errorf("config validation: %v", err)
80
}
@@ -87,7 +88,7 @@ func (c *Collector) Init() error {
88
return nil
89
}
90
90
-func (c *Collector) Check() error {
91
+func (c *Collector) Check(context.Context) error {
92
mx, err := c.collect()
93
if err != nil {
94
return err
@@ -104,7 +105,7 @@ func (c *Collector) Charts() *module.Charts {
105
return c.charts
106
}
107
107
-func (c *Collector) Collect() map[string]int64 {
108
+func (c *Collector) Collect(context.Context) map[string]int64 {
109
mx, err := c.collect()
110
if err != nil {
111
c.Error(err)
@@ -117,4 +118,4 @@ func (c *Collector) Collect() map[string]int64 {
118
return mx
119
}
120
120
-func (c *Collector) Cleanup() {}
121
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/portcheck/collector_test.go
+14
-13
@@ -3,6 +3,7 @@
3
package portcheck
4
5
import (
6
+ "context"
7
"errors"
8
"net"
9
"os"
@@ -39,32 +40,32 @@ func TestCollector_Init(t *testing.T) {
40
41
collr.Host = "127.0.0.1"
42
collr.Ports = []int{39001, 39002}
42
- assert.NoError(t, collr.Init())
43
+ assert.NoError(t, collr.Init(context.Background()))
44
assert.Len(t, collr.tcpPorts, 2)
45
}
46
func TestCollector_InitNG(t *testing.T) {
47
collr := New()
48
48
- assert.Error(t, collr.Init())
49
+ assert.Error(t, collr.Init(context.Background()))
50
collr.Host = "127.0.0.1"
50
- assert.Error(t, collr.Init())
51
+ assert.Error(t, collr.Init(context.Background()))
52
collr.Ports = []int{39001, 39002}
52
- assert.NoError(t, collr.Init())
53
+ assert.NoError(t, collr.Init(context.Background()))
54
}
55
56
func TestCollector_Check(t *testing.T) {
56
- assert.Error(t, New().Check())
57
+ assert.Error(t, New().Check(context.Background()))
58
}
59
60
func TestCollector_Cleanup(t *testing.T) {
60
- New().Cleanup()
61
+ New().Cleanup(context.Background())
62
}
63
64
func TestCollector_Charts(t *testing.T) {
65
collr := New()
66
collr.Ports = []int{1, 2}
67
collr.Host = "localhost"
67
- require.NoError(t, collr.Init())
68
+ require.NoError(t, collr.Init(context.Background()))
69
}
70
71
func TestCollector_Collect(t *testing.T) {
@@ -74,8 +75,8 @@ func TestCollector_Collect(t *testing.T) {
75
collr.Ports = []int{39001, 39002}
76
collr.UpdateEvery = 5
77
collr.dialTCP = testDial(nil)
77
- require.NoError(t, collr.Init())
78
- require.NoError(t, collr.Check())
78
+ require.NoError(t, collr.Init(context.Background()))
79
+ require.NoError(t, collr.Check(context.Background()))
80
81
copyLatencyDuration := func(dst, src map[string]int64) {
82
for k := range dst {
@@ -97,7 +98,7 @@ func TestCollector_Collect(t *testing.T) {
98
"tcp_port_39002_success": 1,
99
"tcp_port_39002_timeout": 0,
100
}
100
- mx := collr.Collect()
101
+ mx := collr.Collect(context.Background())
102
copyLatencyDuration(expected, mx)
103
104
assert.Equal(t, expected, mx)
@@ -114,7 +115,7 @@ func TestCollector_Collect(t *testing.T) {
115
"tcp_port_39002_success": 1,
116
"tcp_port_39002_timeout": 0,
117
}
117
- mx = collr.Collect()
118
+ mx = collr.Collect(context.Background())
119
copyLatencyDuration(expected, mx)
120
121
assert.Equal(t, expected, mx)
@@ -133,7 +134,7 @@ func TestCollector_Collect(t *testing.T) {
134
"tcp_port_39002_success": 0,
135
"tcp_port_39002_timeout": 0,
136
}
136
- mx = collr.Collect()
137
+ mx = collr.Collect(context.Background())
138
copyLatencyDuration(expected, mx)
139
140
assert.Equal(t, expected, mx)
@@ -152,7 +153,7 @@ func TestCollector_Collect(t *testing.T) {
153
"tcp_port_39002_timeout": 1,
154
"tcp_port_39002_failed": 0,
155
}
155
- mx = collr.Collect()
156
+ mx = collr.Collect(context.Background())
157
copyLatencyDuration(expected, mx)
158
159
assert.Equal(t, expected, mx)
src/go/plugin/go.d/collector/postfix/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package postfix
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -55,7 +56,7 @@ func (c *Collector) Configuration() any {
56
return c.Config
57
}
58
58
-func (c *Collector) Init() error {
59
+func (c *Collector) Init(context.Context) error {
60
if err := c.validateConfig(); err != nil {
61
return fmt.Errorf("config validation: %s", err)
62
}
@@ -69,7 +70,7 @@ func (c *Collector) Init() error {
70
return nil
71
}
72
72
-func (c *Collector) Check() error {
73
+func (c *Collector) Check(context.Context) error {
74
mx, err := c.collect()
75
if err != nil {
76
return err
@@ -86,7 +87,7 @@ func (c *Collector) Charts() *module.Charts {
87
return c.charts
88
}
89
89
-func (c *Collector) Collect() map[string]int64 {
90
+func (c *Collector) Collect(context.Context) map[string]int64 {
91
mx, err := c.collect()
92
if err != nil {
93
c.Error(err)
@@ -99,4 +100,4 @@ func (c *Collector) Collect() map[string]int64 {
100
return mx
101
}
102
102
-func (c *Collector) Cleanup() {}
103
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/postfix/collector_test.go
+9
-8
@@ -3,6 +3,7 @@
3
package postfix
4
5
import (
6
+ "context"
7
"errors"
8
"os"
9
"testing"
@@ -59,9 +60,9 @@ func TestCollector_Init(t *testing.T) {
60
collr.Config = test.config
61
62
if test.wantFail {
62
- assert.Error(t, collr.Init())
63
+ assert.Error(t, collr.Init(context.Background()))
64
} else {
64
- assert.NoError(t, collr.Init())
65
+ assert.NoError(t, collr.Init(context.Background()))
66
}
67
})
68
}
@@ -80,7 +81,7 @@ func TestCollector_Cleanup(t *testing.T) {
81
prepare: func() *Collector {
82
pf := New()
83
pf.exec = prepareMockOK()
83
- _ = pf.Check()
84
+ _ = pf.Check(context.Background())
85
return pf
86
},
87
},
@@ -88,7 +89,7 @@ func TestCollector_Cleanup(t *testing.T) {
89
prepare: func() *Collector {
90
pf := New()
91
pf.exec = prepareMockOK()
91
- _ = pf.Collect()
92
+ _ = pf.Collect(context.Background())
93
return pf
94
},
95
},
@@ -98,7 +99,7 @@ func TestCollector_Cleanup(t *testing.T) {
99
t.Run(name, func(t *testing.T) {
100
collr := test.prepare()
101
101
- assert.NotPanics(t, collr.Cleanup)
102
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
103
})
104
}
105
}
@@ -141,9 +142,9 @@ func TestCollector_Check(t *testing.T) {
142
collr.exec = mock
143
144
if test.wantFail {
144
- assert.Error(t, collr.Check())
145
+ assert.Error(t, collr.Check(context.Background()))
146
} else {
146
- assert.NoError(t, collr.Check())
147
+ assert.NoError(t, collr.Check(context.Background()))
148
}
149
})
150
}
@@ -188,7 +189,7 @@ func TestCollector_Collect(t *testing.T) {
189
mock := test.prepareMock()
190
collr.exec = mock
191
191
- mx := collr.Collect()
192
+ mx := collr.Collect(context.Background())
193
194
assert.Equal(t, test.wantMetrics, mx)
195
})
src/go/plugin/go.d/collector/postgres/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package postgres
4
5
import (
6
+ "context"
7
"database/sql"
8
_ "embed"
9
"errors"
@@ -103,7 +104,7 @@ func (c *Collector) Configuration() any {
104
return c.Config
105
}
106
106
-func (c *Collector) Init() error {
107
+func (c *Collector) Init(context.Context) error {
108
err := c.validateConfig()
109
if err != nil {
110
return fmt.Errorf("config validation: %v", err)
@@ -121,7 +122,7 @@ func (c *Collector) Init() error {
122
return nil
123
}
124
124
-func (c *Collector) Check() error {
125
+func (c *Collector) Check(context.Context) error {
126
mx, err := c.collect()
127
if err != nil {
128
return err
@@ -136,7 +137,7 @@ func (c *Collector) Charts() *module.Charts {
137
return c.charts
138
}
139
139
-func (c *Collector) Collect() map[string]int64 {
140
+func (c *Collector) Collect(context.Context) map[string]int64 {
141
mx, err := c.collect()
142
if err != nil {
143
c.Error(err)
@@ -148,7 +149,7 @@ func (c *Collector) Collect() map[string]int64 {
149
return mx
150
}
151
151
-func (c *Collector) Cleanup() {
152
+func (c *Collector) Cleanup(context.Context) {
153
if c.db == nil {
154
return
155
}
src/go/plugin/go.d/collector/postgres/collector_test.go
+11
-10
@@ -5,6 +5,7 @@ package postgres
5
import (
6
"bufio"
7
"bytes"
8
+ "context"
9
"database/sql/driver"
10
"errors"
11
"fmt"
@@ -120,9 +121,9 @@ func TestCollector_Init(t *testing.T) {
121
collr.Config = test.config
122
123
if test.wantFail {
123
- assert.Error(t, collr.Init())
124
+ assert.Error(t, collr.Init(context.Background()))
125
} else {
125
- assert.NoError(t, collr.Init())
126
+ assert.NoError(t, collr.Init(context.Background()))
127
}
128
})
129
}
@@ -226,14 +227,14 @@ func TestCollector_Check(t *testing.T) {
227
collr.db = db
228
defer func() { _ = db.Close() }()
229
229
- require.NoError(t, collr.Init())
230
+ require.NoError(t, collr.Init(context.Background()))
231
232
test.prepareMock(t, collr, mock)
233
234
if test.wantFail {
234
- assert.Error(t, collr.Check())
235
+ assert.Error(t, collr.Check(context.Background()))
236
} else {
236
- assert.NoError(t, collr.Check())
237
+ assert.NoError(t, collr.Check(context.Background()))
238
}
239
assert.NoError(t, mock.ExpectationsWereMet())
240
})
@@ -287,7 +288,7 @@ func TestCollector_Collect(t *testing.T) {
288
mockExpect(t, m, queryColumnsStats(), dataVer140004ColumnsStats)
289
},
290
check: func(t *testing.T, collr *Collector) {
290
- mx := collr.Collect()
291
+ mx := collr.Collect(context.Background())
292
293
expected := map[string]int64{
294
"autovacuum_analyze": 0,
@@ -609,7 +610,7 @@ func TestCollector_Collect(t *testing.T) {
610
mockExpectErr(m, queryServerVersion())
611
},
612
check: func(t *testing.T, collr *Collector) {
612
- mx := collr.Collect()
613
+ mx := collr.Collect(context.Background())
614
var expected map[string]int64
615
assert.Equal(t, expected, mx)
616
},
@@ -625,7 +626,7 @@ func TestCollector_Collect(t *testing.T) {
626
mockExpectErr(m, querySettingsMaxConnections())
627
},
628
check: func(t *testing.T, collr *Collector) {
628
- mx := collr.Collect()
629
+ mx := collr.Collect(context.Background())
630
var expected map[string]int64
631
assert.Equal(t, expected, mx)
632
},
@@ -644,7 +645,7 @@ func TestCollector_Collect(t *testing.T) {
645
mockExpectErr(m, queryServerCurrentConnectionsUsed())
646
},
647
check: func(t *testing.T, collr *Collector) {
647
- mx := collr.Collect()
648
+ mx := collr.Collect(context.Background())
649
var expected map[string]int64
650
assert.Equal(t, expected, mx)
651
},
@@ -663,7 +664,7 @@ func TestCollector_Collect(t *testing.T) {
664
collr.db = db
665
defer func() { _ = db.Close() }()
666
666
- require.NoError(t, collr.Init())
667
+ require.NoError(t, collr.Init(context.Background()))
668
669
for i, step := range test {
670
t.Run(fmt.Sprintf("step[%d]", i), func(t *testing.T) {
src/go/plugin/go.d/collector/powerdns/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package powerdns
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -58,7 +59,7 @@ func (c *Collector) Configuration() any {
59
return c.Config
60
}
61
61
-func (c *Collector) Init() error {
62
+func (c *Collector) Init(context.Context) error {
63
err := c.validateConfig()
64
if err != nil {
65
return fmt.Errorf("config validation: %v", err)
@@ -79,7 +80,7 @@ func (c *Collector) Init() error {
80
return nil
81
}
82
82
-func (c *Collector) Check() error {
83
+func (c *Collector) Check(context.Context) error {
84
mx, err := c.collect()
85
if err != nil {
86
return err
@@ -94,7 +95,7 @@ func (c *Collector) Charts() *module.Charts {
95
return c.charts
96
}
97
97
-func (c *Collector) Collect() map[string]int64 {
98
+func (c *Collector) Collect(context.Context) map[string]int64 {
99
ms, err := c.collect()
100
if err != nil {
101
c.Error(err)
@@ -106,7 +107,7 @@ func (c *Collector) Collect() map[string]int64 {
107
return ms
108
}
109
109
-func (c *Collector) Cleanup() {
110
+func (c *Collector) Cleanup(context.Context) {
111
if c.httpClient == nil {
112
return
113
}
src/go/plugin/go.d/collector/powerdns/collector_test.go
+10
-9
@@ -3,6 +3,7 @@
3
package powerdns
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -76,9 +77,9 @@ func TestCollector_Init(t *testing.T) {
77
collr.Config = test.config
78
79
if test.wantFail {
79
- assert.Error(t, collr.Init())
80
+ assert.Error(t, collr.Init(context.Background()))
81
} else {
81
- assert.NoError(t, collr.Init())
82
+ assert.NoError(t, collr.Init(context.Background()))
83
}
84
})
85
}
@@ -114,12 +115,12 @@ func TestCollector_Check(t *testing.T) {
115
t.Run(name, func(t *testing.T) {
116
collr, cleanup := test.prepare()
117
defer cleanup()
117
- require.NoError(t, collr.Init())
118
+ require.NoError(t, collr.Init(context.Background()))
119
120
if test.wantFail {
120
- assert.Error(t, collr.Check())
121
+ assert.Error(t, collr.Check(context.Background()))
122
} else {
122
- assert.NoError(t, collr.Check())
123
+ assert.NoError(t, collr.Check(context.Background()))
124
}
125
})
126
}
@@ -127,12 +128,12 @@ func TestCollector_Check(t *testing.T) {
128
129
func TestCollector_Charts(t *testing.T) {
130
collr := New()
130
- require.NoError(t, collr.Init())
131
+ require.NoError(t, collr.Init(context.Background()))
132
assert.NotNil(t, collr.Charts())
133
}
134
135
func TestCollector_Cleanup(t *testing.T) {
135
- assert.NotPanics(t, New().Cleanup)
136
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
137
}
138
139
func TestCollector_Collect(t *testing.T) {
@@ -242,9 +243,9 @@ func TestCollector_Collect(t *testing.T) {
243
t.Run(name, func(t *testing.T) {
244
collr, cleanup := test.prepare()
245
defer cleanup()
245
- require.NoError(t, collr.Init())
246
+ require.NoError(t, collr.Init(context.Background()))
247
247
- mx := collr.Collect()
248
+ mx := collr.Collect(context.Background())
249
250
assert.Equal(t, test.wantCollected, mx)
251
if len(test.wantCollected) > 0 {
src/go/plugin/go.d/collector/powerdns_recursor/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package powerdns_recursor
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -58,7 +59,7 @@ func (c *Collector) Configuration() any {
59
return c.Config
60
}
61
61
-func (c *Collector) Init() error {
62
+func (c *Collector) Init(context.Context) error {
63
err := c.validateConfig()
64
if err != nil {
65
return fmt.Errorf("config validation: %v", err)
@@ -79,7 +80,7 @@ func (c *Collector) Init() error {
80
return nil
81
}
82
82
-func (c *Collector) Check() error {
83
+func (c *Collector) Check(context.Context) error {
84
mx, err := c.collect()
85
if err != nil {
86
return err
@@ -94,7 +95,7 @@ func (c *Collector) Charts() *module.Charts {
95
return c.charts
96
}
97
97
-func (c *Collector) Collect() map[string]int64 {
98
+func (c *Collector) Collect(context.Context) map[string]int64 {
99
ms, err := c.collect()
100
if err != nil {
101
c.Error(err)
@@ -106,7 +107,7 @@ func (c *Collector) Collect() map[string]int64 {
107
return ms
108
}
109
109
-func (c *Collector) Cleanup() {
110
+func (c *Collector) Cleanup(context.Context) {
111
if c.httpClient == nil {
112
return
113
}
src/go/plugin/go.d/collector/powerdns_recursor/collector_test.go
+10
-9
@@ -3,6 +3,7 @@
3
package powerdns_recursor
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -76,9 +77,9 @@ func TestCollector_Init(t *testing.T) {
77
collr.Config = test.config
78
79
if test.wantFail {
79
- assert.Error(t, collr.Init())
80
+ assert.Error(t, collr.Init(context.Background()))
81
} else {
81
- assert.NoError(t, collr.Init())
82
+ assert.NoError(t, collr.Init(context.Background()))
83
}
84
})
85
}
@@ -114,12 +115,12 @@ func TestCollector_Check(t *testing.T) {
115
t.Run(name, func(t *testing.T) {
116
collr, cleanup := test.prepare()
117
defer cleanup()
117
- require.NoError(t, collr.Init())
118
+ require.NoError(t, collr.Init(context.Background()))
119
120
if test.wantFail {
120
- assert.Error(t, collr.Check())
121
+ assert.Error(t, collr.Check(context.Background()))
122
} else {
122
- assert.NoError(t, collr.Check())
123
+ assert.NoError(t, collr.Check(context.Background()))
124
}
125
})
126
}
@@ -127,12 +128,12 @@ func TestCollector_Check(t *testing.T) {
128
129
func TestCollector_Charts(t *testing.T) {
130
collr := New()
130
- require.NoError(t, collr.Init())
131
+ require.NoError(t, collr.Init(context.Background()))
132
assert.NotNil(t, collr.Charts())
133
}
134
135
func TestCollector_Cleanup(t *testing.T) {
135
- assert.NotPanics(t, New().Cleanup)
136
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
137
}
138
139
func TestCollector_Collect(t *testing.T) {
@@ -277,9 +278,9 @@ func TestCollector_Collect(t *testing.T) {
278
t.Run(name, func(t *testing.T) {
279
collr, cleanup := test.prepare()
280
defer cleanup()
280
- require.NoError(t, collr.Init())
281
+ require.NoError(t, collr.Init(context.Background()))
282
282
- mx := collr.Collect()
283
+ mx := collr.Collect(context.Background())
284
285
assert.Equal(t, test.wantCollected, mx)
286
if len(test.wantCollected) > 0 {
src/go/plugin/go.d/collector/prometheus/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package prometheus
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -82,7 +83,7 @@ func (c *Collector) Configuration() any {
83
return c.Config
84
}
85
85
-func (c *Collector) Init() error {
86
+func (c *Collector) Init(context.Context) error {
87
if err := c.validateConfig(); err != nil {
88
return fmt.Errorf("validating config: %v", err)
89
}
@@ -108,7 +109,7 @@ func (c *Collector) Init() error {
109
return nil
110
}
111
111
-func (c *Collector) Check() error {
112
+func (c *Collector) Check(context.Context) error {
113
mx, err := c.collect()
114
if err != nil {
115
return err
@@ -123,7 +124,7 @@ func (c *Collector) Charts() *module.Charts {
124
return c.charts
125
}
126
126
-func (c *Collector) Collect() map[string]int64 {
127
+func (c *Collector) Collect(context.Context) map[string]int64 {
128
mx, err := c.collect()
129
if err != nil {
130
c.Error(err)
@@ -135,7 +136,7 @@ func (c *Collector) Collect() map[string]int64 {
136
return mx
137
}
138
138
-func (c *Collector) Cleanup() {
139
+func (c *Collector) Cleanup(context.Context) {
140
if c.prom != nil && c.prom.HTTPClient() != nil {
141
c.prom.HTTPClient().CloseIdleConnections()
142
}
src/go/plugin/go.d/collector/prometheus/collector_test.go
+11
-10
@@ -3,6 +3,7 @@
3
package prometheus
4
5
import (
6
+ "context"
7
"fmt"
8
"net/http"
9
"net/http/httptest"
@@ -63,21 +64,21 @@ func TestCollector_Init(t *testing.T) {
64
collr.Config = test.config
65
66
if test.wantFail {
66
- assert.Error(t, collr.Init())
67
+ assert.Error(t, collr.Init(context.Background()))
68
} else {
68
- assert.NoError(t, collr.Init())
69
+ assert.NoError(t, collr.Init(context.Background()))
70
}
71
})
72
}
73
}
74
75
func TestCollector_Cleanup(t *testing.T) {
75
- assert.NotPanics(t, New().Cleanup)
76
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
77
78
collr := New()
79
collr.URL = "http://127.0.0.1"
79
- require.NoError(t, collr.Init())
80
- assert.NotPanics(t, collr.Cleanup)
80
+ require.NoError(t, collr.Init(context.Background()))
81
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
82
}
83
84
func TestCollector_Check(t *testing.T) {
@@ -188,12 +189,12 @@ test_counter_no_meta_metric_1_total{label1="value2"} 11
189
collr, cleanup := test.prepare()
190
defer cleanup()
191
191
- require.NoError(t, collr.Init())
192
+ require.NoError(t, collr.Init(context.Background()))
193
194
if test.wantFail {
194
- assert.Error(t, collr.Check())
195
+ assert.Error(t, collr.Check(context.Background()))
196
} else {
196
- assert.NoError(t, collr.Check())
197
+ assert.NoError(t, collr.Check(context.Background()))
198
}
199
})
200
}
@@ -577,7 +578,7 @@ test_gauge_no_meta_metric_1{label1="value2"} 12
578
defer srv.Close()
579
580
collr.URL = srv.URL
580
- require.NoError(t, collr.Init())
581
+ require.NoError(t, collr.Init(context.Background()))
582
583
for num, step := range test.steps {
584
t.Run(fmt.Sprintf("step num %d ('%s')", num+1, step.desc), func(t *testing.T) {
@@ -587,7 +588,7 @@ test_gauge_no_meta_metric_1{label1="value2"} 12
588
var mx map[string]int64
589
590
for i := 0; i < maxNotSeenTimes+1; i++ {
590
- mx = collr.Collect()
591
+ mx = collr.Collect(context.Background())
592
}
593
594
assert.Equal(t, step.wantCollected, mx)
src/go/plugin/go.d/collector/proxysql/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package proxysql
4
5
import (
6
+ "context"
7
"database/sql"
8
_ "embed"
9
"errors"
@@ -65,7 +66,7 @@ func (c *Collector) Configuration() any {
66
return c.Config
67
}
68
68
-func (c *Collector) Init() error {
69
+func (c *Collector) Init(context.Context) error {
70
if c.DSN == "" {
71
return errors.New("dsn not set")
72
}
@@ -75,7 +76,7 @@ func (c *Collector) Init() error {
76
return nil
77
}
78
78
-func (c *Collector) Check() error {
79
+func (c *Collector) Check(context.Context) error {
80
mx, err := c.collect()
81
if err != nil {
82
return err
@@ -90,7 +91,7 @@ func (c *Collector) Charts() *module.Charts {
91
return c.charts
92
}
93
93
-func (c *Collector) Collect() map[string]int64 {
94
+func (c *Collector) Collect(context.Context) map[string]int64 {
95
mx, err := c.collect()
96
if err != nil {
97
c.Error(err)
@@ -102,7 +103,7 @@ func (c *Collector) Collect() map[string]int64 {
103
return mx
104
}
105
105
-func (c *Collector) Cleanup() {
106
+func (c *Collector) Cleanup(context.Context) {
107
if c.db == nil {
108
return
109
}
src/go/plugin/go.d/collector/proxysql/collector_test.go
+9
-8
@@ -5,6 +5,7 @@ package proxysql
5
import (
6
"bufio"
7
"bytes"
8
+ "context"
9
"database/sql/driver"
10
"errors"
11
"fmt"
@@ -73,9 +74,9 @@ func TestCollector_Init(t *testing.T) {
74
collr.Config = test.config
75
76
if test.wantFail {
76
- assert.Error(t, collr.Init())
77
+ assert.Error(t, collr.Init(context.Background()))
78
} else {
78
- assert.NoError(t, collr.Init())
79
+ assert.NoError(t, collr.Init(context.Background()))
80
}
81
})
82
}
@@ -104,7 +105,7 @@ func TestCollector_Cleanup(t *testing.T) {
105
collr, cleanup := prepare(t)
106
defer cleanup()
107
107
- assert.NotPanics(t, collr.Cleanup)
108
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
109
assert.Nil(t, collr.db)
110
})
111
}
@@ -176,14 +177,14 @@ func TestCollector_Check(t *testing.T) {
177
collr.db = db
178
defer func() { _ = db.Close() }()
179
179
- require.NoError(t, collr.Init())
180
+ require.NoError(t, collr.Init(context.Background()))
181
182
test.prepareMock(t, mock)
183
184
if test.wantFail {
184
- assert.Error(t, collr.Check())
185
+ assert.Error(t, collr.Check(context.Background()))
186
} else {
186
- assert.NoError(t, collr.Check())
187
+ assert.NoError(t, collr.Check(context.Background()))
188
}
189
assert.NoError(t, mock.ExpectationsWereMet())
190
})
@@ -208,7 +209,7 @@ func TestCollector_Collect(t *testing.T) {
209
mockExpect(t, m, queryStatsMySQLConnectionPool, dataVer2010StatsMySQLConnectionPool)
210
},
211
check: func(t *testing.T, my *Collector) {
211
- mx := my.Collect()
212
+ mx := my.Collect(context.Background())
213
214
expected := map[string]int64{
215
"Access_Denied_Max_Connections": 0,
@@ -1163,7 +1164,7 @@ func TestCollector_Collect(t *testing.T) {
1164
my.db = db
1165
defer func() { _ = db.Close() }()
1166
1166
- require.NoError(t, my.Init())
1167
+ require.NoError(t, my.Init(context.Background()))
1168
1169
for i, step := range test {
1170
t.Run(fmt.Sprintf("step[%d]", i), func(t *testing.T) {
src/go/plugin/go.d/collector/pulsar/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package pulsar
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -81,7 +82,7 @@ func (c *Collector) Configuration() any {
82
return c.Config
83
}
84
84
-func (c *Collector) Init() error {
85
+func (c *Collector) Init(context.Context) error {
86
if err := c.validateConfig(); err != nil {
87
return fmt.Errorf("config validation: %v", err)
88
}
@@ -101,7 +102,7 @@ func (c *Collector) Init() error {
102
return nil
103
}
104
104
-func (c *Collector) Check() error {
105
+func (c *Collector) Check(context.Context) error {
106
mx, err := c.collect()
107
if err != nil {
108
return err
@@ -116,7 +117,7 @@ func (c *Collector) Charts() *Charts {
117
return c.charts
118
}
119
119
-func (c *Collector) Collect() map[string]int64 {
120
+func (c *Collector) Collect(context.Context) map[string]int64 {
121
mx, err := c.collect()
122
if err != nil {
123
c.Error(err)
@@ -128,7 +129,7 @@ func (c *Collector) Collect() map[string]int64 {
129
return mx
130
}
131
131
-func (c *Collector) Cleanup() {
132
+func (c *Collector) Cleanup(context.Context) {
133
if c.prom != nil && c.prom.HTTPClient() != nil {
134
c.prom.HTTPClient().CloseIdleConnections()
135
}
src/go/plugin/go.d/collector/pulsar/collector_test.go
+18
-17
@@ -3,6 +3,7 @@
3
package pulsar
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -80,16 +81,16 @@ func TestCollector_Init(t *testing.T) {
81
collr.Config = test.config
82
83
if test.wantFail {
83
- assert.Error(t, collr.Init())
84
+ assert.Error(t, collr.Init(context.Background()))
85
} else {
85
- assert.NoError(t, collr.Init())
86
+ assert.NoError(t, collr.Init(context.Background()))
87
}
88
})
89
}
90
}
91
92
func TestCollector_Cleanup(t *testing.T) {
92
- assert.NotPanics(t, New().Cleanup)
93
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
94
}
95
96
func TestCollector_Check(t *testing.T) {
@@ -111,9 +112,9 @@ func TestCollector_Check(t *testing.T) {
112
defer srv.Close()
113
114
if test.wantFail {
114
- assert.Error(t, collr.Check())
115
+ assert.Error(t, collr.Check(context.Background()))
116
} else {
116
- assert.NoError(t, collr.Check())
117
+ assert.NoError(t, collr.Check(context.Background()))
118
}
119
})
120
}
@@ -139,7 +140,7 @@ func TestCollector_Collect_ReturnsNilOnErrors(t *testing.T) {
140
collr, srv := test.prepare(t)
141
defer srv.Close()
142
142
- assert.Nil(t, collr.Collect())
143
+ assert.Nil(t, collr.Collect(context.Background()))
144
})
145
}
146
}
@@ -169,9 +170,9 @@ func TestCollector_Collect(t *testing.T) {
170
defer srv.Close()
171
172
for i := 0; i < 10; i++ {
172
- _ = collr.Collect()
173
+ _ = collr.Collect(context.Background())
174
}
174
- mx := collr.Collect()
175
+ mx := collr.Collect(context.Background())
176
177
require.NotNil(t, mx)
178
require.Equal(t, test.expected, mx)
@@ -186,7 +187,7 @@ func TestCollector_Collect_RemoveAddNamespacesTopicsInRuntime(t *testing.T) {
187
188
oldNsCharts := Charts{}
189
189
- require.NotNil(t, collr.Collect())
190
+ require.NotNil(t, collr.Collect(context.Background()))
191
oldLength := len(*collr.Charts())
192
193
for _, chart := range *collr.Charts() {
@@ -197,7 +198,7 @@ func TestCollector_Collect_RemoveAddNamespacesTopicsInRuntime(t *testing.T) {
198
}
199
}
200
200
- require.NotNil(t, collr.Collect())
201
+ require.NotNil(t, collr.Collect(context.Background()))
202
203
l := oldLength + len(*collr.nsCharts)*2 // 2 new namespaces
204
assert.Truef(t, len(*collr.Charts()) == l, "expected %d charts, but got %d", l, len(*collr.Charts()))
@@ -221,7 +222,7 @@ func prepareClientServerStdV250Namespaces(t *testing.T) (*Collector, *httptest.S
222
223
collr := New()
224
collr.URL = srv.URL
224
- require.NoError(t, collr.Init())
225
+ require.NoError(t, collr.Init(context.Background()))
226
227
return collr, srv
228
}
@@ -235,7 +236,7 @@ func prepareClientServerStdV250Topics(t *testing.T) (*Collector, *httptest.Serve
236
237
collr := New()
238
collr.URL = srv.URL
238
- require.NoError(t, collr.Init())
239
+ require.NoError(t, collr.Init(context.Background()))
240
241
return collr, srv
242
}
@@ -263,7 +264,7 @@ func prepareClientServersDynamicStdV250Topics(t *testing.T) (*Collector, *httpte
264
265
collr := New()
266
collr.URL = srv.URL
266
- require.NoError(t, collr.Init())
267
+ require.NoError(t, collr.Init(context.Background()))
268
269
return collr, srv
270
}
@@ -277,7 +278,7 @@ func prepareClientServerNonPulsar(t *testing.T) (*Collector, *httptest.Server) {
278
279
collr := New()
280
collr.URL = srv.URL
280
- require.NoError(t, collr.Init())
281
+ require.NoError(t, collr.Init(context.Background()))
282
283
return collr, srv
284
}
@@ -291,7 +292,7 @@ func prepareClientServerInvalidData(t *testing.T) (*Collector, *httptest.Server)
292
293
collr := New()
294
collr.URL = srv.URL
294
- require.NoError(t, collr.Init())
295
+ require.NoError(t, collr.Init(context.Background()))
296
297
return collr, srv
298
}
@@ -305,7 +306,7 @@ func prepareClientServer404(t *testing.T) (*Collector, *httptest.Server) {
306
307
collr := New()
308
collr.URL = srv.URL
308
- require.NoError(t, collr.Init())
309
+ require.NoError(t, collr.Init(context.Background()))
310
311
return collr, srv
312
}
@@ -316,7 +317,7 @@ func prepareClientServerConnectionRefused(t *testing.T) (*Collector, *httptest.S
317
318
collr := New()
319
collr.URL = "http://127.0.0.1:38001/metrics"
319
- require.NoError(t, collr.Init())
320
+ require.NoError(t, collr.Init(context.Background()))
321
322
return collr, srv
323
}
src/go/plugin/go.d/collector/puppet/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package puppet
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -59,7 +60,7 @@ func (c *Collector) Configuration() any {
60
return c.Config
61
}
62
62
-func (c *Collector) Init() error {
63
+func (c *Collector) Init(context.Context) error {
64
if c.URL == "" {
65
return errors.New("url not set")
66
}
@@ -76,7 +77,7 @@ func (c *Collector) Init() error {
77
return nil
78
}
79
79
-func (c *Collector) Check() error {
80
+func (c *Collector) Check(context.Context) error {
81
mx, err := c.collect()
82
if err != nil {
83
return err
@@ -93,7 +94,7 @@ func (c *Collector) Charts() *module.Charts {
94
return c.charts
95
}
96
96
-func (c *Collector) Collect() map[string]int64 {
97
+func (c *Collector) Collect(context.Context) map[string]int64 {
98
mx, err := c.collect()
99
if err != nil {
100
c.Error(err)
@@ -106,7 +107,7 @@ func (c *Collector) Collect() map[string]int64 {
107
return mx
108
}
109
109
-func (c *Collector) Cleanup() {
110
+func (c *Collector) Cleanup(context.Context) {
111
if c.httpClient != nil {
112
c.httpClient.CloseIdleConnections()
113
}
src/go/plugin/go.d/collector/puppet/collector_test.go
+10
-9
@@ -3,6 +3,7 @@
3
package puppet
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -61,9 +62,9 @@ func TestCollector_Init(t *testing.T) {
62
collr.Config = test.config
63
64
if test.wantFail {
64
- assert.Error(t, collr.Init())
65
+ assert.Error(t, collr.Init(context.Background()))
66
} else {
66
- assert.NoError(t, collr.Init())
67
+ assert.NoError(t, collr.Init(context.Background()))
68
}
69
})
70
}
@@ -102,9 +103,9 @@ func TestCollector_Check(t *testing.T) {
103
defer cleanup()
104
105
if test.wantFail {
105
- assert.Error(t, collr.Check())
106
+ assert.Error(t, collr.Check(context.Background()))
107
} else {
107
- assert.NoError(t, collr.Check())
108
+ assert.NoError(t, collr.Check(context.Background()))
109
}
110
})
111
}
@@ -148,7 +149,7 @@ func TestCollector_Collect(t *testing.T) {
149
collr, cleanup := test.prepare(t)
150
defer cleanup()
151
151
- mx := collr.Collect()
152
+ mx := collr.Collect(context.Background())
153
154
require.Equal(t, test.wantMetrics, mx)
155
@@ -177,7 +178,7 @@ func prepareCaseOkDefault(t *testing.T) (*Collector, func()) {
178
179
collr := New()
180
collr.URL = srv.URL
180
- require.NoError(t, collr.Init())
181
+ require.NoError(t, collr.Init(context.Background()))
182
183
return collr, srv.Close
184
}
@@ -208,7 +209,7 @@ func prepareCaseUnexpectedJsonResponse(t *testing.T) (*Collector, func()) {
209
210
collr := New()
211
collr.URL = srv.URL
211
- require.NoError(t, collr.Init())
212
+ require.NoError(t, collr.Init(context.Background()))
213
214
return collr, srv.Close
215
}
@@ -222,7 +223,7 @@ func prepareCaseInvalidFormatResponse(t *testing.T) (*Collector, func()) {
223
224
collr := New()
225
collr.URL = srv.URL
225
- require.NoError(t, collr.Init())
226
+ require.NoError(t, collr.Init(context.Background()))
227
228
return collr, srv.Close
229
}
@@ -231,7 +232,7 @@ func prepareCaseConnectionRefused(t *testing.T) (*Collector, func()) {
232
t.Helper()
233
collr := New()
234
collr.URL = "http://127.0.0.1:65001"
234
- require.NoError(t, collr.Init())
235
+ require.NoError(t, collr.Init(context.Background()))
236
237
return collr, func() {}
238
}
src/go/plugin/go.d/collector/rabbitmq/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package rabbitmq
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -69,7 +70,7 @@ func (c *Collector) Configuration() any {
70
return c.Config
71
}
72
72
-func (c *Collector) Init() error {
73
+func (c *Collector) Init(context.Context) error {
74
if c.URL == "" {
75
return errors.New("config: url not set")
76
}
@@ -86,7 +87,7 @@ func (c *Collector) Init() error {
87
return nil
88
}
89
89
-func (c *Collector) Check() error {
90
+func (c *Collector) Check(context.Context) error {
91
mx, err := c.collect()
92
if err != nil {
93
return err
@@ -101,7 +102,7 @@ func (c *Collector) Charts() *module.Charts {
102
return c.charts
103
}
104
104
-func (c *Collector) Collect() map[string]int64 {
105
+func (c *Collector) Collect(context.Context) map[string]int64 {
106
mx, err := c.collect()
107
if err != nil {
108
c.Error(err)
@@ -114,7 +115,7 @@ func (c *Collector) Collect() map[string]int64 {
115
return mx
116
}
117
117
-func (c *Collector) Cleanup() {
118
+func (c *Collector) Cleanup(context.Context) {
119
if c.httpClient != nil {
120
c.httpClient.CloseIdleConnections()
121
}
src/go/plugin/go.d/collector/rabbitmq/collector_test.go
+11
-10
@@ -3,6 +3,7 @@
3
package rabbitmq
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -69,9 +70,9 @@ func TestCollector_Init(t *testing.T) {
70
collr.Config = test.config
71
72
if test.wantFail {
72
- assert.Error(t, collr.Init())
73
+ assert.Error(t, collr.Init(context.Background()))
74
} else {
74
- assert.NoError(t, collr.Init())
75
+ assert.NoError(t, collr.Init(context.Background()))
76
}
77
})
78
}
@@ -82,12 +83,12 @@ func TestCollector_Charts(t *testing.T) {
83
}
84
85
func TestCollector_Cleanup(t *testing.T) {
85
- assert.NotPanics(t, New().Cleanup)
86
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
87
88
collr := New()
88
- require.NoError(t, collr.Init())
89
+ require.NoError(t, collr.Init(context.Background()))
90
90
- assert.NotPanics(t, collr.Cleanup)
91
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
92
}
93
94
func TestCollector_Check(t *testing.T) {
@@ -105,12 +106,12 @@ func TestCollector_Check(t *testing.T) {
106
collr, cleanup := test.prepare()
107
defer cleanup()
108
108
- require.NoError(t, collr.Init())
109
+ require.NoError(t, collr.Init(context.Background()))
110
111
if test.wantFail {
111
- assert.Error(t, collr.Check())
112
+ assert.Error(t, collr.Check(context.Background()))
113
} else {
113
- assert.NoError(t, collr.Check())
114
+ assert.NoError(t, collr.Check(context.Background()))
115
}
116
})
117
}
@@ -359,9 +360,9 @@ func TestCollector_Collect(t *testing.T) {
360
collr, cleanup := test.prepare()
361
defer cleanup()
362
362
- require.NoError(t, collr.Init())
363
+ require.NoError(t, collr.Init(context.Background()))
364
364
- mx := collr.Collect()
365
+ mx := collr.Collect(context.Background())
366
367
assert.Equal(t, test.wantCollected, mx)
368
src/go/plugin/go.d/collector/redis/collector.go
+4
-4
@@ -84,7 +84,7 @@ func (c *Collector) Configuration() any {
84
return c.Config
85
}
86
87
-func (c *Collector) Init() error {
87
+func (c *Collector) Init(context.Context) error {
88
err := c.validateConfig()
89
if err != nil {
90
return fmt.Errorf("config validation: %v", err)
@@ -105,7 +105,7 @@ func (c *Collector) Init() error {
105
return nil
106
}
107
108
-func (c *Collector) Check() error {
108
+func (c *Collector) Check(context.Context) error {
109
mx, err := c.collect()
110
if err != nil {
111
return err
@@ -120,7 +120,7 @@ func (c *Collector) Charts() *module.Charts {
120
return c.charts
121
}
122
123
-func (c *Collector) Collect() map[string]int64 {
123
+func (c *Collector) Collect(context.Context) map[string]int64 {
124
ms, err := c.collect()
125
if err != nil {
126
c.Error(err)
@@ -132,7 +132,7 @@ func (c *Collector) Collect() map[string]int64 {
132
return ms
133
}
134
135
-func (c *Collector) Cleanup() {
135
+func (c *Collector) Cleanup(context.Context) {
136
if c.rdb == nil {
137
return
138
}
src/go/plugin/go.d/collector/redis/collector_test.go
+12
-12
@@ -71,9 +71,9 @@ func TestCollector_Init(t *testing.T) {
71
collr.Config = test.config
72
73
if test.wantFail {
74
- assert.Error(t, collr.Init())
74
+ assert.Error(t, collr.Init(context.Background()))
75
} else {
76
- assert.NoError(t, collr.Init())
76
+ assert.NoError(t, collr.Init(context.Background()))
77
}
78
})
79
}
@@ -102,9 +102,9 @@ func TestCollector_Check(t *testing.T) {
102
collr := test.prepare(t)
103
104
if test.wantFail {
105
- assert.Error(t, collr.Check())
105
+ assert.Error(t, collr.Check(context.Background()))
106
} else {
107
- assert.NoError(t, collr.Check())
107
+ assert.NoError(t, collr.Check(context.Background()))
108
}
109
})
110
}
@@ -112,20 +112,20 @@ func TestCollector_Check(t *testing.T) {
112
113
func TestCollector_Charts(t *testing.T) {
114
collr := New()
115
- require.NoError(t, collr.Init())
115
+ require.NoError(t, collr.Init(context.Background()))
116
117
assert.NotNil(t, collr.Charts())
118
}
119
120
func TestCollector_Cleanup(t *testing.T) {
121
collr := New()
122
- assert.NotPanics(t, collr.Cleanup)
122
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
123
124
- require.NoError(t, collr.Init())
124
+ require.NoError(t, collr.Init(context.Background()))
125
m := &mockRedisClient{}
126
collr.rdb = m
127
128
- collr.Cleanup()
128
+ collr.Cleanup(context.Background())
129
130
assert.True(t, m.calledClose)
131
}
@@ -298,7 +298,7 @@ func TestCollector_Collect(t *testing.T) {
298
t.Run(name, func(t *testing.T) {
299
collr := test.prepare(t)
300
301
- mx := collr.Collect()
301
+ mx := collr.Collect(context.Background())
302
303
copyTimeRelatedMetrics(mx, test.wantCollected)
304
@@ -314,7 +314,7 @@ func TestCollector_Collect(t *testing.T) {
314
315
func prepareRedisV609(t *testing.T) *Collector {
316
collr := New()
317
- require.NoError(t, collr.Init())
317
+ require.NoError(t, collr.Init(context.Background()))
318
collr.rdb = &mockRedisClient{
319
result: dataVer609InfoAll,
320
}
@@ -323,7 +323,7 @@ func prepareRedisV609(t *testing.T) *Collector {
323
324
func prepareRedisErrorOnInfo(t *testing.T) *Collector {
325
collr := New()
326
- require.NoError(t, collr.Init())
326
+ require.NoError(t, collr.Init(context.Background()))
327
collr.rdb = &mockRedisClient{
328
errOnInfo: true,
329
}
@@ -332,7 +332,7 @@ func prepareRedisErrorOnInfo(t *testing.T) *Collector {
332
333
func prepareRedisWithPikaMetrics(t *testing.T) *Collector {
334
collr := New()
335
- require.NoError(t, collr.Init())
335
+ require.NoError(t, collr.Init(context.Background()))
336
collr.rdb = &mockRedisClient{
337
result: dataPikaInfoAll,
338
}
src/go/plugin/go.d/collector/rethinkdb/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package rethinkdb
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"time"
@@ -59,14 +60,14 @@ func (c *Collector) Configuration() any {
60
return c.Config
61
}
62
62
-func (c *Collector) Init() error {
63
+func (c *Collector) Init(context.Context) error {
64
if c.Address == "" {
65
return errors.New("config: address is not set")
66
}
67
return nil
68
}
69
69
-func (c *Collector) Check() error {
70
+func (c *Collector) Check(context.Context) error {
71
mx, err := c.collect()
72
if err != nil {
73
return err
@@ -81,7 +82,7 @@ func (c *Collector) Charts() *module.Charts {
82
return c.charts
83
}
84
84
-func (c *Collector) Collect() map[string]int64 {
85
+func (c *Collector) Collect(context.Context) map[string]int64 {
86
ms, err := c.collect()
87
if err != nil {
88
c.Error(err)
@@ -93,7 +94,7 @@ func (c *Collector) Collect() map[string]int64 {
94
return ms
95
}
96
96
-func (c *Collector) Cleanup() {
97
+func (c *Collector) Cleanup(context.Context) {
98
if c.rdb != nil {
99
if err := c.rdb.close(); err != nil {
100
c.Warningf("cleanup: error on closing client [%s]: %v", c.Address, err)
src/go/plugin/go.d/collector/rethinkdb/collector_test.go
+12
-11
@@ -4,6 +4,7 @@ package rethinkdb
4
5
import (
6
"bytes"
7
+ "context"
8
"errors"
9
"fmt"
10
"os"
@@ -63,9 +64,9 @@ func TestCollector_Init(t *testing.T) {
64
collr.Config = test.config
65
66
if test.wantFail {
66
- assert.Error(t, collr.Init())
67
+ assert.Error(t, collr.Init(context.Background()))
68
} else {
68
- assert.NoError(t, collr.Init())
69
+ assert.NoError(t, collr.Init(context.Background()))
70
}
71
})
72
}
@@ -86,7 +87,7 @@ func TestCollector_Cleanup(t *testing.T) {
87
collr.newConn = func(config Config) (rdbConn, error) {
88
return &mockRethinkdbConn{dataStats: dataStats}, nil
89
}
89
- _ = collr.Check()
90
+ _ = collr.Check(context.Background())
91
return collr
92
},
93
},
@@ -96,7 +97,7 @@ func TestCollector_Cleanup(t *testing.T) {
97
collr.newConn = func(config Config) (rdbConn, error) {
98
return &mockRethinkdbConn{dataStats: dataStats}, nil
99
}
99
- _ = collr.Check()
100
+ _ = collr.Check(context.Background())
101
return collr
102
},
103
},
@@ -106,7 +107,7 @@ func TestCollector_Cleanup(t *testing.T) {
107
t.Run(name, func(t *testing.T) {
108
collr := test.prepare()
109
109
- assert.NotPanics(t, collr.Cleanup)
110
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
111
})
112
}
113
}
@@ -135,14 +136,14 @@ func TestCollector_Check(t *testing.T) {
136
collr := test.prepare()
137
138
if test.wantFail {
138
- assert.Error(t, collr.Check())
139
+ assert.Error(t, collr.Check(context.Background()))
140
} else {
140
- assert.NoError(t, collr.Check())
141
+ assert.NoError(t, collr.Check(context.Background()))
142
}
143
144
if m, ok := collr.rdb.(*mockRethinkdbConn); ok {
145
assert.False(t, m.disconnectCalled, "rdb close before cleanup")
145
- collr.Cleanup()
146
+ collr.Cleanup(context.Background())
147
assert.True(t, m.disconnectCalled, "rdb close after cleanup")
148
}
149
})
@@ -203,9 +204,9 @@ func TestCollector_Collect(t *testing.T) {
204
t.Run(name, func(t *testing.T) {
205
collr := test.prepare()
206
206
- require.NoError(t, collr.Init())
207
+ require.NoError(t, collr.Init(context.Background()))
208
208
- mx := collr.Collect()
209
+ mx := collr.Collect(context.Background())
210
211
require.Equal(t, test.wantMetrics, mx)
212
@@ -217,7 +218,7 @@ func TestCollector_Collect(t *testing.T) {
218
219
if m, ok := collr.rdb.(*mockRethinkdbConn); ok {
220
assert.False(t, m.disconnectCalled, "rdb close before cleanup")
220
- collr.Cleanup()
221
+ collr.Cleanup(context.Background())
222
assert.True(t, m.disconnectCalled, "rdb close after cleanup")
223
}
224
})
src/go/plugin/go.d/collector/riakkv/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package riakkv
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -69,7 +70,7 @@ func (c *Collector) Configuration() any {
70
return c.Config
71
}
72
72
-func (c *Collector) Init() error {
73
+func (c *Collector) Init(context.Context) error {
74
if c.URL == "" {
75
return errors.New("config: url not set")
76
}
@@ -86,7 +87,7 @@ func (c *Collector) Init() error {
87
return nil
88
}
89
89
-func (c *Collector) Check() error {
90
+func (c *Collector) Check(context.Context) error {
91
mx, err := c.collect()
92
if err != nil {
93
return err
@@ -102,7 +103,7 @@ func (c *Collector) Charts() *module.Charts {
103
return c.charts
104
}
105
105
-func (c *Collector) Collect() map[string]int64 {
106
+func (c *Collector) Collect(context.Context) map[string]int64 {
107
mx, err := c.collect()
108
if err != nil {
109
c.Error(err)
@@ -114,7 +115,7 @@ func (c *Collector) Collect() map[string]int64 {
115
return mx
116
}
117
117
-func (c *Collector) Cleanup() {
118
+func (c *Collector) Cleanup(context.Context) {
119
if c.httpClient != nil {
120
c.httpClient.CloseIdleConnections()
121
}
src/go/plugin/go.d/collector/riakkv/collector_test.go
+11
-10
@@ -3,6 +3,7 @@
3
package riakkv
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -62,9 +63,9 @@ func TestCollector_Init(t *testing.T) {
63
collr.Config = test.config
64
65
if test.wantFail {
65
- assert.Error(t, collr.Init())
66
+ assert.Error(t, collr.Init(context.Background()))
67
} else {
67
- assert.NoError(t, collr.Init())
68
+ assert.NoError(t, collr.Init(context.Background()))
69
}
70
})
71
}
@@ -99,9 +100,9 @@ func TestCollector_Check(t *testing.T) {
100
defer cleanup()
101
102
if test.wantFail {
102
- assert.Error(t, collr.Check())
103
+ assert.Error(t, collr.Check(context.Background()))
104
} else {
104
- assert.NoError(t, collr.Check())
105
+ assert.NoError(t, collr.Check(context.Background()))
106
}
107
})
108
}
@@ -202,9 +203,9 @@ func TestCollector_Collect(t *testing.T) {
203
collr, cleanup := test.prepare(t)
204
defer cleanup()
205
205
- _ = collr.Check()
206
+ _ = collr.Check(context.Background())
207
207
- mx := collr.Collect()
208
+ mx := collr.Collect(context.Background())
209
210
require.Equal(t, test.wantMetrics, mx)
211
@@ -224,7 +225,7 @@ func caseOkResponse(t *testing.T) (*Collector, func()) {
225
}))
226
collr := New()
227
collr.URL = srv.URL
227
- require.NoError(t, collr.Init())
228
+ require.NoError(t, collr.Init(context.Background()))
229
230
return collr, srv.Close
231
}
@@ -237,7 +238,7 @@ func caseInvalidDataResponse(t *testing.T) (*Collector, func()) {
238
}))
239
collr := New()
240
collr.URL = srv.URL
240
- require.NoError(t, collr.Init())
241
+ require.NoError(t, collr.Init(context.Background()))
242
243
return collr, srv.Close
244
}
@@ -246,7 +247,7 @@ func caseConnectionRefused(t *testing.T) (*Collector, func()) {
247
t.Helper()
248
collr := New()
249
collr.URL = "http://127.0.0.1:65001"
249
- require.NoError(t, collr.Init())
250
+ require.NoError(t, collr.Init(context.Background()))
251
252
return collr, func() {}
253
}
@@ -259,7 +260,7 @@ func case404(t *testing.T) (*Collector, func()) {
260
}))
261
collr := New()
262
collr.URL = srv.URL
262
- require.NoError(t, collr.Init())
263
+ require.NoError(t, collr.Init(context.Background()))
264
265
return collr, srv.Close
266
}
src/go/plugin/go.d/collector/rspamd/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package rspamd
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -59,7 +60,7 @@ func (c *Collector) Configuration() any {
60
return c.Config
61
}
62
62
-func (c *Collector) Init() error {
63
+func (c *Collector) Init(context.Context) error {
64
if c.URL == "" {
65
return errors.New("config: url not set")
66
}
@@ -76,7 +77,7 @@ func (c *Collector) Init() error {
77
return nil
78
}
79
79
-func (c *Collector) Check() error {
80
+func (c *Collector) Check(context.Context) error {
81
mx, err := c.collect()
82
if err != nil {
83
return err
@@ -93,7 +94,7 @@ func (c *Collector) Charts() *module.Charts {
94
return c.charts
95
}
96
96
-func (c *Collector) Collect() map[string]int64 {
97
+func (c *Collector) Collect(context.Context) map[string]int64 {
98
mx, err := c.collect()
99
if err != nil {
100
c.Error(err)
@@ -106,7 +107,7 @@ func (c *Collector) Collect() map[string]int64 {
107
return mx
108
}
109
109
-func (c *Collector) Cleanup() {
110
+func (c *Collector) Cleanup(context.Context) {
111
if c.httpClient != nil {
112
c.httpClient.CloseIdleConnections()
113
}
src/go/plugin/go.d/collector/rspamd/collector_test.go
+10
-9
@@ -3,6 +3,7 @@
3
package rspamd
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -61,9 +62,9 @@ func TestCollector_Init(t *testing.T) {
62
collr.Config = test.config
63
64
if test.wantFail {
64
- assert.Error(t, collr.Init())
65
+ assert.Error(t, collr.Init(context.Background()))
66
} else {
66
- assert.NoError(t, collr.Init())
67
+ assert.NoError(t, collr.Init(context.Background()))
68
}
69
})
70
}
@@ -102,9 +103,9 @@ func TestCollector_Check(t *testing.T) {
103
defer cleanup()
104
105
if test.wantFail {
105
- assert.Error(t, collr.Check())
106
+ assert.Error(t, collr.Check(context.Background()))
107
} else {
107
- assert.NoError(t, collr.Check())
108
+ assert.NoError(t, collr.Check(context.Background()))
109
}
110
})
111
}
@@ -153,7 +154,7 @@ func TestCollector_Collect(t *testing.T) {
154
collr, cleanup := test.prepare(t)
155
defer cleanup()
156
156
- mx := collr.Collect()
157
+ mx := collr.Collect(context.Background())
158
159
require.Equal(t, test.wantMetrics, mx)
160
@@ -178,7 +179,7 @@ func prepareCaseOk(t *testing.T) (*Collector, func()) {
179
180
collr := New()
181
collr.URL = srv.URL
181
- require.NoError(t, collr.Init())
182
+ require.NoError(t, collr.Init(context.Background()))
183
184
return collr, srv.Close
185
}
@@ -214,7 +215,7 @@ func prepareCaseUnexpectedJsonResponse(t *testing.T) (*Collector, func()) {
215
216
collr := New()
217
collr.URL = srv.URL
217
- require.NoError(t, collr.Init())
218
+ require.NoError(t, collr.Init(context.Background()))
219
220
return collr, srv.Close
221
}
@@ -228,7 +229,7 @@ func prepareCaseInvalidFormatResponse(t *testing.T) (*Collector, func()) {
229
230
collr := New()
231
collr.URL = srv.URL
231
- require.NoError(t, collr.Init())
232
+ require.NoError(t, collr.Init(context.Background()))
233
234
return collr, srv.Close
235
}
@@ -237,7 +238,7 @@ func prepareCaseConnectionRefused(t *testing.T) (*Collector, func()) {
238
t.Helper()
239
collr := New()
240
collr.URL = "http://127.0.0.1:65001/stat"
240
- require.NoError(t, collr.Init())
241
+ require.NoError(t, collr.Init(context.Background()))
242
243
return collr, func() {}
244
}
src/go/plugin/go.d/collector/samba/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package samba
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -56,7 +57,7 @@ func (c *Collector) Configuration() any {
57
return c.Config
58
}
59
59
-func (c *Collector) Init() error {
60
+func (c *Collector) Init(context.Context) error {
61
smbStatus, err := c.initSmbStatusBinary()
62
if err != nil {
63
return fmt.Errorf("smbstatus exec initialization: %v", err)
@@ -66,7 +67,7 @@ func (c *Collector) Init() error {
67
return nil
68
}
69
69
-func (c *Collector) Check() error {
70
+func (c *Collector) Check(context.Context) error {
71
mx, err := c.collect()
72
if err != nil {
73
return err
@@ -83,7 +84,7 @@ func (c *Collector) Charts() *module.Charts {
84
return c.charts
85
}
86
86
-func (c *Collector) Collect() map[string]int64 {
87
+func (c *Collector) Collect(context.Context) map[string]int64 {
88
mx, err := c.collect()
89
if err != nil {
90
c.Error(err)
@@ -96,4 +97,4 @@ func (c *Collector) Collect() map[string]int64 {
97
return mx
98
}
99
99
-func (c *Collector) Cleanup() {}
100
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/samba/collector_test.go
+9
-8
@@ -3,6 +3,7 @@
3
package samba
4
5
import (
6
+ "context"
7
"errors"
8
"os"
9
"testing"
@@ -52,9 +53,9 @@ func TestCollector_Init(t *testing.T) {
53
collr.Config = test.config
54
55
if test.wantFail {
55
- assert.Error(t, collr.Init())
56
+ assert.Error(t, collr.Init(context.Background()))
57
} else {
57
- assert.NoError(t, collr.Init())
58
+ assert.NoError(t, collr.Init(context.Background()))
59
}
60
})
61
}
@@ -73,7 +74,7 @@ func TestCollector_Cleanup(t *testing.T) {
74
prepare: func() *Collector {
75
collr := New()
76
collr.exec = prepareMockOk()
76
- _ = collr.Check()
77
+ _ = collr.Check(context.Background())
78
return collr
79
},
80
},
@@ -81,7 +82,7 @@ func TestCollector_Cleanup(t *testing.T) {
82
prepare: func() *Collector {
83
collr := New()
84
collr.exec = prepareMockOk()
84
- _ = collr.Collect()
85
+ _ = collr.Collect(context.Background())
86
return collr
87
},
88
},
@@ -91,7 +92,7 @@ func TestCollector_Cleanup(t *testing.T) {
92
t.Run(name, func(t *testing.T) {
93
collr := test.prepare()
94
94
- assert.NotPanics(t, collr.Cleanup)
95
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
96
})
97
}
98
}
@@ -130,9 +131,9 @@ func TestCollector_Check(t *testing.T) {
131
collr.exec = mock
132
133
if test.wantFail {
133
- assert.Error(t, collr.Check())
134
+ assert.Error(t, collr.Check(context.Background()))
135
} else {
135
- assert.NoError(t, collr.Check())
136
+ assert.NoError(t, collr.Check(context.Background()))
137
}
138
})
139
}
@@ -288,7 +289,7 @@ func TestCollector_Collect(t *testing.T) {
289
mock := test.prepareMock()
290
collr.exec = mock
291
291
- mx := collr.Collect()
292
+ mx := collr.Collect(context.Background())
293
294
assert.Equal(t, test.wantMetrics, mx)
295
src/go/plugin/go.d/collector/scaleio/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package scaleio
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -71,7 +72,7 @@ func (c *Collector) Configuration() any {
72
return c.Config
73
}
74
74
-func (c *Collector) Init() error {
75
+func (c *Collector) Init(context.Context) error {
76
if c.Username == "" || c.Password == "" {
77
return errors.New("config: username and password aren't set")
78
}
@@ -88,7 +89,7 @@ func (c *Collector) Init() error {
89
return nil
90
}
91
91
-func (c *Collector) Check() error {
92
+func (c *Collector) Check(context.Context) error {
93
if err := c.client.Login(); err != nil {
94
return err
95
}
@@ -106,7 +107,7 @@ func (c *Collector) Charts() *module.Charts {
107
return c.charts
108
}
109
109
-func (c *Collector) Collect() map[string]int64 {
110
+func (c *Collector) Collect(context.Context) map[string]int64 {
111
mx, err := c.collect()
112
if err != nil {
113
c.Error(err)
@@ -119,7 +120,7 @@ func (c *Collector) Collect() map[string]int64 {
120
return mx
121
}
122
122
-func (c *Collector) Cleanup() {
123
+func (c *Collector) Cleanup(context.Context) {
124
if c.client == nil {
125
return
126
}
src/go/plugin/go.d/collector/scaleio/collector_test.go
+17
-16
@@ -3,6 +3,7 @@
3
package scaleio
4
5
import (
6
+ "context"
7
"encoding/json"
8
"net/http/httptest"
9
"os"
@@ -43,10 +44,10 @@ func TestCollector_Init(t *testing.T) {
44
collr.Username = "username"
45
collr.Password = "password"
46
46
- assert.NoError(t, collr.Init())
47
+ assert.NoError(t, collr.Init(context.Background()))
48
}
49
func TestCollector_Init_UsernameAndPasswordNotSet(t *testing.T) {
49
- assert.Error(t, New().Init())
50
+ assert.Error(t, New().Init(context.Background()))
51
}
52
53
func TestCollector_Init_ErrorOnCreatingClientWrongTLSCA(t *testing.T) {
@@ -55,24 +56,24 @@ func TestCollector_Init_ErrorOnCreatingClientWrongTLSCA(t *testing.T) {
56
collr.Password = "password"
57
collr.ClientConfig.TLSConfig.TLSCA = "testdata/tls"
58
58
- assert.Error(t, collr.Init())
59
+ assert.Error(t, collr.Init(context.Background()))
60
}
61
62
func TestCollector_Check(t *testing.T) {
63
srv, _, collr := prepareSrvMockScaleIO(t)
64
defer srv.Close()
64
- require.NoError(t, collr.Init())
65
+ require.NoError(t, collr.Init(context.Background()))
66
66
- assert.NoError(t, collr.Check())
67
+ assert.NoError(t, collr.Check(context.Background()))
68
}
69
70
func TestCollector_Check_ErrorOnLogin(t *testing.T) {
71
srv, mock, collr := prepareSrvMockScaleIO(t)
72
defer srv.Close()
72
- require.NoError(t, collr.Init())
73
+ require.NoError(t, collr.Init(context.Background()))
74
mock.Password = "new password"
75
75
- assert.Error(t, collr.Check())
76
+ assert.Error(t, collr.Check(context.Background()))
77
}
78
79
func TestCollector_Charts(t *testing.T) {
@@ -82,18 +83,18 @@ func TestCollector_Charts(t *testing.T) {
83
func TestCollector_Cleanup(t *testing.T) {
84
srv, _, collr := prepareSrvMockScaleIO(t)
85
defer srv.Close()
85
- require.NoError(t, collr.Init())
86
- require.NoError(t, collr.Check())
86
+ require.NoError(t, collr.Init(context.Background()))
87
+ require.NoError(t, collr.Check(context.Background()))
88
88
- collr.Cleanup()
89
+ collr.Cleanup(context.Background())
90
assert.False(t, collr.client.LoggedIn())
91
}
92
93
func TestCollector_Collect(t *testing.T) {
94
srv, _, collr := prepareSrvMockScaleIO(t)
95
defer srv.Close()
95
- require.NoError(t, collr.Init())
96
- require.NoError(t, collr.Check())
96
+ require.NoError(t, collr.Init(context.Background()))
97
+ require.NoError(t, collr.Check(context.Background()))
98
99
expected := map[string]int64{
100
"sdc_6076fd0f00000000_bandwidth_read": 0,
@@ -298,7 +299,7 @@ func TestCollector_Collect(t *testing.T) {
299
"system_total_iops_write": 617200,
300
}
301
301
- mx := collr.Collect()
302
+ mx := collr.Collect(context.Background())
303
304
assert.Equal(t, expected, mx)
305
@@ -308,11 +309,11 @@ func TestCollector_Collect(t *testing.T) {
309
func TestCollector_Collect_ConnectionRefused(t *testing.T) {
310
srv, _, collr := prepareSrvMockScaleIO(t)
311
defer srv.Close()
311
- require.NoError(t, collr.Init())
312
- require.NoError(t, collr.Check())
312
+ require.NoError(t, collr.Init(context.Background()))
313
+ require.NoError(t, collr.Check(context.Background()))
314
collr.client.Request.URL = "http://127.0.0.1:38001"
315
315
- assert.Nil(t, collr.Collect())
316
+ assert.Nil(t, collr.Collect(context.Background()))
317
}
318
319
func testCharts(t *testing.T, collr *Collector, collected map[string]int64) {
src/go/plugin/go.d/collector/sensors/collector.go
+5
-4
@@ -5,6 +5,7 @@
5
package sensors
6
7
import (
8
+ "context"
9
_ "embed"
10
"errors"
11
@@ -65,7 +66,7 @@ func (c *Collector) Configuration() any {
66
return c.Config
67
}
68
68
-func (c *Collector) Init() error {
69
+func (c *Collector) Init(context.Context) error {
70
sc := lmsensors.New()
71
sc.Logger = c.Logger
72
c.sc = sc
@@ -73,7 +74,7 @@ func (c *Collector) Init() error {
74
return nil
75
}
76
76
-func (c *Collector) Check() error {
77
+func (c *Collector) Check(context.Context) error {
78
mx, err := c.collect()
79
if err != nil {
80
return err
@@ -90,7 +91,7 @@ func (c *Collector) Charts() *module.Charts {
91
return c.charts
92
}
93
93
-func (c *Collector) Collect() map[string]int64 {
94
+func (c *Collector) Collect(context.Context) map[string]int64 {
95
mx, err := c.collect()
96
if err != nil {
97
c.Error(err)
@@ -103,4 +104,4 @@ func (c *Collector) Collect() map[string]int64 {
104
return mx
105
}
106
106
-func (c *Collector) Cleanup() {}
107
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/sensors/collector_test.go
+9
-8
@@ -5,6 +5,7 @@
5
package sensors
6
7
import (
8
+ "context"
9
"errors"
10
"os"
11
"testing"
@@ -52,9 +53,9 @@ func TestCollector_Init(t *testing.T) {
53
collr.Config = test.config
54
55
if test.wantFail {
55
- assert.Error(t, collr.Init())
56
+ assert.Error(t, collr.Init(context.Background()))
57
} else {
57
- assert.NoError(t, collr.Init())
58
+ assert.NoError(t, collr.Init(context.Background()))
59
}
60
})
61
}
@@ -73,7 +74,7 @@ func TestCollector_Cleanup(t *testing.T) {
74
prepare: func() *Collector {
75
collr := New()
76
collr.sc = prepareMockScannerOk()
76
- _ = collr.Check()
77
+ _ = collr.Check(context.Background())
78
return collr
79
},
80
},
@@ -81,7 +82,7 @@ func TestCollector_Cleanup(t *testing.T) {
82
prepare: func() *Collector {
83
collr := New()
84
collr.sc = prepareMockScannerOk()
84
- _ = collr.Collect()
85
+ _ = collr.Collect(context.Background())
86
return collr
87
},
88
},
@@ -91,7 +92,7 @@ func TestCollector_Cleanup(t *testing.T) {
92
t.Run(name, func(t *testing.T) {
93
collr := test.prepare()
94
94
- assert.NotPanics(t, collr.Cleanup)
95
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
96
})
97
}
98
}
@@ -121,9 +122,9 @@ func TestCollector_Check(t *testing.T) {
122
collr.sc = test.prepareMock()
123
124
if test.wantFail {
124
- assert.Error(t, collr.Check())
125
+ assert.Error(t, collr.Check(context.Background()))
126
} else {
126
- assert.NoError(t, collr.Check())
127
+ assert.NoError(t, collr.Check(context.Background()))
128
}
129
})
130
}
@@ -270,7 +271,7 @@ func TestCollector_Collect(t *testing.T) {
271
var mx map[string]int64
272
273
for i := 0; i < 10; i++ {
273
- mx = collr.Collect()
274
+ mx = collr.Collect(context.Background())
275
}
276
277
assert.Equal(t, test.wantMetrics, mx)
src/go/plugin/go.d/collector/smartctl/collector.go
+5
-4
@@ -5,6 +5,7 @@
5
package smartctl
6
7
import (
8
+ "context"
9
_ "embed"
10
"errors"
11
"fmt"
@@ -86,7 +87,7 @@ func (c *Collector) Configuration() any {
87
return c.Config
88
}
89
89
-func (c *Collector) Init() error {
90
+func (c *Collector) Init(context.Context) error {
91
if err := c.validateConfig(); err != nil {
92
return fmt.Errorf("config validation: %s", err)
93
}
@@ -106,7 +107,7 @@ func (c *Collector) Init() error {
107
return nil
108
}
109
109
-func (c *Collector) Check() error {
110
+func (c *Collector) Check(context.Context) error {
111
mx, err := c.collect()
112
if err != nil {
113
return err
@@ -123,7 +124,7 @@ func (c *Collector) Charts() *module.Charts {
124
return c.charts
125
}
126
126
-func (c *Collector) Collect() map[string]int64 {
127
+func (c *Collector) Collect(context.Context) map[string]int64 {
128
mx, err := c.collect()
129
if err != nil {
130
c.Error(err)
@@ -136,4 +137,4 @@ func (c *Collector) Collect() map[string]int64 {
137
return mx
138
}
139
139
-func (c *Collector) Cleanup() {}
140
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/smartctl/collector_test.go
+9
-8
@@ -5,6 +5,7 @@
5
package smartctl
6
7
import (
8
+ "context"
9
"fmt"
10
"os"
11
"testing"
@@ -82,9 +83,9 @@ func TestCollector_Init(t *testing.T) {
83
collr := New()
84
85
if test.wantFail {
85
- assert.Error(t, collr.Init())
86
+ assert.Error(t, collr.Init(context.Background()))
87
} else {
87
- assert.NoError(t, collr.Init())
88
+ assert.NoError(t, collr.Init(context.Background()))
89
}
90
})
91
}
@@ -103,7 +104,7 @@ func TestCollector_Cleanup(t *testing.T) {
104
prepare: func() *Collector {
105
collr := New()
106
collr.exec = prepareMockOkTypeSata()
106
- _ = collr.Check()
107
+ _ = collr.Check(context.Background())
108
return collr
109
},
110
},
@@ -111,7 +112,7 @@ func TestCollector_Cleanup(t *testing.T) {
112
prepare: func() *Collector {
113
collr := New()
114
collr.exec = prepareMockOkTypeSata()
114
- _ = collr.Collect()
115
+ _ = collr.Collect(context.Background())
116
return collr
117
},
118
},
@@ -121,7 +122,7 @@ func TestCollector_Cleanup(t *testing.T) {
122
t.Run(name, func(t *testing.T) {
123
collr := test.prepare()
124
124
- assert.NotPanics(t, collr.Cleanup)
125
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
126
})
127
}
128
}
@@ -160,9 +161,9 @@ func TestCollector_Check(t *testing.T) {
161
collr.exec = mock
162
163
if test.wantFail {
163
- assert.Error(t, collr.Check())
164
+ assert.Error(t, collr.Check(context.Background()))
165
} else {
165
- assert.NoError(t, collr.Check())
166
+ assert.NoError(t, collr.Check(context.Background()))
167
}
168
})
169
}
@@ -364,7 +365,7 @@ func TestCollector_Collect(t *testing.T) {
365
366
var mx map[string]int64
367
for i := 0; i < 10; i++ {
367
- mx = collr.Collect()
368
+ mx = collr.Collect(context.Background())
369
}
370
371
assert.Equal(t, test.wantMetrics, mx)
src/go/plugin/go.d/collector/snmp/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package snmp
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -83,7 +84,7 @@ func (c *Collector) Configuration() any {
84
return c.Config
85
}
86
86
-func (c *Collector) Init() error {
87
+func (c *Collector) Init(context.Context) error {
88
err := c.validateConfig()
89
if err != nil {
90
return fmt.Errorf("config validation failed: %v", err)
@@ -118,7 +119,7 @@ func (c *Collector) Init() error {
119
return nil
120
}
121
121
-func (c *Collector) Check() error {
122
+func (c *Collector) Check(context.Context) error {
123
mx, err := c.collect()
124
if err != nil {
125
return err
@@ -135,7 +136,7 @@ func (c *Collector) Charts() *module.Charts {
136
return c.charts
137
}
138
138
-func (c *Collector) Collect() map[string]int64 {
139
+func (c *Collector) Collect(context.Context) map[string]int64 {
140
mx, err := c.collect()
141
if err != nil {
142
c.Error(err)
@@ -148,7 +149,7 @@ func (c *Collector) Collect() map[string]int64 {
149
return mx
150
}
151
151
-func (c *Collector) Cleanup() {
152
+func (c *Collector) Cleanup(context.Context) {
153
if c.snmpClient != nil {
154
_ = c.snmpClient.Close()
155
}
src/go/plugin/go.d/collector/snmp/collector_test.go
+13
-12
@@ -3,6 +3,7 @@
3
package snmp
4
5
import (
6
+ "context"
7
"encoding/hex"
8
"errors"
9
"fmt"
@@ -88,9 +89,9 @@ func TestCollector_Init(t *testing.T) {
89
collr := test.prepareSNMP()
90
91
if test.wantFail {
91
- assert.Error(t, collr.Init())
92
+ assert.Error(t, collr.Init(context.Background()))
93
} else {
93
- assert.NoError(t, collr.Init())
94
+ assert.NoError(t, collr.Init(context.Background()))
95
}
96
})
97
}
@@ -107,7 +108,7 @@ func TestCollector_Cleanup(t *testing.T) {
108
collr.newSnmpClient = func() gosnmp.Handler { return m }
109
setMockClientInitExpect(m)
110
110
- require.NoError(t, collr.Init())
111
+ require.NoError(t, collr.Init(context.Background()))
112
113
m.EXPECT().Close().Times(1)
114
@@ -121,7 +122,7 @@ func TestCollector_Cleanup(t *testing.T) {
122
collr.newSnmpClient = func() gosnmp.Handler { return m }
123
setMockClientInitExpect(m)
124
124
- require.NoError(t, collr.Init())
125
+ require.NoError(t, collr.Init(context.Background()))
126
127
collr.snmpClient = nil
128
@@ -137,7 +138,7 @@ func TestCollector_Cleanup(t *testing.T) {
138
139
collr := test.prepareSNMP(t, mockSNMP)
140
140
- assert.NotPanics(t, collr.Cleanup)
141
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
142
})
143
}
144
}
@@ -182,10 +183,10 @@ func TestCollector_Charts(t *testing.T) {
183
collr := test.prepareSNMP(t, mockSNMP)
184
collr.newSnmpClient = func() gosnmp.Handler { return mockSNMP }
185
185
- require.NoError(t, collr.Init())
186
+ require.NoError(t, collr.Init(context.Background()))
187
188
if test.doCollect {
188
- _ = collr.Collect()
189
+ _ = collr.Collect(context.Background())
190
}
191
192
assert.Equal(t, test.wantNumCharts, len(*collr.Charts()))
@@ -256,12 +257,12 @@ func TestCollector_Check(t *testing.T) {
257
collr := test.prepareSNMP(mockSNMP)
258
collr.newSnmpClient = func() gosnmp.Handler { return mockSNMP }
259
259
- require.NoError(t, collr.Init())
260
+ require.NoError(t, collr.Init(context.Background()))
261
262
if test.wantFail {
262
- assert.Error(t, collr.Check())
263
+ assert.Error(t, collr.Check(context.Background()))
264
} else {
264
- assert.NoError(t, collr.Check())
265
+ assert.NoError(t, collr.Check(context.Background()))
266
}
267
})
268
}
@@ -468,9 +469,9 @@ func TestCollector_Collect(t *testing.T) {
469
collr := test.prepareSNMP(mockSNMP)
470
collr.newSnmpClient = func() gosnmp.Handler { return mockSNMP }
471
471
- require.NoError(t, collr.Init())
472
+ require.NoError(t, collr.Init(context.Background()))
473
473
- mx := collr.Collect()
474
+ mx := collr.Collect(context.Background())
475
476
assert.Equal(t, test.wantCollected, mx)
477
})
src/go/plugin/go.d/collector/spigotmc/collect.go
+3
-2
@@ -3,6 +3,7 @@
3
package spigotmc
4
5
import (
6
+ "context"
7
"errors"
8
"fmt"
9
"regexp"
@@ -30,11 +31,11 @@ func (c *Collector) collect() (map[string]int64, error) {
31
mx := make(map[string]int64)
32
33
if err := c.collectTPS(mx); err != nil {
33
- c.Cleanup()
34
+ c.Cleanup(context.Background())
35
return nil, fmt.Errorf("failed to collect '%s': %v", cmdTPS, err)
36
}
37
if err := c.collectList(mx); err != nil {
37
- c.Cleanup()
38
+ c.Cleanup(context.Background())
39
return nil, fmt.Errorf("failed to collect '%s': %v", cmdList, err)
40
}
41
src/go/plugin/go.d/collector/spigotmc/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package spigotmc
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"time"
@@ -54,7 +55,7 @@ func (c *Collector) Configuration() any {
55
return c.Config
56
}
57
57
-func (c *Collector) Init() error {
58
+func (c *Collector) Init(context.Context) error {
59
if c.Address == "" {
60
return errors.New("config: 'address' required but not set")
61
}
@@ -64,7 +65,7 @@ func (c *Collector) Init() error {
65
return nil
66
}
67
67
-func (c *Collector) Check() error {
68
+func (c *Collector) Check(context.Context) error {
69
mx, err := c.collect()
70
if err != nil {
71
return err
@@ -81,7 +82,7 @@ func (c *Collector) Charts() *module.Charts {
82
return c.charts
83
}
84
84
-func (c *Collector) Collect() map[string]int64 {
85
+func (c *Collector) Collect(context.Context) map[string]int64 {
86
mx, err := c.collect()
87
if err != nil {
88
c.Error(err)
@@ -94,7 +95,7 @@ func (c *Collector) Collect() map[string]int64 {
95
return mx
96
}
97
97
-func (c *Collector) Cleanup() {
98
+func (c *Collector) Cleanup(context.Context) {
99
if c.conn != nil {
100
if err := c.conn.disconnect(); err != nil {
101
c.Warningf("error on disconnect: %s", err)
src/go/plugin/go.d/collector/spigotmc/collector_test.go
+10
-9
@@ -3,6 +3,7 @@
3
package spigotmc
4
5
import (
6
+ "context"
7
"errors"
8
"os"
9
"testing"
@@ -66,9 +67,9 @@ func TestCollector_Init(t *testing.T) {
67
collr.Config = test.config
68
69
if test.wantFail {
69
- assert.Error(t, collr.Init())
70
+ assert.Error(t, collr.Init(context.Background()))
71
} else {
71
- assert.NoError(t, collr.Init())
72
+ assert.NoError(t, collr.Init(context.Background()))
73
}
74
})
75
}
@@ -87,7 +88,7 @@ func TestCollector_Cleanup(t *testing.T) {
88
prepare: func() *Collector {
89
collr := New()
90
collr.newConn = func(Config) rconConn { return prepareMockOk() }
90
- _ = collr.Check()
91
+ _ = collr.Check(context.Background())
92
return collr
93
},
94
},
@@ -95,7 +96,7 @@ func TestCollector_Cleanup(t *testing.T) {
96
prepare: func() *Collector {
97
collr := New()
98
collr.newConn = func(Config) rconConn { return prepareMockOk() }
98
- _ = collr.Collect()
99
+ _ = collr.Collect(context.Background())
100
return collr
101
},
102
},
@@ -105,7 +106,7 @@ func TestCollector_Cleanup(t *testing.T) {
106
t.Run(name, func(t *testing.T) {
107
collr := test.prepare()
108
108
- assert.NotPanics(t, collr.Cleanup)
109
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
110
})
111
}
112
}
@@ -144,9 +145,9 @@ func TestCollector_Check(t *testing.T) {
145
collr.newConn = func(Config) rconConn { return mock }
146
147
if test.wantFail {
147
- assert.Error(t, collr.Check())
148
+ assert.Error(t, collr.Check(context.Background()))
149
} else {
149
- assert.NoError(t, collr.Check())
150
+ assert.NoError(t, collr.Check(context.Background()))
151
}
152
})
153
}
@@ -244,7 +245,7 @@ func TestCollector_Collect(t *testing.T) {
245
mock := test.prepareMock()
246
collr.newConn = func(Config) rconConn { return mock }
247
247
- mx := collr.Collect()
248
+ mx := collr.Collect(context.Background())
249
250
require.Equal(t, test.wantMetrics, mx, "want metrics")
251
@@ -254,7 +255,7 @@ func TestCollector_Collect(t *testing.T) {
255
}
256
257
assert.Equal(t, test.disconnectBeforeCleanup, mock.disconnectCalled, "disconnect before cleanup")
257
- collr.Cleanup()
258
+ collr.Cleanup(context.Background())
259
assert.Equal(t, test.disconnectAfterCleanup, mock.disconnectCalled, "disconnect after cleanup")
260
})
261
}
src/go/plugin/go.d/collector/squid/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package squid
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -59,7 +60,7 @@ func (c *Collector) Configuration() any {
60
return c.Config
61
}
62
62
-func (c *Collector) Init() error {
63
+func (c *Collector) Init(context.Context) error {
64
if c.URL == "" {
65
return errors.New("config: url not set")
66
}
@@ -76,7 +77,7 @@ func (c *Collector) Init() error {
77
return nil
78
}
79
79
-func (c *Collector) Check() error {
80
+func (c *Collector) Check(context.Context) error {
81
mx, err := c.collect()
82
if err != nil {
83
return err
@@ -93,7 +94,7 @@ func (c *Collector) Charts() *module.Charts {
94
return c.charts
95
}
96
96
-func (c *Collector) Collect() map[string]int64 {
97
+func (c *Collector) Collect(context.Context) map[string]int64 {
98
mx, err := c.collect()
99
if err != nil {
100
c.Error(err)
@@ -106,7 +107,7 @@ func (c *Collector) Collect() map[string]int64 {
107
return mx
108
}
109
109
-func (c *Collector) Cleanup() {
110
+func (c *Collector) Cleanup(context.Context) {
111
if c.httpClient != nil {
112
c.httpClient.CloseIdleConnections()
113
}
src/go/plugin/go.d/collector/squid/collector_test.go
+10
-9
@@ -3,6 +3,7 @@
3
package squid
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -60,9 +61,9 @@ func TestCollector_Init(t *testing.T) {
61
collr.Config = test.config
62
63
if test.wantFail {
63
- assert.Error(t, collr.Init())
64
+ assert.Error(t, collr.Init(context.Background()))
65
} else {
65
- assert.NoError(t, collr.Init())
66
+ assert.NoError(t, collr.Init(context.Background()))
67
}
68
})
69
}
@@ -101,9 +102,9 @@ func TestCollector_Check(t *testing.T) {
102
defer cleanup()
103
104
if test.wantFail {
104
- assert.Error(t, collr.Check())
105
+ assert.Error(t, collr.Check(context.Background()))
106
} else {
106
- assert.NoError(t, collr.Check())
107
+ assert.NoError(t, collr.Check(context.Background()))
108
}
109
})
110
}
@@ -147,7 +148,7 @@ func TestCollector_Collect(t *testing.T) {
148
collr, cleanup := test.prepare(t)
149
defer cleanup()
150
150
- mx := collr.Collect()
151
+ mx := collr.Collect(context.Background())
152
153
require.Equal(t, test.wantMetrics, mx)
154
@@ -173,7 +174,7 @@ func prepareCaseSuccess(t *testing.T) (*Collector, func()) {
174
175
collr := New()
176
collr.URL = srv.URL
176
- require.NoError(t, collr.Init())
177
+ require.NoError(t, collr.Init(context.Background()))
178
179
return collr, srv.Close
180
}
@@ -192,7 +193,7 @@ Fusce et felis pulvinar, posuere sem non, porttitor eros.`)
193
194
collr := New()
195
collr.URL = srv.URL
195
- require.NoError(t, collr.Init())
196
+ require.NoError(t, collr.Init(context.Background()))
197
198
return collr, srv.Close
199
}
@@ -208,7 +209,7 @@ func prepareCaseEmptyResponse(t *testing.T) (*Collector, func()) {
209
210
collr := New()
211
collr.URL = srv.URL
211
- require.NoError(t, collr.Init())
212
+ require.NoError(t, collr.Init(context.Background()))
213
214
return collr, srv.Close
215
}
@@ -217,7 +218,7 @@ func prepareCaseConnectionRefused(t *testing.T) (*Collector, func()) {
218
t.Helper()
219
collr := New()
220
collr.URL = "http://127.0.0.1:65001"
220
- require.NoError(t, collr.Init())
221
+ require.NoError(t, collr.Init(context.Background()))
222
223
return collr, func() {}
224
}
src/go/plugin/go.d/collector/squidlog/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package squidlog
4
5
import (
6
+ "context"
7
_ "embed"
8
"fmt"
9
@@ -64,13 +65,13 @@ func (c *Collector) Configuration() any {
65
return c.Config
66
}
67
67
-func (c *Collector) Init() error {
68
+func (c *Collector) Init(context.Context) error {
69
c.line = newEmptyLogLine()
70
c.mx = newMetricsData()
71
return nil
72
}
73
73
-func (c *Collector) Check() error {
74
+func (c *Collector) Check(context.Context) error {
75
// Note: these inits are here to make auto-detection retry working
76
if err := c.createLogReader(); err != nil {
77
return fmt.Errorf("failed to create log reader: %v", err)
@@ -91,7 +92,7 @@ func (c *Collector) Charts() *module.Charts {
92
return c.charts
93
}
94
94
-func (c *Collector) Collect() map[string]int64 {
95
+func (c *Collector) Collect(context.Context) map[string]int64 {
96
mx, err := c.collect()
97
if err != nil {
98
c.Error(err)
@@ -103,7 +104,7 @@ func (c *Collector) Collect() map[string]int64 {
104
return mx
105
}
106
106
-func (c *Collector) Cleanup() {
107
+func (c *Collector) Cleanup(context.Context) {
108
if c.file != nil {
109
_ = c.file.Close()
110
}
src/go/plugin/go.d/collector/squidlog/collector_test.go
+18
-17
@@ -4,6 +4,7 @@ package squidlog
4
5
import (
6
"bytes"
7
+ "context"
8
"os"
9
"testing"
10
@@ -42,7 +43,7 @@ func TestNew(t *testing.T) {
43
func TestCollector_Init(t *testing.T) {
44
collr := New()
45
45
- assert.NoError(t, collr.Init())
46
+ assert.NoError(t, collr.Init(context.Background()))
47
}
48
49
func TestCollector_Check(t *testing.T) {
@@ -50,30 +51,30 @@ func TestCollector_Check(t *testing.T) {
51
52
func TestCollector_Check_ErrorOnCreatingLogReaderNoLogFile(t *testing.T) {
53
collr := New()
53
- defer collr.Cleanup()
54
+ defer collr.Cleanup(context.Background())
55
collr.Path = "testdata/not_exists.log"
55
- require.NoError(t, collr.Init())
56
+ require.NoError(t, collr.Init(context.Background()))
57
57
- assert.Error(t, collr.Check())
58
+ assert.Error(t, collr.Check(context.Background()))
59
}
60
61
func TestSquid_Check_ErrorOnCreatingParserUnknownFormat(t *testing.T) {
62
collr := New()
62
- defer collr.Cleanup()
63
+ defer collr.Cleanup(context.Background())
64
collr.Path = "testdata/unknown.log"
64
- require.NoError(t, collr.Init())
65
+ require.NoError(t, collr.Init(context.Background()))
66
66
- assert.Error(t, collr.Check())
67
+ assert.Error(t, collr.Check(context.Background()))
68
}
69
70
func TestSquid_Check_ErrorOnCreatingParserZeroKnownFields(t *testing.T) {
71
collr := New()
71
- defer collr.Cleanup()
72
+ defer collr.Cleanup(context.Background())
73
collr.Path = "testdata/access.log"
74
collr.ParserConfig.CSV.Format = "$one $two"
74
- require.NoError(t, collr.Init())
75
+ require.NoError(t, collr.Init(context.Background()))
76
76
- assert.Error(t, collr.Check())
77
+ assert.Error(t, collr.Check(context.Background()))
78
}
79
80
func TestCollector_Charts(t *testing.T) {
@@ -85,7 +86,7 @@ func TestCollector_Charts(t *testing.T) {
86
}
87
88
func TestCollector_Cleanup(t *testing.T) {
88
- New().Cleanup()
89
+ New().Cleanup(context.Background())
90
}
91
92
func TestCollector_Collect(t *testing.T) {
@@ -159,7 +160,7 @@ func TestCollector_Collect(t *testing.T) {
160
"unmatched": 16,
161
}
162
162
- collected := collr.Collect()
163
+ collected := collr.Collect(context.Background())
164
165
assert.Equal(t, expected, collected)
166
testCharts(t, collr, collected)
@@ -236,9 +237,9 @@ func TestCollector_Collect_ReturnOldDataIfNothingRead(t *testing.T) {
237
"unmatched": 16,
238
}
239
239
- _ = collr.Collect()
240
+ _ = collr.Collect(context.Background())
241
241
- mx := collr.Collect()
242
+ mx := collr.Collect(context.Background())
243
244
assert.Equal(t, expected, mx)
245
@@ -281,9 +282,9 @@ func prepareSquidCollect(t *testing.T) *Collector {
282
t.Helper()
283
collr := New()
284
collr.Path = "testdata/access.log"
284
- require.NoError(t, collr.Init())
285
- require.NoError(t, collr.Check())
286
- defer collr.Cleanup()
285
+ require.NoError(t, collr.Init(context.Background()))
286
+ require.NoError(t, collr.Check(context.Background()))
287
+ defer collr.Cleanup(context.Background())
288
289
p, err := logs.NewCSVParser(collr.ParserConfig.CSV, bytes.NewReader(dataNativeFormatAccessLog))
290
require.NoError(t, err)
src/go/plugin/go.d/collector/squidlog/init.go
+2
-1
@@ -3,6 +3,7 @@
3
package squidlog
4
5
import (
6
+ "context"
7
"fmt"
8
"strings"
9
@@ -10,7 +11,7 @@ import (
11
)
12
13
func (c *Collector) createLogReader() error {
13
- c.Cleanup()
14
+ c.Cleanup(context.Background())
15
c.Debug("starting log reader creating")
16
17
reader, err := logs.Open(c.Path, c.ExcludePath, c.Logger)
src/go/plugin/go.d/collector/storcli/collector.go
+5
-4
@@ -5,6 +5,7 @@
5
package storcli
6
7
import (
8
+ "context"
9
_ "embed"
10
"errors"
11
"fmt"
@@ -62,7 +63,7 @@ func (c *Collector) Configuration() any {
63
return c.Config
64
}
65
65
-func (c *Collector) Init() error {
66
+func (c *Collector) Init(context.Context) error {
67
storExec, err := c.initStorCliExec()
68
if err != nil {
69
return fmt.Errorf("storcli exec initialization: %v", err)
@@ -72,7 +73,7 @@ func (c *Collector) Init() error {
73
return nil
74
}
75
75
-func (c *Collector) Check() error {
76
+func (c *Collector) Check(context.Context) error {
77
mx, err := c.collect()
78
if err != nil {
79
return err
@@ -89,7 +90,7 @@ func (c *Collector) Charts() *module.Charts {
90
return c.charts
91
}
92
92
-func (c *Collector) Collect() map[string]int64 {
93
+func (c *Collector) Collect(context.Context) map[string]int64 {
94
mx, err := c.collect()
95
if err != nil {
96
c.Error(err)
@@ -102,4 +103,4 @@ func (c *Collector) Collect() map[string]int64 {
103
return mx
104
}
105
105
-func (c *Collector) Cleanup() {}
106
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/storcli/collector_test.go
+9
-8
@@ -5,6 +5,7 @@
5
package storcli
6
7
import (
8
+ "context"
9
"errors"
10
"os"
11
"testing"
@@ -57,9 +58,9 @@ func TestCollector_Init(t *testing.T) {
58
collr := New()
59
60
if test.wantFail {
60
- assert.Error(t, collr.Init())
61
+ assert.Error(t, collr.Init(context.Background()))
62
} else {
62
- assert.NoError(t, collr.Init())
63
+ assert.NoError(t, collr.Init(context.Background()))
64
}
65
})
66
}
@@ -78,7 +79,7 @@ func TestCollector_Cleanup(t *testing.T) {
79
prepare: func() *Collector {
80
collr := New()
81
collr.exec = prepareMockMegaRaidOK()
81
- _ = collr.Check()
82
+ _ = collr.Check(context.Background())
83
return collr
84
},
85
},
@@ -86,7 +87,7 @@ func TestCollector_Cleanup(t *testing.T) {
87
prepare: func() *Collector {
88
collr := New()
89
collr.exec = prepareMockMegaRaidOK()
89
- _ = collr.Collect()
90
+ _ = collr.Collect(context.Background())
91
return collr
92
},
93
},
@@ -96,7 +97,7 @@ func TestCollector_Cleanup(t *testing.T) {
97
t.Run(name, func(t *testing.T) {
98
collr := test.prepare()
99
99
- assert.NotPanics(t, collr.Cleanup)
100
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
101
})
102
}
103
}
@@ -135,9 +136,9 @@ func TestCollector_Check(t *testing.T) {
136
collr.exec = mock
137
138
if test.wantFail {
138
- assert.Error(t, collr.Check())
139
+ assert.Error(t, collr.Check(context.Background()))
140
} else {
140
- assert.NoError(t, collr.Check())
141
+ assert.NoError(t, collr.Check(context.Background()))
142
}
143
})
144
}
@@ -230,7 +231,7 @@ func TestCollector_Collect(t *testing.T) {
231
mock := test.prepareMock()
232
collr.exec = mock
233
233
- mx := collr.Collect()
234
+ mx := collr.Collect(context.Background())
235
236
assert.Equal(t, test.wantMetrics, mx)
237
src/go/plugin/go.d/collector/supervisord/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package supervisord
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -59,7 +60,7 @@ func (c *Collector) Configuration() any {
60
return c.Config
61
}
62
62
-func (c *Collector) Init() error {
63
+func (c *Collector) Init(context.Context) error {
64
err := c.verifyConfig()
65
if err != nil {
66
return fmt.Errorf("verify config: %v", err)
@@ -74,7 +75,7 @@ func (c *Collector) Init() error {
75
return nil
76
}
77
77
-func (c *Collector) Check() error {
78
+func (c *Collector) Check(context.Context) error {
79
mx, err := c.collect()
80
if err != nil {
81
return err
@@ -89,7 +90,7 @@ func (c *Collector) Charts() *module.Charts {
90
return c.charts
91
}
92
92
-func (c *Collector) Collect() map[string]int64 {
93
+func (c *Collector) Collect(context.Context) map[string]int64 {
94
ms, err := c.collect()
95
if err != nil {
96
c.Error(err)
@@ -101,7 +102,7 @@ func (c *Collector) Collect() map[string]int64 {
102
return ms
103
}
104
104
-func (c *Collector) Cleanup() {
105
+func (c *Collector) Cleanup(context.Context) {
106
if c.client != nil {
107
c.client.closeIdleConnections()
108
}
src/go/plugin/go.d/collector/supervisord/collector_test.go
+15
-14
@@ -3,6 +3,7 @@
3
package supervisord
4
5
import (
6
+ "context"
7
"errors"
8
"os"
9
"testing"
@@ -55,9 +56,9 @@ func TestCollector_Init(t *testing.T) {
56
collr.Config = test.config
57
58
if test.wantFail {
58
- assert.Error(t, collr.Init())
59
+ assert.Error(t, collr.Init(context.Background()))
60
} else {
60
- assert.NoError(t, collr.Init())
61
+ assert.NoError(t, collr.Init(context.Background()))
62
}
63
})
64
}
@@ -83,12 +84,12 @@ func TestCollector_Check(t *testing.T) {
84
for name, test := range tests {
85
t.Run(name, func(t *testing.T) {
86
collr := test.prepare(t)
86
- defer collr.Cleanup()
87
+ defer collr.Cleanup(context.Background())
88
89
if test.wantFail {
89
- assert.Error(t, collr.Check())
90
+ assert.Error(t, collr.Check(context.Background()))
91
} else {
91
- assert.NoError(t, collr.Check())
92
+ assert.NoError(t, collr.Check(context.Background()))
93
}
94
})
95
}
@@ -96,20 +97,20 @@ func TestCollector_Check(t *testing.T) {
97
98
func TestCollector_Charts(t *testing.T) {
99
collr := New()
99
- require.NoError(t, collr.Init())
100
+ require.NoError(t, collr.Init(context.Background()))
101
102
assert.NotNil(t, collr.Charts())
103
}
104
105
func TestCollector_Cleanup(t *testing.T) {
106
collr := New()
106
- assert.NotPanics(t, collr.Cleanup)
107
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
108
108
- require.NoError(t, collr.Init())
109
+ require.NoError(t, collr.Init(context.Background()))
110
m := &mockSupervisorClient{}
111
collr.client = m
112
112
- collr.Cleanup()
113
+ collr.Cleanup(context.Background())
114
115
assert.True(t, m.calledCloseIdleConnections)
116
}
@@ -167,9 +168,9 @@ func TestCollector_Collect(t *testing.T) {
168
for name, test := range tests {
169
t.Run(name, func(t *testing.T) {
170
collr := test.prepare(t)
170
- defer collr.Cleanup()
171
+ defer collr.Cleanup(context.Background())
172
172
- mx := collr.Collect()
173
+ mx := collr.Collect(context.Background())
174
assert.Equal(t, test.wantCollected, mx)
175
if len(test.wantCollected) > 0 {
176
module.TestMetricsHasAllChartsDims(t, collr.Charts(), mx)
@@ -180,21 +181,21 @@ func TestCollector_Collect(t *testing.T) {
181
182
func prepareSupervisordSuccessOnGetAllProcessInfo(t *testing.T) *Collector {
183
collr := New()
183
- require.NoError(t, collr.Init())
184
+ require.NoError(t, collr.Init(context.Background()))
185
collr.client = &mockSupervisorClient{}
186
return collr
187
}
188
189
func prepareSupervisordZeroProcessesOnGetAllProcessInfo(t *testing.T) *Collector {
190
collr := New()
190
- require.NoError(t, collr.Init())
191
+ require.NoError(t, collr.Init(context.Background()))
192
collr.client = &mockSupervisorClient{returnZeroProcesses: true}
193
return collr
194
}
195
196
func prepareSupervisordErrorOnGetAllProcessInfo(t *testing.T) *Collector {
197
collr := New()
197
- require.NoError(t, collr.Init())
198
+ require.NoError(t, collr.Init(context.Background()))
199
collr.client = &mockSupervisorClient{errOnGetAllProcessInfo: true}
200
return collr
201
}
src/go/plugin/go.d/collector/systemdunits/collector.go
+5
-4
@@ -5,6 +5,7 @@
5
package systemdunits
6
7
import (
8
+ "context"
9
_ "embed"
10
"errors"
11
"fmt"
@@ -83,7 +84,7 @@ func (c *Collector) Configuration() any {
84
return c.Config
85
}
86
86
-func (c *Collector) Init() error {
87
+func (c *Collector) Init(context.Context) error {
88
if err := c.validateConfig(); err != nil {
89
return fmt.Errorf("config validation: %v", err)
90
}
@@ -102,7 +103,7 @@ func (c *Collector) Init() error {
103
return nil
104
}
105
105
-func (c *Collector) Check() error {
106
+func (c *Collector) Check(context.Context) error {
107
mx, err := c.collect()
108
if err != nil {
109
return err
@@ -119,7 +120,7 @@ func (c *Collector) Charts() *module.Charts {
120
return c.charts
121
}
122
122
-func (c *Collector) Collect() map[string]int64 {
123
+func (c *Collector) Collect(context.Context) map[string]int64 {
124
mx, err := c.collect()
125
if err != nil {
126
c.Error(err)
@@ -131,6 +132,6 @@ func (c *Collector) Collect() map[string]int64 {
132
return mx
133
}
134
134
-func (c *Collector) Cleanup() {
135
+func (c *Collector) Cleanup(context.Context) {
136
c.closeConnection()
137
}
src/go/plugin/go.d/collector/systemdunits/collector_test.go
+13
-13
@@ -64,9 +64,9 @@ func TestCollector_Init(t *testing.T) {
64
collr.Config = test.config
65
66
if test.wantFail {
67
- assert.Error(t, collr.Init())
67
+ assert.Error(t, collr.Init(context.Background()))
68
} else {
69
- assert.NoError(t, collr.Init())
69
+ assert.NoError(t, collr.Init(context.Background()))
70
}
71
})
72
}
@@ -131,12 +131,12 @@ func TestCollector_Check(t *testing.T) {
131
for name, test := range tests {
132
t.Run(name, func(t *testing.T) {
133
collr := test.prepare()
134
- require.NoError(t, collr.Init())
134
+ require.NoError(t, collr.Init(context.Background()))
135
136
if test.wantFail {
137
- assert.Error(t, collr.Check())
137
+ assert.Error(t, collr.Check(context.Background()))
138
} else {
139
- assert.NoError(t, collr.Check())
139
+ assert.NoError(t, collr.Check(context.Background()))
140
}
141
})
142
}
@@ -144,7 +144,7 @@ func TestCollector_Check(t *testing.T) {
144
145
func TestCollector_Charts(t *testing.T) {
146
collr := New()
147
- require.NoError(t, collr.Init())
147
+ require.NoError(t, collr.Init(context.Background()))
148
assert.NotNil(t, collr.Charts())
149
}
150
@@ -154,10 +154,10 @@ func TestCollector_Cleanup(t *testing.T) {
154
client := prepareOKClient(230)
155
collr.client = client
156
157
- require.NoError(t, collr.Init())
158
- require.NotNil(t, collr.Collect())
157
+ require.NoError(t, collr.Init(context.Background()))
158
+ require.NotNil(t, collr.Collect(context.Background()))
159
conn := collr.conn
160
- collr.Cleanup()
160
+ collr.Cleanup(context.Background())
161
162
assert.Nil(t, collr.conn)
163
v, _ := conn.(*mockConn)
@@ -851,12 +851,12 @@ func TestCollector_Collect(t *testing.T) {
851
for name, test := range tests {
852
t.Run(name, func(t *testing.T) {
853
collr := test.prepare()
854
- require.NoError(t, collr.Init())
854
+ require.NoError(t, collr.Init(context.Background()))
855
856
var mx map[string]int64
857
858
for i := 0; i < 10; i++ {
859
- mx = collr.Collect()
859
+ mx = collr.Collect(context.Background())
860
}
861
862
assert.Equal(t, test.wantCollected, mx)
@@ -872,11 +872,11 @@ func TestCollector_connectionReuse(t *testing.T) {
872
collr.Include = []string{"*"}
873
client := prepareOKClient(230)
874
collr.client = client
875
- require.NoError(t, collr.Init())
875
+ require.NoError(t, collr.Init(context.Background()))
876
877
var collected map[string]int64
878
for i := 0; i < 10; i++ {
879
- collected = collr.Collect()
879
+ collected = collr.Collect(context.Background())
880
}
881
882
assert.NotEmpty(t, collected)
src/go/plugin/go.d/collector/tengine/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package tengine
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -59,7 +60,7 @@ func (c *Collector) Configuration() any {
60
return c.Config
61
}
62
62
-func (c *Collector) Init() error {
63
+func (c *Collector) Init(context.Context) error {
64
if c.URL == "" {
65
return errors.New("config: url not set")
66
}
@@ -76,7 +77,7 @@ func (c *Collector) Init() error {
77
return nil
78
}
79
79
-func (c *Collector) Check() error {
80
+func (c *Collector) Check(context.Context) error {
81
mx, err := c.collect()
82
if err != nil {
83
return err
@@ -93,7 +94,7 @@ func (c *Collector) Charts() *module.Charts {
94
return c.charts
95
}
96
96
-func (c *Collector) Collect() map[string]int64 {
97
+func (c *Collector) Collect(context.Context) map[string]int64 {
98
mx, err := c.collect()
99
if err != nil {
100
c.Error(err)
@@ -107,7 +108,7 @@ func (c *Collector) Collect() map[string]int64 {
108
return mx
109
}
110
110
-func (c *Collector) Cleanup() {
111
+func (c *Collector) Cleanup(context.Context) {
112
if c.httpClient != nil {
113
c.httpClient.CloseIdleConnections()
114
}
src/go/plugin/go.d/collector/tengine/collector_test.go
+14
-13
@@ -3,6 +3,7 @@
3
package tengine
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -36,13 +37,13 @@ func TestCollector_ConfigurationSerialize(t *testing.T) {
37
}
38
39
func TestCollector_Cleanup(t *testing.T) {
39
- New().Cleanup()
40
+ New().Cleanup(context.Background())
41
}
42
43
func TestCollector_Init(t *testing.T) {
44
collr := New()
45
45
- require.NoError(t, collr.Init())
46
+ require.NoError(t, collr.Init(context.Background()))
47
}
48
49
func TestCollector_Check(t *testing.T) {
@@ -55,16 +56,16 @@ func TestCollector_Check(t *testing.T) {
56
57
collr := New()
58
collr.URL = ts.URL
58
- require.NoError(t, collr.Init())
59
- assert.NoError(t, collr.Check())
59
+ require.NoError(t, collr.Init(context.Background()))
60
+ assert.NoError(t, collr.Check(context.Background()))
61
}
62
63
func TestCollector_CheckNG(t *testing.T) {
64
collr := New()
65
66
collr.URL = "http://127.0.0.1:38001/us"
66
- require.NoError(t, collr.Init())
67
- assert.Error(t, collr.Check())
67
+ require.NoError(t, collr.Init(context.Background()))
68
+ assert.Error(t, collr.Check(context.Background()))
69
}
70
71
func TestCollector_Charts(t *testing.T) { assert.NotNil(t, New().Charts()) }
@@ -79,8 +80,8 @@ func TestCollector_Collect(t *testing.T) {
80
81
collr := New()
82
collr.URL = ts.URL
82
- require.NoError(t, collr.Init())
83
- require.NoError(t, collr.Check())
83
+ require.NoError(t, collr.Init(context.Background()))
84
+ require.NoError(t, collr.Check(context.Background()))
85
86
expected := map[string]int64{
87
"bytes_in": 5944,
@@ -114,7 +115,7 @@ func TestCollector_Collect(t *testing.T) {
115
"ups_tries": 268,
116
}
117
117
- assert.Equal(t, expected, collr.Collect())
118
+ assert.Equal(t, expected, collr.Collect(context.Background()))
119
}
120
121
func TestCollector_InvalidData(t *testing.T) {
@@ -127,8 +128,8 @@ func TestCollector_InvalidData(t *testing.T) {
128
129
collr := New()
130
collr.URL = ts.URL
130
- require.NoError(t, collr.Init())
131
- assert.Error(t, collr.Check())
131
+ require.NoError(t, collr.Init(context.Background()))
132
+ assert.Error(t, collr.Check(context.Background()))
133
}
134
135
func TestCollector_404(t *testing.T) {
@@ -141,6 +142,6 @@ func TestCollector_404(t *testing.T) {
142
143
collr := New()
144
collr.URL = ts.URL
144
- require.NoError(t, collr.Init())
145
- assert.Error(t, collr.Check())
145
+ require.NoError(t, collr.Init(context.Background()))
146
+ assert.Error(t, collr.Check(context.Background()))
147
}
src/go/plugin/go.d/collector/testrandom/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package testrandom
4
5
import (
6
+ "context"
7
_ "embed"
8
"fmt"
9
"math/rand"
@@ -72,7 +73,7 @@ func (c *Collector) Configuration() any {
73
return c.Config
74
}
75
75
-func (c *Collector) Init() error {
76
+func (c *Collector) Init(context.Context) error {
77
err := c.validateConfig()
78
if err != nil {
79
return fmt.Errorf("config validation: %v", err)
@@ -86,7 +87,7 @@ func (c *Collector) Init() error {
87
return nil
88
}
89
89
-func (c *Collector) Check() error {
90
+func (c *Collector) Check(context.Context) error {
91
return nil
92
}
93
@@ -94,7 +95,7 @@ func (c *Collector) Charts() *module.Charts {
95
return c.charts
96
}
97
97
-func (c *Collector) Collect() map[string]int64 {
98
+func (c *Collector) Collect(context.Context) map[string]int64 {
99
mx, err := c.collect()
100
if err != nil {
101
c.Error(err)
@@ -106,4 +107,4 @@ func (c *Collector) Collect() map[string]int64 {
107
return mx
108
}
109
109
-func (c *Collector) Cleanup() {}
110
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/testrandom/collector_test.go
+10
-9
@@ -3,6 +3,7 @@
3
package testrandom
4
5
import (
6
+ "context"
7
"os"
8
"testing"
9
@@ -109,9 +110,9 @@ func TestCollector_Init(t *testing.T) {
110
collr.Config = test.config
111
112
if test.wantFail {
112
- assert.Error(t, collr.Init())
113
+ assert.Error(t, collr.Init(context.Background()))
114
} else {
114
- assert.NoError(t, collr.Init())
115
+ assert.NoError(t, collr.Init(context.Background()))
116
}
117
})
118
}
@@ -131,12 +132,12 @@ func TestCollector_Check(t *testing.T) {
132
for name, test := range tests {
133
t.Run(name, func(t *testing.T) {
134
collr := test.prepare()
134
- require.NoError(t, collr.Init())
135
+ require.NoError(t, collr.Init(context.Background()))
136
137
if test.wantFail {
137
- assert.Error(t, collr.Check())
138
+ assert.Error(t, collr.Check(context.Background()))
139
} else {
139
- assert.NoError(t, collr.Check())
140
+ assert.NoError(t, collr.Check(context.Background()))
141
}
142
})
143
}
@@ -156,7 +157,7 @@ func TestCollector_Charts(t *testing.T) {
157
"initialized collector": {
158
prepare: func(t *testing.T) *Collector {
159
collr := New()
159
- require.NoError(t, collr.Init())
160
+ require.NoError(t, collr.Init(context.Background()))
161
return collr
162
},
163
},
@@ -176,7 +177,7 @@ func TestCollector_Charts(t *testing.T) {
177
}
178
179
func TestCollector_Cleanup(t *testing.T) {
179
- assert.NotPanics(t, New().Cleanup)
180
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
181
}
182
183
func TestCollector_Collect(t *testing.T) {
@@ -253,9 +254,9 @@ func TestCollector_Collect(t *testing.T) {
254
for name, test := range tests {
255
t.Run(name, func(t *testing.T) {
256
collr := test.prepare()
256
- require.NoError(t, collr.Init())
257
+ require.NoError(t, collr.Init(context.Background()))
258
258
- mx := collr.Collect()
259
+ mx := collr.Collect(context.Background())
260
261
assert.Equal(t, test.wantCollected, mx)
262
module.TestMetricsHasAllChartsDims(t, collr.Charts(), mx)
src/go/plugin/go.d/collector/tomcat/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package tomcat
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -64,7 +65,7 @@ func (c *Collector) Configuration() any {
65
return c.Config
66
}
67
67
-func (c *Collector) Init() error {
68
+func (c *Collector) Init(context.Context) error {
69
if err := c.validateConfig(); err != nil {
70
return fmt.Errorf("config validation: %v", err)
71
}
@@ -82,7 +83,7 @@ func (c *Collector) Init() error {
83
return nil
84
}
85
85
-func (c *Collector) Check() error {
86
+func (c *Collector) Check(context.Context) error {
87
mx, err := c.collect()
88
if err != nil {
89
return err
@@ -99,7 +100,7 @@ func (c *Collector) Charts() *module.Charts {
100
return c.charts
101
}
102
102
-func (c *Collector) Collect() map[string]int64 {
103
+func (c *Collector) Collect(context.Context) map[string]int64 {
104
mx, err := c.collect()
105
if err != nil {
106
c.Error(err)
@@ -112,7 +113,7 @@ func (c *Collector) Collect() map[string]int64 {
113
return mx
114
}
115
115
-func (c *Collector) Cleanup() {
116
+func (c *Collector) Cleanup(context.Context) {
117
if c.httpClient != nil {
118
c.httpClient.CloseIdleConnections()
119
}
src/go/plugin/go.d/collector/tomcat/collector_test.go
+10
-9
@@ -3,6 +3,7 @@
3
package tomcat
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -61,9 +62,9 @@ func TestCollector_Init(t *testing.T) {
62
collr.Config = test.config
63
64
if test.wantFail {
64
- assert.Error(t, collr.Init())
65
+ assert.Error(t, collr.Init(context.Background()))
66
} else {
66
- assert.NoError(t, collr.Init())
67
+ assert.NoError(t, collr.Init(context.Background()))
68
}
69
})
70
}
@@ -102,9 +103,9 @@ func TestCollector_Check(t *testing.T) {
103
defer cleanup()
104
105
if test.wantFail {
105
- assert.Error(t, collr.Check())
106
+ assert.Error(t, collr.Check(context.Background()))
107
} else {
107
- assert.NoError(t, collr.Check())
108
+ assert.NoError(t, collr.Check(context.Background()))
109
}
110
})
111
}
@@ -181,7 +182,7 @@ func TestCollector_Collect(t *testing.T) {
182
collr, cleanup := test.prepare(t)
183
defer cleanup()
184
184
- mx := collr.Collect()
185
+ mx := collr.Collect(context.Background())
186
187
require.Equal(t, test.wantMetrics, mx)
188
@@ -210,7 +211,7 @@ func prepareCaseSuccess(t *testing.T) (*Collector, func()) {
211
}))
212
collr := New()
213
collr.URL = srv.URL
213
- require.NoError(t, collr.Init())
214
+ require.NoError(t, collr.Init(context.Background()))
215
216
return collr, srv.Close
217
}
@@ -219,7 +220,7 @@ func prepareCaseConnectionRefused(t *testing.T) (*Collector, func()) {
220
t.Helper()
221
collr := New()
222
collr.URL = "http://127.0.0.1:65001"
222
- require.NoError(t, collr.Init())
223
+ require.NoError(t, collr.Init(context.Background()))
224
225
return collr, func() {}
226
}
@@ -252,7 +253,7 @@ func prepareCaseUnexpectedXMLResponse(t *testing.T) (*Collector, func()) {
253
254
collr := New()
255
collr.URL = srv.URL
255
- require.NoError(t, collr.Init())
256
+ require.NoError(t, collr.Init(context.Background()))
257
258
return collr, srv.Close
259
}
@@ -266,7 +267,7 @@ func prepareCaseInvalidFormatResponse(t *testing.T) (*Collector, func()) {
267
268
collr := New()
269
collr.URL = srv.URL
269
- require.NoError(t, collr.Init())
270
+ require.NoError(t, collr.Init(context.Background()))
271
272
return collr, srv.Close
273
}
src/go/plugin/go.d/collector/tor/collect.go
+2
-1
@@ -5,6 +5,7 @@ package tor
5
import (
6
"bufio"
7
"bytes"
8
+ "context"
9
"fmt"
10
"strconv"
11
"strings"
@@ -21,7 +22,7 @@ func (c *Collector) collect() (map[string]int64, error) {
22
23
mx := make(map[string]int64)
24
if err := c.collectServerInfo(mx); err != nil {
24
- c.Cleanup()
25
+ c.Cleanup(context.Background())
26
return nil, err
27
}
28
src/go/plugin/go.d/collector/tor/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package tor
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"time"
@@ -54,7 +55,7 @@ func (c *Collector) Configuration() any {
55
return c.Config
56
}
57
57
-func (c *Collector) Init() error {
58
+func (c *Collector) Init(context.Context) error {
59
if c.Address == "" {
60
return errors.New("config: address not set")
61
}
@@ -62,7 +63,7 @@ func (c *Collector) Init() error {
63
return nil
64
}
65
65
-func (c *Collector) Check() error {
66
+func (c *Collector) Check(context.Context) error {
67
mx, err := c.collect()
68
if err != nil {
69
return err
@@ -79,7 +80,7 @@ func (c *Collector) Charts() *module.Charts {
80
return c.charts
81
}
82
82
-func (c *Collector) Collect() map[string]int64 {
83
+func (c *Collector) Collect(context.Context) map[string]int64 {
84
mx, err := c.collect()
85
if err != nil {
86
c.Error(err)
@@ -92,7 +93,7 @@ func (c *Collector) Collect() map[string]int64 {
93
return mx
94
}
95
95
-func (c *Collector) Cleanup() {
96
+func (c *Collector) Cleanup(context.Context) {
97
if c.conn != nil {
98
c.conn.disconnect()
99
c.conn = nil
src/go/plugin/go.d/collector/tor/collector_test.go
+10
-9
@@ -4,6 +4,7 @@ package tor
4
5
import (
6
"bufio"
7
+ "context"
8
"errors"
9
"fmt"
10
"io"
@@ -62,9 +63,9 @@ func TestCollector_Init(t *testing.T) {
63
collr.Config = test.config
64
65
if test.wantFail {
65
- assert.Error(t, collr.Init())
66
+ assert.Error(t, collr.Init(context.Background()))
67
} else {
67
- assert.NoError(t, collr.Init())
68
+ assert.NoError(t, collr.Init(context.Background()))
69
}
70
})
71
}
@@ -105,15 +106,15 @@ func TestCollector_Check(t *testing.T) {
106
t.Errorf("mock tor daemon start timed out")
107
}
108
108
- require.NoError(t, collr.Init())
109
+ require.NoError(t, collr.Init(context.Background()))
110
111
if test.wantFail {
111
- assert.Error(t, collr.Check())
112
+ assert.Error(t, collr.Check(context.Background()))
113
} else {
113
- assert.NoError(t, collr.Check())
114
+ assert.NoError(t, collr.Check(context.Background()))
115
}
116
116
- collr.Cleanup()
117
+ collr.Cleanup(context.Background())
118
119
select {
120
case <-daemon.stopped:
@@ -162,9 +163,9 @@ func TestCollector_Collect(t *testing.T) {
163
t.Errorf("mock tor daemon start timed out")
164
}
165
165
- require.NoError(t, collr.Init())
166
+ require.NoError(t, collr.Init(context.Background()))
167
167
- mx := collr.Collect()
168
+ mx := collr.Collect(context.Background())
169
170
require.Equal(t, test.wantMetrics, mx)
171
@@ -174,7 +175,7 @@ func TestCollector_Collect(t *testing.T) {
175
module.TestMetricsHasAllChartsDims(t, collr.Charts(), mx)
176
}
177
177
- collr.Cleanup()
178
+ collr.Cleanup(context.Background())
179
180
select {
181
case <-daemon.stopped:
src/go/plugin/go.d/collector/traefik/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package traefik
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -84,7 +85,7 @@ func (c *Collector) Configuration() any {
85
return c.Config
86
}
87
87
-func (c *Collector) Init() error {
88
+func (c *Collector) Init(context.Context) error {
89
if err := c.validateConfig(); err != nil {
90
return fmt.Errorf("config validation: %v", err)
91
}
@@ -98,7 +99,7 @@ func (c *Collector) Init() error {
99
return nil
100
}
101
101
-func (c *Collector) Check() error {
102
+func (c *Collector) Check(context.Context) error {
103
mx, err := c.collect()
104
if err != nil {
105
return err
@@ -113,7 +114,7 @@ func (c *Collector) Charts() *module.Charts {
114
return c.charts
115
}
116
116
-func (c *Collector) Collect() map[string]int64 {
117
+func (c *Collector) Collect(context.Context) map[string]int64 {
118
mx, err := c.collect()
119
if err != nil {
120
c.Error(err)
@@ -126,4 +127,4 @@ func (c *Collector) Collect() map[string]int64 {
127
return mx
128
}
129
129
-func (c *Collector) Cleanup() {}
130
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/traefik/collector_test.go
+12
-11
@@ -3,6 +3,7 @@
3
package traefik
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -68,9 +69,9 @@ func TestCollector_Init(t *testing.T) {
69
collr.Config = test.config
70
71
if test.wantFail {
71
- assert.Error(t, collr.Init())
72
+ assert.Error(t, collr.Init(context.Background()))
73
} else {
73
- assert.NoError(t, collr.Init())
74
+ assert.NoError(t, collr.Init(context.Background()))
75
}
76
})
77
}
@@ -81,7 +82,7 @@ func TestCollector_Charts(t *testing.T) {
82
}
83
84
func TestCollector_Cleanup(t *testing.T) {
84
- assert.NotPanics(t, New().Cleanup)
85
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
86
}
87
88
func TestCollector_Check(t *testing.T) {
@@ -113,9 +114,9 @@ func TestCollector_Check(t *testing.T) {
114
defer cleanup()
115
116
if test.wantFail {
116
- assert.Error(t, collr.Check())
117
+ assert.Error(t, collr.Check(context.Background()))
118
} else {
118
- assert.NoError(t, collr.Check())
119
+ assert.NoError(t, collr.Check(context.Background()))
120
}
121
})
122
}
@@ -243,7 +244,7 @@ func TestCollector_Collect(t *testing.T) {
244
245
var mx map[string]int64
246
for _, want := range test.wantCollected {
246
- mx = collr.Collect()
247
+ mx = collr.Collect(context.Background())
248
assert.Equal(t, want, mx)
249
}
250
if len(test.wantCollected) > 0 {
@@ -261,7 +262,7 @@ func prepareCaseTraefikV221Metrics(t *testing.T) (*Collector, func()) {
262
}))
263
collr := New()
264
collr.URL = srv.URL
264
- require.NoError(t, collr.Init())
265
+ require.NoError(t, collr.Init(context.Background()))
266
267
return collr, srv.Close
268
}
@@ -298,7 +299,7 @@ traefik_entrypoint_request_duration_seconds_count{code="300",entrypoint="web",me
299
}))
300
collr := New()
301
collr.URL = srv.URL
301
- require.NoError(t, collr.Init())
302
+ require.NoError(t, collr.Init(context.Background()))
303
304
return collr, srv.Close
305
}
@@ -326,7 +327,7 @@ application_backend_http_responses_total{proxy="infra-vernemq-ws",code="other"}
327
}))
328
collr := New()
329
collr.URL = srv.URL
329
- require.NoError(t, collr.Init())
330
+ require.NoError(t, collr.Init(context.Background()))
331
332
return collr, srv.Close
333
}
@@ -339,7 +340,7 @@ func prepareCase404Response(t *testing.T) (*Collector, func()) {
340
}))
341
collr := New()
342
collr.URL = srv.URL
342
- require.NoError(t, collr.Init())
343
+ require.NoError(t, collr.Init(context.Background()))
344
345
return collr, srv.Close
346
}
@@ -348,7 +349,7 @@ func prepareCaseConnectionRefused(t *testing.T) (*Collector, func()) {
349
t.Helper()
350
collr := New()
351
collr.URL = "http://127.0.0.1:38001"
351
- require.NoError(t, collr.Init())
352
+ require.NoError(t, collr.Init(context.Background()))
353
354
return collr, func() {}
355
}
src/go/plugin/go.d/collector/typesense/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package typesense
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -65,7 +66,7 @@ func (c *Collector) Configuration() any {
66
return c.Config
67
}
68
68
-func (c *Collector) Init() error {
69
+func (c *Collector) Init(context.Context) error {
70
if c.URL == "" {
71
return errors.New("configL: url not configured")
72
}
@@ -86,7 +87,7 @@ func (c *Collector) Init() error {
87
return nil
88
}
89
89
-func (c *Collector) Check() error {
90
+func (c *Collector) Check(context.Context) error {
91
mx, err := c.collect()
92
if err != nil {
93
return err
@@ -102,7 +103,7 @@ func (c *Collector) Charts() *module.Charts {
103
return c.charts
104
}
105
105
-func (c *Collector) Collect() map[string]int64 {
106
+func (c *Collector) Collect(context.Context) map[string]int64 {
107
mx, err := c.collect()
108
if err != nil {
109
c.Error(err)
@@ -114,7 +115,7 @@ func (c *Collector) Collect() map[string]int64 {
115
return mx
116
}
117
117
-func (c *Collector) Cleanup() {
118
+func (c *Collector) Cleanup(context.Context) {
119
if c.httpClient != nil {
120
c.httpClient.CloseIdleConnections()
121
}
src/go/plugin/go.d/collector/typesense/collector_test.go
+13
-12
@@ -3,6 +3,7 @@
3
package typesense
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -68,9 +69,9 @@ func TestCollector_Init(t *testing.T) {
69
collr.Config = test.config
70
71
if test.wantFail {
71
- assert.Error(t, collr.Init())
72
+ assert.Error(t, collr.Init(context.Background()))
73
} else {
73
- assert.NoError(t, collr.Init())
74
+ assert.NoError(t, collr.Init(context.Background()))
75
}
76
})
77
}
@@ -113,9 +114,9 @@ func TestCollector_Check(t *testing.T) {
114
defer cleanup()
115
116
if test.wantFail {
116
- assert.Error(t, collr.Check())
117
+ assert.Error(t, collr.Check(context.Background()))
118
} else {
118
- assert.NoError(t, collr.Check())
119
+ assert.NoError(t, collr.Check(context.Background()))
120
}
121
})
122
}
@@ -183,9 +184,9 @@ func TestCollector_Collect(t *testing.T) {
184
collr, cleanup := test.prepare(t)
185
defer cleanup()
186
186
- _ = collr.Check()
187
+ _ = collr.Check(context.Background())
188
188
- mx := collr.Collect()
189
+ mx := collr.Collect(context.Background())
190
191
require.Equal(t, test.wantMetrics, mx)
192
@@ -220,7 +221,7 @@ func caseOk(t *testing.T) (*Collector, func()) {
221
collr := New()
222
collr.URL = srv.URL
223
collr.APIKey = testApiKey
223
- require.NoError(t, collr.Init())
224
+ require.NoError(t, collr.Init(context.Background()))
225
226
return collr, srv.Close
227
}
@@ -247,7 +248,7 @@ func caseOkNoApiKey(t *testing.T) (*Collector, func()) {
248
collr := New()
249
collr.URL = srv.URL
250
collr.APIKey = ""
250
- require.NoError(t, collr.Init())
251
+ require.NoError(t, collr.Init(context.Background()))
252
253
return collr, srv.Close
254
}
@@ -277,7 +278,7 @@ func caseUnexpectedJsonResponse(t *testing.T) (*Collector, func()) {
278
}))
279
collr := New()
280
collr.URL = srv.URL
280
- require.NoError(t, collr.Init())
281
+ require.NoError(t, collr.Init(context.Background()))
282
283
return collr, srv.Close
284
}
@@ -290,7 +291,7 @@ func caseInvalidDataResponse(t *testing.T) (*Collector, func()) {
291
}))
292
collr := New()
293
collr.URL = srv.URL
293
- require.NoError(t, collr.Init())
294
+ require.NoError(t, collr.Init(context.Background()))
295
296
return collr, srv.Close
297
}
@@ -299,7 +300,7 @@ func caseConnectionRefused(t *testing.T) (*Collector, func()) {
300
t.Helper()
301
collr := New()
302
collr.URL = "http://127.0.0.1:65001"
302
- require.NoError(t, collr.Init())
303
+ require.NoError(t, collr.Init(context.Background()))
304
305
return collr, func() {}
306
}
@@ -312,7 +313,7 @@ func case404(t *testing.T) (*Collector, func()) {
313
}))
314
collr := New()
315
collr.URL = srv.URL
315
- require.NoError(t, collr.Init())
316
+ require.NoError(t, collr.Init(context.Background()))
317
318
return collr, srv.Close
319
}
src/go/plugin/go.d/collector/unbound/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package unbound
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -72,7 +73,7 @@ func (c *Collector) Configuration() any {
73
return c.Config
74
}
75
75
-func (c *Collector) Init() error {
76
+func (c *Collector) Init(context.Context) error {
77
if enabled := c.initConfig(); !enabled {
78
return errors.New("remote control is disabled in the configuration file")
79
}
@@ -91,7 +92,7 @@ func (c *Collector) Init() error {
92
return nil
93
}
94
94
-func (c *Collector) Check() error {
95
+func (c *Collector) Check(context.Context) error {
96
mx, err := c.collect()
97
if err != nil {
98
return err
@@ -106,7 +107,7 @@ func (c *Collector) Charts() *module.Charts {
107
return c.charts
108
}
109
109
-func (c *Collector) Collect() map[string]int64 {
110
+func (c *Collector) Collect(context.Context) map[string]int64 {
111
mx, err := c.collect()
112
if err != nil {
113
c.Error(err)
@@ -118,7 +119,7 @@ func (c *Collector) Collect() map[string]int64 {
119
return mx
120
}
121
121
-func (c *Collector) Cleanup() {
122
+func (c *Collector) Cleanup(context.Context) {
123
if c.client != nil {
124
_ = c.client.Disconnect()
125
}
src/go/plugin/go.d/collector/unbound/collector_test.go
+28
-27
@@ -5,6 +5,7 @@ package unbound
5
import (
6
"bufio"
7
"bytes"
8
+ "context"
9
"errors"
10
"fmt"
11
"os"
@@ -57,7 +58,7 @@ func TestCollector_ConfigurationSerialize(t *testing.T) {
58
func TestCollector_Init(t *testing.T) {
59
collr := prepareNonTLSUnbound()
60
60
- assert.NoError(t, collr.Init())
61
+ assert.NoError(t, collr.Init(context.Background()))
62
}
63
64
func TestCollector_Init_SetEverythingFromUnboundConf(t *testing.T) {
@@ -76,7 +77,7 @@ func TestCollector_Init_SetEverythingFromUnboundConf(t *testing.T) {
77
},
78
}
79
79
- assert.NoError(t, collr.Init())
80
+ assert.NoError(t, collr.Init(context.Background()))
81
assert.Equal(t, expectedConfig, collr.Config)
82
}
83
@@ -84,66 +85,66 @@ func TestCollector_Init_DisabledInUnboundConf(t *testing.T) {
85
collr := prepareNonTLSUnbound()
86
collr.ConfPath = "testdata/unbound_disabled.conf"
87
87
- assert.Error(t, collr.Init())
88
+ assert.Error(t, collr.Init(context.Background()))
89
}
90
91
func TestCollector_Init_HandleEmptyConfig(t *testing.T) {
92
collr := prepareNonTLSUnbound()
93
collr.ConfPath = "testdata/unbound_empty.conf"
94
94
- assert.NoError(t, collr.Init())
95
+ assert.NoError(t, collr.Init(context.Background()))
96
}
97
98
func TestCollector_Init_HandleNonExistentConfig(t *testing.T) {
99
collr := prepareNonTLSUnbound()
100
collr.ConfPath = "testdata/unbound_non_existent.conf"
101
101
- assert.NoError(t, collr.Init())
102
+ assert.NoError(t, collr.Init(context.Background()))
103
}
104
105
func TestCollector_Check(t *testing.T) {
106
collr := prepareNonTLSUnbound()
106
- require.NoError(t, collr.Init())
107
+ require.NoError(t, collr.Init(context.Background()))
108
collr.client = mockUnboundClient{data: dataCommonStats, err: false}
109
109
- assert.NoError(t, collr.Check())
110
+ assert.NoError(t, collr.Check(context.Background()))
111
}
112
113
func TestCollector_Check_ErrorDuringScrapingUnbound(t *testing.T) {
114
collr := prepareNonTLSUnbound()
114
- require.NoError(t, collr.Init())
115
+ require.NoError(t, collr.Init(context.Background()))
116
collr.client = mockUnboundClient{err: true}
117
117
- assert.Error(t, collr.Check())
118
+ assert.Error(t, collr.Check(context.Background()))
119
}
120
121
func TestCollector_Cleanup(t *testing.T) {
121
- New().Cleanup()
122
+ New().Cleanup(context.Background())
123
}
124
125
func TestCollector_Charts(t *testing.T) {
126
collr := prepareNonTLSUnbound()
126
- require.NoError(t, collr.Init())
127
+ require.NoError(t, collr.Init(context.Background()))
128
129
assert.NotNil(t, collr.Charts())
130
}
131
132
func TestCollector_Collect(t *testing.T) {
133
collr := prepareNonTLSUnbound()
133
- require.NoError(t, collr.Init())
134
+ require.NoError(t, collr.Init(context.Background()))
135
collr.client = mockUnboundClient{data: dataCommonStats, err: false}
136
136
- mx := collr.Collect()
137
+ mx := collr.Collect(context.Background())
138
assert.Equal(t, expectedCommon, mx)
139
testCharts(t, collr, mx)
140
}
141
142
func TestCollector_Collect_ExtendedStats(t *testing.T) {
143
collr := prepareNonTLSUnbound()
143
- require.NoError(t, collr.Init())
144
+ require.NoError(t, collr.Init(context.Background()))
145
collr.client = mockUnboundClient{data: dataExtendedStats, err: false}
146
146
- mx := collr.Collect()
147
+ mx := collr.Collect(context.Background())
148
assert.Equal(t, expectedExtended, mx)
149
testCharts(t, collr, mx)
150
}
@@ -160,7 +161,7 @@ func TestCollector_Collect_LifeCycleCumulativeExtendedStats(t *testing.T) {
161
162
collr := prepareNonTLSUnbound()
163
collr.Cumulative = true
163
- require.NoError(t, collr.Init())
164
+ require.NoError(t, collr.Init(context.Background()))
165
ubClient := &mockUnboundClient{err: false}
166
collr.client = ubClient
167
@@ -168,7 +169,7 @@ func TestCollector_Collect_LifeCycleCumulativeExtendedStats(t *testing.T) {
169
for i, test := range tests {
170
t.Run(fmt.Sprintf("run %d", i+1), func(t *testing.T) {
171
ubClient.data = test.input
171
- mx = collr.Collect()
172
+ mx = collr.Collect(context.Background())
173
assert.Equal(t, test.expected, mx)
174
})
175
}
@@ -188,7 +189,7 @@ func TestCollector_Collect_LifeCycleResetExtendedStats(t *testing.T) {
189
190
collr := prepareNonTLSUnbound()
191
collr.Cumulative = false
191
- require.NoError(t, collr.Init())
192
+ require.NoError(t, collr.Init(context.Background()))
193
ubClient := &mockUnboundClient{err: false}
194
collr.client = ubClient
195
@@ -196,7 +197,7 @@ func TestCollector_Collect_LifeCycleResetExtendedStats(t *testing.T) {
197
for i, test := range tests {
198
t.Run(fmt.Sprintf("run %d", i+1), func(t *testing.T) {
199
ubClient.data = test.input
199
- mx = collr.Collect()
200
+ mx = collr.Collect(context.Background())
201
assert.Equal(t, test.expected, mx)
202
})
203
}
@@ -206,35 +207,35 @@ func TestCollector_Collect_LifeCycleResetExtendedStats(t *testing.T) {
207
208
func TestCollector_Collect_EmptyResponse(t *testing.T) {
209
collr := prepareNonTLSUnbound()
209
- require.NoError(t, collr.Init())
210
+ require.NoError(t, collr.Init(context.Background()))
211
collr.client = mockUnboundClient{data: []byte{}, err: false}
212
212
- assert.Nil(t, collr.Collect())
213
+ assert.Nil(t, collr.Collect(context.Background()))
214
}
215
216
func TestCollector_Collect_ErrorResponse(t *testing.T) {
217
collr := prepareNonTLSUnbound()
217
- require.NoError(t, collr.Init())
218
+ require.NoError(t, collr.Init(context.Background()))
219
collr.client = mockUnboundClient{data: []byte("error unknown command 'unknown'"), err: false}
220
220
- assert.Nil(t, collr.Collect())
221
+ assert.Nil(t, collr.Collect(context.Background()))
222
}
223
224
func TestCollector_Collect_ErrorOnSend(t *testing.T) {
225
collr := prepareNonTLSUnbound()
225
- require.NoError(t, collr.Init())
226
+ require.NoError(t, collr.Init(context.Background()))
227
collr.client = mockUnboundClient{err: true}
228
228
- assert.Nil(t, collr.Collect())
229
+ assert.Nil(t, collr.Collect(context.Background()))
230
}
231
232
func TestCollector_Collect_ErrorOnParseBadSyntax(t *testing.T) {
233
collr := prepareNonTLSUnbound()
233
- require.NoError(t, collr.Init())
234
+ require.NoError(t, collr.Init(context.Background()))
235
data := strings.Repeat("zk_avg_latency 0\nzk_min_latency 0\nzk_mix_latency 0\n", 10)
236
collr.client = mockUnboundClient{data: []byte(data), err: false}
237
237
- assert.Nil(t, collr.Collect())
238
+ assert.Nil(t, collr.Collect(context.Background()))
239
}
240
241
func prepareNonTLSUnbound() *Collector {
src/go/plugin/go.d/collector/upsd/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package upsd
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"time"
@@ -58,7 +59,7 @@ func (c *Collector) Configuration() any {
59
return c.Config
60
}
61
61
-func (c *Collector) Init() error {
62
+func (c *Collector) Init(context.Context) error {
63
if c.Address == "" {
64
return errors.New("config: 'address' not set")
65
}
@@ -66,7 +67,7 @@ func (c *Collector) Init() error {
67
return nil
68
}
69
69
-func (c *Collector) Check() error {
70
+func (c *Collector) Check(context.Context) error {
71
mx, err := c.collect()
72
if err != nil {
73
return err
@@ -81,7 +82,7 @@ func (c *Collector) Charts() *module.Charts {
82
return c.charts
83
}
84
84
-func (c *Collector) Collect() map[string]int64 {
85
+func (c *Collector) Collect(context.Context) map[string]int64 {
86
mx, err := c.collect()
87
if err != nil {
88
c.Error(err)
@@ -93,7 +94,7 @@ func (c *Collector) Collect() map[string]int64 {
94
return mx
95
}
96
96
-func (c *Collector) Cleanup() {
97
+func (c *Collector) Cleanup(context.Context) {
98
if c.conn == nil {
99
return
100
}
src/go/plugin/go.d/collector/upsd/collector_test.go
+15
-14
@@ -3,6 +3,7 @@
3
package upsd
4
5
import (
6
+ "context"
7
"errors"
8
"fmt"
9
"os"
@@ -33,16 +34,16 @@ func TestCollector_ConfigurationSerialize(t *testing.T) {
34
}
35
36
func TestCollector_Cleanup(t *testing.T) {
36
- upcollrd := New()
37
+ collr := New()
38
38
- require.NotPanics(t, upcollrd.Cleanup)
39
+ require.NotPanics(t, func() { collr.Cleanup(context.Background()) })
40
41
mock := prepareMockConnOK()
41
- upcollrd.newUpsdConn = func(Config) upsdConn { return mock }
42
+ collr.newUpsdConn = func(Config) upsdConn { return mock }
43
43
- require.NoError(t, upcollrd.Init())
44
- _ = upcollrd.Collect()
45
- require.NotPanics(t, upcollrd.Cleanup)
44
+ require.NoError(t, collr.Init(context.Background()))
45
+ _ = collr.Collect(context.Background())
46
+ require.NotPanics(t, func() { collr.Cleanup(context.Background()) })
47
assert.True(t, mock.calledDisconnect)
48
}
49
@@ -67,9 +68,9 @@ func TestCollector_Init(t *testing.T) {
68
collr.Config = test.config
69
70
if test.wantFail {
70
- assert.Error(t, collr.Init())
71
+ assert.Error(t, collr.Init(context.Background()))
72
} else {
72
- assert.NoError(t, collr.Init())
73
+ assert.NoError(t, collr.Init(context.Background()))
74
}
75
})
76
}
@@ -113,12 +114,12 @@ func TestCollector_Check(t *testing.T) {
114
collr := test.prepareUpsd()
115
collr.newUpsdConn = func(Config) upsdConn { return test.prepareMock() }
116
116
- require.NoError(t, collr.Init())
117
+ require.NoError(t, collr.Init(context.Background()))
118
119
if test.wantFail {
119
- assert.Error(t, collr.Check())
120
+ assert.Error(t, collr.Check(context.Background()))
121
} else {
121
- assert.NoError(t, collr.Check())
122
+ assert.NoError(t, collr.Check(context.Background()))
123
}
124
})
125
}
@@ -126,7 +127,7 @@ func TestCollector_Check(t *testing.T) {
127
128
func TestCollector_Charts(t *testing.T) {
129
collr := New()
129
- require.NoError(t, collr.Init())
130
+ require.NoError(t, collr.Init(context.Background()))
131
assert.NotNil(t, collr.Charts())
132
}
133
@@ -246,12 +247,12 @@ func TestCollector_Collect(t *testing.T) {
247
for name, test := range tests {
248
t.Run(name, func(t *testing.T) {
249
collr := test.prepareUpsd()
249
- require.NoError(t, collr.Init())
250
+ require.NoError(t, collr.Init(context.Background()))
251
252
mock := test.prepareMock()
253
collr.newUpsdConn = func(Config) upsdConn { return mock }
254
254
- mx := collr.Collect()
255
+ mx := collr.Collect(context.Background())
256
257
assert.Equal(t, test.wantCollected, mx)
258
assert.Equalf(t, test.wantCharts, len(*collr.Charts()), "number of charts")
src/go/plugin/go.d/collector/uwsgi/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package uwsgi
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"time"
@@ -54,7 +55,7 @@ func (c *Collector) Configuration() any {
55
return c.Config
56
}
57
57
-func (c *Collector) Init() error {
58
+func (c *Collector) Init(context.Context) error {
59
if c.Address == "" {
60
return errors.New("config: 'address' not set")
61
}
@@ -64,7 +65,7 @@ func (c *Collector) Init() error {
65
return nil
66
}
67
67
-func (c *Collector) Check() error {
68
+func (c *Collector) Check(context.Context) error {
69
mx, err := c.collect()
70
if err != nil {
71
return err
@@ -81,7 +82,7 @@ func (c *Collector) Charts() *module.Charts {
82
return c.charts
83
}
84
84
-func (c *Collector) Collect() map[string]int64 {
85
+func (c *Collector) Collect(context.Context) map[string]int64 {
86
mx, err := c.collect()
87
if err != nil {
88
c.Error(err)
@@ -94,4 +95,4 @@ func (c *Collector) Collect() map[string]int64 {
95
return mx
96
}
97
97
-func (c *Collector) Cleanup() {}
98
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/uwsgi/collector_test.go
+9
-8
@@ -3,6 +3,7 @@
3
package uwsgi
4
5
import (
6
+ "context"
7
"errors"
8
"os"
9
"testing"
@@ -61,9 +62,9 @@ func TestCollector_Init(t *testing.T) {
62
collr.Config = test.config
63
64
if test.wantFail {
64
- assert.Error(t, collr.Init())
65
+ assert.Error(t, collr.Init(context.Background()))
66
} else {
66
- assert.NoError(t, collr.Init())
67
+ assert.NoError(t, collr.Init(context.Background()))
68
}
69
})
70
}
@@ -82,7 +83,7 @@ func TestCollector_Cleanup(t *testing.T) {
83
prepare: func() *Collector {
84
collr := New()
85
collr.conn = prepareMockOk()
85
- _ = collr.Check()
86
+ _ = collr.Check(context.Background())
87
return collr
88
},
89
},
@@ -90,7 +91,7 @@ func TestCollector_Cleanup(t *testing.T) {
91
prepare: func() *Collector {
92
collr := New()
93
collr.conn = prepareMockOk()
93
- _ = collr.Collect()
94
+ _ = collr.Collect(context.Background())
95
return collr
96
},
97
},
@@ -100,7 +101,7 @@ func TestCollector_Cleanup(t *testing.T) {
101
t.Run(name, func(t *testing.T) {
102
collr := test.prepare()
103
103
- assert.NotPanics(t, collr.Cleanup)
104
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
105
})
106
}
107
}
@@ -139,9 +140,9 @@ func TestCollector_Check(t *testing.T) {
140
collr.conn = mock
141
142
if test.wantFail {
142
- assert.Error(t, collr.Check())
143
+ assert.Error(t, collr.Check(context.Background()))
144
} else {
144
- assert.NoError(t, collr.Check())
145
+ assert.NoError(t, collr.Check(context.Background()))
146
}
147
})
148
}
@@ -239,7 +240,7 @@ func TestCollector_Collect(t *testing.T) {
240
mock := test.prepareMock()
241
collr.conn = mock
242
242
- mx := collr.Collect()
243
+ mx := collr.Collect(context.Background())
244
245
require.Equal(t, test.wantMetrics, mx)
246
src/go/plugin/go.d/collector/varnish/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package varnish
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -62,7 +63,7 @@ func (c *Collector) Configuration() any {
63
return c.Config
64
}
65
65
-func (c *Collector) Init() error {
66
+func (c *Collector) Init(context.Context) error {
67
vs, err := c.initVarnishstatBinary()
68
if err != nil {
69
return fmt.Errorf("init varnishstat exec: %v", err)
@@ -72,7 +73,7 @@ func (c *Collector) Init() error {
73
return nil
74
}
75
75
-func (c *Collector) Check() error {
76
+func (c *Collector) Check(context.Context) error {
77
mx, err := c.collect()
78
if err != nil {
79
return err
@@ -89,7 +90,7 @@ func (c *Collector) Charts() *module.Charts {
90
return c.charts
91
}
92
92
-func (c *Collector) Collect() map[string]int64 {
93
+func (c *Collector) Collect(context.Context) map[string]int64 {
94
mx, err := c.collect()
95
if err != nil {
96
c.Error(err)
@@ -102,4 +103,4 @@ func (c *Collector) Collect() map[string]int64 {
103
return mx
104
}
105
105
-func (c *Collector) Cleanup() {}
106
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/varnish/collector_test.go
+9
-8
@@ -3,6 +3,7 @@
3
package varnish
4
5
import (
6
+ "context"
7
"errors"
8
"os"
9
"testing"
@@ -51,9 +52,9 @@ func TestCollector_Init(t *testing.T) {
52
collr.Config = test.config
53
54
if test.wantFail {
54
- assert.Error(t, collr.Init())
55
+ assert.Error(t, collr.Init(context.Background()))
56
} else {
56
- assert.NoError(t, collr.Init())
57
+ assert.NoError(t, collr.Init(context.Background()))
58
}
59
})
60
}
@@ -72,7 +73,7 @@ func TestCollector_Cleanup(t *testing.T) {
73
prepare: func() *Collector {
74
collr := New()
75
collr.exec = prepareMockOkVer71()
75
- _ = collr.Check()
76
+ _ = collr.Check(context.Background())
77
return collr
78
},
79
},
@@ -80,7 +81,7 @@ func TestCollector_Cleanup(t *testing.T) {
81
prepare: func() *Collector {
82
collr := New()
83
collr.exec = prepareMockOkVer71()
83
- _ = collr.Collect()
84
+ _ = collr.Collect(context.Background())
85
return collr
86
},
87
},
@@ -90,7 +91,7 @@ func TestCollector_Cleanup(t *testing.T) {
91
t.Run(name, func(t *testing.T) {
92
collr := test.prepare()
93
93
- assert.NotPanics(t, collr.Cleanup)
94
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
95
})
96
}
97
}
@@ -124,9 +125,9 @@ func TestCollector_Check(t *testing.T) {
125
collr.exec = test.prepareMock()
126
127
if test.wantFail {
127
- assert.Error(t, collr.Check())
128
+ assert.Error(t, collr.Check(context.Background()))
129
} else {
129
- assert.NoError(t, collr.Check())
130
+ assert.NoError(t, collr.Check(context.Background()))
131
}
132
})
133
}
@@ -206,7 +207,7 @@ func TestCollector_Collect(t *testing.T) {
207
collr := New()
208
collr.exec = test.prepareMock()
209
209
- mx := collr.Collect()
210
+ mx := collr.Collect(context.Background())
211
212
assert.Equal(t, test.wantMetrics, mx)
213
src/go/plugin/go.d/collector/vcsa/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package vcsa
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -74,7 +75,7 @@ func (c *Collector) Configuration() any {
75
return c.Config
76
}
77
77
-func (c *Collector) Init() error {
78
+func (c *Collector) Init(context.Context) error {
79
if err := c.validateConfig(); err != nil {
80
return fmt.Errorf("invalid config: %v", err)
81
}
@@ -91,7 +92,7 @@ func (c *Collector) Init() error {
92
return nil
93
}
94
94
-func (c *Collector) Check() error {
95
+func (c *Collector) Check(context.Context) error {
96
err := c.client.Login()
97
if err != nil {
98
return err
@@ -113,7 +114,7 @@ func (c *Collector) Charts() *module.Charts {
114
return c.charts
115
}
116
116
-func (c *Collector) Collect() map[string]int64 {
117
+func (c *Collector) Collect(context.Context) map[string]int64 {
118
mx, err := c.collect()
119
if err != nil {
120
c.Error(err)
@@ -125,7 +126,7 @@ func (c *Collector) Collect() map[string]int64 {
126
return mx
127
}
128
128
-func (c *Collector) Cleanup() {
129
+func (c *Collector) Cleanup(context.Context) {
130
if c.client == nil {
131
return
132
}
src/go/plugin/go.d/collector/vcsa/collector_test.go
+21
-20
@@ -3,6 +3,7 @@
3
package vcsa
4
5
import (
6
+ "context"
7
"errors"
8
"os"
9
"testing"
@@ -34,57 +35,57 @@ func TestCollector_ConfigurationSerialize(t *testing.T) {
35
func TestCollector_Init(t *testing.T) {
36
collr := prepareVCSA()
37
37
- assert.NoError(t, collr.Init())
38
+ assert.NoError(t, collr.Init(context.Background()))
39
assert.NotNil(t, collr.client)
40
}
41
42
func TestCollector_InitErrorOnValidatingInitParameters(t *testing.T) {
43
collr := New()
44
44
- assert.Error(t, collr.Init())
45
+ assert.Error(t, collr.Init(context.Background()))
46
}
47
48
func TestCollector_InitErrorOnCreatingClient(t *testing.T) {
49
collr := prepareVCSA()
50
collr.ClientConfig.TLSConfig.TLSCA = "testdata/tls"
51
51
- assert.Error(t, collr.Init())
52
+ assert.Error(t, collr.Init(context.Background()))
53
}
54
55
func TestCollector_Check(t *testing.T) {
56
collr := prepareVCSA()
56
- require.NoError(t, collr.Init())
57
+ require.NoError(t, collr.Init(context.Background()))
58
collr.client = &mockVCenterHealthClient{}
59
59
- assert.NoError(t, collr.Check())
60
+ assert.NoError(t, collr.Check(context.Background()))
61
}
62
63
func TestCollector_CheckErrorOnLogin(t *testing.T) {
64
collr := prepareVCSA()
64
- require.NoError(t, collr.Init())
65
+ require.NoError(t, collr.Init(context.Background()))
66
collr.client = &mockVCenterHealthClient{
67
login: func() error { return errors.New("login mock error") },
68
}
69
69
- assert.Error(t, collr.Check())
70
+ assert.Error(t, collr.Check(context.Background()))
71
}
72
73
func TestCollector_CheckEnsureLoggedIn(t *testing.T) {
74
collr := prepareVCSA()
74
- require.NoError(t, collr.Init())
75
+ require.NoError(t, collr.Init(context.Background()))
76
mock := &mockVCenterHealthClient{}
77
collr.client = mock
78
78
- assert.NoError(t, collr.Check())
79
+ assert.NoError(t, collr.Check(context.Background()))
80
assert.True(t, mock.loginCalls == 1)
81
}
82
83
func TestCollector_Cleanup(t *testing.T) {
84
collr := prepareVCSA()
84
- require.NoError(t, collr.Init())
85
+ require.NoError(t, collr.Init(context.Background()))
86
mock := &mockVCenterHealthClient{}
87
collr.client = mock
87
- collr.Cleanup()
88
+ collr.Cleanup(context.Background())
89
90
assert.True(t, mock.logoutCalls == 1)
91
}
@@ -92,7 +93,7 @@ func TestCollector_Cleanup(t *testing.T) {
93
func TestCollector_CleanupWithNilClient(t *testing.T) {
94
collr := prepareVCSA()
95
95
- assert.NotPanics(t, collr.Cleanup)
96
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
97
}
98
99
func TestCollector_Charts(t *testing.T) {
@@ -101,7 +102,7 @@ func TestCollector_Charts(t *testing.T) {
102
103
func TestCollector_Collect(t *testing.T) {
104
collr := prepareVCSA()
104
- require.NoError(t, collr.Init())
105
+ require.NoError(t, collr.Init(context.Background()))
106
mock := &mockVCenterHealthClient{}
107
collr.client = mock
108
@@ -155,33 +156,33 @@ func TestCollector_Collect(t *testing.T) {
156
"system_status_yellow": 0,
157
}
158
158
- assert.Equal(t, expected, collr.Collect())
159
+ assert.Equal(t, expected, collr.Collect(context.Background()))
160
}
161
162
func TestCollector_CollectEnsurePingIsCalled(t *testing.T) {
163
collr := prepareVCSA()
163
- require.NoError(t, collr.Init())
164
+ require.NoError(t, collr.Init(context.Background()))
165
mock := &mockVCenterHealthClient{}
166
collr.client = mock
166
- collr.Collect()
167
+ collr.Collect(context.Background())
168
169
assert.True(t, mock.pingCalls == 1)
170
}
171
172
func TestCollector_CollectErrorOnPing(t *testing.T) {
173
collr := prepareVCSA()
173
- require.NoError(t, collr.Init())
174
+ require.NoError(t, collr.Init(context.Background()))
175
mock := &mockVCenterHealthClient{
176
ping: func() error { return errors.New("ping mock error") },
177
}
178
collr.client = mock
179
179
- assert.Zero(t, collr.Collect())
180
+ assert.Zero(t, collr.Collect(context.Background()))
181
}
182
183
func TestCollector_CollectErrorOnHealthCalls(t *testing.T) {
184
collr := prepareVCSA()
184
- require.NoError(t, collr.Init())
185
+ require.NoError(t, collr.Init(context.Background()))
186
mock := &mockVCenterHealthClient{
187
applMgmt: func() (string, error) { return "", errors.New("applMgmt mock error") },
188
databaseStorage: func() (string, error) { return "", errors.New("databaseStorage mock error") },
@@ -194,7 +195,7 @@ func TestCollector_CollectErrorOnHealthCalls(t *testing.T) {
195
}
196
collr.client = mock
197
197
- assert.Zero(t, collr.Collect())
198
+ assert.Zero(t, collr.Collect(context.Background()))
199
}
200
201
func prepareVCSA() *Collector {
src/go/plugin/go.d/collector/vernemq/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package vernemq
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -67,7 +68,7 @@ func (c *Collector) Configuration() any {
68
return c.Config
69
}
70
70
-func (c *Collector) Init() error {
71
+func (c *Collector) Init(context.Context) error {
72
if err := c.validateConfig(); err != nil {
73
return fmt.Errorf("config validation: %v", err)
74
}
@@ -81,7 +82,7 @@ func (c *Collector) Init() error {
82
return nil
83
}
84
84
-func (c *Collector) Check() error {
85
+func (c *Collector) Check(context.Context) error {
86
mx, err := c.collect()
87
if err != nil {
88
return err
@@ -98,7 +99,7 @@ func (c *Collector) Charts() *module.Charts {
99
return c.charts
100
}
101
101
-func (c *Collector) Collect() map[string]int64 {
102
+func (c *Collector) Collect(context.Context) map[string]int64 {
103
mx, err := c.collect()
104
if err != nil {
105
c.Error(err)
@@ -111,7 +112,7 @@ func (c *Collector) Collect() map[string]int64 {
112
return mx
113
}
114
114
-func (c *Collector) Cleanup() {
115
+func (c *Collector) Cleanup(context.Context) {
116
if c.prom != nil && c.prom.HTTPClient() != nil {
117
c.prom.HTTPClient().CloseIdleConnections()
118
}
src/go/plugin/go.d/collector/vernemq/collector_test.go
+13
-12
@@ -3,6 +3,7 @@
3
package vernemq
4
5
import (
6
+ "context"
7
"net/http"
8
"net/http/httptest"
9
"os"
@@ -63,9 +64,9 @@ func TestCollector_Init(t *testing.T) {
64
collr.Config = test.config
65
66
if test.wantFail {
66
- assert.Error(t, collr.Init())
67
+ assert.Error(t, collr.Init(context.Background()))
68
} else {
68
- assert.NoError(t, collr.Init())
69
+ assert.NoError(t, collr.Init(context.Background()))
70
}
71
})
72
}
@@ -76,7 +77,7 @@ func TestCollector_Charts(t *testing.T) {
77
}
78
79
func TestCollector_Cleanup(t *testing.T) {
79
- assert.NotPanics(t, New().Cleanup)
80
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
81
}
82
83
func TestCollector_Check(t *testing.T) {
@@ -116,9 +117,9 @@ func TestCollector_Check(t *testing.T) {
117
defer cleanup()
118
119
if test.wantFail {
119
- assert.Error(t, collr.Check())
120
+ assert.Error(t, collr.Check(context.Background()))
121
} else {
121
- assert.NoError(t, collr.Check())
122
+ assert.NoError(t, collr.Check(context.Background()))
123
}
124
})
125
}
@@ -642,7 +643,7 @@ func TestCollector_Collect(t *testing.T) {
643
collr, cleanup := test.prepare(t)
644
defer cleanup()
645
645
- mx := collr.Collect()
646
+ mx := collr.Collect(context.Background())
647
648
require.Equal(t, test.wantMetrics, mx)
649
@@ -664,7 +665,7 @@ func caseOkVer201(t *testing.T) (*Collector, func()) {
665
666
collr := New()
667
collr.URL = srv.URL
667
- require.NoError(t, collr.Init())
668
+ require.NoError(t, collr.Init(context.Background()))
669
670
return collr, srv.Close
671
}
@@ -678,7 +679,7 @@ func caseOkVer1101(t *testing.T) (*Collector, func()) {
679
680
collr := New()
681
collr.URL = srv.URL
681
- require.NoError(t, collr.Init())
682
+ require.NoError(t, collr.Init(context.Background()))
683
684
return collr, srv.Close
685
}
@@ -703,7 +704,7 @@ wmi_os_processes_limit 4.294967295e+09
704
705
collr := New()
706
collr.URL = srv.URL
706
- require.NoError(t, collr.Init())
707
+ require.NoError(t, collr.Init(context.Background()))
708
709
return collr, srv.Close
710
}
@@ -716,7 +717,7 @@ func caseInvalidDataResponse(t *testing.T) (*Collector, func()) {
717
}))
718
collr := New()
719
collr.URL = srv.URL
719
- require.NoError(t, collr.Init())
720
+ require.NoError(t, collr.Init(context.Background()))
721
722
return collr, srv.Close
723
}
@@ -725,7 +726,7 @@ func caseConnectionRefused(t *testing.T) (*Collector, func()) {
726
t.Helper()
727
collr := New()
728
collr.URL = "http://127.0.0.1:65001"
728
- require.NoError(t, collr.Init())
729
+ require.NoError(t, collr.Init(context.Background()))
730
731
return collr, func() {}
732
}
@@ -739,7 +740,7 @@ func case404(t *testing.T) (*Collector, func()) {
740
741
collr := New()
742
collr.URL = srv.URL
742
- require.NoError(t, collr.Init())
743
+ require.NoError(t, collr.Init(context.Background()))
744
745
return collr, srv.Close
746
}
src/go/plugin/go.d/collector/vsphere/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package vsphere
4
5
import (
6
+ "context"
7
_ "embed"
8
"fmt"
9
"sync"
@@ -89,7 +90,7 @@ func (c *Collector) Configuration() any {
90
return c.Config
91
}
92
92
-func (c *Collector) Init() error {
93
+func (c *Collector) Init(context.Context) error {
94
if err := c.validateConfig(); err != nil {
95
return fmt.Errorf("error on validating config: %v", err)
96
}
@@ -114,7 +115,7 @@ func (c *Collector) Init() error {
115
return nil
116
}
117
117
-func (c *Collector) Check() error {
118
+func (c *Collector) Check(context.Context) error {
119
return nil
120
}
121
@@ -122,7 +123,7 @@ func (c *Collector) Charts() *module.Charts {
123
return c.charts
124
}
125
125
-func (c *Collector) Collect() map[string]int64 {
126
+func (c *Collector) Collect(context.Context) map[string]int64 {
127
mx, err := c.collect()
128
if err != nil {
129
c.Error(err)
@@ -134,7 +135,7 @@ func (c *Collector) Collect() map[string]int64 {
135
return mx
136
}
137
137
-func (c *Collector) Cleanup() {
138
+func (c *Collector) Cleanup(context.Context) {
139
if c.discoveryTask == nil {
140
return
141
}
src/go/plugin/go.d/collector/vsphere/collector_test.go
+23
-22
@@ -2,6 +2,7 @@
2
package vsphere
3
4
import (
5
+ "context"
6
"crypto/tls"
7
"os"
8
"strings"
@@ -42,7 +43,7 @@ func TestCollector_Init(t *testing.T) {
43
collr, _, teardown := prepareVSphereSim(t)
44
defer teardown()
45
45
- assert.NoError(t, collr.Init())
46
+ assert.NoError(t, collr.Init(context.Background()))
47
assert.NotNil(t, collr.discoverer)
48
assert.NotNil(t, collr.scraper)
49
assert.NotNil(t, collr.resources)
@@ -55,7 +56,7 @@ func TestCollector_Init_ReturnsFalseIfURLNotSet(t *testing.T) {
56
defer teardown()
57
collr.URL = ""
58
58
- assert.Error(t, collr.Init())
59
+ assert.Error(t, collr.Init(context.Background()))
60
}
61
62
func TestCollector_Init_ReturnsFalseIfUsernameNotSet(t *testing.T) {
@@ -63,7 +64,7 @@ func TestCollector_Init_ReturnsFalseIfUsernameNotSet(t *testing.T) {
64
defer teardown()
65
collr.Username = ""
66
66
- assert.Error(t, collr.Init())
67
+ assert.Error(t, collr.Init(context.Background()))
68
}
69
70
func TestCollector_Init_ReturnsFalseIfPasswordNotSet(t *testing.T) {
@@ -71,7 +72,7 @@ func TestCollector_Init_ReturnsFalseIfPasswordNotSet(t *testing.T) {
72
defer teardown()
73
collr.Password = ""
74
74
- assert.Error(t, collr.Init())
75
+ assert.Error(t, collr.Init(context.Background()))
76
}
77
78
func TestCollector_Init_ReturnsFalseIfClientWrongTLSCA(t *testing.T) {
@@ -79,7 +80,7 @@ func TestCollector_Init_ReturnsFalseIfClientWrongTLSCA(t *testing.T) {
80
defer teardown()
81
collr.ClientConfig.TLSConfig.TLSCA = "testdata/tls"
82
82
- assert.Error(t, collr.Init())
83
+ assert.Error(t, collr.Init(context.Background()))
84
}
85
86
func TestCollector_Init_ReturnsFalseIfConnectionRefused(t *testing.T) {
@@ -87,7 +88,7 @@ func TestCollector_Init_ReturnsFalseIfConnectionRefused(t *testing.T) {
88
defer teardown()
89
collr.URL = "http://127.0.0.1:32001"
90
90
- assert.Error(t, collr.Init())
91
+ assert.Error(t, collr.Init(context.Background()))
92
}
93
94
func TestCollector_Init_ReturnsFalseIfInvalidHostVMIncludeFormat(t *testing.T) {
@@ -95,16 +96,16 @@ func TestCollector_Init_ReturnsFalseIfInvalidHostVMIncludeFormat(t *testing.T) {
96
defer teardown()
97
98
collr.HostsInclude = match.HostIncludes{"invalid"}
98
- assert.Error(t, collr.Init())
99
+ assert.Error(t, collr.Init(context.Background()))
100
101
collr.HostsInclude = collr.HostsInclude[:0]
102
103
collr.VMsInclude = match.VMIncludes{"invalid"}
103
- assert.Error(t, collr.Init())
104
+ assert.Error(t, collr.Init(context.Background()))
105
}
106
107
func TestCollector_Check(t *testing.T) {
107
- assert.NoError(t, New().Check())
108
+ assert.NoError(t, New().Check(context.Background()))
109
}
110
111
func TestCollector_Charts(t *testing.T) {
@@ -115,23 +116,23 @@ func TestCollector_Cleanup(t *testing.T) {
116
collr, _, teardown := prepareVSphereSim(t)
117
defer teardown()
118
118
- require.NoError(t, collr.Init())
119
+ require.NoError(t, collr.Init(context.Background()))
120
120
- collr.Cleanup()
121
+ collr.Cleanup(context.Background())
122
time.Sleep(time.Second)
123
assert.True(t, collr.discoveryTask.isStopped())
124
assert.False(t, collr.discoveryTask.isRunning())
125
}
126
127
func TestCollector_Cleanup_NotPanicsIfNotInitialized(t *testing.T) {
127
- assert.NotPanics(t, New().Cleanup)
128
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
129
}
130
131
func TestCollector_Collect(t *testing.T) {
132
collr, model, teardown := prepareVSphereSim(t)
133
defer teardown()
134
134
- require.NoError(t, collr.Init())
135
+ require.NoError(t, collr.Init(context.Background()))
136
137
collr.scraper = mockScraper{collr.scraper}
138
@@ -330,7 +331,7 @@ func TestCollector_Collect(t *testing.T) {
331
"vm-72_sys.uptime.latest": 200,
332
}
333
333
- mx := collr.Collect()
334
+ mx := collr.Collect(context.Background())
335
336
require.Equal(t, expected, mx)
337
@@ -347,8 +348,8 @@ func TestCollector_Collect_RemoveHostsVMsInRuntime(t *testing.T) {
348
collr, _, teardown := prepareVSphereSim(t)
349
defer teardown()
350
350
- require.NoError(t, collr.Init())
351
- require.NoError(t, collr.Check())
351
+ require.NoError(t, collr.Init(context.Background()))
352
+ require.NoError(t, collr.Check(context.Background()))
353
354
okHostID := "host-58"
355
okVMID := "vm-63"
@@ -359,7 +360,7 @@ func TestCollector_Collect_RemoveHostsVMsInRuntime(t *testing.T) {
360
361
numOfRuns := 5
362
for i := 0; i < numOfRuns; i++ {
362
- collr.Collect()
363
+ collr.Collect(context.Background())
364
}
365
366
host := collr.resources.Hosts.Get(okHostID)
@@ -382,7 +383,7 @@ func TestCollector_Collect_RemoveHostsVMsInRuntime(t *testing.T) {
383
}
384
385
for i := numOfRuns; i < failedUpdatesLimit; i++ {
385
- collr.Collect()
386
+ collr.Collect(context.Background())
387
}
388
389
assert.Len(t, collr.discoveredHosts, 1)
@@ -403,12 +404,12 @@ func TestCollector_Collect_Run(t *testing.T) {
404
defer teardown()
405
406
collr.DiscoveryInterval = confopt.Duration(time.Second * 2)
406
- require.NoError(t, collr.Init())
407
- require.NoError(t, collr.Check())
407
+ require.NoError(t, collr.Init(context.Background()))
408
+ require.NoError(t, collr.Check(context.Background()))
409
410
runs := 20
411
for i := 0; i < runs; i++ {
411
- assert.True(t, len(collr.Collect()) > 0)
412
+ assert.True(t, len(collr.Collect(context.Background())) > 0)
413
if i < 6 {
414
time.Sleep(time.Second)
415
}
@@ -424,7 +425,7 @@ func TestCollector_Collect_Run(t *testing.T) {
425
func prepareVSphereSim(t *testing.T) (collr *Collector, model *simulator.Model, teardown func()) {
426
model, srv := createSim(t)
427
collr = New()
427
- teardown = func() { model.Remove(); srv.Close(); collr.Cleanup() }
428
+ teardown = func() { model.Remove(); srv.Close(); collr.Cleanup(context.Background()) }
429
430
collr.Username = "administrator"
431
collr.Password = "password"
src/go/plugin/go.d/collector/w1sensor/collector.go
+5
-4
@@ -5,6 +5,7 @@
5
package w1sensor
6
7
import (
8
+ "context"
9
_ "embed"
10
"errors"
11
@@ -53,7 +54,7 @@ func (c *Collector) Configuration() any {
54
return c.Config
55
}
56
56
-func (c *Collector) Init() error {
57
+func (c *Collector) Init(context.Context) error {
58
if c.SensorsPath == "" {
59
return errors.New("config: no sensors path specified")
60
}
@@ -61,7 +62,7 @@ func (c *Collector) Init() error {
62
return nil
63
}
64
64
-func (c *Collector) Check() error {
65
+func (c *Collector) Check(context.Context) error {
66
mx, err := c.collect()
67
if err != nil {
68
return err
@@ -78,7 +79,7 @@ func (c *Collector) Charts() *module.Charts {
79
return c.charts
80
}
81
81
-func (c *Collector) Collect() map[string]int64 {
82
+func (c *Collector) Collect(context.Context) map[string]int64 {
83
mx, err := c.collect()
84
if err != nil {
85
c.Error(err)
@@ -91,4 +92,4 @@ func (c *Collector) Collect() map[string]int64 {
92
return mx
93
}
94
94
-func (c *Collector) Cleanup() {}
95
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/w1sensor/collector_test.go
+9
-8
@@ -5,6 +5,7 @@
5
package w1sensor
6
7
import (
8
+ "context"
9
"os"
10
"testing"
11
@@ -51,9 +52,9 @@ func TestCollector_Init(t *testing.T) {
52
collr.Config = test.config
53
54
if test.wantFail {
54
- assert.Error(t, collr.Init())
55
+ assert.Error(t, collr.Init(context.Background()))
56
} else {
56
- assert.NoError(t, collr.Init())
57
+ assert.NoError(t, collr.Init(context.Background()))
58
}
59
})
60
}
@@ -71,14 +72,14 @@ func TestAP_Cleanup(t *testing.T) {
72
"after check": {
73
prepare: func() *Collector {
74
collr := prepareCaseOk()
74
- _ = collr.Check()
75
+ _ = collr.Check(context.Background())
76
return collr
77
},
78
},
79
"after collect": {
80
prepare: func() *Collector {
81
collr := prepareCaseOk()
81
- _ = collr.Collect()
82
+ _ = collr.Collect(context.Background())
83
return collr
84
},
85
},
@@ -88,7 +89,7 @@ func TestAP_Cleanup(t *testing.T) {
89
t.Run(name, func(t *testing.T) {
90
collr := test.prepare()
91
91
- assert.NotPanics(t, collr.Cleanup)
92
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
93
})
94
}
95
}
@@ -117,9 +118,9 @@ func TestCollector_Check(t *testing.T) {
118
collr := test.prepareMock()
119
120
if test.wantFail {
120
- assert.Error(t, collr.Check())
121
+ assert.Error(t, collr.Check(context.Background()))
122
} else {
122
- assert.NoError(t, collr.Check())
123
+ assert.NoError(t, collr.Check(context.Background()))
124
}
125
})
126
}
@@ -151,7 +152,7 @@ func TestW1Sensors_Collect(t *testing.T) {
152
t.Run(name, func(t *testing.T) {
153
collr := test.prepareMock()
154
154
- mx := collr.Collect()
155
+ mx := collr.Collect(context.Background())
156
157
assert.Equal(t, test.wantMetrics, mx)
158
src/go/plugin/go.d/collector/weblog/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package weblog
4
5
import (
6
+ "context"
7
_ "embed"
8
"fmt"
9
@@ -100,7 +101,7 @@ func (c *Collector) Configuration() any {
101
return c.Config
102
}
103
103
-func (c *Collector) Init() error {
104
+func (c *Collector) Init(context.Context) error {
105
if err := c.createURLPatterns(); err != nil {
106
return fmt.Errorf("init failed: %v", err)
107
}
@@ -123,7 +124,7 @@ func (c *Collector) Init() error {
124
return nil
125
}
126
126
-func (c *Collector) Check() error {
127
+func (c *Collector) Check(context.Context) error {
128
// Note: these inits are here to make auto-detection retry working
129
if err := c.createLogReader(); err != nil {
130
return fmt.Errorf("failed to create log reader: %v", err)
@@ -144,7 +145,7 @@ func (c *Collector) Charts() *module.Charts {
145
return c.charts
146
}
147
147
-func (c *Collector) Collect() map[string]int64 {
148
+func (c *Collector) Collect(context.Context) map[string]int64 {
149
mx, err := c.collect()
150
if err != nil {
151
c.Error(err)
@@ -156,7 +157,7 @@ func (c *Collector) Collect() map[string]int64 {
157
return mx
158
}
159
159
-func (c *Collector) Cleanup() {
160
+func (c *Collector) Cleanup(context.Context) {
161
if c.file != nil {
162
_ = c.file.Close()
163
}
src/go/plugin/go.d/collector/weblog/collector_test.go
+44
-43
@@ -4,6 +4,7 @@ package weblog
4
5
import (
6
"bytes"
7
+ "context"
8
"fmt"
9
"os"
10
"reflect"
@@ -51,73 +52,73 @@ func TestCollector_ConfigurationSerialize(t *testing.T) {
52
func TestCollector_Init(t *testing.T) {
53
collr := New()
54
54
- assert.NoError(t, collr.Init())
55
+ assert.NoError(t, collr.Init(context.Background()))
56
}
57
58
func TestCollector_Init_ErrorOnCreatingURLPatterns(t *testing.T) {
59
collr := New()
60
collr.URLPatterns = []userPattern{{Match: "* !*"}}
61
61
- assert.Error(t, collr.Init())
62
+ assert.Error(t, collr.Init(context.Background()))
63
}
64
65
func TestCollector_Init_ErrorOnCreatingCustomFields(t *testing.T) {
66
collr := New()
67
collr.CustomFields = []customField{{Patterns: []userPattern{{Name: "p1", Match: "* !*"}}}}
68
68
- assert.Error(t, collr.Init())
69
+ assert.Error(t, collr.Init(context.Background()))
70
}
71
72
func TestCollector_Check(t *testing.T) {
73
collr := New()
73
- defer collr.Cleanup()
74
+ defer collr.Cleanup(context.Background())
75
collr.Path = "testdata/common.log"
75
- require.NoError(t, collr.Init())
76
+ require.NoError(t, collr.Init(context.Background()))
77
77
- assert.NoError(t, collr.Check())
78
+ assert.NoError(t, collr.Check(context.Background()))
79
}
80
81
func TestCollector_Check_ErrorOnCreatingLogReaderNoLogFile(t *testing.T) {
82
collr := New()
82
- defer collr.Cleanup()
83
+ defer collr.Cleanup(context.Background())
84
collr.Path = "testdata/not_exists.log"
84
- require.NoError(t, collr.Init())
85
+ require.NoError(t, collr.Init(context.Background()))
86
86
- assert.Error(t, collr.Check())
87
+ assert.Error(t, collr.Check(context.Background()))
88
}
89
90
func TestCollector_Check_ErrorOnCreatingParserUnknownFormat(t *testing.T) {
91
collr := New()
91
- defer collr.Cleanup()
92
+ defer collr.Cleanup(context.Background())
93
collr.Path = "testdata/custom.log"
93
- require.NoError(t, collr.Init())
94
+ require.NoError(t, collr.Init(context.Background()))
95
95
- assert.Error(t, collr.Check())
96
+ assert.Error(t, collr.Check(context.Background()))
97
}
98
99
func TestCollector_Check_ErrorOnCreatingParserEmptyLine(t *testing.T) {
100
collr := New()
100
- defer collr.Cleanup()
101
+ defer collr.Cleanup(context.Background())
102
collr.Path = "testdata/custom.log"
103
collr.ParserConfig.LogType = logs.TypeCSV
104
collr.ParserConfig.CSV.Format = "$one $two"
104
- require.NoError(t, collr.Init())
105
+ require.NoError(t, collr.Init(context.Background()))
106
106
- assert.Error(t, collr.Check())
107
+ assert.Error(t, collr.Check(context.Background()))
108
}
109
110
func TestCollector_Charts(t *testing.T) {
111
collr := New()
111
- defer collr.Cleanup()
112
+ defer collr.Cleanup(context.Background())
113
collr.Path = "testdata/common.log"
113
- require.NoError(t, collr.Init())
114
- require.NoError(t, collr.Check())
114
+ require.NoError(t, collr.Init(context.Background()))
115
+ require.NoError(t, collr.Check(context.Background()))
116
117
assert.NotNil(t, collr.Charts())
118
}
119
120
func TestCollector_Cleanup(t *testing.T) {
120
- New().Cleanup()
121
+ New().Cleanup(context.Background())
122
}
123
124
func TestCollector_Collect(t *testing.T) {
@@ -312,7 +313,7 @@ func TestCollector_Collect(t *testing.T) {
313
"url_ptn_org_resp_code_401": 9,
314
}
315
315
- mx := collr.Collect()
316
+ mx := collr.Collect(context.Background())
317
assert.Equal(t, expected, mx)
318
testCharts(t, collr, mx)
319
}
@@ -392,7 +393,7 @@ func TestCollector_Collect_CommonLogFormat(t *testing.T) {
393
"upstream_resp_time_sum": 0,
394
}
395
395
- mx := collr.Collect()
396
+ mx := collr.Collect(context.Background())
397
assert.Equal(t, expected, mx)
398
testCharts(t, collr, mx)
399
}
@@ -462,7 +463,7 @@ func TestCollector_Collect_CustomLogs(t *testing.T) {
463
"upstream_resp_time_sum": 0,
464
}
465
465
- mx := collr.Collect()
466
+ mx := collr.Collect(context.Background())
467
assert.Equal(t, expected, mx)
468
testCharts(t, collr, mx)
469
}
@@ -564,7 +565,7 @@ func TestCollector_Collect_CustomTimeFieldsLogs(t *testing.T) {
565
"upstream_resp_time_sum": 0,
566
}
567
567
- mx := collr.Collect()
568
+ mx := collr.Collect(context.Background())
569
assert.Equal(t, expected, mx)
570
testCharts(t, collr, mx)
571
}
@@ -640,7 +641,7 @@ func TestCollector_Collect_CustomNumericFieldsLogs(t *testing.T) {
641
"upstream_resp_time_sum": 0,
642
}
643
643
- mx := collr.Collect()
644
+ mx := collr.Collect(context.Background())
645
646
assert.Equal(t, expected, mx)
647
testCharts(t, collr, mx)
@@ -714,7 +715,7 @@ func TestCollector_IISLogs(t *testing.T) {
715
"upstream_resp_time_sum": 0,
716
}
717
717
- mx := collr.Collect()
718
+ mx := collr.Collect(context.Background())
719
assert.Equal(t, expected, mx)
720
}
721
@@ -1187,9 +1188,9 @@ func prepareWebLogCollectFull(t *testing.T) *Collector {
1188
}
1189
collr := New()
1190
collr.Config = cfg
1190
- require.NoError(t, collr.Init())
1191
- require.NoError(t, collr.Check())
1192
- defer collr.Cleanup()
1191
+ require.NoError(t, collr.Init(context.Background()))
1192
+ require.NoError(t, collr.Check(context.Background()))
1193
+ defer collr.Cleanup(context.Background())
1194
1195
p, err := logs.NewCSVParser(collr.ParserConfig.CSV, bytes.NewReader(dataFullLog))
1196
require.NoError(t, err)
@@ -1230,9 +1231,9 @@ func prepareWebLogCollectCommon(t *testing.T) *Collector {
1231
1232
collr := New()
1233
collr.Config = cfg
1233
- require.NoError(t, collr.Init())
1234
- require.NoError(t, collr.Check())
1235
- defer collr.Cleanup()
1234
+ require.NoError(t, collr.Init(context.Background()))
1235
+ require.NoError(t, collr.Check(context.Background()))
1236
+ defer collr.Cleanup(context.Background())
1237
1238
p, err := logs.NewCSVParser(collr.ParserConfig.CSV, bytes.NewReader(dataCommonLog))
1239
require.NoError(t, err)
@@ -1282,9 +1283,9 @@ func prepareWebLogCollectCustom(t *testing.T) *Collector {
1283
}
1284
collr := New()
1285
collr.Config = cfg
1285
- require.NoError(t, collr.Init())
1286
- require.NoError(t, collr.Check())
1287
- defer collr.Cleanup()
1286
+ require.NoError(t, collr.Init(context.Background()))
1287
+ require.NoError(t, collr.Check(context.Background()))
1288
+ defer collr.Cleanup(context.Background())
1289
1290
p, err := logs.NewCSVParser(collr.ParserConfig.CSV, bytes.NewReader(dataCustomLog))
1291
require.NoError(t, err)
@@ -1328,9 +1329,9 @@ func prepareWebLogCollectCustomTimeFields(t *testing.T) *Collector {
1329
}
1330
collr := New()
1331
collr.Config = cfg
1331
- require.NoError(t, collr.Init())
1332
- require.NoError(t, collr.Check())
1333
- defer collr.Cleanup()
1332
+ require.NoError(t, collr.Init(context.Background()))
1333
+ require.NoError(t, collr.Check(context.Background()))
1334
+ defer collr.Cleanup(context.Background())
1335
1336
p, err := logs.NewCSVParser(collr.ParserConfig.CSV, bytes.NewReader(dataCustomTimeFieldLog))
1337
require.NoError(t, err)
@@ -1374,9 +1375,9 @@ func prepareWebLogCollectCustomNumericFields(t *testing.T) *Collector {
1375
}
1376
collr := New()
1377
collr.Config = cfg
1377
- require.NoError(t, collr.Init())
1378
- require.NoError(t, collr.Check())
1379
- defer collr.Cleanup()
1378
+ require.NoError(t, collr.Init(context.Background()))
1379
+ require.NoError(t, collr.Check(context.Background()))
1380
+ defer collr.Cleanup(context.Background())
1381
1382
p, err := logs.NewCSVParser(collr.ParserConfig.CSV, bytes.NewReader(dataCustomTimeFieldLog))
1383
require.NoError(t, err)
@@ -1424,9 +1425,9 @@ func prepareWebLogCollectIISFields(t *testing.T) *Collector {
1425
1426
collr := New()
1427
collr.Config = cfg
1427
- require.NoError(t, collr.Init())
1428
- require.NoError(t, collr.Check())
1429
- defer collr.Cleanup()
1428
+ require.NoError(t, collr.Init(context.Background()))
1429
+ require.NoError(t, collr.Check(context.Background()))
1430
+ defer collr.Cleanup(context.Background())
1431
1432
p, err := logs.NewCSVParser(collr.ParserConfig.CSV, bytes.NewReader(dataIISLog))
1433
require.NoError(t, err)
src/go/plugin/go.d/collector/weblog/init.go
+2
-1
@@ -3,6 +3,7 @@
3
package weblog
4
5
import (
6
+ "context"
7
"errors"
8
"fmt"
9
"strings"
@@ -136,7 +137,7 @@ func (c *Collector) createLogLine() {
137
}
138
139
func (c *Collector) createLogReader() error {
139
- c.Cleanup()
140
+ c.Cleanup(context.Background())
141
c.Debug("starting log reader creating")
142
143
reader, err := logs.Open(c.Path, c.ExcludePath, c.Logger)
src/go/plugin/go.d/collector/whoisquery/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package whoisquery
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -57,7 +58,7 @@ func (c *Collector) Configuration() any {
58
return c.Config
59
}
60
60
-func (c *Collector) Init() error {
61
+func (c *Collector) Init(context.Context) error {
62
if err := c.validateConfig(); err != nil {
63
return fmt.Errorf("config validation: %v", err)
64
}
@@ -73,7 +74,7 @@ func (c *Collector) Init() error {
74
return nil
75
}
76
76
-func (c *Collector) Check() error {
77
+func (c *Collector) Check(context.Context) error {
78
mx, err := c.collect()
79
if err != nil {
80
return err
@@ -88,7 +89,7 @@ func (c *Collector) Charts() *module.Charts {
89
return c.charts
90
}
91
91
-func (c *Collector) Collect() map[string]int64 {
92
+func (c *Collector) Collect(context.Context) map[string]int64 {
93
mx, err := c.collect()
94
if err != nil {
95
c.Error(err)
@@ -100,4 +101,4 @@ func (c *Collector) Collect() map[string]int64 {
101
return mx
102
}
103
103
-func (c *Collector) Cleanup() {}
104
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/whoisquery/collector_test.go
+11
-10
@@ -3,6 +3,7 @@
3
package whoisquery
4
5
import (
6
+ "context"
7
"errors"
8
"os"
9
"testing"
@@ -32,13 +33,13 @@ func TestCollector_ConfigurationSerialize(t *testing.T) {
33
}
34
35
func TestCollector_Cleanup(t *testing.T) {
35
- New().Cleanup()
36
+ New().Cleanup(context.Background())
37
}
38
39
func TestCollector_Charts(t *testing.T) {
40
collr := New()
41
collr.Source = "example.com"
41
- require.NoError(t, collr.Init())
42
+ require.NoError(t, collr.Init(context.Background()))
43
44
assert.NotNil(t, collr.Charts())
45
}
@@ -66,9 +67,9 @@ func TestCollector_Init(t *testing.T) {
67
collr.Config = test.config
68
69
if test.err {
69
- assert.Error(t, collr.Init())
70
+ assert.Error(t, collr.Init(context.Background()))
71
} else {
71
- require.NoError(t, collr.Init())
72
+ require.NoError(t, collr.Init(context.Background()))
73
74
var typeOK bool
75
if test.providerType == net {
@@ -85,23 +86,23 @@ func TestCollector_Check(t *testing.T) {
86
collr := New()
87
collr.prov = &mockProvider{remTime: 12345.678}
88
88
- assert.NoError(t, collr.Check())
89
+ assert.NoError(t, collr.Check(context.Background()))
90
}
91
92
func TestCollector_Check_ReturnsFalseOnProviderError(t *testing.T) {
93
collr := New()
94
collr.prov = &mockProvider{err: true}
95
95
- assert.Error(t, collr.Check())
96
+ assert.Error(t, collr.Check(context.Background()))
97
}
98
99
func TestCollector_Collect(t *testing.T) {
100
collr := New()
101
collr.Source = "example.com"
101
- require.NoError(t, collr.Init())
102
+ require.NoError(t, collr.Init(context.Background()))
103
collr.prov = &mockProvider{remTime: 12345}
104
104
- mx := collr.Collect()
105
+ mx := collr.Collect(context.Background())
106
107
expected := map[string]int64{
108
"expiry": 12345,
@@ -117,10 +118,10 @@ func TestCollector_Collect(t *testing.T) {
118
func TestCollector_Collect_ReturnsNilOnProviderError(t *testing.T) {
119
collr := New()
120
collr.Source = "example.com"
120
- require.NoError(t, collr.Init())
121
+ require.NoError(t, collr.Init(context.Background()))
122
collr.prov = &mockProvider{err: true}
123
123
- assert.Nil(t, collr.Collect())
124
+ assert.Nil(t, collr.Collect(context.Background()))
125
}
126
127
type mockProvider struct {
src/go/plugin/go.d/collector/windows/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package windows
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -122,7 +123,7 @@ func (c *Collector) Configuration() any {
123
return c.Config
124
}
125
125
-func (c *Collector) Init() error {
126
+func (c *Collector) Init(context.Context) error {
127
if err := c.validateConfig(); err != nil {
128
return fmt.Errorf("config validation: %v", err)
129
}
@@ -136,7 +137,7 @@ func (c *Collector) Init() error {
137
return nil
138
}
139
139
-func (c *Collector) Check() error {
140
+func (c *Collector) Check(context.Context) error {
141
mx, err := c.collect()
142
if err != nil {
143
return err
@@ -151,7 +152,7 @@ func (c *Collector) Charts() *module.Charts {
152
return c.charts
153
}
154
154
-func (c *Collector) Collect() map[string]int64 {
155
+func (c *Collector) Collect(context.Context) map[string]int64 {
156
ms, err := c.collect()
157
if err != nil {
158
c.Error(err)
@@ -163,7 +164,7 @@ func (c *Collector) Collect() map[string]int64 {
164
return ms
165
}
166
166
-func (c *Collector) Cleanup() {
167
+func (c *Collector) Cleanup(context.Context) {
168
if c.prom != nil && c.prom.HTTPClient() != nil {
169
c.prom.HTTPClient().CloseIdleConnections()
170
}
src/go/plugin/go.d/collector/windows/collector_test.go
+9
-8
@@ -3,6 +3,7 @@
3
package windows
4
5
import (
6
+ "context"
7
"fmt"
8
"net/http"
9
"net/http/httptest"
@@ -67,9 +68,9 @@ func TestCollector_Init(t *testing.T) {
68
collr.Config = test.config
69
70
if test.wantFail {
70
- assert.Error(t, collr.Init())
71
+ assert.Error(t, collr.Init(context.Background()))
72
} else {
72
- assert.NoError(t, collr.Init())
73
+ assert.NoError(t, collr.Init(context.Background()))
74
}
75
})
76
}
@@ -102,12 +103,12 @@ func TestCollector_Check(t *testing.T) {
103
collr, cleanup := test.prepare()
104
defer cleanup()
105
105
- require.NoError(t, collr.Init())
106
+ require.NoError(t, collr.Init(context.Background()))
107
108
if test.wantFail {
108
- assert.Error(t, collr.Check())
109
+ assert.Error(t, collr.Check(context.Background()))
110
} else {
110
- assert.NoError(t, collr.Check())
111
+ assert.NoError(t, collr.Check(context.Background()))
112
}
113
})
114
}
@@ -118,7 +119,7 @@ func TestCollector_Charts(t *testing.T) {
119
}
120
121
func TestCollector_Cleanup(t *testing.T) {
121
- assert.NotPanics(t, New().Cleanup)
122
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
123
}
124
125
func TestCollector_Collect(t *testing.T) {
@@ -799,9 +800,9 @@ func TestCollector_Collect(t *testing.T) {
800
collr, cleanup := test.prepare()
801
defer cleanup()
802
802
- require.NoError(t, collr.Init())
803
+ require.NoError(t, collr.Init(context.Background()))
804
804
- mx := collr.Collect()
805
+ mx := collr.Collect(context.Background())
806
807
if mx != nil && test.wantCollected != nil {
808
mx["system_up_time"] = test.wantCollected["system_up_time"]
src/go/plugin/go.d/collector/wireguard/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package wireguard
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"time"
@@ -63,11 +64,11 @@ func (c *Collector) Configuration() any {
64
return c.Config
65
}
66
66
-func (c *Collector) Init() error {
67
+func (c *Collector) Init(context.Context) error {
68
return nil
69
}
70
70
-func (c *Collector) Check() error {
71
+func (c *Collector) Check(context.Context) error {
72
mx, err := c.collect()
73
if err != nil {
74
return err
@@ -82,7 +83,7 @@ func (c *Collector) Charts() *module.Charts {
83
return c.charts
84
}
85
85
-func (c *Collector) Collect() map[string]int64 {
86
+func (c *Collector) Collect(context.Context) map[string]int64 {
87
mx, err := c.collect()
88
if err != nil {
89
c.Error(err)
@@ -94,7 +95,7 @@ func (c *Collector) Collect() map[string]int64 {
95
return mx
96
}
97
97
-func (c *Collector) Cleanup() {
98
+func (c *Collector) Cleanup(context.Context) {
99
if c.client == nil {
100
return
101
}
src/go/plugin/go.d/collector/wireguard/collector_test.go
+27
-26
@@ -3,6 +3,7 @@
3
package wireguard
4
5
import (
6
+ "context"
7
"errors"
8
"fmt"
9
"os"
@@ -36,7 +37,7 @@ func TestCollector_ConfigurationSerialize(t *testing.T) {
37
}
38
39
func TestCollector_Init(t *testing.T) {
39
- assert.NoError(t, New().Init())
40
+ assert.NoError(t, New().Init(context.Background()))
41
}
42
43
func TestCollector_Charts(t *testing.T) {
@@ -55,15 +56,15 @@ func TestCollector_Cleanup(t *testing.T) {
56
},
57
"after Init": {
58
wantClose: false,
58
- prepare: func(c *Collector) { _ = c.Init() },
59
+ prepare: func(c *Collector) { _ = c.Init(context.Background()) },
60
},
61
"after Check": {
62
wantClose: true,
62
- prepare: func(c *Collector) { _ = c.Init(); _ = c.Check() },
63
+ prepare: func(c *Collector) { _ = c.Init(context.Background()); _ = c.Check(context.Background()) },
64
},
65
"after Collect": {
66
wantClose: true,
66
- prepare: func(c *Collector) { _ = c.Init(); _ = c.Collect() },
67
+ prepare: func(c *Collector) { _ = c.Init(context.Background()); _ = c.Collect(context.Background()) },
68
},
69
}
70
@@ -75,7 +76,7 @@ func TestCollector_Cleanup(t *testing.T) {
76
77
test.prepare(collr)
78
78
- require.NotPanics(t, collr.Cleanup)
79
+ require.NotPanics(t, func() { collr.Cleanup(context.Background()) })
80
81
if test.wantClose {
82
assert.True(t, m.closeCalled)
@@ -133,13 +134,13 @@ func TestCollector_Check(t *testing.T) {
134
for name, test := range tests {
135
t.Run(name, func(t *testing.T) {
136
collr := New()
136
- require.NoError(t, collr.Init())
137
+ require.NoError(t, collr.Init(context.Background()))
138
test.prepare(collr)
139
140
if test.wantFail {
140
- assert.Error(t, collr.Check())
141
+ assert.Error(t, collr.Check(context.Background()))
142
} else {
142
- assert.NoError(t, collr.Check())
143
+ assert.NoError(t, collr.Check(context.Background()))
144
}
145
})
146
}
@@ -158,7 +159,7 @@ func TestCollector_Collect(t *testing.T) {
159
m.devices = append(m.devices, prepareDevice(2))
160
},
161
check: func(t *testing.T, collr *Collector) {
161
- mx := collr.Collect()
162
+ mx := collr.Collect(context.Background())
163
164
expected := map[string]int64{
165
"device_wg1_peers": 0,
@@ -189,7 +190,7 @@ func TestCollector_Collect(t *testing.T) {
190
m.devices = append(m.devices, d2)
191
},
192
check: func(t *testing.T, collr *Collector) {
192
- mx := collr.Collect()
193
+ mx := collr.Collect(context.Background())
194
195
expected := map[string]int64{
196
"device_wg1_peers": 2,
@@ -229,7 +230,7 @@ func TestCollector_Collect(t *testing.T) {
230
m.devices = append(m.devices, d1)
231
},
232
check: func(t *testing.T, collr *Collector) {
232
- mx := collr.Collect()
233
+ mx := collr.Collect(context.Background())
234
235
expected := map[string]int64{
236
"device_wg1_peers": 4,
@@ -255,7 +256,7 @@ func TestCollector_Collect(t *testing.T) {
256
m.devices = append(m.devices, prepareDevice(1))
257
},
258
check: func(t *testing.T, collr *Collector) {
258
- _ = collr.Collect()
259
+ _ = collr.Collect(context.Background())
260
assert.Equal(t, len(deviceChartsTmpl)*1, len(*collr.Charts()))
261
},
262
},
@@ -264,7 +265,7 @@ func TestCollector_Collect(t *testing.T) {
265
m.devices = append(m.devices, prepareDevice(2))
266
},
267
check: func(t *testing.T, collr *Collector) {
267
- mx := collr.Collect()
268
+ mx := collr.Collect(context.Background())
269
270
expected := map[string]int64{
271
"device_wg1_peers": 0,
@@ -288,7 +289,7 @@ func TestCollector_Collect(t *testing.T) {
289
m.devices = append(m.devices, prepareDevice(2))
290
},
291
check: func(t *testing.T, collr *Collector) {
291
- _ = collr.Collect()
292
+ _ = collr.Collect(context.Background())
293
},
294
},
295
{
@@ -296,7 +297,7 @@ func TestCollector_Collect(t *testing.T) {
297
m.devices = m.devices[:len(m.devices)-1]
298
},
299
check: func(t *testing.T, collr *Collector) {
299
- _ = collr.Collect()
300
+ _ = collr.Collect(context.Background())
301
assert.Equal(t, len(deviceChartsTmpl)*2, len(*collr.Charts()))
302
assert.Equal(t, 0, calcObsoleteCharts(collr.Charts()))
303
},
@@ -309,7 +310,7 @@ func TestCollector_Collect(t *testing.T) {
310
m.devices = append(m.devices, prepareDevice(2))
311
},
312
check: func(t *testing.T, collr *Collector) {
312
- _ = collr.Collect()
313
+ _ = collr.Collect(context.Background())
314
},
315
},
316
{
@@ -319,7 +320,7 @@ func TestCollector_Collect(t *testing.T) {
320
check: func(t *testing.T, collr *Collector) {
321
collr.cleanupEvery = time.Second
322
time.Sleep(time.Second)
322
- _ = collr.Collect()
323
+ _ = collr.Collect(context.Background())
324
assert.Equal(t, len(deviceChartsTmpl)*2, len(*collr.Charts()))
325
assert.Equal(t, len(deviceChartsTmpl)*1, calcObsoleteCharts(collr.Charts()))
326
},
@@ -331,7 +332,7 @@ func TestCollector_Collect(t *testing.T) {
332
m.devices = append(m.devices, prepareDevice(1))
333
},
334
check: func(t *testing.T, collr *Collector) {
334
- _ = collr.Collect()
335
+ _ = collr.Collect(context.Background())
336
assert.Equal(t, len(deviceChartsTmpl)*1, len(*collr.Charts()))
337
},
338
},
@@ -341,7 +342,7 @@ func TestCollector_Collect(t *testing.T) {
342
d1.Peers = append(d1.Peers, preparePeer("11"))
343
},
344
check: func(t *testing.T, collr *Collector) {
344
- mx := collr.Collect()
345
+ mx := collr.Collect(context.Background())
346
347
expected := map[string]int64{
348
"device_wg1_peers": 1,
@@ -366,7 +367,7 @@ func TestCollector_Collect(t *testing.T) {
367
m.devices = append(m.devices, d1)
368
},
369
check: func(t *testing.T, collr *Collector) {
369
- _ = collr.Collect()
370
+ _ = collr.Collect(context.Background())
371
},
372
},
373
{
@@ -375,7 +376,7 @@ func TestCollector_Collect(t *testing.T) {
376
d1.Peers = d1.Peers[:len(d1.Peers)-1]
377
},
378
check: func(t *testing.T, collr *Collector) {
378
- _ = collr.Collect()
379
+ _ = collr.Collect(context.Background())
380
assert.Equal(t, len(deviceChartsTmpl)*1+len(peerChartsTmpl)*2, len(*collr.Charts()))
381
assert.Equal(t, 0, calcObsoleteCharts(collr.Charts()))
382
},
@@ -390,7 +391,7 @@ func TestCollector_Collect(t *testing.T) {
391
m.devices = append(m.devices, d1)
392
},
393
check: func(t *testing.T, collr *Collector) {
393
- _ = collr.Collect()
394
+ _ = collr.Collect(context.Background())
395
},
396
},
397
{
@@ -401,7 +402,7 @@ func TestCollector_Collect(t *testing.T) {
402
check: func(t *testing.T, collr *Collector) {
403
collr.cleanupEvery = time.Second
404
time.Sleep(time.Second)
404
- _ = collr.Collect()
405
+ _ = collr.Collect(context.Background())
406
assert.Equal(t, len(deviceChartsTmpl)*1+len(peerChartsTmpl)*2, len(*collr.Charts()))
407
assert.Equal(t, len(peerChartsTmpl)*1, calcObsoleteCharts(collr.Charts()))
408
},
@@ -411,7 +412,7 @@ func TestCollector_Collect(t *testing.T) {
412
{
413
prepareMock: func(m *mockClient) {},
414
check: func(t *testing.T, collr *Collector) {
414
- assert.Equal(t, map[string]int64(nil), collr.Collect())
415
+ assert.Equal(t, map[string]int64(nil), collr.Collect(context.Background()))
416
},
417
},
418
},
@@ -421,7 +422,7 @@ func TestCollector_Collect(t *testing.T) {
422
m.errOnDevices = true
423
},
424
check: func(t *testing.T, collr *Collector) {
424
- assert.Equal(t, map[string]int64(nil), collr.Collect())
425
+ assert.Equal(t, map[string]int64(nil), collr.Collect(context.Background()))
426
},
427
},
428
},
@@ -430,7 +431,7 @@ func TestCollector_Collect(t *testing.T) {
431
for name, test := range tests {
432
t.Run(name, func(t *testing.T) {
433
collr := New()
433
- require.NoError(t, collr.Init())
434
+ require.NoError(t, collr.Init(context.Background()))
435
m := &mockClient{}
436
collr.client = m
437
src/go/plugin/go.d/collector/x509check/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package x509check
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -66,7 +67,7 @@ func (c *Collector) Configuration() any {
67
return c.Config
68
}
69
69
-func (c *Collector) Init() error {
70
+func (c *Collector) Init(context.Context) error {
71
if err := c.validateConfig(); err != nil {
72
return fmt.Errorf("config validation: %v", err)
73
}
@@ -80,7 +81,7 @@ func (c *Collector) Init() error {
81
return nil
82
}
83
83
-func (c *Collector) Check() error {
84
+func (c *Collector) Check(context.Context) error {
85
mx, err := c.collect()
86
if err != nil {
87
return err
@@ -95,7 +96,7 @@ func (c *Collector) Charts() *module.Charts {
96
return c.charts
97
}
98
98
-func (c *Collector) Collect() map[string]int64 {
99
+func (c *Collector) Collect(context.Context) map[string]int64 {
100
mx, err := c.collect()
101
if err != nil {
102
c.Error(err)
@@ -107,4 +108,4 @@ func (c *Collector) Collect() map[string]int64 {
108
return mx
109
}
110
110
-func (c *Collector) Cleanup() {}
111
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/x509check/collector_test.go
+11
-10
@@ -3,6 +3,7 @@
3
package x509check
4
5
import (
6
+ "context"
7
"crypto/x509"
8
"errors"
9
"os"
@@ -34,13 +35,13 @@ func TestCollector_ConfigurationSerialize(t *testing.T) {
35
}
36
37
func TestCollector_Cleanup(t *testing.T) {
37
- assert.NotPanics(t, New().Cleanup)
38
+ assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
39
}
40
41
func TestCollector_Charts(t *testing.T) {
42
collr := New()
43
collr.Source = "https://example.com"
43
- require.NoError(t, collr.Init())
44
+ require.NoError(t, collr.Init(context.Background()))
45
assert.NotNil(t, collr.Charts())
46
}
47
@@ -90,9 +91,9 @@ func TestCollector_Init(t *testing.T) {
91
collr.Config = test.config
92
93
if test.err {
93
- assert.Error(t, collr.Init())
94
+ assert.Error(t, collr.Init(context.Background()))
95
} else {
95
- require.NoError(t, collr.Init())
96
+ require.NoError(t, collr.Init(context.Background()))
97
98
var typeOK bool
99
switch test.providerType {
@@ -114,23 +115,23 @@ func TestCollector_Check(t *testing.T) {
115
collr := New()
116
collr.prov = &mockProvider{certs: []*x509.Certificate{{}}}
117
117
- assert.NoError(t, collr.Check())
118
+ assert.NoError(t, collr.Check(context.Background()))
119
}
120
121
func TestCollector_Check_ReturnsFalseOnProviderError(t *testing.T) {
122
collr := New()
123
collr.prov = &mockProvider{err: true}
124
124
- assert.Error(t, collr.Check())
125
+ assert.Error(t, collr.Check(context.Background()))
126
}
127
128
func TestCollector_Collect(t *testing.T) {
129
collr := New()
130
collr.Source = "https://example.com"
130
- require.NoError(t, collr.Init())
131
+ require.NoError(t, collr.Init(context.Background()))
132
collr.prov = &mockProvider{certs: []*x509.Certificate{{}}}
133
133
- mx := collr.Collect()
134
+ mx := collr.Collect(context.Background())
135
136
assert.NotZero(t, mx)
137
module.TestMetricsHasAllChartsDims(t, collr.Charts(), mx)
@@ -140,13 +141,13 @@ func TestCollector_Collect_ReturnsNilOnProviderError(t *testing.T) {
141
collr := New()
142
collr.prov = &mockProvider{err: true}
143
143
- assert.Nil(t, collr.Collect())
144
+ assert.Nil(t, collr.Collect(context.Background()))
145
}
146
147
func TestCollector_Collect_ReturnsNilOnZeroCertificates(t *testing.T) {
148
collr := New()
149
collr.prov = &mockProvider{certs: []*x509.Certificate{}}
149
- mx := collr.Collect()
150
+ mx := collr.Collect(context.Background())
151
152
assert.Nil(t, mx)
153
}
src/go/plugin/go.d/collector/zfspool/collector.go
+5
-4
@@ -5,6 +5,7 @@
5
package zfspool
6
7
import (
8
+ "context"
9
_ "embed"
10
"errors"
11
"fmt"
@@ -62,7 +63,7 @@ func (c *Collector) Configuration() any {
63
return c.Config
64
}
65
65
-func (c *Collector) Init() error {
66
+func (c *Collector) Init(context.Context) error {
67
if err := c.validateConfig(); err != nil {
68
return fmt.Errorf("config validation: %s", err)
69
}
@@ -76,7 +77,7 @@ func (c *Collector) Init() error {
77
return nil
78
}
79
79
-func (c *Collector) Check() error {
80
+func (c *Collector) Check(context.Context) error {
81
mx, err := c.collect()
82
if err != nil {
83
return err
@@ -93,7 +94,7 @@ func (c *Collector) Charts() *module.Charts {
94
return c.charts
95
}
96
96
-func (c *Collector) Collect() map[string]int64 {
97
+func (c *Collector) Collect(context.Context) map[string]int64 {
98
mx, err := c.collect()
99
if err != nil {
100
c.Error(err)
@@ -106,4 +107,4 @@ func (c *Collector) Collect() map[string]int64 {
107
return mx
108
}
109
109
-func (c *Collector) Cleanup() {}
110
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/zfspool/collector_test.go
+9
-8
@@ -5,6 +5,7 @@
5
package zfspool
6
7
import (
8
+ "context"
9
"errors"
10
"os"
11
"strings"
@@ -68,9 +69,9 @@ func TestCollector_Init(t *testing.T) {
69
collr.Config = test.config
70
71
if test.wantFail {
71
- assert.Error(t, collr.Init())
72
+ assert.Error(t, collr.Init(context.Background()))
73
} else {
73
- assert.NoError(t, collr.Init())
74
+ assert.NoError(t, collr.Init(context.Background()))
75
}
76
})
77
}
@@ -89,7 +90,7 @@ func TestCollector_Cleanup(t *testing.T) {
90
prepare: func() *Collector {
91
collr := New()
92
collr.exec = prepareMockOk()
92
- _ = collr.Check()
93
+ _ = collr.Check(context.Background())
94
return collr
95
},
96
},
@@ -97,7 +98,7 @@ func TestCollector_Cleanup(t *testing.T) {
98
prepare: func() *Collector {
99
collr := New()
100
collr.exec = prepareMockOk()
100
- _ = collr.Collect()
101
+ _ = collr.Collect(context.Background())
102
return collr
103
},
104
},
@@ -107,7 +108,7 @@ func TestCollector_Cleanup(t *testing.T) {
108
t.Run(name, func(t *testing.T) {
109
collr := test.prepare()
110
110
- assert.NotPanics(t, collr.Cleanup)
111
+ assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
112
})
113
}
114
}
@@ -146,9 +147,9 @@ func TestCollector_Check(t *testing.T) {
147
collr.exec = mock
148
149
if test.wantFail {
149
- assert.Error(t, collr.Check())
150
+ assert.Error(t, collr.Check(context.Background()))
151
} else {
151
- assert.NoError(t, collr.Check())
152
+ assert.NoError(t, collr.Check(context.Background()))
153
}
154
})
155
}
@@ -381,7 +382,7 @@ func TestCollector_Collect(t *testing.T) {
382
mock := test.prepareMock()
383
collr.exec = mock
384
384
- mx := collr.Collect()
385
+ mx := collr.Collect(context.Background())
386
387
assert.Equal(t, test.wantMetrics, mx)
388
src/go/plugin/go.d/collector/zookeeper/collector.go
+5
-4
@@ -3,6 +3,7 @@
3
package zookeeper
4
5
import (
6
+ "context"
7
_ "embed"
8
"errors"
9
"fmt"
@@ -52,7 +53,7 @@ func (c *Collector) Configuration() any {
53
return c.Config
54
}
55
55
-func (c *Collector) Init() error {
56
+func (c *Collector) Init(context.Context) error {
57
if err := c.verifyConfig(); err != nil {
58
return fmt.Errorf("invalid config: %v", err)
59
}
@@ -66,7 +67,7 @@ func (c *Collector) Init() error {
67
return nil
68
}
69
69
-func (c *Collector) Check() error {
70
+func (c *Collector) Check(context.Context) error {
71
mx, err := c.collect()
72
if err != nil {
73
return err
@@ -81,7 +82,7 @@ func (c *Collector) Charts() *Charts {
82
return charts.Copy()
83
}
84
84
-func (c *Collector) Collect() map[string]int64 {
85
+func (c *Collector) Collect(context.Context) map[string]int64 {
86
mx, err := c.collect()
87
if err != nil {
88
c.Error(err)
@@ -93,4 +94,4 @@ func (c *Collector) Collect() map[string]int64 {
94
return mx
95
}
96
96
-func (c *Collector) Cleanup() {}
97
+func (c *Collector) Cleanup(context.Context) {}
src/go/plugin/go.d/collector/zookeeper/collector_test.go
+18
-17
@@ -5,6 +5,7 @@ package zookeeper
5
import (
6
"bufio"
7
"bytes"
8
+ "context"
9
"errors"
10
"os"
11
"testing"
@@ -41,7 +42,7 @@ func TestCollector_ConfigurationSerialize(t *testing.T) {
42
func TestCollector_Init(t *testing.T) {
43
collr := New()
44
44
- assert.NoError(t, collr.Init())
45
+ assert.NoError(t, collr.Init(context.Background()))
46
assert.NotNil(t, collr.fetcher)
47
}
48
@@ -50,23 +51,23 @@ func TestCollector_InitErrorOnCreatingTLSConfig(t *testing.T) {
51
collr.UseTLS = true
52
collr.TLSConfig.TLSCA = "testdata/tls"
53
53
- assert.Error(t, collr.Init())
54
+ assert.Error(t, collr.Init(context.Background()))
55
}
56
57
func TestCollector_Check(t *testing.T) {
58
collr := New()
58
- require.NoError(t, collr.Init())
59
+ require.NoError(t, collr.Init(context.Background()))
60
collr.fetcher = &mockZookeeperFetcher{data: dataMntrMetrics}
61
61
- assert.NoError(t, collr.Check())
62
+ assert.NoError(t, collr.Check(context.Background()))
63
}
64
65
func TestCollector_CheckErrorOnFetch(t *testing.T) {
66
collr := New()
66
- require.NoError(t, collr.Init())
67
+ require.NoError(t, collr.Init(context.Background()))
68
collr.fetcher = &mockZookeeperFetcher{err: true}
69
69
- assert.Error(t, collr.Check())
70
+ assert.Error(t, collr.Check(context.Background()))
71
}
72
73
func TestCollector_Charts(t *testing.T) {
@@ -74,12 +75,12 @@ func TestCollector_Charts(t *testing.T) {
75
}
76
77
func TestCollector_Cleanup(t *testing.T) {
77
- New().Cleanup()
78
+ New().Cleanup(context.Background())
79
}
80
81
func TestCollector_Collect(t *testing.T) {
82
collr := New()
82
- require.NoError(t, collr.Init())
83
+ require.NoError(t, collr.Init(context.Background()))
84
collr.fetcher = &mockZookeeperFetcher{data: dataMntrMetrics}
85
86
expected := map[string]int64{
@@ -99,7 +100,7 @@ func TestCollector_Collect(t *testing.T) {
100
"znode_count": 5,
101
}
102
102
- mx := collr.Collect()
103
+ mx := collr.Collect(context.Background())
104
105
assert.Equal(t, expected, mx)
106
module.TestMetricsHasAllChartsDims(t, collr.Charts(), mx)
@@ -107,34 +108,34 @@ func TestCollector_Collect(t *testing.T) {
108
109
func TestCollector_CollectMntrNotInWhiteList(t *testing.T) {
110
collr := New()
110
- require.NoError(t, collr.Init())
111
+ require.NoError(t, collr.Init(context.Background()))
112
collr.fetcher = &mockZookeeperFetcher{data: dataMntrNotInWhiteListResponse}
113
113
- assert.Nil(t, collr.Collect())
114
+ assert.Nil(t, collr.Collect(context.Background()))
115
}
116
117
func TestCollector_CollectMntrEmptyResponse(t *testing.T) {
118
collr := New()
118
- require.NoError(t, collr.Init())
119
+ require.NoError(t, collr.Init(context.Background()))
120
collr.fetcher = &mockZookeeperFetcher{}
121
121
- assert.Nil(t, collr.Collect())
122
+ assert.Nil(t, collr.Collect(context.Background()))
123
}
124
125
func TestCollector_CollectMntrInvalidData(t *testing.T) {
126
collr := New()
126
- require.NoError(t, collr.Init())
127
+ require.NoError(t, collr.Init(context.Background()))
128
collr.fetcher = &mockZookeeperFetcher{data: []byte("hello \nand good buy\n")}
129
129
- assert.Nil(t, collr.Collect())
130
+ assert.Nil(t, collr.Collect(context.Background()))
131
}
132
133
func TestCollector_CollectMntrReceiveError(t *testing.T) {
134
collr := New()
134
- require.NoError(t, collr.Init())
135
+ require.NoError(t, collr.Init(context.Background()))
136
collr.fetcher = &mockZookeeperFetcher{err: true}
137
137
- assert.Nil(t, collr.Collect())
138
+ assert.Nil(t, collr.Collect(context.Background()))
139
}
140
141
type mockZookeeperFetcher struct {
src/go/plugin/go.d/examples/simple/main.go
+5
-4
@@ -3,6 +3,7 @@
3
package main
4
5
import (
6
+ "context"
7
"errors"
8
"fmt"
9
"log/slog"
@@ -25,11 +26,11 @@ type example struct {
26
module.Base
27
}
28
28
-func (e *example) Cleanup() {}
29
+func (e *example) Cleanup(context.Context) {}
30
30
-func (e *example) Init() error { return nil }
31
+func (e *example) Init(context.Context) error { return nil }
32
32
-func (e *example) Check() error { return nil }
33
+func (e *example) Check(context.Context) error { return nil }
34
35
func (e *example) Charts() *module.Charts {
36
return &module.Charts{
@@ -45,7 +46,7 @@ func (e *example) Charts() *module.Charts {
46
}
47
func (e *example) Configuration() any { return nil }
48
48
-func (e *example) Collect() map[string]int64 {
49
+func (e *example) Collect(context.Context) map[string]int64 {
50
return map[string]int64{
51
"random0": rand.Int63n(100),
52
"random1": rand.Int63n(100),