From b723a59d2aa176c5b8a20ac7a335f97d74e05f75 Mon Sep 17 00:00:00 2001 From: Ilya Mashchenko Date: Tue, 10 Dec 2024 23:04:59 +0200 Subject: [PATCH] chore(go.d): pass context to init/check/collect/cleanup (#19180) pass ctx to init/check/collect/cleanup --- src/go/plugin/go.d/agent/agent_test.go | 8 +- src/go/plugin/go.d/agent/jobmgr/dyncfg.go | 6 +- src/go/plugin/go.d/agent/jobmgr/sim_test.go | 4 +- src/go/plugin/go.d/agent/module/job.go | 11 +-- src/go/plugin/go.d/agent/module/job_test.go | 33 +++---- src/go/plugin/go.d/agent/module/mock.go | 29 ++++--- src/go/plugin/go.d/agent/module/mock_test.go | 26 +++--- src/go/plugin/go.d/agent/module/module.go | 9 +- .../go.d/collector/activemq/collector.go | 9 +- .../go.d/collector/activemq/collector_test.go | 25 +++--- .../go.d/collector/adaptecraid/collector.go | 9 +- .../collector/adaptecraid/collector_test.go | 17 ++-- src/go/plugin/go.d/collector/ap/collector.go | 9 +- .../go.d/collector/ap/collector_test.go | 17 ++-- .../plugin/go.d/collector/apache/collector.go | 9 +- .../go.d/collector/apache/collector_test.go | 27 +++--- .../plugin/go.d/collector/apcupsd/collect.go | 3 +- .../go.d/collector/apcupsd/collector.go | 9 +- .../go.d/collector/apcupsd/collector_test.go | 25 +++--- .../go.d/collector/beanstalk/collect.go | 3 +- .../go.d/collector/beanstalk/collector.go | 9 +- .../collector/beanstalk/collector_test.go | 19 ++-- .../plugin/go.d/collector/bind/collector.go | 9 +- .../go.d/collector/bind/collector_test.go | 43 +++++---- src/go/plugin/go.d/collector/boinc/collect.go | 3 +- .../plugin/go.d/collector/boinc/collector.go | 9 +- .../go.d/collector/boinc/collector_test.go | 19 ++-- .../go.d/collector/cassandra/collector.go | 9 +- .../collector/cassandra/collector_test.go | 15 ++-- .../plugin/go.d/collector/ceph/collector.go | 9 +- .../go.d/collector/ceph/collector_test.go | 19 ++-- .../plugin/go.d/collector/chrony/collector.go | 9 +- .../go.d/collector/chrony/collector_test.go | 25 +++--- .../go.d/collector/clickhouse/collector.go | 9 +- .../collector/clickhouse/collector_test.go | 17 ++-- .../go.d/collector/cockroachdb/collector.go | 9 +- .../collector/cockroachdb/collector_test.go | 35 ++++---- .../plugin/go.d/collector/consul/collector.go | 9 +- .../go.d/collector/consul/collector_test.go | 27 +++--- .../go.d/collector/coredns/collector.go | 9 +- .../go.d/collector/coredns/collector_test.go | 41 ++++----- .../go.d/collector/couchbase/collector.go | 9 +- .../collector/couchbase/collector_test.go | 19 ++-- .../go.d/collector/couchdb/collector.go | 9 +- .../go.d/collector/couchdb/collector_test.go | 21 ++--- .../go.d/collector/dmcache/collector.go | 9 +- .../go.d/collector/dmcache/collector_test.go | 17 ++-- .../go.d/collector/dnsdist/collector.go | 9 +- .../go.d/collector/dnsdist/collector_test.go | 19 ++-- .../go.d/collector/dnsmasq/collector.go | 9 +- .../go.d/collector/dnsmasq/collector_test.go | 19 ++-- .../go.d/collector/dnsmasq_dhcp/collector.go | 9 +- .../collector/dnsmasq_dhcp/collector_test.go | 29 ++++--- .../go.d/collector/dnsquery/collector.go | 9 +- .../go.d/collector/dnsquery/collector_test.go | 17 ++-- .../plugin/go.d/collector/docker/collector.go | 8 +- .../go.d/collector/docker/collector_test.go | 22 ++--- .../go.d/collector/docker_engine/collector.go | 9 +- .../collector/docker_engine/collector_test.go | 33 +++---- .../go.d/collector/dockerhub/collector.go | 9 +- .../collector/dockerhub/collector_test.go | 29 ++++--- .../go.d/collector/dovecot/collector.go | 9 +- .../go.d/collector/dovecot/collector_test.go | 19 ++-- .../go.d/collector/elasticsearch/collector.go | 9 +- .../collector/elasticsearch/collector_test.go | 21 ++--- .../plugin/go.d/collector/envoy/collector.go | 9 +- .../go.d/collector/envoy/collector_test.go | 25 +++--- .../plugin/go.d/collector/exim/collector.go | 9 +- .../go.d/collector/exim/collector_test.go | 17 ++-- .../go.d/collector/fail2ban/collector.go | 9 +- .../go.d/collector/fail2ban/collector_test.go | 17 ++-- .../go.d/collector/filecheck/collector.go | 9 +- .../collector/filecheck/collector_test.go | 15 ++-- .../go.d/collector/fluentd/collector.go | 9 +- .../go.d/collector/fluentd/collector_test.go | 29 ++++--- .../go.d/collector/freeradius/collector.go | 9 +- .../collector/freeradius/collector_test.go | 19 ++-- .../plugin/go.d/collector/gearman/collect.go | 5 +- .../go.d/collector/gearman/collector.go | 9 +- .../go.d/collector/gearman/collector_test.go | 19 ++-- .../plugin/go.d/collector/geth/collector.go | 9 +- .../go.d/collector/haproxy/collector.go | 9 +- .../go.d/collector/haproxy/collector_test.go | 21 ++--- .../go.d/collector/hddtemp/collector.go | 9 +- .../go.d/collector/hddtemp/collector_test.go | 17 ++-- .../plugin/go.d/collector/hdfs/collector.go | 9 +- .../go.d/collector/hdfs/collector_test.go | 55 ++++++------ .../plugin/go.d/collector/hpssa/collector.go | 9 +- .../go.d/collector/hpssa/collector_test.go | 17 ++-- .../go.d/collector/httpcheck/collector.go | 9 +- .../collector/httpcheck/collector_test.go | 23 ++--- .../go.d/collector/icecast/collector.go | 9 +- .../go.d/collector/icecast/collector_test.go | 23 ++--- .../go.d/collector/intelgpu/collector.go | 9 +- .../go.d/collector/intelgpu/collector_test.go | 17 ++-- .../plugin/go.d/collector/ipfs/collector.go | 9 +- .../go.d/collector/ipfs/collector_test.go | 19 ++-- .../go.d/collector/isc_dhcpd/collector.go | 9 +- .../collector/isc_dhcpd/collector_test.go | 19 ++-- .../go.d/collector/k8s_kubelet/collector.go | 9 +- .../collector/k8s_kubelet/collector_test.go | 31 +++---- .../go.d/collector/k8s_kubeproxy/collector.go | 9 +- .../collector/k8s_kubeproxy/collector_test.go | 29 ++++--- .../go.d/collector/k8s_state/collector.go | 8 +- .../collector/k8s_state/collector_test.go | 42 ++++----- .../go.d/collector/lighttpd/collector.go | 9 +- .../go.d/collector/lighttpd/collector_test.go | 33 +++---- .../go.d/collector/litespeed/collector.go | 10 +-- .../collector/litespeed/collector_test.go | 11 +-- .../plugin/go.d/collector/logind/collector.go | 9 +- .../go.d/collector/logind/collector_test.go | 23 ++--- .../go.d/collector/logstash/collector.go | 9 +- .../go.d/collector/logstash/collector_test.go | 21 ++--- src/go/plugin/go.d/collector/lvm/collector.go | 9 +- .../go.d/collector/lvm/collector_test.go | 17 ++-- .../go.d/collector/maxscale/collector.go | 9 +- .../go.d/collector/maxscale/collector_test.go | 23 ++--- .../go.d/collector/megacli/collector.go | 9 +- .../go.d/collector/megacli/collector_test.go | 17 ++-- .../go.d/collector/memcached/collector.go | 9 +- .../collector/memcached/collector_test.go | 19 ++-- .../go.d/collector/mongodb/collector.go | 9 +- .../go.d/collector/mongodb/collector_test.go | 21 ++--- .../plugin/go.d/collector/monit/collector.go | 9 +- .../go.d/collector/monit/collector_test.go | 23 ++--- .../plugin/go.d/collector/mysql/collector.go | 9 +- .../go.d/collector/mysql/collector_test.go | 31 +++---- .../plugin/go.d/collector/nginx/collector.go | 9 +- .../go.d/collector/nginx/collector_test.go | 33 +++---- .../go.d/collector/nginxplus/collector.go | 10 +-- .../collector/nginxplus/collector_test.go | 19 ++-- .../go.d/collector/nginxunit/collector.go | 9 +- .../collector/nginxunit/collector_test.go | 23 ++--- .../go.d/collector/nginxvts/collector.go | 9 +- .../go.d/collector/nginxvts/collector_test.go | 21 ++--- src/go/plugin/go.d/collector/nsd/collector.go | 9 +- .../go.d/collector/nsd/collector_test.go | 17 ++-- .../plugin/go.d/collector/ntpd/collector.go | 9 +- .../go.d/collector/ntpd/collector_test.go | 26 +++--- .../go.d/collector/nvidia_smi/collector.go | 9 +- .../collector/nvidia_smi/collector_test.go | 21 ++--- .../plugin/go.d/collector/nvme/collector.go | 9 +- .../go.d/collector/nvme/collector_test.go | 23 ++--- .../plugin/go.d/collector/openldap/collect.go | 5 +- .../go.d/collector/openldap/collector.go | 9 +- .../go.d/collector/openldap/collector_test.go | 19 ++-- .../go.d/collector/openvpn/collector.go | 11 +-- .../go.d/collector/openvpn/collector_test.go | 27 +++--- .../collector/openvpn_status_log/collector.go | 9 +- .../openvpn_status_log/collector_test.go | 23 ++--- .../go.d/collector/oracledb/collector.go | 9 +- .../go.d/collector/oracledb/collector_test.go | 17 ++-- .../go.d/collector/pgbouncer/collector.go | 9 +- .../collector/pgbouncer/collector_test.go | 21 ++--- .../go.d/collector/phpdaemon/collector.go | 9 +- .../collector/phpdaemon/collector_test.go | 31 +++---- .../plugin/go.d/collector/phpfpm/collector.go | 9 +- .../go.d/collector/phpfpm/collector_test.go | 45 +++++----- .../plugin/go.d/collector/pihole/collector.go | 9 +- .../go.d/collector/pihole/collector_test.go | 17 ++-- .../plugin/go.d/collector/pika/collector.go | 8 +- .../go.d/collector/pika/collector_test.go | 24 ++--- .../plugin/go.d/collector/ping/collector.go | 9 +- .../go.d/collector/ping/collector_test.go | 17 ++-- .../go.d/collector/portcheck/collector.go | 9 +- .../collector/portcheck/collector_test.go | 27 +++--- .../go.d/collector/postfix/collector.go | 9 +- .../go.d/collector/postfix/collector_test.go | 17 ++-- .../go.d/collector/postgres/collector.go | 9 +- .../go.d/collector/postgres/collector_test.go | 21 ++--- .../go.d/collector/powerdns/collector.go | 9 +- .../go.d/collector/powerdns/collector_test.go | 19 ++-- .../collector/powerdns_recursor/collector.go | 9 +- .../powerdns_recursor/collector_test.go | 19 ++-- .../go.d/collector/prometheus/collector.go | 9 +- .../collector/prometheus/collector_test.go | 21 ++--- .../go.d/collector/proxysql/collector.go | 9 +- .../go.d/collector/proxysql/collector_test.go | 17 ++-- .../plugin/go.d/collector/pulsar/collector.go | 9 +- .../go.d/collector/pulsar/collector_test.go | 35 ++++---- .../plugin/go.d/collector/puppet/collector.go | 9 +- .../go.d/collector/puppet/collector_test.go | 19 ++-- .../go.d/collector/rabbitmq/collector.go | 9 +- .../go.d/collector/rabbitmq/collector_test.go | 21 ++--- .../plugin/go.d/collector/redis/collector.go | 8 +- .../go.d/collector/redis/collector_test.go | 24 ++--- .../go.d/collector/rethinkdb/collector.go | 9 +- .../collector/rethinkdb/collector_test.go | 23 ++--- .../plugin/go.d/collector/riakkv/collector.go | 9 +- .../go.d/collector/riakkv/collector_test.go | 21 ++--- .../plugin/go.d/collector/rspamd/collector.go | 9 +- .../go.d/collector/rspamd/collector_test.go | 19 ++-- .../plugin/go.d/collector/samba/collector.go | 9 +- .../go.d/collector/samba/collector_test.go | 17 ++-- .../go.d/collector/scaleio/collector.go | 9 +- .../go.d/collector/scaleio/collector_test.go | 33 +++---- .../go.d/collector/sensors/collector.go | 9 +- .../go.d/collector/sensors/collector_test.go | 17 ++-- .../go.d/collector/smartctl/collector.go | 9 +- .../go.d/collector/smartctl/collector_test.go | 17 ++-- .../plugin/go.d/collector/snmp/collector.go | 9 +- .../go.d/collector/snmp/collector_test.go | 25 +++--- .../plugin/go.d/collector/spigotmc/collect.go | 5 +- .../go.d/collector/spigotmc/collector.go | 9 +- .../go.d/collector/spigotmc/collector_test.go | 19 ++-- .../plugin/go.d/collector/squid/collector.go | 9 +- .../go.d/collector/squid/collector_test.go | 19 ++-- .../go.d/collector/squidlog/collector.go | 9 +- .../go.d/collector/squidlog/collector_test.go | 35 ++++---- src/go/plugin/go.d/collector/squidlog/init.go | 3 +- .../go.d/collector/storcli/collector.go | 9 +- .../go.d/collector/storcli/collector_test.go | 17 ++-- .../go.d/collector/supervisord/collector.go | 9 +- .../collector/supervisord/collector_test.go | 29 ++++--- .../go.d/collector/systemdunits/collector.go | 9 +- .../collector/systemdunits/collector_test.go | 26 +++--- .../go.d/collector/tengine/collector.go | 9 +- .../go.d/collector/tengine/collector_test.go | 27 +++--- .../go.d/collector/testrandom/collector.go | 9 +- .../collector/testrandom/collector_test.go | 19 ++-- .../plugin/go.d/collector/tomcat/collector.go | 9 +- .../go.d/collector/tomcat/collector_test.go | 19 ++-- src/go/plugin/go.d/collector/tor/collect.go | 3 +- src/go/plugin/go.d/collector/tor/collector.go | 9 +- .../go.d/collector/tor/collector_test.go | 19 ++-- .../go.d/collector/traefik/collector.go | 9 +- .../go.d/collector/traefik/collector_test.go | 23 ++--- .../go.d/collector/typesense/collector.go | 9 +- .../collector/typesense/collector_test.go | 25 +++--- .../go.d/collector/unbound/collector.go | 9 +- .../go.d/collector/unbound/collector_test.go | 55 ++++++------ .../plugin/go.d/collector/upsd/collector.go | 9 +- .../go.d/collector/upsd/collector_test.go | 29 ++++--- .../plugin/go.d/collector/uwsgi/collector.go | 9 +- .../go.d/collector/uwsgi/collector_test.go | 17 ++-- .../go.d/collector/varnish/collector.go | 9 +- .../go.d/collector/varnish/collector_test.go | 17 ++-- .../plugin/go.d/collector/vcsa/collector.go | 9 +- .../go.d/collector/vcsa/collector_test.go | 41 ++++----- .../go.d/collector/vernemq/collector.go | 9 +- .../go.d/collector/vernemq/collector_test.go | 25 +++--- .../go.d/collector/vsphere/collector.go | 9 +- .../go.d/collector/vsphere/collector_test.go | 45 +++++----- .../go.d/collector/w1sensor/collector.go | 9 +- .../go.d/collector/w1sensor/collector_test.go | 17 ++-- .../plugin/go.d/collector/weblog/collector.go | 9 +- .../go.d/collector/weblog/collector_test.go | 87 ++++++++++--------- src/go/plugin/go.d/collector/weblog/init.go | 3 +- .../go.d/collector/whoisquery/collector.go | 9 +- .../collector/whoisquery/collector_test.go | 21 ++--- .../go.d/collector/windows/collector.go | 9 +- .../go.d/collector/windows/collector_test.go | 17 ++-- .../go.d/collector/wireguard/collector.go | 9 +- .../collector/wireguard/collector_test.go | 53 +++++------ .../go.d/collector/x509check/collector.go | 9 +- .../collector/x509check/collector_test.go | 21 ++--- .../go.d/collector/zfspool/collector.go | 9 +- .../go.d/collector/zfspool/collector_test.go | 17 ++-- .../go.d/collector/zookeeper/collector.go | 9 +- .../collector/zookeeper/collector_test.go | 35 ++++---- src/go/plugin/go.d/examples/simple/main.go | 9 +- 261 files changed, 2232 insertions(+), 1973 deletions(-) diff --git a/src/go/plugin/go.d/agent/agent_test.go b/src/go/plugin/go.d/agent/agent_test.go index 39e12f751c36f9..2122ead756f4f2 100644 --- a/src/go/plugin/go.d/agent/agent_test.go +++ b/src/go/plugin/go.d/agent/agent_test.go @@ -68,13 +68,13 @@ func prepareRegistry(mux *sync.Mutex, stats map[string]int, names ...string) mod func prepareMockModule(name string, mux *sync.Mutex, stats map[string]int) module.Module { return &module.MockModule{ - InitFunc: func() error { + InitFunc: func(context.Context) error { mux.Lock() defer mux.Unlock() stats[name+"_init"]++ return nil }, - CheckFunc: func() error { + CheckFunc: func(context.Context) error { mux.Lock() defer mux.Unlock() stats[name+"_check"]++ @@ -88,13 +88,13 @@ func prepareMockModule(name string, mux *sync.Mutex, stats map[string]int) modul &module.Chart{ID: "id", Title: "title", Units: "units", Dims: module.Dims{{ID: "id1"}}}, } }, - CollectFunc: func() map[string]int64 { + CollectFunc: func(context.Context) map[string]int64 { mux.Lock() defer mux.Unlock() stats[name+"_collect"]++ return map[string]int64{"id1": 1} }, - CleanupFunc: func() { + CleanupFunc: func(context.Context) { mux.Lock() defer mux.Unlock() stats[name+"_cleanup"]++ diff --git a/src/go/plugin/go.d/agent/jobmgr/dyncfg.go b/src/go/plugin/go.d/agent/jobmgr/dyncfg.go index e8eb59f78d16cf..4df7909cf59264 100644 --- a/src/go/plugin/go.d/agent/jobmgr/dyncfg.go +++ b/src/go/plugin/go.d/agent/jobmgr/dyncfg.go @@ -265,13 +265,13 @@ func (m *Manager) dyncfgConfigTest(fn functions.Function) { slog.String("job", cfg.Name()), ) - defer job.Cleanup() + defer job.Cleanup(context.Background()) - if err := job.Init(); err != nil { + if err := job.Init(context.Background()); err != nil { m.dyncfgRespf(fn, 422, "Job initialization failed: %v", err) return } - if err := job.Check(); err != nil { + if err := job.Check(context.Background()); err != nil { m.dyncfgRespf(fn, 422, "Job check failed: %v", err) return } diff --git a/src/go/plugin/go.d/agent/jobmgr/sim_test.go b/src/go/plugin/go.d/agent/jobmgr/sim_test.go index 63369c33ffad92..da966af4a4cb75 100644 --- a/src/go/plugin/go.d/agent/jobmgr/sim_test.go +++ b/src/go/plugin/go.d/agent/jobmgr/sim_test.go @@ -134,7 +134,7 @@ func prepareMockRegistry() module.Registry { ChartsFunc: func() *module.Charts { return &module.Charts{&module.Chart{ID: "id", Title: "title", Units: "units", Dims: module.Dims{{ID: "id1"}}}} }, - CollectFunc: func() map[string]int64 { return map[string]int64{"id1": 1} }, + CollectFunc: func(context.Context) map[string]int64 { return map[string]int64{"id1": 1} }, } }, Config: func() any { @@ -144,7 +144,7 @@ func prepareMockRegistry() module.Registry { reg.Register("fail", module.Creator{ Create: func() module.Module { return &module.MockModule{ - InitFunc: func() error { return errors.New("mock failed init") }, + InitFunc: func(context.Context) error { return errors.New("mock failed init") }, } }, }) diff --git a/src/go/plugin/go.d/agent/module/job.go b/src/go/plugin/go.d/agent/module/job.go index cf6952895f6730..7d058976055bd0 100644 --- a/src/go/plugin/go.d/agent/module/job.go +++ b/src/go/plugin/go.d/agent/module/job.go @@ -4,6 +4,7 @@ package module import ( "bytes" + "context" "errors" "fmt" "io" @@ -224,7 +225,7 @@ func (j *Job) AutoDetection() (err error) { } } if err != nil { - j.module.Cleanup() + j.module.Cleanup(context.TODO()) } }() @@ -282,7 +283,7 @@ LOOP: } } } - j.module.Cleanup() + j.module.Cleanup(context.TODO()) j.Cleanup() j.stop <- struct{}{} } @@ -342,7 +343,7 @@ func (j *Job) init() error { return nil } - if err := j.module.Init(); err != nil { + if err := j.module.Init(context.TODO()); err != nil { return err } @@ -352,7 +353,7 @@ func (j *Job) init() error { } func (j *Job) check() error { - if err := j.module.Check(); err != nil { + if err := j.module.Check(context.TODO()); err != nil { if j.AutoDetectTries != infTries { j.AutoDetectTries-- } @@ -405,7 +406,7 @@ func (j *Job) collect() (result map[string]int64) { } } }() - return j.module.Collect() + return j.module.Collect(context.TODO()) } func (j *Job) processMetrics(metrics map[string]int64, startTime time.Time, sinceLastRun int) bool { diff --git a/src/go/plugin/go.d/agent/module/job_test.go b/src/go/plugin/go.d/agent/module/job_test.go index c87f840d5ee1b6..835ac5df5c79f1 100644 --- a/src/go/plugin/go.d/agent/module/job_test.go +++ b/src/go/plugin/go.d/agent/module/job_test.go @@ -3,6 +3,7 @@ package module import ( + "context" "errors" "fmt" "io" @@ -73,10 +74,10 @@ func TestJob_AutoDetectionEvery(t *testing.T) { func TestJob_RetryAutoDetection(t *testing.T) { job := newTestJob() m := &MockModule{ - InitFunc: func() error { + InitFunc: func(context.Context) error { return nil }, - CheckFunc: func() error { return errors.New("check error") }, + CheckFunc: func(context.Context) error { return errors.New("check error") }, ChartsFunc: func() *Charts { return &Charts{} }, @@ -104,11 +105,11 @@ func TestJob_AutoDetection(t *testing.T) { job := newTestJob() var v int m := &MockModule{ - InitFunc: func() error { + InitFunc: func(context.Context) error { v++ return nil }, - CheckFunc: func() error { + CheckFunc: func(context.Context) error { v++ return nil }, @@ -126,7 +127,7 @@ func TestJob_AutoDetection(t *testing.T) { func TestJob_AutoDetection_FailInit(t *testing.T) { job := newTestJob() m := &MockModule{ - InitFunc: func() error { + InitFunc: func(context.Context) error { return errors.New("init error") }, } @@ -139,10 +140,10 @@ func TestJob_AutoDetection_FailInit(t *testing.T) { func TestJob_AutoDetection_FailCheck(t *testing.T) { job := newTestJob() m := &MockModule{ - InitFunc: func() error { + InitFunc: func(context.Context) error { return nil }, - CheckFunc: func() error { + CheckFunc: func(context.Context) error { return errors.New("check error") }, } @@ -155,10 +156,10 @@ func TestJob_AutoDetection_FailCheck(t *testing.T) { func TestJob_AutoDetection_FailPostCheck(t *testing.T) { job := newTestJob() m := &MockModule{ - InitFunc: func() error { + InitFunc: func(context.Context) error { return nil }, - CheckFunc: func() error { + CheckFunc: func(context.Context) error { return nil }, ChartsFunc: func() *Charts { @@ -174,7 +175,7 @@ func TestJob_AutoDetection_FailPostCheck(t *testing.T) { func TestJob_AutoDetection_PanicInit(t *testing.T) { job := newTestJob() m := &MockModule{ - InitFunc: func() error { + InitFunc: func(context.Context) error { panic("panic in Init") }, } @@ -187,10 +188,10 @@ func TestJob_AutoDetection_PanicInit(t *testing.T) { func TestJob_AutoDetection_PanicCheck(t *testing.T) { job := newTestJob() m := &MockModule{ - InitFunc: func() error { + InitFunc: func(context.Context) error { return nil }, - CheckFunc: func() error { + CheckFunc: func(context.Context) error { panic("panic in Check") }, } @@ -203,10 +204,10 @@ func TestJob_AutoDetection_PanicCheck(t *testing.T) { func TestJob_AutoDetection_PanicPostCheck(t *testing.T) { job := newTestJob() m := &MockModule{ - InitFunc: func() error { + InitFunc: func(context.Context) error { return nil }, - CheckFunc: func() error { + CheckFunc: func(context.Context) error { return nil }, ChartsFunc: func() *Charts { @@ -234,7 +235,7 @@ func TestJob_Start(t *testing.T) { }, } }, - CollectFunc: func() map[string]int64 { + CollectFunc: func(context.Context) map[string]int64 { return map[string]int64{ "id1": 1, "id2": 2, @@ -261,7 +262,7 @@ func TestJob_Start(t *testing.T) { func TestJob_MainLoop_Panic(t *testing.T) { m := &MockModule{ - CollectFunc: func() map[string]int64 { + CollectFunc: func(context.Context) map[string]int64 { panic("panic in Collect") }, } diff --git a/src/go/plugin/go.d/agent/module/mock.go b/src/go/plugin/go.d/agent/module/mock.go index f83c7dbccd267c..c5f15ae2916b2a 100644 --- a/src/go/plugin/go.d/agent/module/mock.go +++ b/src/go/plugin/go.d/agent/module/mock.go @@ -2,7 +2,10 @@ package module -import "errors" +import ( + "context" + "errors" +) const MockConfigSchema = ` { @@ -38,31 +41,31 @@ type MockModule struct { FailOnInit bool - InitFunc func() error - CheckFunc func() error + InitFunc func(context.Context) error + CheckFunc func(context.Context) error ChartsFunc func() *Charts - CollectFunc func() map[string]int64 - CleanupFunc func() + CollectFunc func(context.Context) map[string]int64 + CleanupFunc func(context.Context) CleanupDone bool } // Init invokes InitFunc. -func (m *MockModule) Init() error { +func (m *MockModule) Init(ctx context.Context) error { if m.FailOnInit { return errors.New("mock init error") } if m.InitFunc == nil { return nil } - return m.InitFunc() + return m.InitFunc(ctx) } // Check invokes CheckFunc. -func (m *MockModule) Check() error { +func (m *MockModule) Check(ctx context.Context) error { if m.CheckFunc == nil { return nil } - return m.CheckFunc() + return m.CheckFunc(ctx) } // Charts invokes ChartsFunc. @@ -74,17 +77,17 @@ func (m *MockModule) Charts() *Charts { } // Collect invokes CollectDunc. -func (m *MockModule) Collect() map[string]int64 { +func (m *MockModule) Collect(ctx context.Context) map[string]int64 { if m.CollectFunc == nil { return nil } - return m.CollectFunc() + return m.CollectFunc(ctx) } // Cleanup sets CleanupDone to true. -func (m *MockModule) Cleanup() { +func (m *MockModule) Cleanup(ctx context.Context) { if m.CleanupFunc != nil { - m.CleanupFunc() + m.CleanupFunc(ctx) } m.CleanupDone = true } diff --git a/src/go/plugin/go.d/agent/module/mock_test.go b/src/go/plugin/go.d/agent/module/mock_test.go index d7521911fc98fb..2f2bbcbad0d7aa 100644 --- a/src/go/plugin/go.d/agent/module/mock_test.go +++ b/src/go/plugin/go.d/agent/module/mock_test.go @@ -3,6 +3,7 @@ package module import ( + "context" "testing" "github.com/stretchr/testify/assert" @@ -11,18 +12,20 @@ import ( func TestMockModule_Init(t *testing.T) { m := &MockModule{} + ctx := context.Background() - assert.NoError(t, m.Init()) - m.InitFunc = func() error { return nil } - assert.NoError(t, m.Init()) + assert.NoError(t, m.Init(ctx)) + m.InitFunc = func(context.Context) error { return nil } + assert.NoError(t, m.Init(ctx)) } func TestMockModule_Check(t *testing.T) { m := &MockModule{} + ctx := context.Background() - assert.NoError(t, m.Check()) - m.CheckFunc = func() error { return nil } - assert.NoError(t, m.Check()) + assert.NoError(t, m.Check(ctx)) + m.CheckFunc = func(context.Context) error { return nil } + assert.NoError(t, m.Check(ctx)) } func TestMockModule_Charts(t *testing.T) { @@ -39,16 +42,19 @@ func TestMockModule_Collect(t *testing.T) { d := map[string]int64{ "1": 1, } + ctx := context.Background() - assert.Nil(t, m.Collect()) - m.CollectFunc = func() map[string]int64 { return d } - assert.Equal(t, d, m.Collect()) + assert.Nil(t, m.Collect(ctx)) + m.CollectFunc = func(ctx context.Context) map[string]int64 { return d } + assert.Equal(t, d, m.Collect(ctx)) } func TestMockModule_Cleanup(t *testing.T) { m := &MockModule{} + ctx := context.Background() + require.False(t, m.CleanupDone) - m.Cleanup() + m.Cleanup(ctx) assert.True(t, m.CleanupDone) } diff --git a/src/go/plugin/go.d/agent/module/module.go b/src/go/plugin/go.d/agent/module/module.go index 8d28d805974346..dbbe7f2fb56498 100644 --- a/src/go/plugin/go.d/agent/module/module.go +++ b/src/go/plugin/go.d/agent/module/module.go @@ -3,6 +3,7 @@ package module import ( + "context" "encoding/json" "testing" @@ -18,20 +19,20 @@ import ( type Module interface { // Init does initialization. // If it returns error, the job will be disabled. - Init() error + Init(context.Context) error // Check is called after Init. // If it returns error, the job will be disabled. - Check() error + Check(context.Context) error // Charts returns the chart definition. Charts() *Charts // Collect collects metrics. - Collect() map[string]int64 + Collect(context.Context) map[string]int64 // Cleanup Cleanup - Cleanup() + Cleanup(context.Context) GetBase() *Base diff --git a/src/go/plugin/go.d/collector/activemq/collector.go b/src/go/plugin/go.d/collector/activemq/collector.go index 01543d7ae9dc5b..025752baa5381f 100644 --- a/src/go/plugin/go.d/collector/activemq/collector.go +++ b/src/go/plugin/go.d/collector/activemq/collector.go @@ -3,6 +3,7 @@ package activemq import ( + "context" _ "embed" "errors" "fmt" @@ -74,7 +75,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -101,7 +102,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -117,13 +118,13 @@ func (c *Collector) Charts() *Charts { return c.charts } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.apiClient != nil && c.apiClient.httpClient != nil { c.apiClient.httpClient.CloseIdleConnections() } } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { diff --git a/src/go/plugin/go.d/collector/activemq/collector_test.go b/src/go/plugin/go.d/collector/activemq/collector_test.go index d37b598c39cd7a..9060b129ccf374 100644 --- a/src/go/plugin/go.d/collector/activemq/collector_test.go +++ b/src/go/plugin/go.d/collector/activemq/collector_test.go @@ -3,6 +3,7 @@ package activemq import ( + "context" "net/http" "net/http/httptest" "os" @@ -156,11 +157,11 @@ func TestCollector_Init(t *testing.T) { // NG case collr.Webadmin = "" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) // OK case collr.Webadmin = "webadmin" - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.apiClient) } @@ -181,8 +182,8 @@ func TestCollector_Check(t *testing.T) { collr.HTTPConfig.RequestConfig = web.RequestConfig{URL: ts.URL} collr.Webadmin = "webadmin" - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) } func TestCollector_Charts(t *testing.T) { @@ -190,7 +191,7 @@ func TestCollector_Charts(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - New().Cleanup() + New().Cleanup(context.Background()) } func TestCollector_Collect(t *testing.T) { @@ -214,8 +215,8 @@ func TestCollector_Collect(t *testing.T) { collr.HTTPConfig.RequestConfig = web.RequestConfig{URL: ts.URL} collr.Webadmin = "webadmin" - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) cases := []struct { expected map[string]int64 @@ -303,7 +304,7 @@ func TestCollector_Collect(t *testing.T) { } for _, c := range cases { - require.Equal(t, c.expected, collr.Collect()) + require.Equal(t, c.expected, collr.Collect(context.Background())) assert.Len(t, collr.activeQueues, c.numQueues) assert.Len(t, collr.activeTopics, c.numTopics) assert.Len(t, *collr.charts, c.numCharts) @@ -321,8 +322,8 @@ func TestCollector_404(t *testing.T) { collr.Webadmin = "webadmin" collr.HTTPConfig.RequestConfig = web.RequestConfig{URL: ts.URL} - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_InvalidData(t *testing.T) { @@ -335,6 +336,6 @@ func TestCollector_InvalidData(t *testing.T) { collr.Webadmin = "webadmin" collr.HTTPConfig.RequestConfig = web.RequestConfig{URL: ts.URL} - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } diff --git a/src/go/plugin/go.d/collector/adaptecraid/collector.go b/src/go/plugin/go.d/collector/adaptecraid/collector.go index 35a6649edbc8fe..eedb65ac44abf3 100644 --- a/src/go/plugin/go.d/collector/adaptecraid/collector.go +++ b/src/go/plugin/go.d/collector/adaptecraid/collector.go @@ -5,6 +5,7 @@ package adaptecraid import ( + "context" _ "embed" "errors" "fmt" @@ -60,7 +61,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { arcconf, err := c.initArcconfCliExec() if err != nil { return fmt.Errorf("arcconf exec initialization: %v", err) @@ -70,7 +71,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -87,7 +88,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -100,4 +101,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/adaptecraid/collector_test.go b/src/go/plugin/go.d/collector/adaptecraid/collector_test.go index d9ec27f71ec258..d6d7c971e68f59 100644 --- a/src/go/plugin/go.d/collector/adaptecraid/collector_test.go +++ b/src/go/plugin/go.d/collector/adaptecraid/collector_test.go @@ -5,6 +5,7 @@ package adaptecraid import ( + "context" "errors" "os" "testing" @@ -59,9 +60,9 @@ func TestCollector_Init(t *testing.T) { collr := New() if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -80,7 +81,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOkCurrent() - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -88,7 +89,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOkCurrent() - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -98,7 +99,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -141,9 +142,9 @@ func TestCollector_Check(t *testing.T) { collr.exec = mock if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -218,7 +219,7 @@ func TestCollector_Collect(t *testing.T) { mock := test.prepareMock() collr.exec = mock - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantMetrics, mx) assert.Len(t, *collr.Charts(), test.wantCharts) diff --git a/src/go/plugin/go.d/collector/ap/collector.go b/src/go/plugin/go.d/collector/ap/collector.go index 6fd4da24e3a428..ab3e494d8923ac 100644 --- a/src/go/plugin/go.d/collector/ap/collector.go +++ b/src/go/plugin/go.d/collector/ap/collector.go @@ -5,6 +5,7 @@ package ap import ( + "context" _ "embed" "errors" "fmt" @@ -60,7 +61,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %s", err) } @@ -74,7 +75,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -91,7 +92,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -104,4 +105,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/ap/collector_test.go b/src/go/plugin/go.d/collector/ap/collector_test.go index a2e94fb894d4f4..651da39465c6a4 100644 --- a/src/go/plugin/go.d/collector/ap/collector_test.go +++ b/src/go/plugin/go.d/collector/ap/collector_test.go @@ -5,6 +5,7 @@ package ap import ( + "context" "errors" "os" "testing" @@ -66,9 +67,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -87,7 +88,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOk() - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -95,7 +96,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOk() - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -105,7 +106,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -147,9 +148,9 @@ func TestCollector_Check(t *testing.T) { collr.exec = test.prepareMock() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -210,7 +211,7 @@ func TestCollector_Collect(t *testing.T) { collr := New() collr.exec = test.prepareMock() - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantMetrics, mx) diff --git a/src/go/plugin/go.d/collector/apache/collector.go b/src/go/plugin/go.d/collector/apache/collector.go index c4da9b3125cc50..dd1b36a2ea8c29 100644 --- a/src/go/plugin/go.d/collector/apache/collector.go +++ b/src/go/plugin/go.d/collector/apache/collector.go @@ -3,6 +3,7 @@ package apache import ( + "context" _ "embed" "errors" "fmt" @@ -63,7 +64,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -80,7 +81,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -96,7 +97,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -108,7 +109,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/apache/collector_test.go b/src/go/plugin/go.d/collector/apache/collector_test.go index b697f01621beee..8899fb550bf33a 100644 --- a/src/go/plugin/go.d/collector/apache/collector_test.go +++ b/src/go/plugin/go.d/collector/apache/collector_test.go @@ -3,6 +3,7 @@ package apache import ( + "context" "net/http" "net/http/httptest" "os" @@ -76,9 +77,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -125,9 +126,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -247,9 +248,9 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare(t) defer cleanup() - _ = collr.Check() + _ = collr.Check(context.Background()) - collected := collr.Collect() + collected := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, collected) assert.Equal(t, test.wantNumOfCharts, len(*collr.Charts())) @@ -265,7 +266,7 @@ func caseMPMEventSimpleStatus(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL + "/server-status?auto" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -278,7 +279,7 @@ func caseMPMEventExtendedStatus(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL + "/server-status?auto" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -291,7 +292,7 @@ func caseMPMPreforkExtendedStatus(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL + "/server-status?auto" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -304,7 +305,7 @@ func caseLighttpdResponse(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL + "/server-status?auto" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -317,7 +318,7 @@ func caseInvalidDataResponse(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL + "/server-status?auto" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -326,7 +327,7 @@ func caseConnectionRefused(t *testing.T) (*Collector, func()) { t.Helper() collr := New() collr.URL = "http://127.0.0.1:65001/server-status?auto" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } @@ -339,7 +340,7 @@ func case404(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL + "/server-status?auto" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } diff --git a/src/go/plugin/go.d/collector/apcupsd/collect.go b/src/go/plugin/go.d/collector/apcupsd/collect.go index fb4c934ced9631..18f1354d7f36e1 100644 --- a/src/go/plugin/go.d/collector/apcupsd/collect.go +++ b/src/go/plugin/go.d/collector/apcupsd/collect.go @@ -3,6 +3,7 @@ package apcupsd import ( + "context" "errors" "fmt" "strings" @@ -22,7 +23,7 @@ func (c *Collector) collect() (map[string]int64, error) { resp, err := c.conn.status() if err != nil { - c.Cleanup() + c.Cleanup(context.Background()) return nil, err } diff --git a/src/go/plugin/go.d/collector/apcupsd/collector.go b/src/go/plugin/go.d/collector/apcupsd/collector.go index bc2a91425a2cb0..01bd288052a17a 100644 --- a/src/go/plugin/go.d/collector/apcupsd/collector.go +++ b/src/go/plugin/go.d/collector/apcupsd/collector.go @@ -3,6 +3,7 @@ package apcupsd import ( + "context" _ "embed" "errors" "time" @@ -53,7 +54,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.Address == "" { return errors.New("config: 'address' not set") } @@ -61,7 +62,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -76,7 +77,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -88,7 +89,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.conn != nil { if err := c.conn.disconnect(); err != nil { c.Warningf("error on disconnect: %v", err) diff --git a/src/go/plugin/go.d/collector/apcupsd/collector_test.go b/src/go/plugin/go.d/collector/apcupsd/collector_test.go index c9856d5ea9996a..4df4cca78c272a 100644 --- a/src/go/plugin/go.d/collector/apcupsd/collector_test.go +++ b/src/go/plugin/go.d/collector/apcupsd/collector_test.go @@ -3,6 +3,7 @@ package apcupsd import ( + "context" "errors" "os" "strings" @@ -39,14 +40,14 @@ func TestCollector_ConfigurationSerialize(t *testing.T) { func TestCollector_Cleanup(t *testing.T) { collr := New() - require.NotPanics(t, collr.Cleanup) + require.NotPanics(t, func() { collr.Cleanup(context.Background()) }) mock := prepareMockOk() collr.newConn = func(Config) apcupsdConn { return mock } - require.NoError(t, collr.Init()) - _ = collr.Collect() - require.NotPanics(t, collr.Cleanup) + require.NoError(t, collr.Init(context.Background())) + _ = collr.Collect(context.Background()) + require.NotPanics(t, func() { collr.Cleanup(context.Background()) }) assert.True(t, mock.calledDisconnect) } @@ -71,9 +72,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -107,12 +108,12 @@ func TestCollector_Check(t *testing.T) { collr := New() collr.newConn = func(Config) apcupsdConn { return test.prepareMock() } - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -120,7 +121,7 @@ func TestCollector_Check(t *testing.T) { func TestCollector_Charts(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.Charts()) } @@ -205,12 +206,12 @@ func TestCollector_Collect(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) mock := test.prepareMock() collr.newConn = func(Config) apcupsdConn { return mock } - mx := collr.Collect() + mx := collr.Collect(context.Background()) if _, ok := mx["battery_seconds_since_replacement"]; ok { mx["battery_seconds_since_replacement"] = 86400 diff --git a/src/go/plugin/go.d/collector/beanstalk/collect.go b/src/go/plugin/go.d/collector/beanstalk/collect.go index f7b0ba65c8afed..859d2352c58230 100644 --- a/src/go/plugin/go.d/collector/beanstalk/collect.go +++ b/src/go/plugin/go.d/collector/beanstalk/collect.go @@ -3,6 +3,7 @@ package beanstalk import ( + "context" "fmt" "slices" "time" @@ -22,7 +23,7 @@ func (c *Collector) collect() (map[string]int64, error) { mx := make(map[string]int64) if err := c.collectStats(mx); err != nil { - c.Cleanup() + c.Cleanup(context.Background()) return nil, err } if err := c.collectTubesStats(mx); err != nil { diff --git a/src/go/plugin/go.d/collector/beanstalk/collector.go b/src/go/plugin/go.d/collector/beanstalk/collector.go index 76185112d8759e..da2ace45b9a588 100644 --- a/src/go/plugin/go.d/collector/beanstalk/collector.go +++ b/src/go/plugin/go.d/collector/beanstalk/collector.go @@ -3,6 +3,7 @@ package beanstalk import ( + "context" _ "embed" "errors" "fmt" @@ -68,7 +69,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -82,7 +83,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -99,7 +100,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -112,7 +113,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.conn != nil { if err := c.conn.disconnect(); err != nil { c.Warningf("error on disconnect: %s", err) diff --git a/src/go/plugin/go.d/collector/beanstalk/collector_test.go b/src/go/plugin/go.d/collector/beanstalk/collector_test.go index 7eae5aa439deff..1a721c7df65903 100644 --- a/src/go/plugin/go.d/collector/beanstalk/collector_test.go +++ b/src/go/plugin/go.d/collector/beanstalk/collector_test.go @@ -4,6 +4,7 @@ package beanstalk import ( "bufio" + "context" "errors" "fmt" "net" @@ -68,9 +69,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -115,15 +116,15 @@ func TestCollector_Check(t *testing.T) { t.Errorf("mock collr daemon start timed out") } - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } - collr.Cleanup() + collr.Cleanup(context.Background()) select { case <-daemon.stopped: @@ -226,9 +227,9 @@ func TestCollector_Collect(t *testing.T) { t.Errorf("mock collr daemon start timed out") } - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) @@ -238,7 +239,7 @@ func TestCollector_Collect(t *testing.T) { module.TestMetricsHasAllChartsDims(t, collr.Charts(), mx) } - collr.Cleanup() + collr.Cleanup(context.Background()) select { case <-daemon.stopped: diff --git a/src/go/plugin/go.d/collector/bind/collector.go b/src/go/plugin/go.d/collector/bind/collector.go index 4c6a8770da87f2..12ff7e7cd30ce5 100644 --- a/src/go/plugin/go.d/collector/bind/collector.go +++ b/src/go/plugin/go.d/collector/bind/collector.go @@ -3,6 +3,7 @@ package bind import ( + "context" _ "embed" "errors" "fmt" @@ -70,7 +71,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config verification: %v", err) } @@ -98,7 +99,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -114,7 +115,7 @@ func (c *Collector) Charts() *Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { @@ -125,7 +126,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/bind/collector_test.go b/src/go/plugin/go.d/collector/bind/collector_test.go index fa64b8223f19f1..54181a6b4dbf78 100644 --- a/src/go/plugin/go.d/collector/bind/collector_test.go +++ b/src/go/plugin/go.d/collector/bind/collector_test.go @@ -3,6 +3,7 @@ package bind import ( + "context" "net/http" "net/http/httptest" "os" @@ -38,18 +39,18 @@ func TestCollector_ConfigurationSerialize(t *testing.T) { module.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML) } -func TestCollector_Cleanup(t *testing.T) { New().Cleanup() } +func TestCollector_Cleanup(t *testing.T) { New().Cleanup(context.Background()) } func TestCollector_Init(t *testing.T) { // OK collr := New() - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.bindAPIClient) //NG collr = New() collr.URL = "" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Check(t *testing.T) { @@ -65,16 +66,16 @@ func TestCollector_Check(t *testing.T) { collr := New() collr.URL = ts.URL + "/json/v1" - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) } func TestCollector_CheckNG(t *testing.T) { collr := New() collr.URL = "http://127.0.0.1:38001/xml/v3" - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_Charts(t *testing.T) { @@ -95,8 +96,8 @@ func TestCollector_CollectJSON(t *testing.T) { collr.URL = ts.URL + "/json/v1" collr.PermitView = "*" - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) expected := map[string]int64{ "_default_Queryv4": 4503685324, @@ -254,7 +255,7 @@ func TestCollector_CollectJSON(t *testing.T) { "Others": 74006, } - assert.Equal(t, expected, collr.Collect()) + assert.Equal(t, expected, collr.Collect(context.Background())) assert.Len(t, *collr.charts, 17) } @@ -272,8 +273,8 @@ func TestCollector_CollectXML3(t *testing.T) { collr.PermitView = "*" collr.URL = ts.URL + "/xml/v3" - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) expected := map[string]int64{ "_bind_CookieClientOk": 0, @@ -507,26 +508,30 @@ func TestCollector_CollectXML3(t *testing.T) { "IQUERY": 199, } - assert.Equal(t, expected, collr.Collect()) + assert.Equal(t, expected, collr.Collect(context.Background())) assert.Len(t, *collr.charts, 20) } func TestCollector_InvalidData(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte("hello and goodbye")) })) + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, _ = w.Write([]byte("hello and goodbye")) + })) defer ts.Close() collr := New() collr.URL = ts.URL + "/json/v1" - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_404(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(404) })) + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(404) + })) defer ts.Close() collr := New() collr.URL = ts.URL + "/json/v1" - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } diff --git a/src/go/plugin/go.d/collector/boinc/collect.go b/src/go/plugin/go.d/collector/boinc/collect.go index 11bdbaaec0d302..ea6fea4acb7fce 100644 --- a/src/go/plugin/go.d/collector/boinc/collect.go +++ b/src/go/plugin/go.d/collector/boinc/collect.go @@ -3,6 +3,7 @@ package boinc import ( + "context" "fmt" "net" ) @@ -18,7 +19,7 @@ func (c *Collector) collect() (map[string]int64, error) { results, err := c.conn.getResults() if err != nil { - c.Cleanup() + c.Cleanup(context.Background()) return nil, err } diff --git a/src/go/plugin/go.d/collector/boinc/collector.go b/src/go/plugin/go.d/collector/boinc/collector.go index a38319f7e5cba6..c4dd692bdd334e 100644 --- a/src/go/plugin/go.d/collector/boinc/collector.go +++ b/src/go/plugin/go.d/collector/boinc/collector.go @@ -3,6 +3,7 @@ package boinc import ( + "context" _ "embed" "errors" "time" @@ -55,7 +56,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.Address == "" { return errors.New("config: 'address' not set") } @@ -63,7 +64,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -80,7 +81,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -93,7 +94,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.conn != nil { c.conn.disconnect() c.conn = nil diff --git a/src/go/plugin/go.d/collector/boinc/collector_test.go b/src/go/plugin/go.d/collector/boinc/collector_test.go index 8c101e6e3d388d..c2afe2d4f16ff1 100644 --- a/src/go/plugin/go.d/collector/boinc/collector_test.go +++ b/src/go/plugin/go.d/collector/boinc/collector_test.go @@ -3,6 +3,7 @@ package boinc import ( + "context" "encoding/xml" "errors" "os" @@ -63,9 +64,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -84,7 +85,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.newConn = func(Config, *logger.Logger) boincConn { return prepareMockOk() } - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -92,7 +93,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.newConn = func(Config, *logger.Logger) boincConn { return prepareMockOk() } - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -102,7 +103,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -133,9 +134,9 @@ func TestCollector_Check(t *testing.T) { collr.newConn = func(Config, *logger.Logger) boincConn { return mock } if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -216,7 +217,7 @@ func TestCollector_Collect(t *testing.T) { mock := test.prepareMock() collr.newConn = func(Config, *logger.Logger) boincConn { return mock } - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) @@ -225,7 +226,7 @@ func TestCollector_Collect(t *testing.T) { } assert.Equal(t, test.disconnectBeforeCleanup, mock.disconnectCalled, "disconnect before cleanup") - collr.Cleanup() + collr.Cleanup(context.Background()) assert.Equal(t, test.disconnectAfterCleanup, mock.disconnectCalled, "disconnect after cleanup") }) } diff --git a/src/go/plugin/go.d/collector/cassandra/collector.go b/src/go/plugin/go.d/collector/cassandra/collector.go index 70cb09441c65a9..495f62bf4cb7f9 100644 --- a/src/go/plugin/go.d/collector/cassandra/collector.go +++ b/src/go/plugin/go.d/collector/cassandra/collector.go @@ -3,6 +3,7 @@ package cassandra import ( + "context" _ "embed" "errors" "fmt" @@ -68,7 +69,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("error on validating config: %v", err) } @@ -82,7 +83,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -98,7 +99,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -110,7 +111,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.prom != nil && c.prom.HTTPClient() != nil { c.prom.HTTPClient().CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/cassandra/collector_test.go b/src/go/plugin/go.d/collector/cassandra/collector_test.go index 541ecf4b6af253..b21276951c414f 100644 --- a/src/go/plugin/go.d/collector/cassandra/collector_test.go +++ b/src/go/plugin/go.d/collector/cassandra/collector_test.go @@ -3,6 +3,7 @@ package cassandra import ( + "context" "net/http" "net/http/httptest" "os" @@ -65,9 +66,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -100,12 +101,12 @@ func TestCollector_Check(t *testing.T) { collr, cleanup := test.prepare() defer cleanup() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -249,9 +250,9 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare() defer cleanup() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantCollected, mx) }) diff --git a/src/go/plugin/go.d/collector/ceph/collector.go b/src/go/plugin/go.d/collector/ceph/collector.go index 4443a39aca8480..597a6dabd78d02 100644 --- a/src/go/plugin/go.d/collector/ceph/collector.go +++ b/src/go/plugin/go.d/collector/ceph/collector.go @@ -3,6 +3,7 @@ package ceph import ( + "context" _ "embed" "errors" "fmt" @@ -77,7 +78,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("invalid config: %v", err) } @@ -91,7 +92,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -108,7 +109,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -121,7 +122,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { if err := c.authLogout(); err != nil { c.Warningf("failed to logout: %v", err) diff --git a/src/go/plugin/go.d/collector/ceph/collector_test.go b/src/go/plugin/go.d/collector/ceph/collector_test.go index bb0c3e25c53210..c977bb9ab2095d 100644 --- a/src/go/plugin/go.d/collector/ceph/collector_test.go +++ b/src/go/plugin/go.d/collector/ceph/collector_test.go @@ -4,6 +4,7 @@ package ceph import ( "bytes" + "context" "encoding/json" "io" "net/http" @@ -72,9 +73,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -105,9 +106,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -227,9 +228,9 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare(t) defer cleanup() - _ = collr.Check() + _ = collr.Check(context.Background()) - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) @@ -299,7 +300,7 @@ func caseOk(t *testing.T) (*Collector, func()) { collr.URL = srv.URL collr.Username = "user" collr.Password = "password" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -310,7 +311,7 @@ func caseConnectionRefused(t *testing.T) (*Collector, func()) { collr.URL = "http://127.0.0.1:65001" collr.Username = "user" collr.Password = "password" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } @@ -325,7 +326,7 @@ func case404(t *testing.T) (*Collector, func()) { collr.URL = srv.URL collr.Username = "user" collr.Password = "password" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } diff --git a/src/go/plugin/go.d/collector/chrony/collector.go b/src/go/plugin/go.d/collector/chrony/collector.go index 6d4bd6390bc858..68350bc507fcc7 100644 --- a/src/go/plugin/go.d/collector/chrony/collector.go +++ b/src/go/plugin/go.d/collector/chrony/collector.go @@ -3,6 +3,7 @@ package chrony import ( + "context" _ "embed" "errors" "fmt" @@ -59,7 +60,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -72,7 +73,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -88,7 +89,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(ctx context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -100,7 +101,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.conn != nil { c.conn.close() c.conn = nil diff --git a/src/go/plugin/go.d/collector/chrony/collector_test.go b/src/go/plugin/go.d/collector/chrony/collector_test.go index 753f3e6e4fa011..52671bcfafdf32 100644 --- a/src/go/plugin/go.d/collector/chrony/collector_test.go +++ b/src/go/plugin/go.d/collector/chrony/collector_test.go @@ -3,6 +3,7 @@ package chrony import ( + "context" "errors" "net" "os" @@ -56,9 +57,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -95,12 +96,12 @@ func TestCollector_Check(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -121,15 +122,15 @@ func TestCollector_Cleanup(t *testing.T) { }, "after Init": { wantClose: false, - prepare: func(c *Collector) { _ = c.Init() }, + prepare: func(c *Collector) { _ = c.Init(context.Background()) }, }, "after Check": { wantClose: true, - prepare: func(c *Collector) { _ = c.Init(); _ = c.Check() }, + prepare: func(c *Collector) { _ = c.Init(context.Background()); _ = c.Check(context.Background()) }, }, "after Collect": { wantClose: true, - prepare: func(c *Collector) { _ = c.Init(); _ = c.Collect() }, + prepare: func(c *Collector) { _ = c.Init(context.Background()); _ = c.Collect(context.Background()) }, }, } @@ -139,7 +140,7 @@ func TestCollector_Cleanup(t *testing.T) { collr := prepareChronyWithMock(m) test.prepare(collr) - require.NotPanics(t, collr.Cleanup) + require.NotPanics(t, func() { collr.Cleanup(context.Background()) }) if test.wantClose { assert.True(t, m.closeCalled) @@ -222,11 +223,11 @@ func TestCollector_Collect(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.exec = &mockChronyc{} - _ = collr.Check() + _ = collr.Check(context.Background()) - mx := collr.Collect() + mx := collr.Collect(context.Background()) copyRefMeasurementTime(mx, test.expected) assert.Equal(t, test.expected, mx) diff --git a/src/go/plugin/go.d/collector/clickhouse/collector.go b/src/go/plugin/go.d/collector/clickhouse/collector.go index cedf1d8e1d5b24..c2db07aa322740 100644 --- a/src/go/plugin/go.d/collector/clickhouse/collector.go +++ b/src/go/plugin/go.d/collector/clickhouse/collector.go @@ -3,6 +3,7 @@ package clickhouse import ( + "context" _ "embed" "errors" "fmt" @@ -68,7 +69,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -85,7 +86,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -102,7 +103,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -115,7 +116,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/clickhouse/collector_test.go b/src/go/plugin/go.d/collector/clickhouse/collector_test.go index 4a5b83b719b371..2a0cdda5dd42dc 100644 --- a/src/go/plugin/go.d/collector/clickhouse/collector_test.go +++ b/src/go/plugin/go.d/collector/clickhouse/collector_test.go @@ -3,6 +3,7 @@ package clickhouse import ( + "context" "net/http" "net/http/httptest" "os" @@ -71,9 +72,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -108,9 +109,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -240,7 +241,7 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare(t) defer cleanup() - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) @@ -275,7 +276,7 @@ func prepareCaseOk(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -289,7 +290,7 @@ func prepareCaseUnexpectedResponse(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -298,7 +299,7 @@ func prepareCaseConnectionRefused(t *testing.T) (*Collector, func()) { t.Helper() collr := New() collr.URL = "http://127.0.0.1:65001/stat" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } diff --git a/src/go/plugin/go.d/collector/cockroachdb/collector.go b/src/go/plugin/go.d/collector/cockroachdb/collector.go index 997ba3de2358b5..d3946a3b91c220 100644 --- a/src/go/plugin/go.d/collector/cockroachdb/collector.go +++ b/src/go/plugin/go.d/collector/cockroachdb/collector.go @@ -3,6 +3,7 @@ package cockroachdb import ( + "context" _ "embed" "errors" "fmt" @@ -66,7 +67,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("error on validating config: %v", err) } @@ -85,7 +86,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -101,7 +102,7 @@ func (c *Collector) Charts() *Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -113,7 +114,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.prom != nil && c.prom.HTTPClient() != nil { c.prom.HTTPClient().CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/cockroachdb/collector_test.go b/src/go/plugin/go.d/collector/cockroachdb/collector_test.go index 01f9f6266299cf..3635d42625a823 100644 --- a/src/go/plugin/go.d/collector/cockroachdb/collector_test.go +++ b/src/go/plugin/go.d/collector/cockroachdb/collector_test.go @@ -3,6 +3,7 @@ package cockroachdb import ( + "context" "net/http" "net/http/httptest" "os" @@ -44,36 +45,36 @@ func TestNew(t *testing.T) { func TestCollector_Init(t *testing.T) { collr := prepareCockroachDB() - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } func TestCollector_Init_ReturnsFalseIfConfigURLIsNotSet(t *testing.T) { collr := prepareCockroachDB() collr.URL = "" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Init_ReturnsFalseIfClientWrongTLSCA(t *testing.T) { collr := prepareCockroachDB() collr.ClientConfig.TLSConfig.TLSCA = "testdata/tls" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Check(t *testing.T) { collr, srv := prepareClientServer(t) defer srv.Close() - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } func TestCollector_Check_ReturnsFalseIfConnectionRefused(t *testing.T) { collr := New() collr.URL = "http://127.0.0.1:38001/metrics" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_Charts(t *testing.T) { @@ -81,7 +82,7 @@ func TestCollector_Charts(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Collect(t *testing.T) { @@ -221,7 +222,7 @@ func TestCollector_Collect(t *testing.T) { "valcount": 124081, } - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, expected, mx) @@ -232,28 +233,28 @@ func TestCollector_Collect_ReturnsNilIfNotCockroachDBMetrics(t *testing.T) { collr, srv := prepareClientServerNotCockroachDBMetricResponse(t) defer srv.Close() - assert.Nil(t, collr.Collect()) + assert.Nil(t, collr.Collect(context.Background())) } func TestCollector_Collect_ReturnsNilIfConnectionRefused(t *testing.T) { collr := prepareCockroachDB() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.Nil(t, collr.Collect()) + assert.Nil(t, collr.Collect(context.Background())) } func TestCollector_Collect_ReturnsNilIfReceiveInvalidResponse(t *testing.T) { collr, ts := prepareClientServerInvalidDataResponse(t) defer ts.Close() - assert.Nil(t, collr.Collect()) + assert.Nil(t, collr.Collect(context.Background())) } func TestCollector_Collect_ReturnsNilIfReceiveResponse404(t *testing.T) { collr, ts := prepareClientServerResponse404(t) defer ts.Close() - assert.Nil(t, collr.Collect()) + assert.Nil(t, collr.Collect(context.Background())) } func prepareCockroachDB() *Collector { @@ -271,7 +272,7 @@ func prepareClientServer(t *testing.T) (*Collector, *httptest.Server) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, ts } @@ -285,7 +286,7 @@ func prepareClientServerNotCockroachDBMetricResponse(t *testing.T) (*Collector, collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, ts } @@ -299,7 +300,7 @@ func prepareClientServerInvalidDataResponse(t *testing.T) (*Collector, *httptest collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, ts } @@ -313,6 +314,6 @@ func prepareClientServerResponse404(t *testing.T) (*Collector, *httptest.Server) collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, ts } diff --git a/src/go/plugin/go.d/collector/consul/collector.go b/src/go/plugin/go.d/collector/consul/collector.go index 634c5e9381ec14..2cf2823f54be1f 100644 --- a/src/go/plugin/go.d/collector/consul/collector.go +++ b/src/go/plugin/go.d/collector/consul/collector.go @@ -3,6 +3,7 @@ package consul import ( + "context" _ "embed" "errors" "fmt" @@ -79,7 +80,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -99,7 +100,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -115,7 +116,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -127,7 +128,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/consul/collector_test.go b/src/go/plugin/go.d/collector/consul/collector_test.go index a8b66c4d3f5de2..8cd8651c4d515f 100644 --- a/src/go/plugin/go.d/collector/consul/collector_test.go +++ b/src/go/plugin/go.d/collector/consul/collector_test.go @@ -3,6 +3,7 @@ package consul import ( + "context" "net/http" "net/http/httptest" "os" @@ -88,9 +89,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -141,9 +142,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -535,7 +536,7 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare(t) defer cleanup() - mx := collr.Collect() + mx := collr.Collect(context.Background()) delete(mx, "autopilot_server_stable_time") delete(test.wantMetrics, "autopilot_server_stable_time") @@ -571,7 +572,7 @@ func caseConsulV1143CloudServerResponse(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -599,7 +600,7 @@ func caseConsulV1132ServerResponse(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -627,7 +628,7 @@ func caseConsulV1132ServerWithHostnameResponse(t *testing.T) (*Collector, func() collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -653,7 +654,7 @@ func caseConsulV1132ServerWithDisabledPrometheus(t *testing.T) (*Collector, func collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -677,7 +678,7 @@ func caseConsulV1132ClientResponse(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -692,7 +693,7 @@ func caseInvalidDataResponse(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -701,7 +702,7 @@ func caseConnectionRefused(t *testing.T) (*Collector, func()) { t.Helper() collr := New() collr.URL = "http://127.0.0.1:65535/" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } @@ -715,7 +716,7 @@ func case404(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } diff --git a/src/go/plugin/go.d/collector/coredns/collector.go b/src/go/plugin/go.d/collector/coredns/collector.go index 7671b85d9cad55..857ecd1245a350 100644 --- a/src/go/plugin/go.d/collector/coredns/collector.go +++ b/src/go/plugin/go.d/collector/coredns/collector.go @@ -3,6 +3,7 @@ package coredns import ( + "context" _ "embed" "errors" "fmt" @@ -74,7 +75,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -104,7 +105,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -120,7 +121,7 @@ func (c *Collector) Charts() *Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { @@ -131,7 +132,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.prom != nil && c.prom.HTTPClient() != nil { c.prom.HTTPClient().CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/coredns/collector_test.go b/src/go/plugin/go.d/collector/coredns/collector_test.go index 4458cb55ded50c..d251fd6d191113 100644 --- a/src/go/plugin/go.d/collector/coredns/collector_test.go +++ b/src/go/plugin/go.d/collector/coredns/collector_test.go @@ -3,6 +3,7 @@ package coredns import ( + "context" "net/http" "net/http/httptest" "os" @@ -50,17 +51,17 @@ func TestCollector_Charts(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - New().Cleanup() + New().Cleanup(context.Background()) } func TestCollector_Init(t *testing.T) { - assert.NoError(t, New().Init()) + assert.NoError(t, New().Init(context.Background())) } func TestCollector_InitNG(t *testing.T) { collr := New() collr.URL = "" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Check(t *testing.T) { @@ -83,8 +84,8 @@ func TestCollector_Check(t *testing.T) { collr := New() collr.URL = ts.URL + "/metrics" - require.NoError(t, collr.Init()) - assert.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.NoError(t, collr.Check(context.Background())) }) } } @@ -92,8 +93,8 @@ func TestCollector_Check(t *testing.T) { func TestCollector_CheckNG(t *testing.T) { collr := New() collr.URL = "http://127.0.0.1:38001/metrics" - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_Collect(t *testing.T) { @@ -118,8 +119,8 @@ func TestCollector_Collect(t *testing.T) { collr.URL = ts.URL + "/metrics" collr.PerServerStats.Includes = []string{"glob:*"} collr.PerZoneStats.Includes = []string{"glob:*"} - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) expected := map[string]int64{ "coredns.io._request_per_ip_family_v4": 19, @@ -441,7 +442,7 @@ func TestCollector_Collect(t *testing.T) { "ya.ru._response_total": 21, } - assert.Equal(t, expected, collr.Collect()) + assert.Equal(t, expected, collr.Collect(context.Background())) }) } } @@ -467,8 +468,8 @@ func TestCollector_CollectNoLoad(t *testing.T) { collr.URL = ts.URL + "/metrics" collr.PerServerStats.Includes = []string{"glob:*"} collr.PerZoneStats.Includes = []string{"glob:*"} - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) expected := map[string]int64{ "no_matching_zone_dropped_total": 0, @@ -520,7 +521,7 @@ func TestCollector_CollectNoLoad(t *testing.T) { "response_total": 0, } - assert.Equal(t, expected, collr.Collect()) + assert.Equal(t, expected, collr.Collect(context.Background())) }) } @@ -536,8 +537,8 @@ func TestCollector_InvalidData(t *testing.T) { collr := New() collr.URL = ts.URL + "/metrics" - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_404(t *testing.T) { @@ -550,8 +551,8 @@ func TestCollector_404(t *testing.T) { collr := New() collr.URL = ts.URL + "/metrics" - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_CollectNoVersion(t *testing.T) { @@ -566,8 +567,8 @@ func TestCollector_CollectNoVersion(t *testing.T) { collr.URL = ts.URL + "/metrics" collr.PerServerStats.Includes = []string{"glob:*"} collr.PerZoneStats.Includes = []string{"glob:*"} - require.NoError(t, collr.Init()) - require.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.Error(t, collr.Check(context.Background())) - assert.Nil(t, collr.Collect()) + assert.Nil(t, collr.Collect(context.Background())) } diff --git a/src/go/plugin/go.d/collector/couchbase/collector.go b/src/go/plugin/go.d/collector/couchbase/collector.go index 84e6726929aa5a..f1a608fac18fcf 100644 --- a/src/go/plugin/go.d/collector/couchbase/collector.go +++ b/src/go/plugin/go.d/collector/couchbase/collector.go @@ -3,6 +3,7 @@ package couchbase import ( + "context" _ "embed" "errors" "fmt" @@ -63,7 +64,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { err := c.validateConfig() if err != nil { return fmt.Errorf("check configuration: %v", err) @@ -84,7 +85,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -100,7 +101,7 @@ func (c *Collector) Charts() *Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -112,7 +113,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient == nil { return } diff --git a/src/go/plugin/go.d/collector/couchbase/collector_test.go b/src/go/plugin/go.d/collector/couchbase/collector_test.go index 40987f55b41055..9703609b34d85a 100644 --- a/src/go/plugin/go.d/collector/couchbase/collector_test.go +++ b/src/go/plugin/go.d/collector/couchbase/collector_test.go @@ -3,6 +3,7 @@ package couchbase import ( + "context" "net/http" "net/http/httptest" "os" @@ -72,9 +73,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -108,9 +109,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -166,7 +167,7 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare(t) defer cleanup() - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantCollected, mx) module.TestMetricsHasAllChartsDims(t, collr.Charts(), mx) @@ -183,7 +184,7 @@ func prepareCouchbaseV660(t *testing.T) (collr *Collector, cleanup func()) { collr = New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -196,7 +197,7 @@ func prepareCouchbaseInvalidData(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -209,7 +210,7 @@ func prepareCouchbase404(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -218,7 +219,7 @@ func prepareCouchbaseConnectionRefused(t *testing.T) (*Collector, func()) { t.Helper() collr := New() collr.URL = "http://127.0.0.1:38001" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } diff --git a/src/go/plugin/go.d/collector/couchdb/collector.go b/src/go/plugin/go.d/collector/couchdb/collector.go index 8f963ceb4d8e22..8ae6bba7bb9efe 100644 --- a/src/go/plugin/go.d/collector/couchdb/collector.go +++ b/src/go/plugin/go.d/collector/couchdb/collector.go @@ -3,6 +3,7 @@ package couchdb import ( + "context" _ "embed" "errors" "fmt" @@ -67,7 +68,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { err := c.validateConfig() if err != nil { return fmt.Errorf("check configuration: %v", err) @@ -90,7 +91,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { if err := c.pingCouchDB(); err != nil { return err } @@ -111,7 +112,7 @@ func (c *Collector) Charts() *Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -123,7 +124,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient == nil { return } diff --git a/src/go/plugin/go.d/collector/couchdb/collector_test.go b/src/go/plugin/go.d/collector/couchdb/collector_test.go index 2c5790fc81db61..5ab1f85e6415ab 100644 --- a/src/go/plugin/go.d/collector/couchdb/collector_test.go +++ b/src/go/plugin/go.d/collector/couchdb/collector_test.go @@ -3,6 +3,7 @@ package couchdb import ( + "context" "net/http" "net/http/httptest" "os" @@ -84,9 +85,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) assert.Equal(t, test.wantNumOfCharts, len(*collr.Charts())) } }) @@ -110,9 +111,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -123,7 +124,7 @@ func TestCollector_Charts(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Collect(t *testing.T) { @@ -359,7 +360,7 @@ func TestCollector_Collect(t *testing.T) { var mx map[string]int64 for i := 0; i < 10; i++ { - mx = collr.Collect() + mx = collr.Collect(context.Background()) } assert.Equal(t, test.wantCollected, mx) @@ -376,7 +377,7 @@ func prepareCouchDB(t *testing.T, createCDB func() *Collector) (collr *Collector srv := prepareCouchDBEndpoint() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -393,7 +394,7 @@ func prepareCouchDBInvalidData(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -406,7 +407,7 @@ func prepareCouchDB404(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -415,7 +416,7 @@ func prepareCouchDBConnectionRefused(t *testing.T) (*Collector, func()) { t.Helper() collr := New() collr.URL = "http://127.0.0.1:38001" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } diff --git a/src/go/plugin/go.d/collector/dmcache/collector.go b/src/go/plugin/go.d/collector/dmcache/collector.go index c3ce956d6a95cc..5b3e4090ea7c7a 100644 --- a/src/go/plugin/go.d/collector/dmcache/collector.go +++ b/src/go/plugin/go.d/collector/dmcache/collector.go @@ -5,6 +5,7 @@ package dmcache import ( + "context" _ "embed" "errors" "fmt" @@ -58,7 +59,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { dmsetup, err := c.initDmsetupCLI() if err != nil { return fmt.Errorf("dmsetup exec initialization: %v", err) @@ -68,7 +69,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -85,7 +86,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -98,4 +99,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/dmcache/collector_test.go b/src/go/plugin/go.d/collector/dmcache/collector_test.go index 5eee944d868688..0afe9fd607c1ff 100644 --- a/src/go/plugin/go.d/collector/dmcache/collector_test.go +++ b/src/go/plugin/go.d/collector/dmcache/collector_test.go @@ -5,6 +5,7 @@ package dmcache import ( + "context" "errors" "os" "testing" @@ -51,9 +52,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -72,7 +73,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOK() - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -80,7 +81,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOK() - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -90,7 +91,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -129,9 +130,9 @@ func TestCollector_Check(t *testing.T) { collr.exec = mock if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -191,7 +192,7 @@ func TestLVM_Collect(t *testing.T) { mock := test.prepareMock() collr.exec = mock - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantMetrics, mx) diff --git a/src/go/plugin/go.d/collector/dnsdist/collector.go b/src/go/plugin/go.d/collector/dnsdist/collector.go index 8f358bbda5d397..7fed17bc9c8965 100644 --- a/src/go/plugin/go.d/collector/dnsdist/collector.go +++ b/src/go/plugin/go.d/collector/dnsdist/collector.go @@ -3,6 +3,7 @@ package dnsdist import ( + "context" _ "embed" "errors" "fmt" @@ -61,7 +62,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { err := c.validateConfig() if err != nil { return fmt.Errorf("config validation: %v", err) @@ -82,7 +83,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -98,7 +99,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { ms, err := c.collect() if err != nil { c.Error(err) @@ -111,7 +112,7 @@ func (c *Collector) Collect() map[string]int64 { return ms } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient == nil { return } diff --git a/src/go/plugin/go.d/collector/dnsdist/collector_test.go b/src/go/plugin/go.d/collector/dnsdist/collector_test.go index baba60b537e4ba..46b54472ad641e 100644 --- a/src/go/plugin/go.d/collector/dnsdist/collector_test.go +++ b/src/go/plugin/go.d/collector/dnsdist/collector_test.go @@ -3,6 +3,7 @@ package dnsdist import ( + "context" "net/http" "net/http/httptest" "os" @@ -74,9 +75,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -84,12 +85,12 @@ func TestCollector_Init(t *testing.T) { func TestCollector_Charts(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.Charts()) } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Check(t *testing.T) { @@ -119,12 +120,12 @@ func TestCollector_Check(t *testing.T) { t.Run(name, func(t *testing.T) { collr, cleanup := test.prepare() defer cleanup() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -187,9 +188,9 @@ func TestCollector_Collect(t *testing.T) { t.Run(name, func(t *testing.T) { collr, cleanup := test.prepare() defer cleanup() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantCollected, mx) if len(test.wantCollected) > 0 { diff --git a/src/go/plugin/go.d/collector/dnsmasq/collector.go b/src/go/plugin/go.d/collector/dnsmasq/collector.go index cdccd4f411a24e..64ba0959ccd336 100644 --- a/src/go/plugin/go.d/collector/dnsmasq/collector.go +++ b/src/go/plugin/go.d/collector/dnsmasq/collector.go @@ -3,6 +3,7 @@ package dnsmasq import ( + "context" _ "embed" "errors" "fmt" @@ -68,7 +69,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { err := c.validateConfig() if err != nil { return fmt.Errorf("config validation: %v", err) @@ -89,7 +90,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -105,7 +106,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { ms, err := c.collect() if err != nil { c.Error(err) @@ -117,4 +118,4 @@ func (c *Collector) Collect() map[string]int64 { return ms } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/dnsmasq/collector_test.go b/src/go/plugin/go.d/collector/dnsmasq/collector_test.go index 61a3ba374d3547..395c4b1343549c 100644 --- a/src/go/plugin/go.d/collector/dnsmasq/collector_test.go +++ b/src/go/plugin/go.d/collector/dnsmasq/collector_test.go @@ -3,6 +3,7 @@ package dnsmasq import ( + "context" "errors" "fmt" "os" @@ -71,9 +72,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -100,12 +101,12 @@ func TestCollector_Check(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { collr := test.prepare() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -113,12 +114,12 @@ func TestCollector_Check(t *testing.T) { func TestCollector_Charts(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.Charts()) } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Collect(t *testing.T) { @@ -150,9 +151,9 @@ func TestCollector_Collect(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { collr := test.prepare() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantCollected, mx) if len(test.wantCollected) > 0 { diff --git a/src/go/plugin/go.d/collector/dnsmasq_dhcp/collector.go b/src/go/plugin/go.d/collector/dnsmasq_dhcp/collector.go index 316268fd5ed19e..a8c38436045068 100644 --- a/src/go/plugin/go.d/collector/dnsmasq_dhcp/collector.go +++ b/src/go/plugin/go.d/collector/dnsmasq_dhcp/collector.go @@ -5,6 +5,7 @@ package dnsmasq_dhcp import ( + "context" _ "embed" "errors" "fmt" @@ -68,7 +69,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -79,7 +80,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -95,7 +96,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -108,4 +109,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/dnsmasq_dhcp/collector_test.go b/src/go/plugin/go.d/collector/dnsmasq_dhcp/collector_test.go index 7e343bba07fbb8..345cab7038a132 100644 --- a/src/go/plugin/go.d/collector/dnsmasq_dhcp/collector_test.go +++ b/src/go/plugin/go.d/collector/dnsmasq_dhcp/collector_test.go @@ -5,6 +5,7 @@ package dnsmasq_dhcp import ( + "context" "os" "testing" @@ -44,14 +45,14 @@ func TestCollector_Init(t *testing.T) { collr.ConfPath = testConfPath collr.ConfDir = testConfDir - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } func TestCollector_InitEmptyLeasesPath(t *testing.T) { collr := New() collr.LeasesPath = "" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_InitInvalidLeasesPath(t *testing.T) { @@ -59,7 +60,7 @@ func TestCollector_InitInvalidLeasesPath(t *testing.T) { collr.LeasesPath = testLeasesPath collr.LeasesPath += "!" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_InitZeroDHCPRanges(t *testing.T) { @@ -68,7 +69,7 @@ func TestCollector_InitZeroDHCPRanges(t *testing.T) { collr.ConfPath = "testdata/dnsmasq3.conf" collr.ConfDir = "" - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } func TestCollector_Check(t *testing.T) { @@ -77,8 +78,8 @@ func TestCollector_Check(t *testing.T) { collr.ConfPath = testConfPath collr.ConfDir = testConfDir - require.NoError(t, collr.Init()) - assert.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.NoError(t, collr.Check(context.Background())) } func TestCollector_Charts(t *testing.T) { @@ -87,13 +88,13 @@ func TestCollector_Charts(t *testing.T) { collr.ConfPath = testConfPath collr.ConfDir = testConfDir - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.Charts()) } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Collect(t *testing.T) { @@ -102,8 +103,8 @@ func TestCollector_Collect(t *testing.T) { collr.ConfPath = testConfPath collr.ConfDir = testConfDir - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) expected := map[string]int64{ "dhcp_range_1230::1-1230::64_allocated_leases": 7, @@ -134,7 +135,7 @@ func TestCollector_Collect(t *testing.T) { "ipv6_dhcp_ranges": 5, } - assert.Equal(t, expected, collr.Collect()) + assert.Equal(t, expected, collr.Collect(context.Background())) } func TestCollector_CollectFailedToOpenLeasesPath(t *testing.T) { @@ -143,11 +144,11 @@ func TestCollector_CollectFailedToOpenLeasesPath(t *testing.T) { collr.ConfPath = testConfPath collr.ConfDir = testConfDir - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) collr.LeasesPath = "" - assert.Nil(t, collr.Collect()) + assert.Nil(t, collr.Collect(context.Background())) } func TestCollector_parseDHCPRangeValue(t *testing.T) { diff --git a/src/go/plugin/go.d/collector/dnsquery/collector.go b/src/go/plugin/go.d/collector/dnsquery/collector.go index a0d606a2366b10..a15c7d286d431d 100644 --- a/src/go/plugin/go.d/collector/dnsquery/collector.go +++ b/src/go/plugin/go.d/collector/dnsquery/collector.go @@ -3,6 +3,7 @@ package dnsquery import ( + "context" _ "embed" "fmt" "time" @@ -76,7 +77,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.verifyConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -96,7 +97,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { return nil } @@ -104,7 +105,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -116,4 +117,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/dnsquery/collector_test.go b/src/go/plugin/go.d/collector/dnsquery/collector_test.go index c843c4ded98ea6..97a05a2f7d6125 100644 --- a/src/go/plugin/go.d/collector/dnsquery/collector_test.go +++ b/src/go/plugin/go.d/collector/dnsquery/collector_test.go @@ -3,6 +3,7 @@ package dnsquery import ( + "context" "errors" "os" "testing" @@ -117,9 +118,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -144,12 +145,12 @@ func TestCollector_Check(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -160,7 +161,7 @@ func TestCollector_Charts(t *testing.T) { collr.Domains = []string{"google.com"} collr.Servers = []string{"192.0.2.0", "192.0.2.1"} - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.Charts()) assert.Len(t, *collr.Charts(), len(dnsChartsTmpl)*len(collr.Servers)) @@ -201,9 +202,9 @@ func TestCollector_Collect(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) }) diff --git a/src/go/plugin/go.d/collector/docker/collector.go b/src/go/plugin/go.d/collector/docker/collector.go index ac7358de31d9f5..438c1141ad397e 100644 --- a/src/go/plugin/go.d/collector/docker/collector.go +++ b/src/go/plugin/go.d/collector/docker/collector.go @@ -79,7 +79,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if addr := dockerhost.FromEnv(); addr != "" && c.Address == docker.DefaultDockerHost { c.Infof("using docker host from environment: %s ", addr) c.Address = addr @@ -87,7 +87,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -103,7 +103,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -115,7 +115,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.client == nil { return } diff --git a/src/go/plugin/go.d/collector/docker/collector_test.go b/src/go/plugin/go.d/collector/docker/collector_test.go index 6a431f98597ed3..bff50657a24944 100644 --- a/src/go/plugin/go.d/collector/docker/collector_test.go +++ b/src/go/plugin/go.d/collector/docker/collector_test.go @@ -59,9 +59,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -82,15 +82,15 @@ func TestCollector_Cleanup(t *testing.T) { }, "after Init": { wantClose: false, - prepare: func(c *Collector) { _ = c.Init() }, + prepare: func(c *Collector) { _ = c.Init(context.Background()) }, }, "after Check": { wantClose: true, - prepare: func(c *Collector) { _ = c.Init(); _ = c.Check() }, + prepare: func(c *Collector) { _ = c.Init(context.Background()); _ = c.Check(context.Background()) }, }, "after Collect": { wantClose: true, - prepare: func(c *Collector) { _ = c.Init(); c.Collect() }, + prepare: func(c *Collector) { _ = c.Init(context.Background()); c.Collect(context.Background()) }, }, } @@ -102,7 +102,7 @@ func TestCollector_Cleanup(t *testing.T) { test.prepare(collr) - require.NotPanics(t, collr.Cleanup) + require.NotPanics(t, func() { collr.Cleanup(context.Background()) }) if test.wantClose { assert.True(t, m.closeCalled) @@ -160,12 +160,12 @@ func TestCollector_Check(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -678,9 +678,9 @@ func TestCollector_Collect(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.expected, mx) diff --git a/src/go/plugin/go.d/collector/docker_engine/collector.go b/src/go/plugin/go.d/collector/docker_engine/collector.go index 34f1d9e1b8257c..41ba7e03169717 100644 --- a/src/go/plugin/go.d/collector/docker_engine/collector.go +++ b/src/go/plugin/go.d/collector/docker_engine/collector.go @@ -3,6 +3,7 @@ package docker_engine import ( + "context" _ "embed" "errors" "fmt" @@ -59,7 +60,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -73,7 +74,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -104,7 +105,7 @@ func (c *Collector) Charts() *Charts { return cs } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -117,7 +118,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.prom != nil && c.prom.HTTPClient() != nil { c.prom.HTTPClient().CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/docker_engine/collector_test.go b/src/go/plugin/go.d/collector/docker_engine/collector_test.go index 01d0971e4b557b..9f4aefcf88147c 100644 --- a/src/go/plugin/go.d/collector/docker_engine/collector_test.go +++ b/src/go/plugin/go.d/collector/docker_engine/collector_test.go @@ -3,6 +3,7 @@ package docker_engine import ( + "context" "net/http" "net/http/httptest" "os" @@ -44,7 +45,7 @@ func TestCollector_ConfigurationSerialize(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Init(t *testing.T) { @@ -73,9 +74,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -101,9 +102,9 @@ func TestCollector_Check(t *testing.T) { defer srv.Close() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -124,7 +125,7 @@ func TestCollector_Charts(t *testing.T) { collr, srv := test.prepare(t) defer srv.Close() - require.NoError(t, collr.Check()) + require.NoError(t, collr.Check(context.Background())) assert.Len(t, *collr.Charts(), test.wantNumCharts) }) } @@ -145,7 +146,7 @@ func TestCollector_Collect_ReturnsNilOnErrors(t *testing.T) { collr, srv := test.prepare(t) defer srv.Close() - assert.Nil(t, collr.Collect()) + assert.Nil(t, collr.Collect(context.Background())) }) } } @@ -251,9 +252,9 @@ func TestCollector_Collect(t *testing.T) { defer srv.Close() for i := 0; i < 10; i++ { - _ = pulsar.Collect() + _ = pulsar.Collect(context.Background()) } - mx := pulsar.Collect() + mx := pulsar.Collect(context.Background()) require.NotNil(t, mx) require.Equal(t, test.expected, mx) @@ -271,7 +272,7 @@ func prepareClientServerV17050CE(t *testing.T) (*Collector, *httptest.Server) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv } @@ -285,7 +286,7 @@ func prepareClientServerV18093CE(t *testing.T) (*Collector, *httptest.Server) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv } @@ -299,7 +300,7 @@ func prepareClientServerV18093CESwarm(t *testing.T) (*Collector, *httptest.Serve collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv } @@ -313,7 +314,7 @@ func prepareClientServerNonDockerEngine(t *testing.T) (*Collector, *httptest.Ser collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv } @@ -327,7 +328,7 @@ func prepareClientServerInvalidData(t *testing.T) (*Collector, *httptest.Server) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv } @@ -341,7 +342,7 @@ func prepareClientServer404(t *testing.T) (*Collector, *httptest.Server) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv } @@ -352,7 +353,7 @@ func prepareClientServerConnectionRefused(t *testing.T) (*Collector, *httptest.S collr := New() collr.URL = "http://127.0.0.1:38001/metrics" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv } diff --git a/src/go/plugin/go.d/collector/dockerhub/collector.go b/src/go/plugin/go.d/collector/dockerhub/collector.go index e4509401b9dc21..0451d0da1d59a1 100644 --- a/src/go/plugin/go.d/collector/dockerhub/collector.go +++ b/src/go/plugin/go.d/collector/dockerhub/collector.go @@ -3,6 +3,7 @@ package dockerhub import ( + "context" _ "embed" "errors" "fmt" @@ -59,7 +60,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -73,7 +74,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -91,7 +92,7 @@ func (c *Collector) Charts() *Charts { return cs } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { @@ -102,7 +103,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.client != nil && c.client.httpClient != nil { c.client.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/dockerhub/collector_test.go b/src/go/plugin/go.d/collector/dockerhub/collector_test.go index d7f06b1f3cf588..99303151b5a4fd 100644 --- a/src/go/plugin/go.d/collector/dockerhub/collector_test.go +++ b/src/go/plugin/go.d/collector/dockerhub/collector_test.go @@ -3,6 +3,7 @@ package dockerhub import ( + "context" "net/http" "net/http/httptest" "os" @@ -42,17 +43,17 @@ func TestCollector_ConfigurationSerialize(t *testing.T) { func TestCollector_Charts(t *testing.T) { assert.NotNil(t, New().Charts()) } -func TestCollector_Cleanup(t *testing.T) { New().Cleanup() } +func TestCollector_Cleanup(t *testing.T) { New().Cleanup(context.Background()) } func TestCollector_Init(t *testing.T) { collr := New() collr.Repositories = []string{"name/repo"} - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.client) } func TestCollector_InitNG(t *testing.T) { - assert.Error(t, New().Init()) + assert.Error(t, New().Init(context.Background())) } func TestCollector_Check(t *testing.T) { @@ -73,16 +74,16 @@ func TestCollector_Check(t *testing.T) { collr := New() collr.URL = ts.URL collr.Repositories = []string{"name1/repo1", "name2/repo2", "name3/repo3"} - require.NoError(t, collr.Init()) - assert.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.NoError(t, collr.Check(context.Background())) } func TestCollector_CheckNG(t *testing.T) { collr := New() collr.URL = "http://127.0.0.1:38001/metrics" collr.Repositories = []string{"name1/repo1", "name2/repo2", "name3/repo3"} - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_Collect(t *testing.T) { @@ -103,8 +104,8 @@ func TestCollector_Collect(t *testing.T) { collr := New() collr.URL = ts.URL collr.Repositories = []string{"name1/repo1", "name2/repo2", "name3/repo3"} - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) expected := map[string]int64{ "star_count_user1/name1": 45, @@ -119,7 +120,7 @@ func TestCollector_Collect(t *testing.T) { "pull_sum": 55620576, } - collected := collr.Collect() + collected := collr.Collect(context.Background()) for k := range collected { if strings.HasPrefix(k, "last") { @@ -140,8 +141,8 @@ func TestCollector_InvalidData(t *testing.T) { collr := New() collr.URL = ts.URL collr.Repositories = []string{"name1/repo1", "name2/repo2", "name3/repo3"} - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_404(t *testing.T) { @@ -154,6 +155,6 @@ func TestCollector_404(t *testing.T) { collr := New() collr.Repositories = []string{"name1/repo1", "name2/repo2", "name3/repo3"} - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } diff --git a/src/go/plugin/go.d/collector/dovecot/collector.go b/src/go/plugin/go.d/collector/dovecot/collector.go index 990622ab308a8b..1475c5db35b9d9 100644 --- a/src/go/plugin/go.d/collector/dovecot/collector.go +++ b/src/go/plugin/go.d/collector/dovecot/collector.go @@ -3,6 +3,7 @@ package dovecot import ( + "context" _ "embed" "errors" "time" @@ -53,7 +54,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.Address == "" { return errors.New("config: 'address' not set") } @@ -61,7 +62,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -78,7 +79,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -91,7 +92,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.conn != nil { c.conn.disconnect() c.conn = nil diff --git a/src/go/plugin/go.d/collector/dovecot/collector_test.go b/src/go/plugin/go.d/collector/dovecot/collector_test.go index 916d82a38a194d..5d54560308239b 100644 --- a/src/go/plugin/go.d/collector/dovecot/collector_test.go +++ b/src/go/plugin/go.d/collector/dovecot/collector_test.go @@ -3,6 +3,7 @@ package dovecot import ( + "context" "errors" "os" "testing" @@ -59,9 +60,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -80,7 +81,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.newConn = func(Config) dovecotConn { return prepareMockOk() } - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -88,7 +89,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.newConn = func(Config) dovecotConn { return prepareMockOk() } - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -98,7 +99,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -137,9 +138,9 @@ func TestCollector_Check(t *testing.T) { collr.newConn = func(Config) dovecotConn { return mock } if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -212,7 +213,7 @@ func TestCollector_Collect(t *testing.T) { mock := test.prepareMock() collr.newConn = func(Config) dovecotConn { return mock } - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) @@ -221,7 +222,7 @@ func TestCollector_Collect(t *testing.T) { } assert.Equal(t, test.disconnectBeforeCleanup, mock.disconnectCalled, "disconnect before cleanup") - collr.Cleanup() + collr.Cleanup(context.Background()) assert.Equal(t, test.disconnectAfterCleanup, mock.disconnectCalled, "disconnect after cleanup") }) } diff --git a/src/go/plugin/go.d/collector/elasticsearch/collector.go b/src/go/plugin/go.d/collector/elasticsearch/collector.go index 9fccb97cbcee7b..11f6150959eec1 100644 --- a/src/go/plugin/go.d/collector/elasticsearch/collector.go +++ b/src/go/plugin/go.d/collector/elasticsearch/collector.go @@ -3,6 +3,7 @@ package elasticsearch import ( + "context" _ "embed" "errors" "fmt" @@ -85,7 +86,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { err := c.validateConfig() if err != nil { return fmt.Errorf("check configuration: %v", err) @@ -100,7 +101,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -116,7 +117,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -128,7 +129,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/elasticsearch/collector_test.go b/src/go/plugin/go.d/collector/elasticsearch/collector_test.go index 331755b4d7f481..fe525f00fccbe9 100644 --- a/src/go/plugin/go.d/collector/elasticsearch/collector_test.go +++ b/src/go/plugin/go.d/collector/elasticsearch/collector_test.go @@ -3,6 +3,7 @@ package elasticsearch import ( + "context" "net/http" "net/http/httptest" "os" @@ -113,9 +114,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -138,9 +139,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -151,7 +152,7 @@ func TestCollector_Charts(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Collect(t *testing.T) { @@ -633,7 +634,7 @@ func TestCollector_Collect(t *testing.T) { var mx map[string]int64 for i := 0; i < 10; i++ { - mx = collr.Collect() + mx = collr.Collect(context.Background()) } assert.Equal(t, test.wantCollected, mx) @@ -649,7 +650,7 @@ func prepareElasticsearch(t *testing.T, createES func() *Collector) (collr *Coll collr = createES() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -666,7 +667,7 @@ func prepareElasticsearchInvalidData(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -679,7 +680,7 @@ func prepareElasticsearch404(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -688,7 +689,7 @@ func prepareElasticsearchConnectionRefused(t *testing.T) (*Collector, func()) { t.Helper() collr := New() collr.URL = "http://127.0.0.1:38001" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } diff --git a/src/go/plugin/go.d/collector/envoy/collector.go b/src/go/plugin/go.d/collector/envoy/collector.go index 95cd3df803a57e..6fa0ee2cbd2fcf 100644 --- a/src/go/plugin/go.d/collector/envoy/collector.go +++ b/src/go/plugin/go.d/collector/envoy/collector.go @@ -3,6 +3,7 @@ package envoy import ( + "context" _ "embed" "errors" "fmt" @@ -74,7 +75,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -88,7 +89,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -104,7 +105,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -116,7 +117,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.prom == nil || c.prom.HTTPClient() == nil { return } diff --git a/src/go/plugin/go.d/collector/envoy/collector_test.go b/src/go/plugin/go.d/collector/envoy/collector_test.go index 1f8e7b28a464d3..0fceea041e3624 100644 --- a/src/go/plugin/go.d/collector/envoy/collector_test.go +++ b/src/go/plugin/go.d/collector/envoy/collector_test.go @@ -3,6 +3,7 @@ package envoy import ( + "context" "net/http" "net/http/httptest" "os" @@ -63,9 +64,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -74,10 +75,10 @@ func TestCollector_Init(t *testing.T) { func TestCollector_Cleanup(t *testing.T) { collr := New() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) - require.NoError(t, collr.Init()) - assert.NotPanics(t, collr.Cleanup) + require.NoError(t, collr.Init(context.Background())) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) } func TestCollector_Charts(t *testing.T) { @@ -86,8 +87,8 @@ func TestCollector_Charts(t *testing.T) { require.Empty(t, *collr.Charts()) - require.NoError(t, collr.Init()) - _ = collr.Collect() + require.NoError(t, collr.Init(context.Background())) + _ = collr.Collect(context.Background()) require.NotEmpty(t, *collr.Charts()) } @@ -119,12 +120,12 @@ func TestCollector_Check(t *testing.T) { collr, cleanup := test.prepare() defer cleanup() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -499,9 +500,9 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare() defer cleanup() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) module.TestMetricsHasAllChartsDims(t, collr.Charts(), mx) diff --git a/src/go/plugin/go.d/collector/exim/collector.go b/src/go/plugin/go.d/collector/exim/collector.go index a0dd3e7095a019..96ff7ea2f0c4d3 100644 --- a/src/go/plugin/go.d/collector/exim/collector.go +++ b/src/go/plugin/go.d/collector/exim/collector.go @@ -3,6 +3,7 @@ package exim import ( + "context" _ "embed" "errors" "fmt" @@ -53,7 +54,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { exim, err := c.initEximExec() if err != nil { return fmt.Errorf("exim exec initialization: %v", err) @@ -63,7 +64,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -80,7 +81,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -93,4 +94,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/exim/collector_test.go b/src/go/plugin/go.d/collector/exim/collector_test.go index a1b66e1d98fbc9..26894116352763 100644 --- a/src/go/plugin/go.d/collector/exim/collector_test.go +++ b/src/go/plugin/go.d/collector/exim/collector_test.go @@ -3,6 +3,7 @@ package exim import ( + "context" "errors" "os" "testing" @@ -49,9 +50,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -70,7 +71,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOK() - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -78,7 +79,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOK() - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -88,7 +89,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -127,9 +128,9 @@ func TestCollector_Check(t *testing.T) { collr.exec = mock if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -166,7 +167,7 @@ func TestCollector_Collect(t *testing.T) { mock := test.prepareMock() collr.exec = mock - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantMetrics, mx) diff --git a/src/go/plugin/go.d/collector/fail2ban/collector.go b/src/go/plugin/go.d/collector/fail2ban/collector.go index 39d55a59bbbe5f..8b588403b52cb2 100644 --- a/src/go/plugin/go.d/collector/fail2ban/collector.go +++ b/src/go/plugin/go.d/collector/fail2ban/collector.go @@ -5,6 +5,7 @@ package fail2ban import ( + "context" _ "embed" "errors" "fmt" @@ -64,7 +65,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { f2bClientExec, err := c.initFail2banClientCliExec() if err != nil { return fmt.Errorf("fail2ban-client exec initialization: %v", err) @@ -74,7 +75,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -91,7 +92,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -104,4 +105,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/fail2ban/collector_test.go b/src/go/plugin/go.d/collector/fail2ban/collector_test.go index 11b93e8c42beb2..76e0b76e7cb505 100644 --- a/src/go/plugin/go.d/collector/fail2ban/collector_test.go +++ b/src/go/plugin/go.d/collector/fail2ban/collector_test.go @@ -5,6 +5,7 @@ package fail2ban import ( + "context" "errors" "os" "testing" @@ -57,9 +58,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -78,7 +79,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOk() - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -86,7 +87,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOk() - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -96,7 +97,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -131,9 +132,9 @@ func TestCollector_Check(t *testing.T) { collr.exec = mock if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -169,7 +170,7 @@ func TestCollector_Collect(t *testing.T) { mock := test.prepareMock() collr.exec = mock - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantMetrics, mx) diff --git a/src/go/plugin/go.d/collector/filecheck/collector.go b/src/go/plugin/go.d/collector/filecheck/collector.go index 791c333741014b..b298982669f191 100644 --- a/src/go/plugin/go.d/collector/filecheck/collector.go +++ b/src/go/plugin/go.d/collector/filecheck/collector.go @@ -3,6 +3,7 @@ package filecheck import ( + "context" _ "embed" "fmt" "time" @@ -78,7 +79,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { err := c.validateConfig() if err != nil { return fmt.Errorf("config validation: %v", err) @@ -102,7 +103,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { return nil } @@ -110,7 +111,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -123,4 +124,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/filecheck/collector_test.go b/src/go/plugin/go.d/collector/filecheck/collector_test.go index 28c48262235d34..63a5f8ef8404d5 100644 --- a/src/go/plugin/go.d/collector/filecheck/collector_test.go +++ b/src/go/plugin/go.d/collector/filecheck/collector_test.go @@ -3,6 +3,7 @@ package filecheck import ( + "context" "os" "strings" "testing" @@ -32,7 +33,7 @@ func TestCollector_ConfigurationSerialize(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Init(t *testing.T) { @@ -100,9 +101,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) } }) } @@ -124,9 +125,9 @@ func TestCollector_Check(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { collr := test.prepare() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) }) } } @@ -239,9 +240,9 @@ func TestCollector_Collect(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { collr := test.prepare() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - mx := collr.Collect() + mx := collr.Collect(context.Background()) copyModTime(test.wantCollected, mx) diff --git a/src/go/plugin/go.d/collector/fluentd/collector.go b/src/go/plugin/go.d/collector/fluentd/collector.go index 0c09a867e7e9e8..a7de7309df772a 100644 --- a/src/go/plugin/go.d/collector/fluentd/collector.go +++ b/src/go/plugin/go.d/collector/fluentd/collector.go @@ -3,6 +3,7 @@ package fluentd import ( + "context" _ "embed" "errors" "fmt" @@ -63,7 +64,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("invalid config: %v", err) } @@ -86,7 +87,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -102,7 +103,7 @@ func (c *Collector) Charts() *Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { @@ -113,7 +114,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.apiClient != nil && c.apiClient.httpClient != nil { c.apiClient.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/fluentd/collector_test.go b/src/go/plugin/go.d/collector/fluentd/collector_test.go index 169f790dee1f97..5872ce789306f4 100644 --- a/src/go/plugin/go.d/collector/fluentd/collector_test.go +++ b/src/go/plugin/go.d/collector/fluentd/collector_test.go @@ -3,6 +3,7 @@ package fluentd import ( + "context" "net/http" "net/http/httptest" "os" @@ -38,13 +39,13 @@ func TestCollector_ConfigurationSerialize(t *testing.T) { func TestCollector_Init(t *testing.T) { // OK collr := New() - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.apiClient) //NG collr = New() collr.URL = "" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Check(t *testing.T) { @@ -57,14 +58,14 @@ func TestCollector_Check(t *testing.T) { // OK collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) // NG collr = New() collr.URL = "http://127.0.0.1:38001/api/plugins.json" - require.NoError(t, collr.Init()) - require.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.Error(t, collr.Check(context.Background())) } func TestCollector_Charts(t *testing.T) { @@ -72,7 +73,7 @@ func TestCollector_Charts(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - New().Cleanup() + New().Cleanup(context.Background()) } func TestCollector_Collect(t *testing.T) { @@ -85,8 +86,8 @@ func TestCollector_Collect(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) expected := map[string]int64{ "output_stdout_stdout_output_retry_count": 0, @@ -94,7 +95,7 @@ func TestCollector_Collect(t *testing.T) { "output_td_tdlog_output_buffer_queue_length": 0, "output_td_tdlog_output_buffer_total_queued_size": 0, } - assert.Equal(t, expected, collr.Collect()) + assert.Equal(t, expected, collr.Collect(context.Background())) assert.Len(t, collr.charts.Get("retry_count").Dims, 2) assert.Len(t, collr.charts.Get("buffer_queue_length").Dims, 1) assert.Len(t, collr.charts.Get("buffer_total_queued_size").Dims, 1) @@ -109,8 +110,8 @@ func TestCollector_InvalidData(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_404(t *testing.T) { @@ -122,6 +123,6 @@ func TestCollector_404(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } diff --git a/src/go/plugin/go.d/collector/freeradius/collector.go b/src/go/plugin/go.d/collector/freeradius/collector.go index 3eaa21dc127f2c..2c8f389cc80787 100644 --- a/src/go/plugin/go.d/collector/freeradius/collector.go +++ b/src/go/plugin/go.d/collector/freeradius/collector.go @@ -3,6 +3,7 @@ package freeradius import ( + "context" _ "embed" "errors" "fmt" @@ -59,7 +60,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -74,7 +75,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -90,7 +91,7 @@ func (c *Collector) Charts() *Charts { return charts.Copy() } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -102,4 +103,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/freeradius/collector_test.go b/src/go/plugin/go.d/collector/freeradius/collector_test.go index 4375d741ed7d77..63721c44607e95 100644 --- a/src/go/plugin/go.d/collector/freeradius/collector_test.go +++ b/src/go/plugin/go.d/collector/freeradius/collector_test.go @@ -3,6 +3,7 @@ package freeradius import ( + "context" "errors" "os" "testing" @@ -35,42 +36,42 @@ func TestCollector_ConfigurationSerialize(t *testing.T) { func TestCollector_Init(t *testing.T) { collr := New() - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } func TestCollector_Init_ReturnsFalseIfAddressNotSet(t *testing.T) { collr := New() collr.Address = "" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Init_ReturnsFalseIfPortNotSet(t *testing.T) { collr := New() collr.Port = 0 - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Init_ReturnsFalseIfSecretNotSet(t *testing.T) { collr := New() collr.Secret = "" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Check(t *testing.T) { collr := New() collr.client = newOKMockClient() - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } func TestCollector_Check_ReturnsFalseIfClientStatusReturnsError(t *testing.T) { collr := New() collr.client = newErrorMockClient() - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_Charts(t *testing.T) { @@ -117,7 +118,7 @@ func TestCollector_Collect(t *testing.T) { "proxy-acct-dropped-requests": 33, "proxy-acct-unknown-types": 34, } - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, expected, mx) module.TestMetricsHasAllChartsDims(t, collr.Charts(), mx) @@ -127,11 +128,11 @@ func TestCollector_Collect_ReturnsNilIfClientStatusReturnsError(t *testing.T) { collr := New() collr.client = newErrorMockClient() - assert.Nil(t, collr.Collect()) + assert.Nil(t, collr.Collect(context.Background())) } func TestCollector_Cleanup(t *testing.T) { - New().Cleanup() + New().Cleanup(context.Background()) } func newOKMockClient() *mockClient { diff --git a/src/go/plugin/go.d/collector/gearman/collect.go b/src/go/plugin/go.d/collector/gearman/collect.go index 7f59da82df32a8..025f59d4a90984 100644 --- a/src/go/plugin/go.d/collector/gearman/collect.go +++ b/src/go/plugin/go.d/collector/gearman/collect.go @@ -5,6 +5,7 @@ package gearman import ( "bufio" "bytes" + "context" "errors" "fmt" "strconv" @@ -22,13 +23,13 @@ func (c *Collector) collect() (map[string]int64, error) { status, err := c.conn.queryStatus() if err != nil { - c.Cleanup() + c.Cleanup(context.Background()) return nil, fmt.Errorf("couldn't query status: %v", err) } prioStatus, err := c.conn.queryPriorityStatus() if err != nil { - c.Cleanup() + c.Cleanup(context.Background()) return nil, fmt.Errorf("couldn't query priority status: %v", err) } diff --git a/src/go/plugin/go.d/collector/gearman/collector.go b/src/go/plugin/go.d/collector/gearman/collector.go index 05074f4ed87b21..c70b5da6eeb58f 100644 --- a/src/go/plugin/go.d/collector/gearman/collector.go +++ b/src/go/plugin/go.d/collector/gearman/collector.go @@ -3,6 +3,7 @@ package gearman import ( + "context" _ "embed" "errors" "time" @@ -58,7 +59,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.Address == "" { return errors.New("config: 'address' not set") } @@ -66,7 +67,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -83,7 +84,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -96,7 +97,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.conn != nil { c.conn.disconnect() c.conn = nil diff --git a/src/go/plugin/go.d/collector/gearman/collector_test.go b/src/go/plugin/go.d/collector/gearman/collector_test.go index c55ce2cb3bc8eb..39e56e6205495a 100644 --- a/src/go/plugin/go.d/collector/gearman/collector_test.go +++ b/src/go/plugin/go.d/collector/gearman/collector_test.go @@ -3,6 +3,7 @@ package gearman import ( + "context" "errors" "os" "testing" @@ -62,9 +63,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -83,7 +84,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.newConn = func(Config) gearmanConn { return prepareMockOk() } - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -91,7 +92,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.newConn = func(Config) gearmanConn { return prepareMockOk() } - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -101,7 +102,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -140,9 +141,9 @@ func TestCollector_Check(t *testing.T) { collr.newConn = func(Config) gearmanConn { return mock } if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -238,7 +239,7 @@ func TestCollector_Collect(t *testing.T) { mock := test.prepareMock() collr.newConn = func(Config) gearmanConn { return mock } - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx, "want metrics") @@ -248,7 +249,7 @@ func TestCollector_Collect(t *testing.T) { } assert.Equal(t, test.disconnectBeforeCleanup, mock.disconnectCalled, "disconnect before cleanup") - collr.Cleanup() + collr.Cleanup(context.Background()) assert.Equal(t, test.disconnectAfterCleanup, mock.disconnectCalled, "disconnect after cleanup") }) } diff --git a/src/go/plugin/go.d/collector/geth/collector.go b/src/go/plugin/go.d/collector/geth/collector.go index 072d9a0933ef03..ca045db27c15d1 100644 --- a/src/go/plugin/go.d/collector/geth/collector.go +++ b/src/go/plugin/go.d/collector/geth/collector.go @@ -3,6 +3,7 @@ package geth import ( + "context" _ "embed" "errors" "fmt" @@ -59,7 +60,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("error on validating config: %v", err) } @@ -73,7 +74,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -88,7 +89,7 @@ func (c *Collector) Charts() *Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -100,7 +101,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.prom != nil && c.prom.HTTPClient() != nil { c.prom.HTTPClient().CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/haproxy/collector.go b/src/go/plugin/go.d/collector/haproxy/collector.go index f10360a6678824..82461f50571b62 100644 --- a/src/go/plugin/go.d/collector/haproxy/collector.go +++ b/src/go/plugin/go.d/collector/haproxy/collector.go @@ -3,6 +3,7 @@ package haproxy import ( + "context" _ "embed" "errors" "fmt" @@ -65,7 +66,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -79,7 +80,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -94,7 +95,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -107,7 +108,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.prom != nil && c.prom.HTTPClient() != nil { c.prom.HTTPClient().CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/haproxy/collector_test.go b/src/go/plugin/go.d/collector/haproxy/collector_test.go index 7f52a16e0b3317..84ffd2b43c3e65 100644 --- a/src/go/plugin/go.d/collector/haproxy/collector_test.go +++ b/src/go/plugin/go.d/collector/haproxy/collector_test.go @@ -3,6 +3,7 @@ package haproxy import ( + "context" "net/http" "net/http/httptest" "os" @@ -68,9 +69,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -81,7 +82,7 @@ func TestCollector_Charts(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Check(t *testing.T) { @@ -113,9 +114,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -173,7 +174,7 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare(t) defer cleanup() - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantCollected, mx) if len(test.wantCollected) > 0 { @@ -191,7 +192,7 @@ func prepareCaseHaproxyV231Metrics(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -219,7 +220,7 @@ application_backend_http_responses_total{proxy="infra-vernemq-ws",code="other"} })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -232,7 +233,7 @@ func prepareCase404Response(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -241,7 +242,7 @@ func prepareCaseConnectionRefused(t *testing.T) (*Collector, func()) { t.Helper() collr := New() collr.URL = "http://127.0.0.1:38001" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } diff --git a/src/go/plugin/go.d/collector/hddtemp/collector.go b/src/go/plugin/go.d/collector/hddtemp/collector.go index e2c75a14a61767..2779dedccff499 100644 --- a/src/go/plugin/go.d/collector/hddtemp/collector.go +++ b/src/go/plugin/go.d/collector/hddtemp/collector.go @@ -3,6 +3,7 @@ package hddtemp import ( + "context" _ "embed" "errors" "fmt" @@ -57,7 +58,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.Address == "" { return fmt.Errorf("config: 'address' not set") } @@ -67,7 +68,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -82,7 +83,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -94,4 +95,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/hddtemp/collector_test.go b/src/go/plugin/go.d/collector/hddtemp/collector_test.go index 9c1343b64446c6..a0c986d682ef22 100644 --- a/src/go/plugin/go.d/collector/hddtemp/collector_test.go +++ b/src/go/plugin/go.d/collector/hddtemp/collector_test.go @@ -3,6 +3,7 @@ package hddtemp import ( + "context" "errors" "os" "testing" @@ -62,9 +63,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -83,7 +84,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.conn = prepareMockAllDisksOk() - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -91,7 +92,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.conn = prepareMockAllDisksOk() - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -101,7 +102,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -139,9 +140,9 @@ func TestCollector_Check(t *testing.T) { collr.conn = test.prepareMock() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -229,7 +230,7 @@ func TestCollector_Collect(t *testing.T) { collr := New() collr.conn = test.prepareMock() - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantMetrics, mx) diff --git a/src/go/plugin/go.d/collector/hdfs/collector.go b/src/go/plugin/go.d/collector/hdfs/collector.go index 6d2b14e00435e6..6dfd271badb84b 100644 --- a/src/go/plugin/go.d/collector/hdfs/collector.go +++ b/src/go/plugin/go.d/collector/hdfs/collector.go @@ -3,6 +3,7 @@ package hdfs import ( + "context" _ "embed" "errors" "fmt" @@ -60,7 +61,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.URL == "" { return errors.New("URL is required but not set") } @@ -74,7 +75,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { typ, err := c.determineNodeType() if err != nil { return fmt.Errorf("error on node type determination : %v", err) @@ -104,7 +105,7 @@ func (c *Collector) Charts() *Charts { } } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -114,7 +115,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/hdfs/collector_test.go b/src/go/plugin/go.d/collector/hdfs/collector_test.go index 393aef4c6f1021..a416390e073e39 100644 --- a/src/go/plugin/go.d/collector/hdfs/collector_test.go +++ b/src/go/plugin/go.d/collector/hdfs/collector_test.go @@ -3,6 +3,7 @@ package hdfs import ( + "context" "net/http" "net/http/httptest" "os" @@ -42,14 +43,14 @@ func TestCollector_ConfigurationSerialize(t *testing.T) { func TestCollector_Init(t *testing.T) { collr := New() - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } func TestCollector_InitErrorOnCreatingClientWrongTLSCA(t *testing.T) { collr := New() collr.ClientConfig.TLSConfig.TLSCA = "testdata/tls" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Check(t *testing.T) { @@ -62,9 +63,9 @@ func TestCollector_Check(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) assert.NotZero(t, collr.nodeType) } @@ -78,9 +79,9 @@ func TestCollector_CheckDataNode(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) assert.Equal(t, dataNodeType, collr.nodeType) } @@ -94,9 +95,9 @@ func TestCollector_CheckNameNode(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) assert.Equal(t, nameNodeType, collr.nodeType) } @@ -110,17 +111,17 @@ func TestCollector_CheckErrorOnNodeTypeDetermination(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_CheckNoResponse(t *testing.T) { collr := New() collr.URL = "http://127.0.0.1:38001/jmx" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_Charts(t *testing.T) { @@ -148,7 +149,7 @@ func TestCollector_ChartsNameNode(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - New().Cleanup() + New().Cleanup(context.Background()) } func TestCollector_CollectDataNode(t *testing.T) { @@ -161,8 +162,8 @@ func TestCollector_CollectDataNode(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) expected := map[string]int64{ "dna_bytes_read": 80689178, @@ -200,7 +201,7 @@ func TestCollector_CollectDataNode(t *testing.T) { "rpc_sent_bytes": 187, } - assert.Equal(t, expected, collr.Collect()) + assert.Equal(t, expected, collr.Collect(context.Background())) } func TestCollector_CollectNameNode(t *testing.T) { @@ -213,8 +214,8 @@ func TestCollector_CollectNameNode(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) expected := map[string]int64{ "fsns_blocks_total": 15, @@ -259,7 +260,7 @@ func TestCollector_CollectNameNode(t *testing.T) { "rpc_sent_bytes": 25067414, } - assert.Equal(t, expected, collr.Collect()) + assert.Equal(t, expected, collr.Collect(context.Background())) } func TestCollector_CollectUnknownNode(t *testing.T) { @@ -272,17 +273,17 @@ func TestCollector_CollectUnknownNode(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.Panics(t, func() { _ = collr.Collect() }) + assert.Panics(t, func() { _ = collr.Collect(context.Background()) }) } func TestCollector_CollectNoResponse(t *testing.T) { collr := New() collr.URL = "http://127.0.0.1:38001/jmx" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.Nil(t, collr.Collect()) + assert.Nil(t, collr.Collect(context.Background())) } func TestCollector_CollectReceiveInvalidResponse(t *testing.T) { @@ -295,9 +296,9 @@ func TestCollector_CollectReceiveInvalidResponse(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.Nil(t, collr.Collect()) + assert.Nil(t, collr.Collect(context.Background())) } func TestCollector_CollectReceive404(t *testing.T) { @@ -310,7 +311,7 @@ func TestCollector_CollectReceive404(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.Nil(t, collr.Collect()) + assert.Nil(t, collr.Collect(context.Background())) } diff --git a/src/go/plugin/go.d/collector/hpssa/collector.go b/src/go/plugin/go.d/collector/hpssa/collector.go index 7dfcc5dad47f4c..db7938a8825600 100644 --- a/src/go/plugin/go.d/collector/hpssa/collector.go +++ b/src/go/plugin/go.d/collector/hpssa/collector.go @@ -3,6 +3,7 @@ package hpssa import ( + "context" _ "embed" "errors" "fmt" @@ -62,7 +63,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { ssacli, err := c.initSsacliBinary() if err != nil { return fmt.Errorf("ssacli exec initialization: %v", err) @@ -72,7 +73,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -89,7 +90,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -102,4 +103,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/hpssa/collector_test.go b/src/go/plugin/go.d/collector/hpssa/collector_test.go index 1fb58ae3a85c24..684562f0ec3142 100644 --- a/src/go/plugin/go.d/collector/hpssa/collector_test.go +++ b/src/go/plugin/go.d/collector/hpssa/collector_test.go @@ -3,6 +3,7 @@ package hpssa import ( + "context" "errors" "os" "testing" @@ -53,9 +54,9 @@ func TestCollector_Init(t *testing.T) { collr := New() if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -74,7 +75,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOkP212andP410i() - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -82,7 +83,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOkP212andP410i() - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -92,7 +93,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -139,9 +140,9 @@ func TestCollector_Check(t *testing.T) { collr.exec = mock if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -385,7 +386,7 @@ func TestCollector_Collect(t *testing.T) { mock := test.prepareMock() collr.exec = mock - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantMetrics, mx) diff --git a/src/go/plugin/go.d/collector/httpcheck/collector.go b/src/go/plugin/go.d/collector/httpcheck/collector.go index 98816eb805af4d..b460fbb412530a 100644 --- a/src/go/plugin/go.d/collector/httpcheck/collector.go +++ b/src/go/plugin/go.d/collector/httpcheck/collector.go @@ -3,6 +3,7 @@ package httpcheck import ( + "context" _ "embed" "errors" "fmt" @@ -80,7 +81,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -119,7 +120,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -134,7 +135,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -146,7 +147,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/httpcheck/collector_test.go b/src/go/plugin/go.d/collector/httpcheck/collector_test.go index 91ab8801cfbbce..c898c5336a61c1 100644 --- a/src/go/plugin/go.d/collector/httpcheck/collector_test.go +++ b/src/go/plugin/go.d/collector/httpcheck/collector_test.go @@ -3,6 +3,7 @@ package httpcheck import ( + "context" "net/http" "net/http/httptest" "os" @@ -77,9 +78,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -101,7 +102,7 @@ func TestCollector_Charts(t *testing.T) { prepare: func(t *testing.T) *Collector { collr := New() collr.URL = "http://127.0.0.1:38001" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr }, @@ -123,11 +124,11 @@ func TestCollector_Charts(t *testing.T) { func TestCollector_Cleanup(t *testing.T) { collr := New() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) collr.URL = "http://127.0.0.1:38001" - require.NoError(t, collr.Init()) - assert.NotPanics(t, collr.Cleanup) + require.NoError(t, collr.Init(context.Background())) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) } func TestCollector_Check(t *testing.T) { @@ -150,12 +151,12 @@ func TestCollector_Check(t *testing.T) { collr, cleanup := test.prepare() defer cleanup() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -459,12 +460,12 @@ func TestCollector_Collect(t *testing.T) { test.update(collr) } - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) var mx map[string]int64 for i := 0; i < 2; i++ { - mx = collr.Collect() + mx = collr.Collect(context.Background()) time.Sleep(time.Duration(collr.UpdateEvery) * time.Second) } diff --git a/src/go/plugin/go.d/collector/icecast/collector.go b/src/go/plugin/go.d/collector/icecast/collector.go index 3e56371b07b91a..6de7e8504645ae 100644 --- a/src/go/plugin/go.d/collector/icecast/collector.go +++ b/src/go/plugin/go.d/collector/icecast/collector.go @@ -3,6 +3,7 @@ package icecast import ( + "context" _ "embed" "errors" "fmt" @@ -63,7 +64,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.URL == "" { return errors.New("url not set") } @@ -80,7 +81,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -97,7 +98,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -110,7 +111,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/icecast/collector_test.go b/src/go/plugin/go.d/collector/icecast/collector_test.go index d1ea03b36c2974..1494abc8223b2c 100644 --- a/src/go/plugin/go.d/collector/icecast/collector_test.go +++ b/src/go/plugin/go.d/collector/icecast/collector_test.go @@ -3,6 +3,7 @@ package icecast import ( + "context" "net/http" "net/http/httptest" "os" @@ -65,9 +66,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -114,9 +115,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -162,7 +163,7 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare(t) defer cleanup() - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) if len(test.wantMetrics) > 0 { @@ -187,7 +188,7 @@ func prepareCaseMultipleSources(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -206,7 +207,7 @@ func prepareCaseSingleSource(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -225,7 +226,7 @@ func prepareCaseNoSources(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -256,7 +257,7 @@ func prepareCaseUnexpectedJsonResponse(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -270,7 +271,7 @@ func prepareCaseInvalidFormatResponse(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -279,7 +280,7 @@ func prepareCaseConnectionRefused(t *testing.T) (*Collector, func()) { t.Helper() collr := New() collr.URL = "http://127.0.0.1:65001" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } diff --git a/src/go/plugin/go.d/collector/intelgpu/collector.go b/src/go/plugin/go.d/collector/intelgpu/collector.go index 035fcb4dec258a..2e9ceadb1f7e2a 100644 --- a/src/go/plugin/go.d/collector/intelgpu/collector.go +++ b/src/go/plugin/go.d/collector/intelgpu/collector.go @@ -3,6 +3,7 @@ package intelgpu import ( + "context" _ "embed" "errors" "fmt" @@ -49,7 +50,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { topExec, err := c.initIntelGPUTopExec() if err != nil { @@ -61,7 +62,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -78,7 +79,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -91,7 +92,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.exec != nil { if err := c.exec.stop(); err != nil { c.Error(err) diff --git a/src/go/plugin/go.d/collector/intelgpu/collector_test.go b/src/go/plugin/go.d/collector/intelgpu/collector_test.go index f220d4e4b563c1..7177f0bae1a63f 100644 --- a/src/go/plugin/go.d/collector/intelgpu/collector_test.go +++ b/src/go/plugin/go.d/collector/intelgpu/collector_test.go @@ -3,6 +3,7 @@ package intelgpu import ( + "context" "errors" "os" "testing" @@ -55,9 +56,9 @@ func TestCollector_Init(t *testing.T) { test.prepare(collr) if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -85,9 +86,9 @@ func TestCollector_Check(t *testing.T) { collr.exec = mock if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -123,7 +124,7 @@ func TestCollector_Collect(t *testing.T) { mock := test.prepareMock() collr.exec = mock - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantMetrics, mx) if len(test.wantMetrics) > 0 { @@ -146,7 +147,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOK() - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -154,7 +155,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOK() - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -166,7 +167,7 @@ func TestCollector_Cleanup(t *testing.T) { mock, ok := collr.exec.(*mockIntelGpuTop) - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) if ok { assert.True(t, mock.stopCalled) diff --git a/src/go/plugin/go.d/collector/ipfs/collector.go b/src/go/plugin/go.d/collector/ipfs/collector.go index cb152ff3eb9467..59ad802cf56d48 100644 --- a/src/go/plugin/go.d/collector/ipfs/collector.go +++ b/src/go/plugin/go.d/collector/ipfs/collector.go @@ -3,6 +3,7 @@ package ipfs import ( + "context" _ "embed" "errors" "fmt" @@ -64,7 +65,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.URL == "" { return errors.New("url not set") } @@ -90,7 +91,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -107,7 +108,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -120,7 +121,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/ipfs/collector_test.go b/src/go/plugin/go.d/collector/ipfs/collector_test.go index 51f6d13e6aa18e..b1b1220bc5e30a 100644 --- a/src/go/plugin/go.d/collector/ipfs/collector_test.go +++ b/src/go/plugin/go.d/collector/ipfs/collector_test.go @@ -3,6 +3,7 @@ package ipfs import ( + "context" "net/http" "net/http/httptest" "os" @@ -67,9 +68,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -112,9 +113,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -162,7 +163,7 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare(t) defer cleanup() - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) @@ -193,7 +194,7 @@ func prepareCaseOkDefault(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -234,7 +235,7 @@ func prepareCaseUnexpectedJsonResponse(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -248,7 +249,7 @@ func prepareCaseInvalidFormatResponse(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -257,7 +258,7 @@ func prepareCaseConnectionRefused(t *testing.T) (*Collector, func()) { t.Helper() collr := New() collr.URL = "http://127.0.0.1:65001" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } diff --git a/src/go/plugin/go.d/collector/isc_dhcpd/collector.go b/src/go/plugin/go.d/collector/isc_dhcpd/collector.go index 68fc6dc8afb04f..a84d83e8e25b50 100644 --- a/src/go/plugin/go.d/collector/isc_dhcpd/collector.go +++ b/src/go/plugin/go.d/collector/isc_dhcpd/collector.go @@ -5,6 +5,7 @@ package isc_dhcpd import ( + "context" _ "embed" "errors" "fmt" @@ -65,7 +66,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { err := c.validateConfig() if err != nil { return fmt.Errorf("config validation: %v", err) @@ -89,7 +90,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -104,7 +105,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -117,4 +118,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/isc_dhcpd/collector_test.go b/src/go/plugin/go.d/collector/isc_dhcpd/collector_test.go index a2fd5c1dfe2c28..3e5c9f1c921be5 100644 --- a/src/go/plugin/go.d/collector/isc_dhcpd/collector_test.go +++ b/src/go/plugin/go.d/collector/isc_dhcpd/collector_test.go @@ -5,6 +5,7 @@ package isc_dhcpd import ( + "context" "os" "testing" @@ -33,7 +34,7 @@ func TestCollector_ConfigurationSerialize(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Init(t *testing.T) { @@ -84,9 +85,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -108,12 +109,12 @@ func TestCollector_Check(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { collr := test.prepare() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -125,7 +126,7 @@ func TestCollector_Charts(t *testing.T) { collr.Pools = []PoolConfig{ {Name: "name", Networks: "192.0.2.0/24"}, } - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.Charts()) } @@ -226,9 +227,9 @@ func TestCollector_Collect(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { collr := test.prepare() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantCollected, mx) if len(mx) > 0 { diff --git a/src/go/plugin/go.d/collector/k8s_kubelet/collector.go b/src/go/plugin/go.d/collector/k8s_kubelet/collector.go index a74642bb878769..92d3c22048adfb 100644 --- a/src/go/plugin/go.d/collector/k8s_kubelet/collector.go +++ b/src/go/plugin/go.d/collector/k8s_kubelet/collector.go @@ -3,6 +3,7 @@ package k8s_kubelet import ( + "context" _ "embed" "errors" "fmt" @@ -70,7 +71,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -88,7 +89,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -103,7 +104,7 @@ func (c *Collector) Charts() *Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { @@ -114,7 +115,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.prom != nil && c.prom.HTTPClient() != nil { c.prom.HTTPClient().CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/k8s_kubelet/collector_test.go b/src/go/plugin/go.d/collector/k8s_kubelet/collector_test.go index b280a9c58b892f..79863ff4f0cfc1 100644 --- a/src/go/plugin/go.d/collector/k8s_kubelet/collector_test.go +++ b/src/go/plugin/go.d/collector/k8s_kubelet/collector_test.go @@ -3,6 +3,7 @@ package k8s_kubelet import ( + "context" "net/http" "net/http/httptest" "os" @@ -42,18 +43,18 @@ func TestCollector_Charts(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - New().Cleanup() + New().Cleanup(context.Background()) } func TestCollector_Init(t *testing.T) { - assert.NoError(t, New().Init()) + assert.NoError(t, New().Init(context.Background())) } func TestCollector_Init_ReadServiceAccountToken(t *testing.T) { collr := New() collr.TokenPath = "testdata/token.txt" - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) assert.Equal(t, "Bearer "+string(dataServiceAccountToken), collr.RequestConfig.Headers["Authorization"]) } @@ -61,7 +62,7 @@ func TestCollector_InitErrorOnCreatingClientWrongTLSCA(t *testing.T) { collr := New() collr.ClientConfig.TLSConfig.TLSCA = "testdata/tls" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Check(t *testing.T) { @@ -74,15 +75,15 @@ func TestCollector_Check(t *testing.T) { collr := New() collr.URL = ts.URL + "/metrics" - require.NoError(t, collr.Init()) - assert.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.NoError(t, collr.Check(context.Background())) } func TestCollector_Check_ConnectionRefused(t *testing.T) { collr := New() collr.URL = "http://127.0.0.1:38001/metrics" - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_Collect(t *testing.T) { @@ -95,8 +96,8 @@ func TestCollector_Collect(t *testing.T) { collr := New() collr.URL = ts.URL + "/metrics" - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) expected := map[string]int64{ "apiserver_audit_requests_rejected_total": 0, @@ -181,7 +182,7 @@ func TestCollector_Collect(t *testing.T) { "volume_manager_plugin_kubernetes.io/secret_state_desired": 4, } - assert.Equal(t, expected, collr.Collect()) + assert.Equal(t, expected, collr.Collect(context.Background())) } func TestCollector_Collect_ReceiveInvalidResponse(t *testing.T) { @@ -194,8 +195,8 @@ func TestCollector_Collect_ReceiveInvalidResponse(t *testing.T) { collr := New() collr.URL = ts.URL + "/metrics" - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_Collect_Receive404(t *testing.T) { @@ -208,6 +209,6 @@ func TestCollector_Collect_Receive404(t *testing.T) { collr := New() collr.URL = ts.URL + "/metrics" - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } diff --git a/src/go/plugin/go.d/collector/k8s_kubeproxy/collector.go b/src/go/plugin/go.d/collector/k8s_kubeproxy/collector.go index 9a63c71b88cdbd..565ad54d54fcf4 100644 --- a/src/go/plugin/go.d/collector/k8s_kubeproxy/collector.go +++ b/src/go/plugin/go.d/collector/k8s_kubeproxy/collector.go @@ -3,6 +3,7 @@ package k8s_kubeproxy import ( + "context" _ "embed" "errors" "fmt" @@ -63,7 +64,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -77,7 +78,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -92,7 +93,7 @@ func (c *Collector) Charts() *Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { @@ -103,7 +104,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.prom != nil && c.prom.HTTPClient() != nil { c.prom.HTTPClient().CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/k8s_kubeproxy/collector_test.go b/src/go/plugin/go.d/collector/k8s_kubeproxy/collector_test.go index 29053c943ee8d1..62748b4a284a04 100644 --- a/src/go/plugin/go.d/collector/k8s_kubeproxy/collector_test.go +++ b/src/go/plugin/go.d/collector/k8s_kubeproxy/collector_test.go @@ -3,6 +3,7 @@ package k8s_kubeproxy import ( + "context" "net/http" "net/http/httptest" "os" @@ -40,17 +41,17 @@ func TestCollector_Charts(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - New().Cleanup() + New().Cleanup(context.Background()) } func TestCollector_Init(t *testing.T) { - assert.NoError(t, New().Init()) + assert.NoError(t, New().Init(context.Background())) } func TestCollector_InitNG(t *testing.T) { collr := New() collr.URL = "" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Check(t *testing.T) { @@ -63,15 +64,15 @@ func TestCollector_Check(t *testing.T) { collr := New() collr.URL = ts.URL + "/metrics" - require.NoError(t, collr.Init()) - assert.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.NoError(t, collr.Check(context.Background())) } func TestCollector_CheckNG(t *testing.T) { collr := New() collr.URL = "http://127.0.0.1:38001/metrics" - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_Collect(t *testing.T) { @@ -84,8 +85,8 @@ func TestCollector_Collect(t *testing.T) { collr := New() collr.URL = ts.URL + "/metrics" - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) expected := map[string]int64{ "sync_proxy_rules_count": 2669, @@ -114,7 +115,7 @@ func TestCollector_Collect(t *testing.T) { "http_request_duration_099": 9464, } - assert.Equal(t, expected, collr.Collect()) + assert.Equal(t, expected, collr.Collect(context.Background())) } func TestCollector_InvalidData(t *testing.T) { @@ -127,8 +128,8 @@ func TestCollector_InvalidData(t *testing.T) { collr := New() collr.URL = ts.URL + "/metrics" - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_404(t *testing.T) { @@ -141,6 +142,6 @@ func TestCollector_404(t *testing.T) { collr := New() collr.URL = ts.URL + "/metrics" - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } diff --git a/src/go/plugin/go.d/collector/k8s_state/collector.go b/src/go/plugin/go.d/collector/k8s_state/collector.go index 8f3d6e086f87dd..844b6422661415 100644 --- a/src/go/plugin/go.d/collector/k8s_state/collector.go +++ b/src/go/plugin/go.d/collector/k8s_state/collector.go @@ -70,7 +70,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { client, err := c.initClient() if err != nil { return fmt.Errorf("init k8s client: %v", err) @@ -84,7 +84,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { if c.client == nil || c.discoverer == nil { return errors.New("not initialized") } @@ -103,7 +103,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { ms, err := c.collect() if err != nil { c.Error(err) @@ -115,7 +115,7 @@ func (c *Collector) Collect() map[string]int64 { return ms } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.ctxCancel == nil { return } diff --git a/src/go/plugin/go.d/collector/k8s_state/collector_test.go b/src/go/plugin/go.d/collector/k8s_state/collector_test.go index cac05eb736d218..88b8f7d11f0d9a 100644 --- a/src/go/plugin/go.d/collector/k8s_state/collector_test.go +++ b/src/go/plugin/go.d/collector/k8s_state/collector_test.go @@ -70,9 +70,9 @@ func TestCollector_Init(t *testing.T) { collr := test.prepare() if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -105,12 +105,12 @@ func TestCollector_Check(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { collr := test.prepare() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -162,14 +162,14 @@ func TestCollector_Cleanup(t *testing.T) { collr := test.prepare() if test.doInit { - _ = collr.Init() + _ = collr.Init(context.Background()) } if test.doCollect { - _ = collr.Collect() + _ = collr.Collect(context.Background()) time.Sleep(collr.initDelay) } - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) time.Sleep(time.Second) if test.doCollect { assert.True(t, collr.discoverer.stopped()) @@ -197,7 +197,7 @@ func TestCollector_Collect(t *testing.T) { ) step1 := func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "discovery_node_discoverer_state": 1, "discovery_pod_discoverer_state": 1, @@ -265,7 +265,7 @@ func TestCollector_Collect(t *testing.T) { ) step1 := func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "discovery_node_discoverer_state": 1, "discovery_pod_discoverer_state": 1, @@ -366,7 +366,7 @@ func TestCollector_Collect(t *testing.T) { ) step1 := func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "discovery_node_discoverer_state": 1, "discovery_pod_discoverer_state": 1, @@ -505,12 +505,12 @@ func TestCollector_Collect(t *testing.T) { pod, ) step1 := func(t *testing.T, collr *Collector) { - _ = collr.Collect() + _ = collr.Collect(context.Background()) _ = client.CoreV1().Pods(pod.Namespace).Delete(ctx, pod.Name, metav1.DeleteOptions{}) } step2 := func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "discovery_node_discoverer_state": 1, "discovery_pod_discoverer_state": 1, @@ -586,7 +586,7 @@ func TestCollector_Collect(t *testing.T) { podUpdated := newPod(node.Name, "pod01") // with set Spec.NodeName step1 := func(t *testing.T, collr *Collector) { - _ = collr.Collect() + _ = collr.Collect(context.Background()) for _, c := range *collr.Charts() { if strings.HasPrefix(c.ID, "pod_") { ok := isLabelValueSet(c, labelKeyNodeName) @@ -597,7 +597,7 @@ func TestCollector_Collect(t *testing.T) { step2 := func(t *testing.T, collr *Collector) { _, _ = client.CoreV1().Pods(podOrig.Namespace).Update(ctx, podUpdated, metav1.UpdateOptions{}) time.Sleep(time.Millisecond * 50) - _ = collr.Collect() + _ = collr.Collect(context.Background()) for _, c := range *collr.Charts() { if strings.HasPrefix(c.ID, "pod_") { @@ -624,12 +624,12 @@ func TestCollector_Collect(t *testing.T) { pod1, ) step1 := func(t *testing.T, collr *Collector) { - _ = collr.Collect() + _ = collr.Collect(context.Background()) _, _ = client.CoreV1().Pods(pod1.Namespace).Create(ctx, pod2, metav1.CreateOptions{}) } step2 := func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "discovery_node_discoverer_state": 1, "discovery_pod_discoverer_state": 1, @@ -840,13 +840,13 @@ func TestCollector_Collect(t *testing.T) { collr := New() collr.newKubeClient = func() (kubernetes.Interface, error) { return test.client, nil } - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) - defer collr.Cleanup() + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) + defer collr.Cleanup(context.Background()) for i, executeStep := range test.steps { if i == 0 { - _ = collr.Collect() + _ = collr.Collect(context.Background()) time.Sleep(collr.initDelay) } else { time.Sleep(time.Second) diff --git a/src/go/plugin/go.d/collector/lighttpd/collector.go b/src/go/plugin/go.d/collector/lighttpd/collector.go index da750e473190cb..133f6d771c1d15 100644 --- a/src/go/plugin/go.d/collector/lighttpd/collector.go +++ b/src/go/plugin/go.d/collector/lighttpd/collector.go @@ -3,6 +3,7 @@ package lighttpd import ( + "context" _ "embed" "errors" "fmt" @@ -59,7 +60,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.URL == "" { return errors.New("URL is required but not set") } @@ -79,7 +80,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -96,7 +97,7 @@ func (c *Collector) Charts() *Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -106,7 +107,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/lighttpd/collector_test.go b/src/go/plugin/go.d/collector/lighttpd/collector_test.go index 396805d62b8d28..62134f270bc19a 100644 --- a/src/go/plugin/go.d/collector/lighttpd/collector_test.go +++ b/src/go/plugin/go.d/collector/lighttpd/collector_test.go @@ -3,6 +3,7 @@ package lighttpd import ( + "context" "net/http" "net/http/httptest" "os" @@ -35,19 +36,19 @@ func TestCollector_ConfigurationSerialize(t *testing.T) { module.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML) } -func TestCollector_Cleanup(t *testing.T) { New().Cleanup() } +func TestCollector_Cleanup(t *testing.T) { New().Cleanup(context.Background()) } func TestCollector_Init(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) } func TestCollector_InitNG(t *testing.T) { collr := New() collr.URL = "" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Check(t *testing.T) { @@ -60,16 +61,16 @@ func TestCollector_Check(t *testing.T) { collr := New() collr.URL = ts.URL + "/server-status?auto" - require.NoError(t, collr.Init()) - assert.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.NoError(t, collr.Check(context.Background())) } func TestCollector_CheckNG(t *testing.T) { collr := New() collr.URL = "http://127.0.0.1:38001/server-status?auto" - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_Charts(t *testing.T) { assert.NotNil(t, New().Charts()) } @@ -84,8 +85,8 @@ func TestCollector_Collect(t *testing.T) { collr := New() collr.URL = ts.URL + "/server-status?auto" - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) expected := map[string]int64{ "scoreboard_waiting": 125, @@ -108,7 +109,7 @@ func TestCollector_Collect(t *testing.T) { "total_accesses": 12, } - assert.Equal(t, expected, collr.Collect()) + assert.Equal(t, expected, collr.Collect(context.Background())) } func TestCollector_InvalidData(t *testing.T) { @@ -121,8 +122,8 @@ func TestCollector_InvalidData(t *testing.T) { collr := New() collr.URL = ts.URL + "/server-status?auto" - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_ApacheData(t *testing.T) { @@ -135,8 +136,8 @@ func TestCollector_ApacheData(t *testing.T) { collr := New() collr.URL = ts.URL + "/server-status?auto" - require.NoError(t, collr.Init()) - require.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.Error(t, collr.Check(context.Background())) } func TestCollector_404(t *testing.T) { @@ -149,6 +150,6 @@ func TestCollector_404(t *testing.T) { collr := New() collr.URL = ts.URL + "/server-status?auto" - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } diff --git a/src/go/plugin/go.d/collector/litespeed/collector.go b/src/go/plugin/go.d/collector/litespeed/collector.go index ca5bea25783273..6bc10ae81f2e63 100644 --- a/src/go/plugin/go.d/collector/litespeed/collector.go +++ b/src/go/plugin/go.d/collector/litespeed/collector.go @@ -5,6 +5,7 @@ package litespeed import ( + "context" _ "embed" "errors" @@ -54,14 +55,14 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.ReportsDir == "" { return errors.New("reports_dir is required") } return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -78,9 +79,8 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() - if err != nil { c.Error(err) return nil @@ -89,4 +89,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/litespeed/collector_test.go b/src/go/plugin/go.d/collector/litespeed/collector_test.go index 4ef4abf7a0c55a..e80549a5ab486b 100644 --- a/src/go/plugin/go.d/collector/litespeed/collector_test.go +++ b/src/go/plugin/go.d/collector/litespeed/collector_test.go @@ -5,6 +5,7 @@ package litespeed import ( + "context" "os" "testing" @@ -55,9 +56,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -87,9 +88,9 @@ func TestCollector_Check(t *testing.T) { collr := test.prepareLitespeed() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -127,7 +128,7 @@ func TestCollector_Collect(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepareLitespeed() - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantMetrics, mx) diff --git a/src/go/plugin/go.d/collector/logind/collector.go b/src/go/plugin/go.d/collector/logind/collector.go index 8e4dc727945ab1..1348f3a8b6cfce 100644 --- a/src/go/plugin/go.d/collector/logind/collector.go +++ b/src/go/plugin/go.d/collector/logind/collector.go @@ -5,6 +5,7 @@ package logind import ( + "context" _ "embed" "errors" "time" @@ -58,11 +59,11 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -77,7 +78,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -89,7 +90,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.conn != nil { c.conn.Close() } diff --git a/src/go/plugin/go.d/collector/logind/collector_test.go b/src/go/plugin/go.d/collector/logind/collector_test.go index 0c6f3e57aaa4c0..0eb7a94871770c 100644 --- a/src/go/plugin/go.d/collector/logind/collector_test.go +++ b/src/go/plugin/go.d/collector/logind/collector_test.go @@ -5,6 +5,7 @@ package logind import ( + "context" "errors" "os" "testing" @@ -52,9 +53,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -75,15 +76,15 @@ func TestCollector_Cleanup(t *testing.T) { }, "after Init": { wantClose: false, - prepare: func(l *Collector) { _ = l.Init() }, + prepare: func(l *Collector) { _ = l.Init(context.Background()) }, }, "after Check": { wantClose: true, - prepare: func(l *Collector) { _ = l.Init(); _ = l.Check() }, + prepare: func(l *Collector) { _ = l.Init(context.Background()); _ = l.Check(context.Background()) }, }, "after Collect": { wantClose: true, - prepare: func(l *Collector) { _ = l.Init(); l.Collect() }, + prepare: func(l *Collector) { _ = l.Init(context.Background()); l.Collect(context.Background()) }, }, } @@ -94,7 +95,7 @@ func TestCollector_Cleanup(t *testing.T) { collr.newLogindConn = func(Config) (logindConnection, error) { return m, nil } test.prepare(collr) - require.NotPanics(t, collr.Cleanup) + require.NotPanics(t, func() { collr.Cleanup(context.Background()) }) if test.wantClose { assert.True(t, m.closeCalled) @@ -139,13 +140,13 @@ func TestCollector_Check(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.conn = test.prepare() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -213,10 +214,10 @@ func TestCollector_Collect(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.conn = test.prepare() - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.expected, mx) }) diff --git a/src/go/plugin/go.d/collector/logstash/collector.go b/src/go/plugin/go.d/collector/logstash/collector.go index f55319f4cf95d3..ffa1f92a62c852 100644 --- a/src/go/plugin/go.d/collector/logstash/collector.go +++ b/src/go/plugin/go.d/collector/logstash/collector.go @@ -3,6 +3,7 @@ package logstash import ( + "context" _ "embed" "errors" "fmt" @@ -62,7 +63,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.URL == "" { return errors.New("config: 'url' cannot be empty") } @@ -79,7 +80,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -94,7 +95,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -106,7 +107,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/logstash/collector_test.go b/src/go/plugin/go.d/collector/logstash/collector_test.go index f33db91af97f4e..78d4967dbe5e8c 100644 --- a/src/go/plugin/go.d/collector/logstash/collector_test.go +++ b/src/go/plugin/go.d/collector/logstash/collector_test.go @@ -3,6 +3,7 @@ package logstash import ( + "context" "net/http" "net/http/httptest" "os" @@ -62,9 +63,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -75,7 +76,7 @@ func TestCollector_Charts(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Check(t *testing.T) { @@ -107,9 +108,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -175,7 +176,7 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare(t) defer cleanup() - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) if len(test.wantMetrics) > 0 { @@ -199,7 +200,7 @@ func caseValidResponse(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -212,7 +213,7 @@ func caseInvalidDataResponse(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -221,7 +222,7 @@ func caseConnectionRefused(t *testing.T) (*Collector, func()) { t.Helper() collr := New() collr.URL = "http://127.0.0.1:65001" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } @@ -234,7 +235,7 @@ func case404(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } diff --git a/src/go/plugin/go.d/collector/lvm/collector.go b/src/go/plugin/go.d/collector/lvm/collector.go index b598e62fd78067..434820554f783a 100644 --- a/src/go/plugin/go.d/collector/lvm/collector.go +++ b/src/go/plugin/go.d/collector/lvm/collector.go @@ -5,6 +5,7 @@ package lvm import ( + "context" _ "embed" "errors" "fmt" @@ -58,7 +59,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { lvmExec, err := c.initLVMCLIExec() if err != nil { return fmt.Errorf("init lvm exec: %v", err) @@ -68,7 +69,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -85,7 +86,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -98,4 +99,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/lvm/collector_test.go b/src/go/plugin/go.d/collector/lvm/collector_test.go index 727a5835f11422..3908e5a1be4aa7 100644 --- a/src/go/plugin/go.d/collector/lvm/collector_test.go +++ b/src/go/plugin/go.d/collector/lvm/collector_test.go @@ -5,6 +5,7 @@ package lvm import ( + "context" "errors" "os" "testing" @@ -57,9 +58,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -78,7 +79,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOK() - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -86,7 +87,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOK() - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -96,7 +97,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -139,9 +140,9 @@ func TestCollector_Check(t *testing.T) { collr.exec = mock if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -183,7 +184,7 @@ func TestCollector_Collect(t *testing.T) { mock := test.prepareMock() collr.exec = mock - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantMetrics, mx) if len(test.wantMetrics) > 0 { diff --git a/src/go/plugin/go.d/collector/maxscale/collector.go b/src/go/plugin/go.d/collector/maxscale/collector.go index 4b80cf6d43cd9c..9bbabe537948ee 100644 --- a/src/go/plugin/go.d/collector/maxscale/collector.go +++ b/src/go/plugin/go.d/collector/maxscale/collector.go @@ -3,6 +3,7 @@ package maxscale import ( + "context" _ "embed" "errors" "fmt" @@ -64,7 +65,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.URL == "" { return errors.New("URL required but not set") } @@ -81,7 +82,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -98,7 +99,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -108,7 +109,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/maxscale/collector_test.go b/src/go/plugin/go.d/collector/maxscale/collector_test.go index 63fc0dee38a0ae..259d2613c99fad 100644 --- a/src/go/plugin/go.d/collector/maxscale/collector_test.go +++ b/src/go/plugin/go.d/collector/maxscale/collector_test.go @@ -3,6 +3,7 @@ package maxscale import ( + "context" "net/http" "net/http/httptest" "os" @@ -65,9 +66,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -106,9 +107,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -181,9 +182,9 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare(t) defer cleanup() - _ = collr.Check() + _ = collr.Check(context.Background()) - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) @@ -213,7 +214,7 @@ func caseOk(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -243,7 +244,7 @@ func caseUnexpectedJsonResponse(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -256,7 +257,7 @@ func caseInvalidDataResponse(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -265,7 +266,7 @@ func caseConnectionRefused(t *testing.T) (*Collector, func()) { t.Helper() collr := New() collr.URL = "http://127.0.0.1:65001" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } @@ -278,7 +279,7 @@ func case404(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } diff --git a/src/go/plugin/go.d/collector/megacli/collector.go b/src/go/plugin/go.d/collector/megacli/collector.go index 657a2b44b4da24..45a8c368c3f550 100644 --- a/src/go/plugin/go.d/collector/megacli/collector.go +++ b/src/go/plugin/go.d/collector/megacli/collector.go @@ -5,6 +5,7 @@ package megacli import ( + "context" _ "embed" "errors" "fmt" @@ -62,7 +63,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { lvmExec, err := c.initMegaCliExec() if err != nil { return fmt.Errorf("init megacli exec: %v", err) @@ -72,7 +73,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -89,7 +90,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -102,4 +103,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/megacli/collector_test.go b/src/go/plugin/go.d/collector/megacli/collector_test.go index 064ee4baa2c1c1..efebe2c862a3e4 100644 --- a/src/go/plugin/go.d/collector/megacli/collector_test.go +++ b/src/go/plugin/go.d/collector/megacli/collector_test.go @@ -5,6 +5,7 @@ package megacli import ( + "context" "errors" "os" "testing" @@ -57,9 +58,9 @@ func TestCollector_Init(t *testing.T) { collr := New() if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -78,7 +79,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOK() - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -86,7 +87,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOK() - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -96,7 +97,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -139,9 +140,9 @@ func TestCollector_Check(t *testing.T) { collr.exec = mock if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -235,7 +236,7 @@ func TestCollector_Collect(t *testing.T) { mock := test.prepareMock() collr.exec = mock - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantMetrics, mx) assert.Len(t, *collr.Charts(), test.wantCharts) diff --git a/src/go/plugin/go.d/collector/memcached/collector.go b/src/go/plugin/go.d/collector/memcached/collector.go index 4142dfdcf43443..a7592717586fd5 100644 --- a/src/go/plugin/go.d/collector/memcached/collector.go +++ b/src/go/plugin/go.d/collector/memcached/collector.go @@ -3,6 +3,7 @@ package memcached import ( + "context" _ "embed" "errors" "time" @@ -53,7 +54,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.Address == "" { return errors.New("config: 'address' not set") } @@ -61,7 +62,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -78,7 +79,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -91,7 +92,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.conn != nil { c.conn.disconnect() c.conn = nil diff --git a/src/go/plugin/go.d/collector/memcached/collector_test.go b/src/go/plugin/go.d/collector/memcached/collector_test.go index 5d643910ced50c..db6f8893d804fd 100644 --- a/src/go/plugin/go.d/collector/memcached/collector_test.go +++ b/src/go/plugin/go.d/collector/memcached/collector_test.go @@ -3,6 +3,7 @@ package memcached import ( + "context" "errors" "os" "testing" @@ -60,9 +61,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -81,7 +82,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.newMemcachedConn = func(config Config) memcachedConn { return prepareMockOk() } - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -89,7 +90,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.newMemcachedConn = func(config Config) memcachedConn { return prepareMockOk() } - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -99,7 +100,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -138,9 +139,9 @@ func TestCollector_Check(t *testing.T) { collr.newMemcachedConn = func(config Config) memcachedConn { return mock } if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -221,7 +222,7 @@ func TestCollector_Collect(t *testing.T) { mock := test.prepareMock() collr.newMemcachedConn = func(config Config) memcachedConn { return mock } - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) @@ -230,7 +231,7 @@ func TestCollector_Collect(t *testing.T) { } assert.Equal(t, test.disconnectBeforeCleanup, mock.disconnectCalled, "disconnect before cleanup") - collr.Cleanup() + collr.Cleanup(context.Background()) assert.Equal(t, test.disconnectAfterCleanup, mock.disconnectCalled, "disconnect after cleanup") }) } diff --git a/src/go/plugin/go.d/collector/mongodb/collector.go b/src/go/plugin/go.d/collector/mongodb/collector.go index 60e7c044e42dc6..35ea1d8a1601bc 100644 --- a/src/go/plugin/go.d/collector/mongodb/collector.go +++ b/src/go/plugin/go.d/collector/mongodb/collector.go @@ -3,6 +3,7 @@ package mongo import ( + "context" _ "embed" "errors" "fmt" @@ -75,7 +76,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.verifyConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -87,7 +88,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -102,7 +103,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -116,7 +117,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.conn == nil { return } diff --git a/src/go/plugin/go.d/collector/mongodb/collector_test.go b/src/go/plugin/go.d/collector/mongodb/collector_test.go index 33a1f7af19c2f7..ab71737963f29e 100644 --- a/src/go/plugin/go.d/collector/mongodb/collector_test.go +++ b/src/go/plugin/go.d/collector/mongodb/collector_test.go @@ -3,6 +3,7 @@ package mongo import ( + "context" "encoding/json" "errors" "os" @@ -75,9 +76,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -114,7 +115,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare(t) - require.NotPanics(t, collr.Cleanup) + require.NotPanics(t, func() { collr.Cleanup(context.Background()) }) if test.wantClose { collr, ok := collr.conn.(*mockMongoClient) require.True(t, ok) @@ -146,15 +147,15 @@ func TestCollector_Check(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { collr := prepareMongo() - defer collr.Cleanup() + defer collr.Cleanup(context.Background()) collr.conn = test.prepare() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -597,12 +598,12 @@ func TestCollector_Collect(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { collr := prepareMongo() - defer collr.Cleanup() + defer collr.Cleanup(context.Background()) collr.conn = test.prepare() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantCollected, mx) }) diff --git a/src/go/plugin/go.d/collector/monit/collector.go b/src/go/plugin/go.d/collector/monit/collector.go index e56b7c45b799ed..89e6faf84670a5 100644 --- a/src/go/plugin/go.d/collector/monit/collector.go +++ b/src/go/plugin/go.d/collector/monit/collector.go @@ -3,6 +3,7 @@ package monit import ( + "context" _ "embed" "errors" "fmt" @@ -64,7 +65,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.URL == "" { return fmt.Errorf("config: monit url is required but not set") } @@ -81,7 +82,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -97,7 +98,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -109,7 +110,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/monit/collector_test.go b/src/go/plugin/go.d/collector/monit/collector_test.go index 94372b24c20275..37aa799da967ac 100644 --- a/src/go/plugin/go.d/collector/monit/collector_test.go +++ b/src/go/plugin/go.d/collector/monit/collector_test.go @@ -3,6 +3,7 @@ package monit import ( + "context" "net/http" "net/http/httptest" "os" @@ -62,9 +63,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -103,9 +104,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -255,9 +256,9 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare(t) defer cleanup() - _ = collr.Check() + _ = collr.Check(context.Background()) - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) @@ -281,7 +282,7 @@ func caseOk(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -330,7 +331,7 @@ func caseUnexpectedXMLResponse(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -343,7 +344,7 @@ func caseInvalidDataResponse(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -352,7 +353,7 @@ func caseConnectionRefused(t *testing.T) (*Collector, func()) { t.Helper() collr := New() collr.URL = "http://127.0.0.1:65001" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } @@ -365,7 +366,7 @@ func case404(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } diff --git a/src/go/plugin/go.d/collector/mysql/collector.go b/src/go/plugin/go.d/collector/mysql/collector.go index 2b54e9ab8eb474..a3f9156da5fd3b 100644 --- a/src/go/plugin/go.d/collector/mysql/collector.go +++ b/src/go/plugin/go.d/collector/mysql/collector.go @@ -3,6 +3,7 @@ package mysql import ( + "context" "database/sql" _ "embed" "errors" @@ -102,7 +103,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.MyCNF != "" { dsn, err := dsnFromFile(c.MyCNF) if err != nil { @@ -128,7 +129,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -143,7 +144,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -155,7 +156,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.db == nil { return } diff --git a/src/go/plugin/go.d/collector/mysql/collector_test.go b/src/go/plugin/go.d/collector/mysql/collector_test.go index 028e19410dfe3e..518976085792be 100644 --- a/src/go/plugin/go.d/collector/mysql/collector_test.go +++ b/src/go/plugin/go.d/collector/mysql/collector_test.go @@ -5,6 +5,7 @@ package mysql import ( "bufio" "bytes" + "context" "database/sql/driver" "errors" "fmt" @@ -117,9 +118,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -148,7 +149,7 @@ func TestCollector_Cleanup(t *testing.T) { collr, cleanup := prepare(t) defer cleanup() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) assert.Nil(t, collr.db) }) } @@ -257,14 +258,14 @@ func TestCollector_Check(t *testing.T) { collr.db = db defer func() { _ = db.Close() }() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) test.prepareMock(t, mock) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } assert.NoError(t, mock.ExpectationsWereMet()) }) @@ -290,7 +291,7 @@ func TestCollector_Collect(t *testing.T) { mockExpect(t, m, queryShowProcessList, dataMariaVer5564ProcessList) }, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "aborted_connects": 0, @@ -425,7 +426,7 @@ func TestCollector_Collect(t *testing.T) { mockExpect(t, m, queryShowProcessList, dataMariaVer1084ProcessList) }, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ @@ -607,7 +608,7 @@ func TestCollector_Collect(t *testing.T) { mockExpect(t, m, queryShowProcessList, dataMariaVer1084ProcessList) }, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "aborted_connects": 2, @@ -791,7 +792,7 @@ func TestCollector_Collect(t *testing.T) { mockExpect(t, m, queryShowProcessList, dataMariaVer1084ProcessList) }, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "aborted_connects": 2, @@ -978,7 +979,7 @@ func TestCollector_Collect(t *testing.T) { mockExpect(t, m, queryShowProcessList, dataMariaVer1084ProcessList) }, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "aborted_connects": 2, @@ -1159,7 +1160,7 @@ func TestCollector_Collect(t *testing.T) { mockExpect(t, m, queryShowProcessList, dataMariaGaleraClusterVer1084ProcessList) }, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "aborted_connects": 0, @@ -1355,7 +1356,7 @@ func TestCollector_Collect(t *testing.T) { mockExpect(t, m, queryShowProcessListPS, dataMySQLVer8030ProcessList) }, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "aborted_connects": 0, @@ -1494,7 +1495,7 @@ func TestCollector_Collect(t *testing.T) { mockExpect(t, m, queryShowProcessListPS, dataPerconaV8029ProcessList) }, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "aborted_connects": 1, @@ -1653,7 +1654,7 @@ func TestCollector_Collect(t *testing.T) { collr.db = db defer func() { _ = db.Close() }() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) for i, step := range test { t.Run(fmt.Sprintf("step[%d]", i), func(t *testing.T) { diff --git a/src/go/plugin/go.d/collector/nginx/collector.go b/src/go/plugin/go.d/collector/nginx/collector.go index 42a28069c9aaae..eb9e809d7b6181 100644 --- a/src/go/plugin/go.d/collector/nginx/collector.go +++ b/src/go/plugin/go.d/collector/nginx/collector.go @@ -3,6 +3,7 @@ package nginx import ( + "context" _ "embed" "errors" "fmt" @@ -59,7 +60,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.URL == "" { return errors.New("nginx URL required but not set") } @@ -76,7 +77,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -93,7 +94,7 @@ func (c *Collector) Charts() *Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -103,7 +104,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/nginx/collector_test.go b/src/go/plugin/go.d/collector/nginx/collector_test.go index 44bfe4ed6ea3fd..0b167ba12b08ad 100644 --- a/src/go/plugin/go.d/collector/nginx/collector_test.go +++ b/src/go/plugin/go.d/collector/nginx/collector_test.go @@ -3,6 +3,7 @@ package nginx import ( + "context" "net/http" "net/http/httptest" "os" @@ -38,13 +39,13 @@ func TestCollector_ConfigurationSerialize(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - New().Cleanup() + New().Cleanup(context.Background()) } func TestCollector_Init(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) } func TestCollector_Check(t *testing.T) { @@ -57,16 +58,16 @@ func TestCollector_Check(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) - assert.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.NoError(t, collr.Check(context.Background())) } func TestCollector_CheckNG(t *testing.T) { collr := New() collr.URL = "http://127.0.0.1:38001/us" - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_Charts(t *testing.T) { @@ -83,8 +84,8 @@ func TestCollector_Collect(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) expected := map[string]int64{ "accepts": 36, @@ -96,7 +97,7 @@ func TestCollector_Collect(t *testing.T) { "writing": 1, } - assert.Equal(t, expected, collr.Collect()) + assert.Equal(t, expected, collr.Collect(context.Background())) } func TestCollector_CollectTengine(t *testing.T) { @@ -109,8 +110,8 @@ func TestCollector_CollectTengine(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) expected := map[string]int64{ "accepts": 1140, @@ -123,7 +124,7 @@ func TestCollector_CollectTengine(t *testing.T) { "writing": 1, } - assert.Equal(t, expected, collr.Collect()) + assert.Equal(t, expected, collr.Collect(context.Background())) } func TestCollector_InvalidData(t *testing.T) { @@ -136,8 +137,8 @@ func TestCollector_InvalidData(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_404(t *testing.T) { @@ -150,6 +151,6 @@ func TestCollector_404(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } diff --git a/src/go/plugin/go.d/collector/nginxplus/collector.go b/src/go/plugin/go.d/collector/nginxplus/collector.go index deb3f2ef83f4ca..6c86380ea41d0f 100644 --- a/src/go/plugin/go.d/collector/nginxplus/collector.go +++ b/src/go/plugin/go.d/collector/nginxplus/collector.go @@ -3,6 +3,7 @@ package nginxplus import ( + "context" _ "embed" "errors" "fmt" @@ -79,7 +80,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.URL == "" { return errors.New("config: 'url' can not be empty'") } @@ -93,7 +94,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -108,9 +109,8 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() - if err != nil { c.Error(err) return nil @@ -119,7 +119,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/nginxplus/collector_test.go b/src/go/plugin/go.d/collector/nginxplus/collector_test.go index eecd972681064d..966bd10898b705 100644 --- a/src/go/plugin/go.d/collector/nginxplus/collector_test.go +++ b/src/go/plugin/go.d/collector/nginxplus/collector_test.go @@ -3,6 +3,7 @@ package nginxplus import ( + "context" "fmt" "net/http" "net/http/httptest" @@ -90,9 +91,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -127,9 +128,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -458,7 +459,7 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare(t) defer cleanup() - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) if len(test.wantMetrics) > 0 { @@ -512,7 +513,7 @@ func caseAPI8AllRequestsOK(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -554,7 +555,7 @@ func caseAPI8AllRequestsExceptStreamOK(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -567,7 +568,7 @@ func caseInvalidDataResponse(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -576,7 +577,7 @@ func caseConnectionRefused(t *testing.T) (*Collector, func()) { t.Helper() collr := New() collr.URL = "http://127.0.0.1:65001" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } diff --git a/src/go/plugin/go.d/collector/nginxunit/collector.go b/src/go/plugin/go.d/collector/nginxunit/collector.go index 31efbd3994c638..3d3d5acafb8e30 100644 --- a/src/go/plugin/go.d/collector/nginxunit/collector.go +++ b/src/go/plugin/go.d/collector/nginxunit/collector.go @@ -3,6 +3,7 @@ package nginxunit import ( + "context" _ "embed" "errors" "fmt" @@ -59,7 +60,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.URL == "" { return errors.New("URL required but not set") } @@ -76,7 +77,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -93,7 +94,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -103,7 +104,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/nginxunit/collector_test.go b/src/go/plugin/go.d/collector/nginxunit/collector_test.go index 68b20c1d74f096..2c976b40179d44 100644 --- a/src/go/plugin/go.d/collector/nginxunit/collector_test.go +++ b/src/go/plugin/go.d/collector/nginxunit/collector_test.go @@ -3,6 +3,7 @@ package nginxunit import ( + "context" "net/http" "net/http/httptest" "os" @@ -62,9 +63,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -103,9 +104,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -155,9 +156,9 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare(t) defer cleanup() - _ = collr.Check() + _ = collr.Check(context.Background()) - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) @@ -183,7 +184,7 @@ func caseOk(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -213,7 +214,7 @@ func caseUnexpectedJsonResponse(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -226,7 +227,7 @@ func caseInvalidDataResponse(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -235,7 +236,7 @@ func caseConnectionRefused(t *testing.T) (*Collector, func()) { t.Helper() collr := New() collr.URL = "http://127.0.0.1:65001" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } @@ -248,7 +249,7 @@ func case404(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } diff --git a/src/go/plugin/go.d/collector/nginxvts/collector.go b/src/go/plugin/go.d/collector/nginxvts/collector.go index cad0eb2246c07f..bb9f5db75fd35d 100644 --- a/src/go/plugin/go.d/collector/nginxvts/collector.go +++ b/src/go/plugin/go.d/collector/nginxvts/collector.go @@ -3,6 +3,7 @@ package nginxvts import ( + "context" _ "embed" "errors" "fmt" @@ -61,14 +62,14 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient == nil { return } c.httpClient.CloseIdleConnections() } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { err := c.validateConfig() if err != nil { return fmt.Errorf("config: %v", err) @@ -89,7 +90,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -104,7 +105,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) diff --git a/src/go/plugin/go.d/collector/nginxvts/collector_test.go b/src/go/plugin/go.d/collector/nginxvts/collector_test.go index 1834fb50349fe4..e7aef84479b9cb 100644 --- a/src/go/plugin/go.d/collector/nginxvts/collector_test.go +++ b/src/go/plugin/go.d/collector/nginxvts/collector_test.go @@ -3,6 +3,7 @@ package nginxvts import ( + "context" "net/http" "net/http/httptest" "os" @@ -75,9 +76,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) assert.Equal(t, test.wantNumOfCharts, len(*collr.Charts())) } }) @@ -101,9 +102,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -114,7 +115,7 @@ func TestCollector_Charts(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Collect(t *testing.T) { @@ -169,7 +170,7 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare(t) defer cleanup() - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantCollected, mx) if test.checkCharts { @@ -185,7 +186,7 @@ func prepareNginxVTS(t *testing.T, createNginxVTS func() *Collector) (collr *Col srv := prepareNginxVTSEndpoint() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -202,7 +203,7 @@ func prepareNginxVTSInvalidData(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -215,7 +216,7 @@ func prepareNginxVTS404(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -224,7 +225,7 @@ func prepareNginxVTSConnectionRefused(t *testing.T) (*Collector, func()) { t.Helper() collr := New() collr.URL = "http://127.0.0.1:18080" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } diff --git a/src/go/plugin/go.d/collector/nsd/collector.go b/src/go/plugin/go.d/collector/nsd/collector.go index e48313505c3153..2c36c3964482aa 100644 --- a/src/go/plugin/go.d/collector/nsd/collector.go +++ b/src/go/plugin/go.d/collector/nsd/collector.go @@ -5,6 +5,7 @@ package nsd import ( + "context" _ "embed" "errors" "fmt" @@ -55,7 +56,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { nsdControl, err := c.initNsdControlExec() if err != nil { return fmt.Errorf("nsd-control exec initialization: %v", err) @@ -65,7 +66,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -82,7 +83,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -95,4 +96,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/nsd/collector_test.go b/src/go/plugin/go.d/collector/nsd/collector_test.go index 7f43e216829bf8..e87bfc3d464a4a 100644 --- a/src/go/plugin/go.d/collector/nsd/collector_test.go +++ b/src/go/plugin/go.d/collector/nsd/collector_test.go @@ -5,6 +5,7 @@ package nsd import ( + "context" "errors" "os" "testing" @@ -54,9 +55,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -75,7 +76,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOK() - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -83,7 +84,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOK() - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -93,7 +94,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -132,9 +133,9 @@ func TestCollector_Check(t *testing.T) { collr.exec = mock if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -288,7 +289,7 @@ func TestCollector_Collect(t *testing.T) { mock := test.prepareMock() collr.exec = mock - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantMetrics, mx) diff --git a/src/go/plugin/go.d/collector/ntpd/collector.go b/src/go/plugin/go.d/collector/ntpd/collector.go index f65f8b15829757..c9b2a4dd3284c8 100644 --- a/src/go/plugin/go.d/collector/ntpd/collector.go +++ b/src/go/plugin/go.d/collector/ntpd/collector.go @@ -3,6 +3,7 @@ package ntpd import ( + "context" _ "embed" "errors" "fmt" @@ -65,7 +66,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.Address == "" { return errors.New("config: 'address' can not be empty") } @@ -81,7 +82,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -96,7 +97,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -108,7 +109,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.client != nil { c.client.close() c.client = nil diff --git a/src/go/plugin/go.d/collector/ntpd/collector_test.go b/src/go/plugin/go.d/collector/ntpd/collector_test.go index faf4ff62869287..25d35b6d362097 100644 --- a/src/go/plugin/go.d/collector/ntpd/collector_test.go +++ b/src/go/plugin/go.d/collector/ntpd/collector_test.go @@ -3,6 +3,7 @@ package ntpd import ( + "context" "errors" "fmt" "os" @@ -54,9 +55,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -77,15 +78,15 @@ func TestCollector_Cleanup(t *testing.T) { }, "after Init": { wantClose: false, - prepare: func(n *Collector) { _ = n.Init() }, + prepare: func(n *Collector) { _ = n.Init(context.Background()) }, }, "after Check": { wantClose: true, - prepare: func(n *Collector) { _ = n.Init(); _ = n.Check() }, + prepare: func(n *Collector) { _ = n.Init(context.Background()); _ = n.Check(context.Background()) }, }, "after Collect": { wantClose: true, - prepare: func(n *Collector) { _ = n.Init(); n.Collect() }, + prepare: func(n *Collector) { _ = n.Init(context.Background()); n.Collect(context.Background()) }, }, } @@ -95,7 +96,7 @@ func TestCollector_Cleanup(t *testing.T) { collr := prepareNTPdWithMock(m, true) test.prepare(collr) - require.NotPanics(t, collr.Cleanup) + require.NotPanics(t, func() { collr.Cleanup(context.Background()) }) if test.wantClose { assert.True(t, m.closeCalled) @@ -137,12 +138,12 @@ func TestCollector_Check(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -258,10 +259,11 @@ func TestCollector_Collect(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - require.NoError(t, collr.Init()) - _ = collr.Check() + require.NoError(t, collr.Init(context.Background())) - mx := collr.Collect() + _ = collr.Check(context.Background()) + + mx := collr.Collect(context.Background()) assert.Equal(t, test.expected, mx) assert.Equal(t, test.expectedCharts, len(*collr.Charts())) diff --git a/src/go/plugin/go.d/collector/nvidia_smi/collector.go b/src/go/plugin/go.d/collector/nvidia_smi/collector.go index 4748bac06d8c9b..04dba9d54aa7c9 100644 --- a/src/go/plugin/go.d/collector/nvidia_smi/collector.go +++ b/src/go/plugin/go.d/collector/nvidia_smi/collector.go @@ -3,6 +3,7 @@ package nvidia_smi import ( + "context" _ "embed" "errors" "runtime" @@ -66,7 +67,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.exec == nil { if runtime.GOOS == "windows" && c.LoopMode { c.LoopMode = false @@ -81,7 +82,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -96,7 +97,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -108,7 +109,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.exec != nil { if err := c.exec.stop(); err != nil { c.Errorf("cleanup: %v", err) diff --git a/src/go/plugin/go.d/collector/nvidia_smi/collector_test.go b/src/go/plugin/go.d/collector/nvidia_smi/collector_test.go index 7c2751baddeadd..b61acd4573cdfa 100644 --- a/src/go/plugin/go.d/collector/nvidia_smi/collector_test.go +++ b/src/go/plugin/go.d/collector/nvidia_smi/collector_test.go @@ -3,6 +3,7 @@ package nvidia_smi import ( + "context" "errors" "fmt" "os" @@ -64,9 +65,9 @@ func TestCollector_Init(t *testing.T) { test.prepare(collr) if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -110,9 +111,9 @@ func TestCollector_Check(t *testing.T) { test.prepare(collr) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -128,7 +129,7 @@ func TestCollector_Collect(t *testing.T) { { prepare: prepareCaseMIGA100, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "gpu_GPU-27b94a00-ed54-5c24-b1fd-1054085de32a_bar1_memory_usage_free": 68718428160, @@ -188,7 +189,7 @@ func TestCollector_Collect(t *testing.T) { { prepare: prepareCaseRTX4090Driver535, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "gpu_GPU-71d1acc2-662d-2166-bf9f-65272d2fc437_bar1_memory_usage_free": 267386880, @@ -238,7 +239,7 @@ func TestCollector_Collect(t *testing.T) { { prepare: prepareCaseRTX3060, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "gpu_GPU-473d8d0f-d462-185c-6b36-6fc23e23e571_bar1_memory_usage_free": 8586788864, @@ -287,7 +288,7 @@ func TestCollector_Collect(t *testing.T) { { prepare: prepareCaseTeslaP100, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "gpu_GPU-d3da8716-eaab-75db-efc1-60e88e1cd55e_bar1_memory_usage_free": 17177772032, @@ -335,7 +336,7 @@ func TestCollector_Collect(t *testing.T) { { prepare: prepareCaseRTX2080Win, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "gpu_GPU-fbd55ed4-1eec-4423-0a47-ad594b4333e3_bar1_memory_usage_free": 266338304, @@ -384,7 +385,7 @@ func TestCollector_Collect(t *testing.T) { { prepare: prepareCaseErrOnQueryGPUInfo, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, map[string]int64(nil), mx) }, diff --git a/src/go/plugin/go.d/collector/nvme/collector.go b/src/go/plugin/go.d/collector/nvme/collector.go index a8df38b5a5ec80..59127142b9e584 100644 --- a/src/go/plugin/go.d/collector/nvme/collector.go +++ b/src/go/plugin/go.d/collector/nvme/collector.go @@ -5,6 +5,7 @@ package nvme import ( + "context" _ "embed" "errors" "fmt" @@ -64,7 +65,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { nvmeExec, err := c.initNVMeCLIExec() if err != nil { return fmt.Errorf("init nvme-cli exec: %v", err) @@ -74,7 +75,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -89,7 +90,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -101,4 +102,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/nvme/collector_test.go b/src/go/plugin/go.d/collector/nvme/collector_test.go index 15d1db3882603f..a536cbdc7900c9 100644 --- a/src/go/plugin/go.d/collector/nvme/collector_test.go +++ b/src/go/plugin/go.d/collector/nvme/collector_test.go @@ -5,6 +5,7 @@ package nvme import ( + "context" "encoding/json" "errors" "fmt" @@ -61,9 +62,9 @@ func TestCollector_Init(t *testing.T) { collr := New() if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -74,7 +75,7 @@ func TestCollector_Charts(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Check(t *testing.T) { @@ -107,9 +108,9 @@ func TestCollector_Check(t *testing.T) { test.prepare(collr) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -126,7 +127,7 @@ func TestCollector_Collect(t *testing.T) { { prepare: prepareCaseOK, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "device_nvme0n1_available_spare": 100, @@ -189,7 +190,7 @@ func TestCollector_Collect(t *testing.T) { { prepare: prepareCaseStringValuesOK, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "device_nvme0n1_available_spare": 100, @@ -252,7 +253,7 @@ func TestCollector_Collect(t *testing.T) { { prepare: prepareCaseFloatValuesOK, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "device_nvme0n1_available_spare": 100, @@ -315,7 +316,7 @@ func TestCollector_Collect(t *testing.T) { { prepare: prepareCaseEmptyList, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, (map[string]int64)(nil), mx) }, @@ -325,7 +326,7 @@ func TestCollector_Collect(t *testing.T) { { prepare: prepareCaseErrOnList, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, (map[string]int64)(nil), mx) }, @@ -335,7 +336,7 @@ func TestCollector_Collect(t *testing.T) { { prepare: prepareCaseErrOnSmartLog, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, (map[string]int64)(nil), mx) }, diff --git a/src/go/plugin/go.d/collector/openldap/collect.go b/src/go/plugin/go.d/collector/openldap/collect.go index 244383f811a131..e43e50f2f97799 100644 --- a/src/go/plugin/go.d/collector/openldap/collect.go +++ b/src/go/plugin/go.d/collector/openldap/collect.go @@ -3,6 +3,7 @@ package openldap import ( + "context" "github.com/go-ldap/ldap/v3" ) @@ -18,11 +19,11 @@ func (c *Collector) collect() (map[string]int64, error) { mx := make(map[string]int64) if err := c.collectMonitorCounters(mx); err != nil { - c.Cleanup() + c.Cleanup(context.Background()) return nil, err } if err := c.collectOperations(mx); err != nil { - c.Cleanup() + c.Cleanup(context.Background()) return nil, err } diff --git a/src/go/plugin/go.d/collector/openldap/collector.go b/src/go/plugin/go.d/collector/openldap/collector.go index 3f925ec7157b65..e454cc4f711ed6 100644 --- a/src/go/plugin/go.d/collector/openldap/collector.go +++ b/src/go/plugin/go.d/collector/openldap/collector.go @@ -3,6 +3,7 @@ package openldap import ( + "context" _ "embed" "errors" "time" @@ -63,7 +64,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.URL == "" { return errors.New("empty LDAP server url") } @@ -74,7 +75,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -91,7 +92,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -104,7 +105,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.conn != nil { if err := c.conn.disconnect(); err != nil { c.Warningf("error disconnecting ldap client: %v", err) diff --git a/src/go/plugin/go.d/collector/openldap/collector_test.go b/src/go/plugin/go.d/collector/openldap/collector_test.go index d0dfa3d107703e..c751415941aa32 100644 --- a/src/go/plugin/go.d/collector/openldap/collector_test.go +++ b/src/go/plugin/go.d/collector/openldap/collector_test.go @@ -3,6 +3,7 @@ package openldap import ( + "context" "errors" "fmt" "os" @@ -58,9 +59,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -79,7 +80,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.newConn = func(Config) ldapConn { return prepareMockOk() } - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -87,7 +88,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.newConn = func(Config) ldapConn { return prepareMockOk() } - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -97,7 +98,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -132,9 +133,9 @@ func TestCollector_Check(t *testing.T) { collr.newConn = func(Config) ldapConn { return mock } if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -195,7 +196,7 @@ func TestCollector_Collect(t *testing.T) { mock := test.prepareMock() collr.newConn = func(Config) ldapConn { return mock } - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) @@ -204,7 +205,7 @@ func TestCollector_Collect(t *testing.T) { } assert.Equal(t, test.disconnectBeforeCleanup, mock.disconnectCalled, "disconnect before cleanup") - collr.Cleanup() + collr.Cleanup(context.Background()) assert.Equal(t, test.disconnectAfterCleanup, mock.disconnectCalled, "disconnect after cleanup") }) } diff --git a/src/go/plugin/go.d/collector/openvpn/collector.go b/src/go/plugin/go.d/collector/openvpn/collector.go index 37adf19be32b71..05e2edef02a547 100644 --- a/src/go/plugin/go.d/collector/openvpn/collector.go +++ b/src/go/plugin/go.d/collector/openvpn/collector.go @@ -3,6 +3,7 @@ package openvpn import ( + "context" _ "embed" "time" @@ -67,7 +68,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return err } @@ -85,7 +86,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(ctx context.Context) error { if err := c.client.Connect(); err != nil { return err } @@ -93,7 +94,7 @@ func (c *Collector) Check() error { ver, err := c.client.Version() if err != nil { - c.Cleanup() + c.Cleanup(ctx) return err } @@ -104,7 +105,7 @@ func (c *Collector) Check() error { func (c *Collector) Charts() *Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -116,7 +117,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.client == nil { return } diff --git a/src/go/plugin/go.d/collector/openvpn/collector_test.go b/src/go/plugin/go.d/collector/openvpn/collector_test.go index be3221e769f6c7..05e98701f8eafe 100644 --- a/src/go/plugin/go.d/collector/openvpn/collector_test.go +++ b/src/go/plugin/go.d/collector/openvpn/collector_test.go @@ -3,6 +3,7 @@ package openvpn import ( + "context" "os" "testing" @@ -34,15 +35,15 @@ func TestCollector_ConfigurationSerialize(t *testing.T) { } func TestCollector_Init(t *testing.T) { - assert.NoError(t, New().Init()) + assert.NoError(t, New().Init(context.Background())) } func TestCollector_Check(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.client = prepareMockOpenVPNClient() - require.NoError(t, collr.Check()) + require.NoError(t, collr.Check(context.Background())) } func TestCollector_Charts(t *testing.T) { @@ -52,20 +53,20 @@ func TestCollector_Charts(t *testing.T) { func TestCollector_Cleanup(t *testing.T) { collr := New() - assert.NotPanics(t, collr.Cleanup) - require.NoError(t, collr.Init()) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) + require.NoError(t, collr.Init(context.Background())) collr.client = prepareMockOpenVPNClient() - require.NoError(t, collr.Check()) - collr.Cleanup() + require.NoError(t, collr.Check(context.Background())) + collr.Cleanup(context.Background()) } func TestCollector_Collect(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.perUserMatcher = matcher.TRUE() collr.client = prepareMockOpenVPNClient() - require.NoError(t, collr.Check()) + require.NoError(t, collr.Check(context.Background())) expected := map[string]int64{ "bytes_in": 1, @@ -75,7 +76,7 @@ func TestCollector_Collect(t *testing.T) { "name_bytes_sent": 2, } - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.NotNil(t, mx) delete(mx, "name_connection_time") assert.Equal(t, expected, mx) @@ -84,12 +85,12 @@ func TestCollector_Collect(t *testing.T) { func TestCollector_Collect_UNDEFUsername(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.perUserMatcher = matcher.TRUE() cl := prepareMockOpenVPNClient() cl.users = testUsersUNDEF collr.client = cl - require.NoError(t, collr.Check()) + require.NoError(t, collr.Check(context.Background())) expected := map[string]int64{ "bytes_in": 1, @@ -99,7 +100,7 @@ func TestCollector_Collect_UNDEFUsername(t *testing.T) { "common_name_bytes_sent": 2, } - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.NotNil(t, mx) delete(mx, "common_name_connection_time") diff --git a/src/go/plugin/go.d/collector/openvpn_status_log/collector.go b/src/go/plugin/go.d/collector/openvpn_status_log/collector.go index 8ccaec905bceb3..f63ed5543fba83 100644 --- a/src/go/plugin/go.d/collector/openvpn_status_log/collector.go +++ b/src/go/plugin/go.d/collector/openvpn_status_log/collector.go @@ -3,6 +3,7 @@ package openvpn_status_log import ( + "context" _ "embed" "errors" "fmt" @@ -52,7 +53,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("error on validating config: %v", err) } @@ -68,7 +69,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -83,7 +84,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -95,4 +96,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/openvpn_status_log/collector_test.go b/src/go/plugin/go.d/collector/openvpn_status_log/collector_test.go index dff674b779a748..35eff95bbe7743 100644 --- a/src/go/plugin/go.d/collector/openvpn_status_log/collector_test.go +++ b/src/go/plugin/go.d/collector/openvpn_status_log/collector_test.go @@ -3,6 +3,7 @@ package openvpn_status_log import ( + "context" "os" "strings" "testing" @@ -66,9 +67,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -93,12 +94,12 @@ func TestCollector_Check(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -131,9 +132,9 @@ func TestCollector_Charts(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - require.NoError(t, collr.Init()) - _ = collr.Check() - _ = collr.Collect() + require.NoError(t, collr.Init(context.Background())) + _ = collr.Check(context.Background()) + _ = collr.Collect(context.Background()) assert.Equal(t, test.wantNumCharts, len(*collr.Charts())) }) @@ -257,10 +258,10 @@ func TestCollector_Collect(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - require.NoError(t, collr.Init()) - _ = collr.Check() + require.NoError(t, collr.Init(context.Background())) + _ = collr.Check(context.Background()) - collected := collr.Collect() + collected := collr.Collect(context.Background()) copyConnTime(collected, test.expected) assert.Equal(t, test.expected, collected) diff --git a/src/go/plugin/go.d/collector/oracledb/collector.go b/src/go/plugin/go.d/collector/oracledb/collector.go index 3b8470ba82b20f..81401bb819ba12 100644 --- a/src/go/plugin/go.d/collector/oracledb/collector.go +++ b/src/go/plugin/go.d/collector/oracledb/collector.go @@ -3,6 +3,7 @@ package oracledb import ( + "context" "database/sql" _ "embed" "errors" @@ -59,7 +60,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { dsn, err := c.validateDSN() if err != nil { return fmt.Errorf("invalid oracle DSN: %w", err) @@ -70,7 +71,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return fmt.Errorf("failed to collect metrics [%s]: %w", c.publicDSN, err) @@ -86,7 +87,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(fmt.Sprintf("failed to collect metrics [%s]: %s", c.publicDSN, err)) @@ -99,7 +100,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.db != nil { if err := c.db.Close(); err != nil { c.Errorf("cleanup: error on closing connection [%s]: %v", c.publicDSN, err) diff --git a/src/go/plugin/go.d/collector/oracledb/collector_test.go b/src/go/plugin/go.d/collector/oracledb/collector_test.go index 3b3723034fc709..a8814a895b0adb 100644 --- a/src/go/plugin/go.d/collector/oracledb/collector_test.go +++ b/src/go/plugin/go.d/collector/oracledb/collector_test.go @@ -5,6 +5,7 @@ package oracledb import ( "bufio" "bytes" + "context" "database/sql/driver" "errors" "fmt" @@ -71,9 +72,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -102,7 +103,7 @@ func TestCollector_Cleanup(t *testing.T) { collr, cleanup := prepare(t) defer cleanup() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) assert.Nil(t, collr.db) }) } @@ -142,14 +143,14 @@ func TestCollector_Check(t *testing.T) { collr.db = db defer func() { _ = db.Close() }() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) test.prepareMock(t, mock) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } assert.NoError(t, mock.ExpectationsWereMet()) }) @@ -243,11 +244,11 @@ func TestCollector_Collect(t *testing.T) { collr.db = db defer func() { _ = db.Close() }() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) test.prepareMock(t, mock) - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) if len(test.wantMetrics) > 0 { diff --git a/src/go/plugin/go.d/collector/pgbouncer/collector.go b/src/go/plugin/go.d/collector/pgbouncer/collector.go index 408641b61d27fc..6bf25aeb985ec6 100644 --- a/src/go/plugin/go.d/collector/pgbouncer/collector.go +++ b/src/go/plugin/go.d/collector/pgbouncer/collector.go @@ -3,6 +3,7 @@ package pgbouncer import ( + "context" "database/sql" _ "embed" "errors" @@ -67,7 +68,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { err := c.validateConfig() if err != nil { return fmt.Errorf("config validation: %v", err) @@ -76,7 +77,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -91,7 +92,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -103,7 +104,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.db == nil { return } diff --git a/src/go/plugin/go.d/collector/pgbouncer/collector_test.go b/src/go/plugin/go.d/collector/pgbouncer/collector_test.go index e216e35efd3e68..43fe4c93f2eb38 100644 --- a/src/go/plugin/go.d/collector/pgbouncer/collector_test.go +++ b/src/go/plugin/go.d/collector/pgbouncer/collector_test.go @@ -5,6 +5,7 @@ package pgbouncer import ( "bufio" "bytes" + "context" "database/sql/driver" "errors" "fmt" @@ -71,9 +72,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -129,14 +130,14 @@ func TestCollector_Check(t *testing.T) { collr.db = db defer func() { _ = db.Close() }() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) test.prepareMock(t, mock) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } assert.NoError(t, mock.ExpectationsWereMet()) }) @@ -159,7 +160,7 @@ func TestCollector_Collect(t *testing.T) { mockExpect(t, m, queryShowPools, dataVer1170Pools) }, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "cl_conns_utilization": 47, @@ -251,7 +252,7 @@ func TestCollector_Collect(t *testing.T) { mockExpectErr(m, queryShowVersion) }, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) var expected map[string]int64 assert.Equal(t, expected, mx) }, @@ -263,7 +264,7 @@ func TestCollector_Collect(t *testing.T) { mockExpect(t, m, queryShowVersion, dataVer170Version) }, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) var expected map[string]int64 assert.Equal(t, expected, mx) }, @@ -276,7 +277,7 @@ func TestCollector_Collect(t *testing.T) { mockExpectErr(m, queryShowConfig) }, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) var expected map[string]int64 assert.Equal(t, expected, mx) }, @@ -294,7 +295,7 @@ func TestCollector_Collect(t *testing.T) { collr.db = db defer func() { _ = db.Close() }() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) for i, step := range test { t.Run(fmt.Sprintf("step[%d]", i), func(t *testing.T) { diff --git a/src/go/plugin/go.d/collector/phpdaemon/collector.go b/src/go/plugin/go.d/collector/phpdaemon/collector.go index 49655d321d5e29..66ee885b05fb38 100644 --- a/src/go/plugin/go.d/collector/phpdaemon/collector.go +++ b/src/go/plugin/go.d/collector/phpdaemon/collector.go @@ -3,6 +3,7 @@ package phpdaemon import ( + "context" _ "embed" "errors" "fmt" @@ -61,7 +62,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.URL == "" { return errors.New("phpDaemon URL is required but not set") } @@ -78,7 +79,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -95,7 +96,7 @@ func (c *Collector) Charts() *Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -105,7 +106,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/phpdaemon/collector_test.go b/src/go/plugin/go.d/collector/phpdaemon/collector_test.go index 62bd847ba8f9a3..febf19dbbad092 100644 --- a/src/go/plugin/go.d/collector/phpdaemon/collector_test.go +++ b/src/go/plugin/go.d/collector/phpdaemon/collector_test.go @@ -3,6 +3,7 @@ package phpdaemon import ( + "context" "net/http" "net/http/httptest" "os" @@ -38,7 +39,7 @@ func TestCollector_ConfigurationSerialize(t *testing.T) { func TestCollector_Init(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) } func TestCollector_Check(t *testing.T) { @@ -51,15 +52,15 @@ func TestCollector_Check(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) - assert.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.NoError(t, collr.Check(context.Background())) } func TestCollector_CheckNG(t *testing.T) { collr := New() collr.URL = "http://127.0.0.1:38001" - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_Charts(t *testing.T) { @@ -76,13 +77,13 @@ func TestCollector_Charts(t *testing.T) { defer ts.Close() collr.URL = ts.URL - require.NoError(t, collr.Init()) - assert.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.NoError(t, collr.Check(context.Background())) assert.True(t, collr.charts.Has(uptimeChart.ID)) } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Collect(t *testing.T) { @@ -95,8 +96,8 @@ func TestCollector_Collect(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) - assert.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.NoError(t, collr.Check(context.Background())) expected := map[string]int64{ "alive": 350, @@ -110,7 +111,7 @@ func TestCollector_Collect(t *testing.T) { "uptime": 15765, } - assert.Equal(t, expected, collr.Collect()) + assert.Equal(t, expected, collr.Collect(context.Background())) } @@ -124,8 +125,8 @@ func TestCollector_InvalidData(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_404(t *testing.T) { @@ -138,6 +139,6 @@ func TestCollector_404(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } diff --git a/src/go/plugin/go.d/collector/phpfpm/collector.go b/src/go/plugin/go.d/collector/phpfpm/collector.go index b9aefc7b982f95..acf04b109ac543 100644 --- a/src/go/plugin/go.d/collector/phpfpm/collector.go +++ b/src/go/plugin/go.d/collector/phpfpm/collector.go @@ -3,6 +3,7 @@ package phpfpm import ( + "context" _ "embed" "errors" "fmt" @@ -59,7 +60,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { cli, err := c.initClient() if err != nil { return fmt.Errorf("init client: %v", err) @@ -69,7 +70,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -84,7 +85,7 @@ func (c *Collector) Charts() *Charts { return charts.Copy() } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -96,4 +97,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/phpfpm/collector_test.go b/src/go/plugin/go.d/collector/phpfpm/collector_test.go index 1700ed12b8c3e0..533d9cf46d83ce 100644 --- a/src/go/plugin/go.d/collector/phpfpm/collector_test.go +++ b/src/go/plugin/go.d/collector/phpfpm/collector_test.go @@ -3,6 +3,7 @@ package phpfpm import ( + "context" "net/http" "net/http/httptest" "os" @@ -46,7 +47,7 @@ func TestCollector_ConfigurationSerialize(t *testing.T) { func TestCollector_Init(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.client) } @@ -60,17 +61,17 @@ func TestCollector_Check(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } func TestCollector_CheckReturnsFalseOnFailure(t *testing.T) { collr := New() collr.URL = "http://127.0.0.1:38001/us" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_Charts(t *testing.T) { @@ -89,9 +90,9 @@ func TestCollector_CollectJSON(t *testing.T) { collr := New() collr.URL = ts.URL + "/?json" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - got := collr.Collect() + got := collr.Collect(context.Background()) want := map[string]int64{ "active": 1, @@ -114,9 +115,9 @@ func TestCollector_CollectJSONFull(t *testing.T) { collr := New() collr.URL = ts.URL + "/?json" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - got := collr.Collect() + got := collr.Collect(context.Background()) want := map[string]int64{ "active": 1, @@ -148,9 +149,9 @@ func TestCollector_CollectNoIdleProcessesJSONFull(t *testing.T) { collr := New() collr.URL = ts.URL + "/?json" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - got := collr.Collect() + got := collr.Collect(context.Background()) want := map[string]int64{ "active": 1, @@ -173,9 +174,9 @@ func TestCollector_CollectText(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - got := collr.Collect() + got := collr.Collect(context.Background()) want := map[string]int64{ "active": 1, @@ -198,9 +199,9 @@ func TestCollector_CollectTextFull(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - got := collr.Collect() + got := collr.Collect(context.Background()) want := map[string]int64{ "active": 1, @@ -232,9 +233,9 @@ func TestCollector_CollectReturnsNothingWhenInvalidData(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.Len(t, collr.Collect(), 0) + assert.Len(t, collr.Collect(context.Background()), 0) } func TestCollector_CollectReturnsNothingWhenEmptyData(t *testing.T) { @@ -247,9 +248,9 @@ func TestCollector_CollectReturnsNothingWhenEmptyData(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.Len(t, collr.Collect(), 0) + assert.Len(t, collr.Collect(context.Background()), 0) } func TestCollector_CollectReturnsNothingWhenBadStatusCode(t *testing.T) { @@ -262,11 +263,11 @@ func TestCollector_CollectReturnsNothingWhenBadStatusCode(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.Len(t, collr.Collect(), 0) + assert.Len(t, collr.Collect(context.Background()), 0) } func TestCollector_Cleanup(t *testing.T) { - New().Cleanup() + New().Cleanup(context.Background()) } diff --git a/src/go/plugin/go.d/collector/pihole/collector.go b/src/go/plugin/go.d/collector/pihole/collector.go index 182a0b068e9c92..20ac7918665588 100644 --- a/src/go/plugin/go.d/collector/pihole/collector.go +++ b/src/go/plugin/go.d/collector/pihole/collector.go @@ -3,6 +3,7 @@ package pihole import ( + "context" _ "embed" "errors" "fmt" @@ -72,7 +73,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -93,7 +94,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -108,7 +109,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -121,7 +122,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/pihole/collector_test.go b/src/go/plugin/go.d/collector/pihole/collector_test.go index e8a72b37ea5040..904d6cf62a578e 100644 --- a/src/go/plugin/go.d/collector/pihole/collector_test.go +++ b/src/go/plugin/go.d/collector/pihole/collector_test.go @@ -3,6 +3,7 @@ package pihole import ( + "context" "fmt" "net/http" "net/http/httptest" @@ -73,9 +74,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -106,9 +107,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -168,7 +169,7 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare(t) defer cleanup() - mx := collr.Collect() + mx := collr.Collect(context.Background()) copyBlockListLastUpdate(mx, test.wantMetrics) require.Equal(t, test.wantMetrics, mx) @@ -185,7 +186,7 @@ func caseSuccessWithWebPassword(t *testing.T) (*Collector, func()) { collr.SetupVarsPath = pathSetupVarsOK collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -196,7 +197,7 @@ func caseFailNoWebPassword(t *testing.T) (*Collector, func()) { collr.SetupVarsPath = pathSetupVarsWrong collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -207,7 +208,7 @@ func caseFailUnsupportedVersion(t *testing.T) (*Collector, func()) { collr.SetupVarsPath = pathSetupVarsOK collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } diff --git a/src/go/plugin/go.d/collector/pika/collector.go b/src/go/plugin/go.d/collector/pika/collector.go index 5bdb2e14a76cf9..9a2732941404e7 100644 --- a/src/go/plugin/go.d/collector/pika/collector.go +++ b/src/go/plugin/go.d/collector/pika/collector.go @@ -71,7 +71,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { err := c.validateConfig() if err != nil { return fmt.Errorf("config validation: %v", err) @@ -92,7 +92,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -107,7 +107,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { ms, err := c.collect() if err != nil { c.Error(err) @@ -119,7 +119,7 @@ func (c *Collector) Collect() map[string]int64 { return ms } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.pdb == nil { return } diff --git a/src/go/plugin/go.d/collector/pika/collector_test.go b/src/go/plugin/go.d/collector/pika/collector_test.go index c2869c099378b1..8430415c076e47 100644 --- a/src/go/plugin/go.d/collector/pika/collector_test.go +++ b/src/go/plugin/go.d/collector/pika/collector_test.go @@ -70,9 +70,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -101,9 +101,9 @@ func TestCollector_Check(t *testing.T) { collr := test.prepare(t) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -111,20 +111,20 @@ func TestCollector_Check(t *testing.T) { func TestCollector_Charts(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.Charts()) } func TestCollector_Cleanup(t *testing.T) { collr := New() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) m := &mockRedisClient{} collr.pdb = m - collr.Cleanup() + collr.Cleanup(context.Background()) assert.True(t, m.calledClose) } @@ -187,7 +187,7 @@ func TestCollector_Collect(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare(t) - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantCollected, mx) if len(test.wantCollected) > 0 { @@ -201,7 +201,7 @@ func TestCollector_Collect(t *testing.T) { func preparePikaV340(t *testing.T) *Collector { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.pdb = &mockRedisClient{ result: dataVer340InfoAll, } @@ -210,7 +210,7 @@ func preparePikaV340(t *testing.T) *Collector { func preparePikaErrorOnInfo(t *testing.T) *Collector { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.pdb = &mockRedisClient{ errOnInfo: true, } @@ -219,7 +219,7 @@ func preparePikaErrorOnInfo(t *testing.T) *Collector { func preparePikaWithRedisMetrics(t *testing.T) *Collector { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.pdb = &mockRedisClient{ result: dataRedisInfoAll, } diff --git a/src/go/plugin/go.d/collector/ping/collector.go b/src/go/plugin/go.d/collector/ping/collector.go index 38336346d6eb3a..d8c4e17cbf207d 100644 --- a/src/go/plugin/go.d/collector/ping/collector.go +++ b/src/go/plugin/go.d/collector/ping/collector.go @@ -3,6 +3,7 @@ package ping import ( + "context" _ "embed" "errors" "fmt" @@ -68,7 +69,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { err := c.validateConfig() if err != nil { return fmt.Errorf("config validation: %v", err) @@ -83,7 +84,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -99,7 +100,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -111,4 +112,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/ping/collector_test.go b/src/go/plugin/go.d/collector/ping/collector_test.go index fb33e047d04896..3e23563b633804 100644 --- a/src/go/plugin/go.d/collector/ping/collector_test.go +++ b/src/go/plugin/go.d/collector/ping/collector_test.go @@ -3,6 +3,7 @@ package ping import ( + "context" "errors" "os" "testing" @@ -59,9 +60,9 @@ func TestCollector_Init(t *testing.T) { collr.UpdateEvery = 1 if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -72,7 +73,7 @@ func TestCollector_Charts(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Check(t *testing.T) { @@ -95,9 +96,9 @@ func TestCollector_Check(t *testing.T) { collr := test.prepare(t) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -147,7 +148,7 @@ func TestCollector_Collect(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare(t) - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) @@ -165,7 +166,7 @@ func casePingSuccess(t *testing.T) *Collector { collr.newProber = func(_ pingProberConfig, _ *logger.Logger) prober { return &mockProber{} } - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr } @@ -176,7 +177,7 @@ func casePingError(t *testing.T) *Collector { collr.newProber = func(_ pingProberConfig, _ *logger.Logger) prober { return &mockProber{errOnPing: true} } - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr } diff --git a/src/go/plugin/go.d/collector/portcheck/collector.go b/src/go/plugin/go.d/collector/portcheck/collector.go index 4f955f569747db..e1c464bba518f8 100644 --- a/src/go/plugin/go.d/collector/portcheck/collector.go +++ b/src/go/plugin/go.d/collector/portcheck/collector.go @@ -3,6 +3,7 @@ package portcheck import ( + "context" _ "embed" "errors" "fmt" @@ -73,7 +74,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -87,7 +88,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -104,7 +105,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -117,4 +118,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/portcheck/collector_test.go b/src/go/plugin/go.d/collector/portcheck/collector_test.go index b84032f421bb4a..a4fa12961c167d 100644 --- a/src/go/plugin/go.d/collector/portcheck/collector_test.go +++ b/src/go/plugin/go.d/collector/portcheck/collector_test.go @@ -3,6 +3,7 @@ package portcheck import ( + "context" "errors" "net" "os" @@ -39,32 +40,32 @@ func TestCollector_Init(t *testing.T) { collr.Host = "127.0.0.1" collr.Ports = []int{39001, 39002} - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) assert.Len(t, collr.tcpPorts, 2) } func TestCollector_InitNG(t *testing.T) { collr := New() - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) collr.Host = "127.0.0.1" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) collr.Ports = []int{39001, 39002} - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } func TestCollector_Check(t *testing.T) { - assert.Error(t, New().Check()) + assert.Error(t, New().Check(context.Background())) } func TestCollector_Cleanup(t *testing.T) { - New().Cleanup() + New().Cleanup(context.Background()) } func TestCollector_Charts(t *testing.T) { collr := New() collr.Ports = []int{1, 2} collr.Host = "localhost" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) } func TestCollector_Collect(t *testing.T) { @@ -74,8 +75,8 @@ func TestCollector_Collect(t *testing.T) { collr.Ports = []int{39001, 39002} collr.UpdateEvery = 5 collr.dialTCP = testDial(nil) - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) copyLatencyDuration := func(dst, src map[string]int64) { for k := range dst { @@ -97,7 +98,7 @@ func TestCollector_Collect(t *testing.T) { "tcp_port_39002_success": 1, "tcp_port_39002_timeout": 0, } - mx := collr.Collect() + mx := collr.Collect(context.Background()) copyLatencyDuration(expected, mx) assert.Equal(t, expected, mx) @@ -114,7 +115,7 @@ func TestCollector_Collect(t *testing.T) { "tcp_port_39002_success": 1, "tcp_port_39002_timeout": 0, } - mx = collr.Collect() + mx = collr.Collect(context.Background()) copyLatencyDuration(expected, mx) assert.Equal(t, expected, mx) @@ -133,7 +134,7 @@ func TestCollector_Collect(t *testing.T) { "tcp_port_39002_success": 0, "tcp_port_39002_timeout": 0, } - mx = collr.Collect() + mx = collr.Collect(context.Background()) copyLatencyDuration(expected, mx) assert.Equal(t, expected, mx) @@ -152,7 +153,7 @@ func TestCollector_Collect(t *testing.T) { "tcp_port_39002_timeout": 1, "tcp_port_39002_failed": 0, } - mx = collr.Collect() + mx = collr.Collect(context.Background()) copyLatencyDuration(expected, mx) assert.Equal(t, expected, mx) diff --git a/src/go/plugin/go.d/collector/postfix/collector.go b/src/go/plugin/go.d/collector/postfix/collector.go index fd0ca1e5f741b1..f5cac1ed86d795 100644 --- a/src/go/plugin/go.d/collector/postfix/collector.go +++ b/src/go/plugin/go.d/collector/postfix/collector.go @@ -3,6 +3,7 @@ package postfix import ( + "context" _ "embed" "errors" "fmt" @@ -55,7 +56,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %s", err) } @@ -69,7 +70,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -86,7 +87,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -99,4 +100,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/postfix/collector_test.go b/src/go/plugin/go.d/collector/postfix/collector_test.go index 90b76a8d81968e..2789517b463336 100644 --- a/src/go/plugin/go.d/collector/postfix/collector_test.go +++ b/src/go/plugin/go.d/collector/postfix/collector_test.go @@ -3,6 +3,7 @@ package postfix import ( + "context" "errors" "os" "testing" @@ -59,9 +60,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -80,7 +81,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { pf := New() pf.exec = prepareMockOK() - _ = pf.Check() + _ = pf.Check(context.Background()) return pf }, }, @@ -88,7 +89,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { pf := New() pf.exec = prepareMockOK() - _ = pf.Collect() + _ = pf.Collect(context.Background()) return pf }, }, @@ -98,7 +99,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -141,9 +142,9 @@ func TestCollector_Check(t *testing.T) { collr.exec = mock if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -188,7 +189,7 @@ func TestCollector_Collect(t *testing.T) { mock := test.prepareMock() collr.exec = mock - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantMetrics, mx) }) diff --git a/src/go/plugin/go.d/collector/postgres/collector.go b/src/go/plugin/go.d/collector/postgres/collector.go index b6c1e533c316f7..2f3e8c3fbb5b34 100644 --- a/src/go/plugin/go.d/collector/postgres/collector.go +++ b/src/go/plugin/go.d/collector/postgres/collector.go @@ -3,6 +3,7 @@ package postgres import ( + "context" "database/sql" _ "embed" "errors" @@ -103,7 +104,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { err := c.validateConfig() if err != nil { return fmt.Errorf("config validation: %v", err) @@ -121,7 +122,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -136,7 +137,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -148,7 +149,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.db == nil { return } diff --git a/src/go/plugin/go.d/collector/postgres/collector_test.go b/src/go/plugin/go.d/collector/postgres/collector_test.go index 1da891f9ebc7c8..a30ae869ccdb45 100644 --- a/src/go/plugin/go.d/collector/postgres/collector_test.go +++ b/src/go/plugin/go.d/collector/postgres/collector_test.go @@ -5,6 +5,7 @@ package postgres import ( "bufio" "bytes" + "context" "database/sql/driver" "errors" "fmt" @@ -120,9 +121,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -226,14 +227,14 @@ func TestCollector_Check(t *testing.T) { collr.db = db defer func() { _ = db.Close() }() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) test.prepareMock(t, collr, mock) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } assert.NoError(t, mock.ExpectationsWereMet()) }) @@ -287,7 +288,7 @@ func TestCollector_Collect(t *testing.T) { mockExpect(t, m, queryColumnsStats(), dataVer140004ColumnsStats) }, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "autovacuum_analyze": 0, @@ -609,7 +610,7 @@ func TestCollector_Collect(t *testing.T) { mockExpectErr(m, queryServerVersion()) }, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) var expected map[string]int64 assert.Equal(t, expected, mx) }, @@ -625,7 +626,7 @@ func TestCollector_Collect(t *testing.T) { mockExpectErr(m, querySettingsMaxConnections()) }, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) var expected map[string]int64 assert.Equal(t, expected, mx) }, @@ -644,7 +645,7 @@ func TestCollector_Collect(t *testing.T) { mockExpectErr(m, queryServerCurrentConnectionsUsed()) }, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) var expected map[string]int64 assert.Equal(t, expected, mx) }, @@ -663,7 +664,7 @@ func TestCollector_Collect(t *testing.T) { collr.db = db defer func() { _ = db.Close() }() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) for i, step := range test { t.Run(fmt.Sprintf("step[%d]", i), func(t *testing.T) { diff --git a/src/go/plugin/go.d/collector/powerdns/collector.go b/src/go/plugin/go.d/collector/powerdns/collector.go index 5499af5fd82e66..390c85eadf0939 100644 --- a/src/go/plugin/go.d/collector/powerdns/collector.go +++ b/src/go/plugin/go.d/collector/powerdns/collector.go @@ -3,6 +3,7 @@ package powerdns import ( + "context" _ "embed" "errors" "fmt" @@ -58,7 +59,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { err := c.validateConfig() if err != nil { return fmt.Errorf("config validation: %v", err) @@ -79,7 +80,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -94,7 +95,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { ms, err := c.collect() if err != nil { c.Error(err) @@ -106,7 +107,7 @@ func (c *Collector) Collect() map[string]int64 { return ms } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient == nil { return } diff --git a/src/go/plugin/go.d/collector/powerdns/collector_test.go b/src/go/plugin/go.d/collector/powerdns/collector_test.go index ce933b0f243434..e2f92e228896a9 100644 --- a/src/go/plugin/go.d/collector/powerdns/collector_test.go +++ b/src/go/plugin/go.d/collector/powerdns/collector_test.go @@ -3,6 +3,7 @@ package powerdns import ( + "context" "net/http" "net/http/httptest" "os" @@ -76,9 +77,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -114,12 +115,12 @@ func TestCollector_Check(t *testing.T) { t.Run(name, func(t *testing.T) { collr, cleanup := test.prepare() defer cleanup() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -127,12 +128,12 @@ func TestCollector_Check(t *testing.T) { func TestCollector_Charts(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.Charts()) } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Collect(t *testing.T) { @@ -242,9 +243,9 @@ func TestCollector_Collect(t *testing.T) { t.Run(name, func(t *testing.T) { collr, cleanup := test.prepare() defer cleanup() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantCollected, mx) if len(test.wantCollected) > 0 { diff --git a/src/go/plugin/go.d/collector/powerdns_recursor/collector.go b/src/go/plugin/go.d/collector/powerdns_recursor/collector.go index 076bcf12a963e9..d17221bb3943c0 100644 --- a/src/go/plugin/go.d/collector/powerdns_recursor/collector.go +++ b/src/go/plugin/go.d/collector/powerdns_recursor/collector.go @@ -3,6 +3,7 @@ package powerdns_recursor import ( + "context" _ "embed" "errors" "fmt" @@ -58,7 +59,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { err := c.validateConfig() if err != nil { return fmt.Errorf("config validation: %v", err) @@ -79,7 +80,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -94,7 +95,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { ms, err := c.collect() if err != nil { c.Error(err) @@ -106,7 +107,7 @@ func (c *Collector) Collect() map[string]int64 { return ms } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient == nil { return } diff --git a/src/go/plugin/go.d/collector/powerdns_recursor/collector_test.go b/src/go/plugin/go.d/collector/powerdns_recursor/collector_test.go index 9513d93a180cc2..5f2d3565b838dc 100644 --- a/src/go/plugin/go.d/collector/powerdns_recursor/collector_test.go +++ b/src/go/plugin/go.d/collector/powerdns_recursor/collector_test.go @@ -3,6 +3,7 @@ package powerdns_recursor import ( + "context" "net/http" "net/http/httptest" "os" @@ -76,9 +77,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -114,12 +115,12 @@ func TestCollector_Check(t *testing.T) { t.Run(name, func(t *testing.T) { collr, cleanup := test.prepare() defer cleanup() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -127,12 +128,12 @@ func TestCollector_Check(t *testing.T) { func TestCollector_Charts(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.Charts()) } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Collect(t *testing.T) { @@ -277,9 +278,9 @@ func TestCollector_Collect(t *testing.T) { t.Run(name, func(t *testing.T) { collr, cleanup := test.prepare() defer cleanup() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantCollected, mx) if len(test.wantCollected) > 0 { diff --git a/src/go/plugin/go.d/collector/prometheus/collector.go b/src/go/plugin/go.d/collector/prometheus/collector.go index 157c35c60bc515..b3aee119a046d9 100644 --- a/src/go/plugin/go.d/collector/prometheus/collector.go +++ b/src/go/plugin/go.d/collector/prometheus/collector.go @@ -3,6 +3,7 @@ package prometheus import ( + "context" _ "embed" "errors" "fmt" @@ -82,7 +83,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("validating config: %v", err) } @@ -108,7 +109,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -123,7 +124,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -135,7 +136,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.prom != nil && c.prom.HTTPClient() != nil { c.prom.HTTPClient().CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/prometheus/collector_test.go b/src/go/plugin/go.d/collector/prometheus/collector_test.go index 5541b3fe8ef3e1..626932ed6343b4 100644 --- a/src/go/plugin/go.d/collector/prometheus/collector_test.go +++ b/src/go/plugin/go.d/collector/prometheus/collector_test.go @@ -3,6 +3,7 @@ package prometheus import ( + "context" "fmt" "net/http" "net/http/httptest" @@ -63,21 +64,21 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) collr := New() collr.URL = "http://127.0.0.1" - require.NoError(t, collr.Init()) - assert.NotPanics(t, collr.Cleanup) + require.NoError(t, collr.Init(context.Background())) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) } func TestCollector_Check(t *testing.T) { @@ -188,12 +189,12 @@ test_counter_no_meta_metric_1_total{label1="value2"} 11 collr, cleanup := test.prepare() defer cleanup() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -577,7 +578,7 @@ test_gauge_no_meta_metric_1{label1="value2"} 12 defer srv.Close() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) for num, step := range test.steps { 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 var mx map[string]int64 for i := 0; i < maxNotSeenTimes+1; i++ { - mx = collr.Collect() + mx = collr.Collect(context.Background()) } assert.Equal(t, step.wantCollected, mx) diff --git a/src/go/plugin/go.d/collector/proxysql/collector.go b/src/go/plugin/go.d/collector/proxysql/collector.go index 0d6a6f10fdf257..218d18a93650ab 100644 --- a/src/go/plugin/go.d/collector/proxysql/collector.go +++ b/src/go/plugin/go.d/collector/proxysql/collector.go @@ -3,6 +3,7 @@ package proxysql import ( + "context" "database/sql" _ "embed" "errors" @@ -65,7 +66,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.DSN == "" { return errors.New("dsn not set") } @@ -75,7 +76,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -90,7 +91,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -102,7 +103,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.db == nil { return } diff --git a/src/go/plugin/go.d/collector/proxysql/collector_test.go b/src/go/plugin/go.d/collector/proxysql/collector_test.go index 21006a0a6bb7c6..edbf7a5a39335e 100644 --- a/src/go/plugin/go.d/collector/proxysql/collector_test.go +++ b/src/go/plugin/go.d/collector/proxysql/collector_test.go @@ -5,6 +5,7 @@ package proxysql import ( "bufio" "bytes" + "context" "database/sql/driver" "errors" "fmt" @@ -73,9 +74,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -104,7 +105,7 @@ func TestCollector_Cleanup(t *testing.T) { collr, cleanup := prepare(t) defer cleanup() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) assert.Nil(t, collr.db) }) } @@ -176,14 +177,14 @@ func TestCollector_Check(t *testing.T) { collr.db = db defer func() { _ = db.Close() }() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) test.prepareMock(t, mock) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } assert.NoError(t, mock.ExpectationsWereMet()) }) @@ -208,7 +209,7 @@ func TestCollector_Collect(t *testing.T) { mockExpect(t, m, queryStatsMySQLConnectionPool, dataVer2010StatsMySQLConnectionPool) }, check: func(t *testing.T, my *Collector) { - mx := my.Collect() + mx := my.Collect(context.Background()) expected := map[string]int64{ "Access_Denied_Max_Connections": 0, @@ -1163,7 +1164,7 @@ func TestCollector_Collect(t *testing.T) { my.db = db defer func() { _ = db.Close() }() - require.NoError(t, my.Init()) + require.NoError(t, my.Init(context.Background())) for i, step := range test { t.Run(fmt.Sprintf("step[%d]", i), func(t *testing.T) { diff --git a/src/go/plugin/go.d/collector/pulsar/collector.go b/src/go/plugin/go.d/collector/pulsar/collector.go index a43b709ba17fe3..b33044ef2564e4 100644 --- a/src/go/plugin/go.d/collector/pulsar/collector.go +++ b/src/go/plugin/go.d/collector/pulsar/collector.go @@ -3,6 +3,7 @@ package pulsar import ( + "context" _ "embed" "errors" "fmt" @@ -81,7 +82,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -101,7 +102,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -116,7 +117,7 @@ func (c *Collector) Charts() *Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -128,7 +129,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.prom != nil && c.prom.HTTPClient() != nil { c.prom.HTTPClient().CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/pulsar/collector_test.go b/src/go/plugin/go.d/collector/pulsar/collector_test.go index ab188c12e70722..98acd151c30a3a 100644 --- a/src/go/plugin/go.d/collector/pulsar/collector_test.go +++ b/src/go/plugin/go.d/collector/pulsar/collector_test.go @@ -3,6 +3,7 @@ package pulsar import ( + "context" "net/http" "net/http/httptest" "os" @@ -80,16 +81,16 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Check(t *testing.T) { @@ -111,9 +112,9 @@ func TestCollector_Check(t *testing.T) { defer srv.Close() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -139,7 +140,7 @@ func TestCollector_Collect_ReturnsNilOnErrors(t *testing.T) { collr, srv := test.prepare(t) defer srv.Close() - assert.Nil(t, collr.Collect()) + assert.Nil(t, collr.Collect(context.Background())) }) } } @@ -169,9 +170,9 @@ func TestCollector_Collect(t *testing.T) { defer srv.Close() for i := 0; i < 10; i++ { - _ = collr.Collect() + _ = collr.Collect(context.Background()) } - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.NotNil(t, mx) require.Equal(t, test.expected, mx) @@ -186,7 +187,7 @@ func TestCollector_Collect_RemoveAddNamespacesTopicsInRuntime(t *testing.T) { oldNsCharts := Charts{} - require.NotNil(t, collr.Collect()) + require.NotNil(t, collr.Collect(context.Background())) oldLength := len(*collr.Charts()) for _, chart := range *collr.Charts() { @@ -197,7 +198,7 @@ func TestCollector_Collect_RemoveAddNamespacesTopicsInRuntime(t *testing.T) { } } - require.NotNil(t, collr.Collect()) + require.NotNil(t, collr.Collect(context.Background())) l := oldLength + len(*collr.nsCharts)*2 // 2 new namespaces 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 collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv } @@ -235,7 +236,7 @@ func prepareClientServerStdV250Topics(t *testing.T) (*Collector, *httptest.Serve collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv } @@ -263,7 +264,7 @@ func prepareClientServersDynamicStdV250Topics(t *testing.T) (*Collector, *httpte collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv } @@ -277,7 +278,7 @@ func prepareClientServerNonPulsar(t *testing.T) (*Collector, *httptest.Server) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv } @@ -291,7 +292,7 @@ func prepareClientServerInvalidData(t *testing.T) (*Collector, *httptest.Server) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv } @@ -305,7 +306,7 @@ func prepareClientServer404(t *testing.T) (*Collector, *httptest.Server) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv } @@ -316,7 +317,7 @@ func prepareClientServerConnectionRefused(t *testing.T) (*Collector, *httptest.S collr := New() collr.URL = "http://127.0.0.1:38001/metrics" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv } diff --git a/src/go/plugin/go.d/collector/puppet/collector.go b/src/go/plugin/go.d/collector/puppet/collector.go index c0904541345cf9..efebec2bdbc208 100644 --- a/src/go/plugin/go.d/collector/puppet/collector.go +++ b/src/go/plugin/go.d/collector/puppet/collector.go @@ -3,6 +3,7 @@ package puppet import ( + "context" _ "embed" "errors" "fmt" @@ -59,7 +60,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.URL == "" { return errors.New("url not set") } @@ -76,7 +77,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -93,7 +94,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -106,7 +107,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/puppet/collector_test.go b/src/go/plugin/go.d/collector/puppet/collector_test.go index bfd7d38436124f..809fcd56f4d64d 100644 --- a/src/go/plugin/go.d/collector/puppet/collector_test.go +++ b/src/go/plugin/go.d/collector/puppet/collector_test.go @@ -3,6 +3,7 @@ package puppet import ( + "context" "net/http" "net/http/httptest" "os" @@ -61,9 +62,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -102,9 +103,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -148,7 +149,7 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare(t) defer cleanup() - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) @@ -177,7 +178,7 @@ func prepareCaseOkDefault(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -208,7 +209,7 @@ func prepareCaseUnexpectedJsonResponse(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -222,7 +223,7 @@ func prepareCaseInvalidFormatResponse(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -231,7 +232,7 @@ func prepareCaseConnectionRefused(t *testing.T) (*Collector, func()) { t.Helper() collr := New() collr.URL = "http://127.0.0.1:65001" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } diff --git a/src/go/plugin/go.d/collector/rabbitmq/collector.go b/src/go/plugin/go.d/collector/rabbitmq/collector.go index 6f8df968fdabd8..6b710dc7c5da26 100644 --- a/src/go/plugin/go.d/collector/rabbitmq/collector.go +++ b/src/go/plugin/go.d/collector/rabbitmq/collector.go @@ -3,6 +3,7 @@ package rabbitmq import ( + "context" _ "embed" "errors" "fmt" @@ -69,7 +70,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.URL == "" { return errors.New("config: url not set") } @@ -86,7 +87,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -101,7 +102,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -114,7 +115,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/rabbitmq/collector_test.go b/src/go/plugin/go.d/collector/rabbitmq/collector_test.go index dbaf6fb53e50f2..750917415d487c 100644 --- a/src/go/plugin/go.d/collector/rabbitmq/collector_test.go +++ b/src/go/plugin/go.d/collector/rabbitmq/collector_test.go @@ -3,6 +3,7 @@ package rabbitmq import ( + "context" "net/http" "net/http/httptest" "os" @@ -69,9 +70,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -82,12 +83,12 @@ func TestCollector_Charts(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) } func TestCollector_Check(t *testing.T) { @@ -105,12 +106,12 @@ func TestCollector_Check(t *testing.T) { collr, cleanup := test.prepare() defer cleanup() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -359,9 +360,9 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare() defer cleanup() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantCollected, mx) diff --git a/src/go/plugin/go.d/collector/redis/collector.go b/src/go/plugin/go.d/collector/redis/collector.go index eedd7f76d51bae..394b0d7fed85ac 100644 --- a/src/go/plugin/go.d/collector/redis/collector.go +++ b/src/go/plugin/go.d/collector/redis/collector.go @@ -84,7 +84,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { err := c.validateConfig() if err != nil { return fmt.Errorf("config validation: %v", err) @@ -105,7 +105,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -120,7 +120,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { ms, err := c.collect() if err != nil { c.Error(err) @@ -132,7 +132,7 @@ func (c *Collector) Collect() map[string]int64 { return ms } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.rdb == nil { return } diff --git a/src/go/plugin/go.d/collector/redis/collector_test.go b/src/go/plugin/go.d/collector/redis/collector_test.go index 52571ce95ebd38..b0cc8139f5ee59 100644 --- a/src/go/plugin/go.d/collector/redis/collector_test.go +++ b/src/go/plugin/go.d/collector/redis/collector_test.go @@ -71,9 +71,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -102,9 +102,9 @@ func TestCollector_Check(t *testing.T) { collr := test.prepare(t) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -112,20 +112,20 @@ func TestCollector_Check(t *testing.T) { func TestCollector_Charts(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.Charts()) } func TestCollector_Cleanup(t *testing.T) { collr := New() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) m := &mockRedisClient{} collr.rdb = m - collr.Cleanup() + collr.Cleanup(context.Background()) assert.True(t, m.calledClose) } @@ -298,7 +298,7 @@ func TestCollector_Collect(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare(t) - mx := collr.Collect() + mx := collr.Collect(context.Background()) copyTimeRelatedMetrics(mx, test.wantCollected) @@ -314,7 +314,7 @@ func TestCollector_Collect(t *testing.T) { func prepareRedisV609(t *testing.T) *Collector { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.rdb = &mockRedisClient{ result: dataVer609InfoAll, } @@ -323,7 +323,7 @@ func prepareRedisV609(t *testing.T) *Collector { func prepareRedisErrorOnInfo(t *testing.T) *Collector { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.rdb = &mockRedisClient{ errOnInfo: true, } @@ -332,7 +332,7 @@ func prepareRedisErrorOnInfo(t *testing.T) *Collector { func prepareRedisWithPikaMetrics(t *testing.T) *Collector { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.rdb = &mockRedisClient{ result: dataPikaInfoAll, } diff --git a/src/go/plugin/go.d/collector/rethinkdb/collector.go b/src/go/plugin/go.d/collector/rethinkdb/collector.go index 1e624befc115dd..c54e80d405b496 100644 --- a/src/go/plugin/go.d/collector/rethinkdb/collector.go +++ b/src/go/plugin/go.d/collector/rethinkdb/collector.go @@ -3,6 +3,7 @@ package rethinkdb import ( + "context" _ "embed" "errors" "time" @@ -59,14 +60,14 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.Address == "" { return errors.New("config: address is not set") } return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -81,7 +82,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { ms, err := c.collect() if err != nil { c.Error(err) @@ -93,7 +94,7 @@ func (c *Collector) Collect() map[string]int64 { return ms } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.rdb != nil { if err := c.rdb.close(); err != nil { c.Warningf("cleanup: error on closing client [%s]: %v", c.Address, err) diff --git a/src/go/plugin/go.d/collector/rethinkdb/collector_test.go b/src/go/plugin/go.d/collector/rethinkdb/collector_test.go index 03279c1bb7ed97..3aa2152de18f34 100644 --- a/src/go/plugin/go.d/collector/rethinkdb/collector_test.go +++ b/src/go/plugin/go.d/collector/rethinkdb/collector_test.go @@ -4,6 +4,7 @@ package rethinkdb import ( "bytes" + "context" "errors" "fmt" "os" @@ -63,9 +64,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -86,7 +87,7 @@ func TestCollector_Cleanup(t *testing.T) { collr.newConn = func(config Config) (rdbConn, error) { return &mockRethinkdbConn{dataStats: dataStats}, nil } - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -96,7 +97,7 @@ func TestCollector_Cleanup(t *testing.T) { collr.newConn = func(config Config) (rdbConn, error) { return &mockRethinkdbConn{dataStats: dataStats}, nil } - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -106,7 +107,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -135,14 +136,14 @@ func TestCollector_Check(t *testing.T) { collr := test.prepare() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } if m, ok := collr.rdb.(*mockRethinkdbConn); ok { assert.False(t, m.disconnectCalled, "rdb close before cleanup") - collr.Cleanup() + collr.Cleanup(context.Background()) assert.True(t, m.disconnectCalled, "rdb close after cleanup") } }) @@ -203,9 +204,9 @@ func TestCollector_Collect(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) @@ -217,7 +218,7 @@ func TestCollector_Collect(t *testing.T) { if m, ok := collr.rdb.(*mockRethinkdbConn); ok { assert.False(t, m.disconnectCalled, "rdb close before cleanup") - collr.Cleanup() + collr.Cleanup(context.Background()) assert.True(t, m.disconnectCalled, "rdb close after cleanup") } }) diff --git a/src/go/plugin/go.d/collector/riakkv/collector.go b/src/go/plugin/go.d/collector/riakkv/collector.go index ba61ded6a0fc8f..596222b41b8147 100644 --- a/src/go/plugin/go.d/collector/riakkv/collector.go +++ b/src/go/plugin/go.d/collector/riakkv/collector.go @@ -3,6 +3,7 @@ package riakkv import ( + "context" _ "embed" "errors" "fmt" @@ -69,7 +70,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.URL == "" { return errors.New("config: url not set") } @@ -86,7 +87,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -102,7 +103,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -114,7 +115,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/riakkv/collector_test.go b/src/go/plugin/go.d/collector/riakkv/collector_test.go index d794dec9be5cd3..7e4fc1119e0a1c 100644 --- a/src/go/plugin/go.d/collector/riakkv/collector_test.go +++ b/src/go/plugin/go.d/collector/riakkv/collector_test.go @@ -3,6 +3,7 @@ package riakkv import ( + "context" "net/http" "net/http/httptest" "os" @@ -62,9 +63,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -99,9 +100,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -202,9 +203,9 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare(t) defer cleanup() - _ = collr.Check() + _ = collr.Check(context.Background()) - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) @@ -224,7 +225,7 @@ func caseOkResponse(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -237,7 +238,7 @@ func caseInvalidDataResponse(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -246,7 +247,7 @@ func caseConnectionRefused(t *testing.T) (*Collector, func()) { t.Helper() collr := New() collr.URL = "http://127.0.0.1:65001" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } @@ -259,7 +260,7 @@ func case404(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } diff --git a/src/go/plugin/go.d/collector/rspamd/collector.go b/src/go/plugin/go.d/collector/rspamd/collector.go index dcd8dcee34734a..f55a379e6282c7 100644 --- a/src/go/plugin/go.d/collector/rspamd/collector.go +++ b/src/go/plugin/go.d/collector/rspamd/collector.go @@ -3,6 +3,7 @@ package rspamd import ( + "context" _ "embed" "errors" "fmt" @@ -59,7 +60,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.URL == "" { return errors.New("config: url not set") } @@ -76,7 +77,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -93,7 +94,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -106,7 +107,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/rspamd/collector_test.go b/src/go/plugin/go.d/collector/rspamd/collector_test.go index 2c208b1f804ff1..a1dcc1c6ee19c6 100644 --- a/src/go/plugin/go.d/collector/rspamd/collector_test.go +++ b/src/go/plugin/go.d/collector/rspamd/collector_test.go @@ -3,6 +3,7 @@ package rspamd import ( + "context" "net/http" "net/http/httptest" "os" @@ -61,9 +62,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -102,9 +103,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -153,7 +154,7 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare(t) defer cleanup() - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) @@ -178,7 +179,7 @@ func prepareCaseOk(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -214,7 +215,7 @@ func prepareCaseUnexpectedJsonResponse(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -228,7 +229,7 @@ func prepareCaseInvalidFormatResponse(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -237,7 +238,7 @@ func prepareCaseConnectionRefused(t *testing.T) (*Collector, func()) { t.Helper() collr := New() collr.URL = "http://127.0.0.1:65001/stat" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } diff --git a/src/go/plugin/go.d/collector/samba/collector.go b/src/go/plugin/go.d/collector/samba/collector.go index 52a76a8c047c4c..96b653a961a346 100644 --- a/src/go/plugin/go.d/collector/samba/collector.go +++ b/src/go/plugin/go.d/collector/samba/collector.go @@ -3,6 +3,7 @@ package samba import ( + "context" _ "embed" "errors" "fmt" @@ -56,7 +57,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { smbStatus, err := c.initSmbStatusBinary() if err != nil { return fmt.Errorf("smbstatus exec initialization: %v", err) @@ -66,7 +67,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -83,7 +84,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -96,4 +97,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/samba/collector_test.go b/src/go/plugin/go.d/collector/samba/collector_test.go index 32175b89a71f5e..bb2d6388fe558b 100644 --- a/src/go/plugin/go.d/collector/samba/collector_test.go +++ b/src/go/plugin/go.d/collector/samba/collector_test.go @@ -3,6 +3,7 @@ package samba import ( + "context" "errors" "os" "testing" @@ -52,9 +53,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -73,7 +74,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOk() - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -81,7 +82,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOk() - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -91,7 +92,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -130,9 +131,9 @@ func TestCollector_Check(t *testing.T) { collr.exec = mock if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -288,7 +289,7 @@ func TestCollector_Collect(t *testing.T) { mock := test.prepareMock() collr.exec = mock - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantMetrics, mx) diff --git a/src/go/plugin/go.d/collector/scaleio/collector.go b/src/go/plugin/go.d/collector/scaleio/collector.go index 2331a2702993a4..791e53a78d3514 100644 --- a/src/go/plugin/go.d/collector/scaleio/collector.go +++ b/src/go/plugin/go.d/collector/scaleio/collector.go @@ -3,6 +3,7 @@ package scaleio import ( + "context" _ "embed" "errors" "fmt" @@ -71,7 +72,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.Username == "" || c.Password == "" { return errors.New("config: username and password aren't set") } @@ -88,7 +89,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { if err := c.client.Login(); err != nil { return err } @@ -106,7 +107,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -119,7 +120,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.client == nil { return } diff --git a/src/go/plugin/go.d/collector/scaleio/collector_test.go b/src/go/plugin/go.d/collector/scaleio/collector_test.go index 87a4cfcacadd4e..b19236c6cb5d11 100644 --- a/src/go/plugin/go.d/collector/scaleio/collector_test.go +++ b/src/go/plugin/go.d/collector/scaleio/collector_test.go @@ -3,6 +3,7 @@ package scaleio import ( + "context" "encoding/json" "net/http/httptest" "os" @@ -43,10 +44,10 @@ func TestCollector_Init(t *testing.T) { collr.Username = "username" collr.Password = "password" - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } func TestCollector_Init_UsernameAndPasswordNotSet(t *testing.T) { - assert.Error(t, New().Init()) + assert.Error(t, New().Init(context.Background())) } func TestCollector_Init_ErrorOnCreatingClientWrongTLSCA(t *testing.T) { @@ -55,24 +56,24 @@ func TestCollector_Init_ErrorOnCreatingClientWrongTLSCA(t *testing.T) { collr.Password = "password" collr.ClientConfig.TLSConfig.TLSCA = "testdata/tls" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Check(t *testing.T) { srv, _, collr := prepareSrvMockScaleIO(t) defer srv.Close() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } func TestCollector_Check_ErrorOnLogin(t *testing.T) { srv, mock, collr := prepareSrvMockScaleIO(t) defer srv.Close() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) mock.Password = "new password" - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_Charts(t *testing.T) { @@ -82,18 +83,18 @@ func TestCollector_Charts(t *testing.T) { func TestCollector_Cleanup(t *testing.T) { srv, _, collr := prepareSrvMockScaleIO(t) defer srv.Close() - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) - collr.Cleanup() + collr.Cleanup(context.Background()) assert.False(t, collr.client.LoggedIn()) } func TestCollector_Collect(t *testing.T) { srv, _, collr := prepareSrvMockScaleIO(t) defer srv.Close() - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) expected := map[string]int64{ "sdc_6076fd0f00000000_bandwidth_read": 0, @@ -298,7 +299,7 @@ func TestCollector_Collect(t *testing.T) { "system_total_iops_write": 617200, } - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, expected, mx) @@ -308,11 +309,11 @@ func TestCollector_Collect(t *testing.T) { func TestCollector_Collect_ConnectionRefused(t *testing.T) { srv, _, collr := prepareSrvMockScaleIO(t) defer srv.Close() - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) collr.client.Request.URL = "http://127.0.0.1:38001" - assert.Nil(t, collr.Collect()) + assert.Nil(t, collr.Collect(context.Background())) } func testCharts(t *testing.T, collr *Collector, collected map[string]int64) { diff --git a/src/go/plugin/go.d/collector/sensors/collector.go b/src/go/plugin/go.d/collector/sensors/collector.go index 0d0fe2a682f08a..735f2605df68ef 100644 --- a/src/go/plugin/go.d/collector/sensors/collector.go +++ b/src/go/plugin/go.d/collector/sensors/collector.go @@ -5,6 +5,7 @@ package sensors import ( + "context" _ "embed" "errors" @@ -65,7 +66,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { sc := lmsensors.New() sc.Logger = c.Logger c.sc = sc @@ -73,7 +74,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -90,7 +91,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -103,4 +104,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/sensors/collector_test.go b/src/go/plugin/go.d/collector/sensors/collector_test.go index 64a9c85fcadd0b..56e7ab4c53aaaf 100644 --- a/src/go/plugin/go.d/collector/sensors/collector_test.go +++ b/src/go/plugin/go.d/collector/sensors/collector_test.go @@ -5,6 +5,7 @@ package sensors import ( + "context" "errors" "os" "testing" @@ -52,9 +53,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -73,7 +74,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.sc = prepareMockScannerOk() - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -81,7 +82,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.sc = prepareMockScannerOk() - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -91,7 +92,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -121,9 +122,9 @@ func TestCollector_Check(t *testing.T) { collr.sc = test.prepareMock() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -270,7 +271,7 @@ func TestCollector_Collect(t *testing.T) { var mx map[string]int64 for i := 0; i < 10; i++ { - mx = collr.Collect() + mx = collr.Collect(context.Background()) } assert.Equal(t, test.wantMetrics, mx) diff --git a/src/go/plugin/go.d/collector/smartctl/collector.go b/src/go/plugin/go.d/collector/smartctl/collector.go index 2c06b9b4363253..56ddce7c1bc8f0 100644 --- a/src/go/plugin/go.d/collector/smartctl/collector.go +++ b/src/go/plugin/go.d/collector/smartctl/collector.go @@ -5,6 +5,7 @@ package smartctl import ( + "context" _ "embed" "errors" "fmt" @@ -86,7 +87,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %s", err) } @@ -106,7 +107,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -123,7 +124,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -136,4 +137,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/smartctl/collector_test.go b/src/go/plugin/go.d/collector/smartctl/collector_test.go index e40bb6dd9a7798..f7f96f78d6527a 100644 --- a/src/go/plugin/go.d/collector/smartctl/collector_test.go +++ b/src/go/plugin/go.d/collector/smartctl/collector_test.go @@ -5,6 +5,7 @@ package smartctl import ( + "context" "fmt" "os" "testing" @@ -82,9 +83,9 @@ func TestCollector_Init(t *testing.T) { collr := New() if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -103,7 +104,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOkTypeSata() - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -111,7 +112,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOkTypeSata() - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -121,7 +122,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -160,9 +161,9 @@ func TestCollector_Check(t *testing.T) { collr.exec = mock if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -364,7 +365,7 @@ func TestCollector_Collect(t *testing.T) { var mx map[string]int64 for i := 0; i < 10; i++ { - mx = collr.Collect() + mx = collr.Collect(context.Background()) } assert.Equal(t, test.wantMetrics, mx) diff --git a/src/go/plugin/go.d/collector/snmp/collector.go b/src/go/plugin/go.d/collector/snmp/collector.go index 409e492aee8905..e461733580e327 100644 --- a/src/go/plugin/go.d/collector/snmp/collector.go +++ b/src/go/plugin/go.d/collector/snmp/collector.go @@ -3,6 +3,7 @@ package snmp import ( + "context" _ "embed" "errors" "fmt" @@ -83,7 +84,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { err := c.validateConfig() if err != nil { return fmt.Errorf("config validation failed: %v", err) @@ -118,7 +119,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -135,7 +136,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -148,7 +149,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.snmpClient != nil { _ = c.snmpClient.Close() } diff --git a/src/go/plugin/go.d/collector/snmp/collector_test.go b/src/go/plugin/go.d/collector/snmp/collector_test.go index bb40e9623a0a7f..418f50dce1edaa 100644 --- a/src/go/plugin/go.d/collector/snmp/collector_test.go +++ b/src/go/plugin/go.d/collector/snmp/collector_test.go @@ -3,6 +3,7 @@ package snmp import ( + "context" "encoding/hex" "errors" "fmt" @@ -88,9 +89,9 @@ func TestCollector_Init(t *testing.T) { collr := test.prepareSNMP() if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -107,7 +108,7 @@ func TestCollector_Cleanup(t *testing.T) { collr.newSnmpClient = func() gosnmp.Handler { return m } setMockClientInitExpect(m) - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) m.EXPECT().Close().Times(1) @@ -121,7 +122,7 @@ func TestCollector_Cleanup(t *testing.T) { collr.newSnmpClient = func() gosnmp.Handler { return m } setMockClientInitExpect(m) - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.snmpClient = nil @@ -137,7 +138,7 @@ func TestCollector_Cleanup(t *testing.T) { collr := test.prepareSNMP(t, mockSNMP) - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -182,10 +183,10 @@ func TestCollector_Charts(t *testing.T) { collr := test.prepareSNMP(t, mockSNMP) collr.newSnmpClient = func() gosnmp.Handler { return mockSNMP } - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.doCollect { - _ = collr.Collect() + _ = collr.Collect(context.Background()) } assert.Equal(t, test.wantNumCharts, len(*collr.Charts())) @@ -256,12 +257,12 @@ func TestCollector_Check(t *testing.T) { collr := test.prepareSNMP(mockSNMP) collr.newSnmpClient = func() gosnmp.Handler { return mockSNMP } - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -468,9 +469,9 @@ func TestCollector_Collect(t *testing.T) { collr := test.prepareSNMP(mockSNMP) collr.newSnmpClient = func() gosnmp.Handler { return mockSNMP } - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantCollected, mx) }) diff --git a/src/go/plugin/go.d/collector/spigotmc/collect.go b/src/go/plugin/go.d/collector/spigotmc/collect.go index 92bb507caaddf5..8e1124d62e60f3 100644 --- a/src/go/plugin/go.d/collector/spigotmc/collect.go +++ b/src/go/plugin/go.d/collector/spigotmc/collect.go @@ -3,6 +3,7 @@ package spigotmc import ( + "context" "errors" "fmt" "regexp" @@ -30,11 +31,11 @@ func (c *Collector) collect() (map[string]int64, error) { mx := make(map[string]int64) if err := c.collectTPS(mx); err != nil { - c.Cleanup() + c.Cleanup(context.Background()) return nil, fmt.Errorf("failed to collect '%s': %v", cmdTPS, err) } if err := c.collectList(mx); err != nil { - c.Cleanup() + c.Cleanup(context.Background()) return nil, fmt.Errorf("failed to collect '%s': %v", cmdList, err) } diff --git a/src/go/plugin/go.d/collector/spigotmc/collector.go b/src/go/plugin/go.d/collector/spigotmc/collector.go index 800e5e70bff6d1..2b92ae24f3ea0a 100644 --- a/src/go/plugin/go.d/collector/spigotmc/collector.go +++ b/src/go/plugin/go.d/collector/spigotmc/collector.go @@ -3,6 +3,7 @@ package spigotmc import ( + "context" _ "embed" "errors" "time" @@ -54,7 +55,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.Address == "" { return errors.New("config: 'address' required but not set") } @@ -64,7 +65,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -81,7 +82,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -94,7 +95,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.conn != nil { if err := c.conn.disconnect(); err != nil { c.Warningf("error on disconnect: %s", err) diff --git a/src/go/plugin/go.d/collector/spigotmc/collector_test.go b/src/go/plugin/go.d/collector/spigotmc/collector_test.go index 5df4e4194d4b72..5725e906cd62c7 100644 --- a/src/go/plugin/go.d/collector/spigotmc/collector_test.go +++ b/src/go/plugin/go.d/collector/spigotmc/collector_test.go @@ -3,6 +3,7 @@ package spigotmc import ( + "context" "errors" "os" "testing" @@ -66,9 +67,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -87,7 +88,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.newConn = func(Config) rconConn { return prepareMockOk() } - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -95,7 +96,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.newConn = func(Config) rconConn { return prepareMockOk() } - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -105,7 +106,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -144,9 +145,9 @@ func TestCollector_Check(t *testing.T) { collr.newConn = func(Config) rconConn { return mock } if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -244,7 +245,7 @@ func TestCollector_Collect(t *testing.T) { mock := test.prepareMock() collr.newConn = func(Config) rconConn { return mock } - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx, "want metrics") @@ -254,7 +255,7 @@ func TestCollector_Collect(t *testing.T) { } assert.Equal(t, test.disconnectBeforeCleanup, mock.disconnectCalled, "disconnect before cleanup") - collr.Cleanup() + collr.Cleanup(context.Background()) assert.Equal(t, test.disconnectAfterCleanup, mock.disconnectCalled, "disconnect after cleanup") }) } diff --git a/src/go/plugin/go.d/collector/squid/collector.go b/src/go/plugin/go.d/collector/squid/collector.go index 62bbaa7c58f421..4d8c3326ef279b 100644 --- a/src/go/plugin/go.d/collector/squid/collector.go +++ b/src/go/plugin/go.d/collector/squid/collector.go @@ -3,6 +3,7 @@ package squid import ( + "context" _ "embed" "errors" "fmt" @@ -59,7 +60,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.URL == "" { return errors.New("config: url not set") } @@ -76,7 +77,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -93,7 +94,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -106,7 +107,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/squid/collector_test.go b/src/go/plugin/go.d/collector/squid/collector_test.go index 0c9448727a57c2..8c6c67294692bd 100644 --- a/src/go/plugin/go.d/collector/squid/collector_test.go +++ b/src/go/plugin/go.d/collector/squid/collector_test.go @@ -3,6 +3,7 @@ package squid import ( + "context" "net/http" "net/http/httptest" "os" @@ -60,9 +61,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -101,9 +102,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -147,7 +148,7 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare(t) defer cleanup() - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) @@ -173,7 +174,7 @@ func prepareCaseSuccess(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -192,7 +193,7 @@ Fusce et felis pulvinar, posuere sem non, porttitor eros.`) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -208,7 +209,7 @@ func prepareCaseEmptyResponse(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -217,7 +218,7 @@ func prepareCaseConnectionRefused(t *testing.T) (*Collector, func()) { t.Helper() collr := New() collr.URL = "http://127.0.0.1:65001" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } diff --git a/src/go/plugin/go.d/collector/squidlog/collector.go b/src/go/plugin/go.d/collector/squidlog/collector.go index 27bcfc0eba65b8..be80fe2cd50660 100644 --- a/src/go/plugin/go.d/collector/squidlog/collector.go +++ b/src/go/plugin/go.d/collector/squidlog/collector.go @@ -3,6 +3,7 @@ package squidlog import ( + "context" _ "embed" "fmt" @@ -64,13 +65,13 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { c.line = newEmptyLogLine() c.mx = newMetricsData() return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { // Note: these inits are here to make auto-detection retry working if err := c.createLogReader(); err != nil { return fmt.Errorf("failed to create log reader: %v", err) @@ -91,7 +92,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -103,7 +104,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.file != nil { _ = c.file.Close() } diff --git a/src/go/plugin/go.d/collector/squidlog/collector_test.go b/src/go/plugin/go.d/collector/squidlog/collector_test.go index 2480861ba2664a..c0c2bf4194d1a5 100644 --- a/src/go/plugin/go.d/collector/squidlog/collector_test.go +++ b/src/go/plugin/go.d/collector/squidlog/collector_test.go @@ -4,6 +4,7 @@ package squidlog import ( "bytes" + "context" "os" "testing" @@ -42,7 +43,7 @@ func TestNew(t *testing.T) { func TestCollector_Init(t *testing.T) { collr := New() - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } func TestCollector_Check(t *testing.T) { @@ -50,30 +51,30 @@ func TestCollector_Check(t *testing.T) { func TestCollector_Check_ErrorOnCreatingLogReaderNoLogFile(t *testing.T) { collr := New() - defer collr.Cleanup() + defer collr.Cleanup(context.Background()) collr.Path = "testdata/not_exists.log" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } func TestSquid_Check_ErrorOnCreatingParserUnknownFormat(t *testing.T) { collr := New() - defer collr.Cleanup() + defer collr.Cleanup(context.Background()) collr.Path = "testdata/unknown.log" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } func TestSquid_Check_ErrorOnCreatingParserZeroKnownFields(t *testing.T) { collr := New() - defer collr.Cleanup() + defer collr.Cleanup(context.Background()) collr.Path = "testdata/access.log" collr.ParserConfig.CSV.Format = "$one $two" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_Charts(t *testing.T) { @@ -85,7 +86,7 @@ func TestCollector_Charts(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - New().Cleanup() + New().Cleanup(context.Background()) } func TestCollector_Collect(t *testing.T) { @@ -159,7 +160,7 @@ func TestCollector_Collect(t *testing.T) { "unmatched": 16, } - collected := collr.Collect() + collected := collr.Collect(context.Background()) assert.Equal(t, expected, collected) testCharts(t, collr, collected) @@ -236,9 +237,9 @@ func TestCollector_Collect_ReturnOldDataIfNothingRead(t *testing.T) { "unmatched": 16, } - _ = collr.Collect() + _ = collr.Collect(context.Background()) - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, expected, mx) @@ -281,9 +282,9 @@ func prepareSquidCollect(t *testing.T) *Collector { t.Helper() collr := New() collr.Path = "testdata/access.log" - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) - defer collr.Cleanup() + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) + defer collr.Cleanup(context.Background()) p, err := logs.NewCSVParser(collr.ParserConfig.CSV, bytes.NewReader(dataNativeFormatAccessLog)) require.NoError(t, err) diff --git a/src/go/plugin/go.d/collector/squidlog/init.go b/src/go/plugin/go.d/collector/squidlog/init.go index b65b9908aa9db7..118e4e1e4ef6eb 100644 --- a/src/go/plugin/go.d/collector/squidlog/init.go +++ b/src/go/plugin/go.d/collector/squidlog/init.go @@ -3,6 +3,7 @@ package squidlog import ( + "context" "fmt" "strings" @@ -10,7 +11,7 @@ import ( ) func (c *Collector) createLogReader() error { - c.Cleanup() + c.Cleanup(context.Background()) c.Debug("starting log reader creating") reader, err := logs.Open(c.Path, c.ExcludePath, c.Logger) diff --git a/src/go/plugin/go.d/collector/storcli/collector.go b/src/go/plugin/go.d/collector/storcli/collector.go index 56ee4495e3df8e..382a9c98595a52 100644 --- a/src/go/plugin/go.d/collector/storcli/collector.go +++ b/src/go/plugin/go.d/collector/storcli/collector.go @@ -5,6 +5,7 @@ package storcli import ( + "context" _ "embed" "errors" "fmt" @@ -62,7 +63,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { storExec, err := c.initStorCliExec() if err != nil { return fmt.Errorf("storcli exec initialization: %v", err) @@ -72,7 +73,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -89,7 +90,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -102,4 +103,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/storcli/collector_test.go b/src/go/plugin/go.d/collector/storcli/collector_test.go index 05997c3faa5fb1..ee30660d4e8eb0 100644 --- a/src/go/plugin/go.d/collector/storcli/collector_test.go +++ b/src/go/plugin/go.d/collector/storcli/collector_test.go @@ -5,6 +5,7 @@ package storcli import ( + "context" "errors" "os" "testing" @@ -57,9 +58,9 @@ func TestCollector_Init(t *testing.T) { collr := New() if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -78,7 +79,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockMegaRaidOK() - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -86,7 +87,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockMegaRaidOK() - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -96,7 +97,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -135,9 +136,9 @@ func TestCollector_Check(t *testing.T) { collr.exec = mock if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -230,7 +231,7 @@ func TestCollector_Collect(t *testing.T) { mock := test.prepareMock() collr.exec = mock - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantMetrics, mx) diff --git a/src/go/plugin/go.d/collector/supervisord/collector.go b/src/go/plugin/go.d/collector/supervisord/collector.go index 0756b7b5eaad5e..27bc6863b3fa32 100644 --- a/src/go/plugin/go.d/collector/supervisord/collector.go +++ b/src/go/plugin/go.d/collector/supervisord/collector.go @@ -3,6 +3,7 @@ package supervisord import ( + "context" _ "embed" "errors" "fmt" @@ -59,7 +60,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { err := c.verifyConfig() if err != nil { return fmt.Errorf("verify config: %v", err) @@ -74,7 +75,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -89,7 +90,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { ms, err := c.collect() if err != nil { c.Error(err) @@ -101,7 +102,7 @@ func (c *Collector) Collect() map[string]int64 { return ms } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.client != nil { c.client.closeIdleConnections() } diff --git a/src/go/plugin/go.d/collector/supervisord/collector_test.go b/src/go/plugin/go.d/collector/supervisord/collector_test.go index c83bd9ce888a9d..3c5d4a13440976 100644 --- a/src/go/plugin/go.d/collector/supervisord/collector_test.go +++ b/src/go/plugin/go.d/collector/supervisord/collector_test.go @@ -3,6 +3,7 @@ package supervisord import ( + "context" "errors" "os" "testing" @@ -55,9 +56,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -83,12 +84,12 @@ func TestCollector_Check(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { collr := test.prepare(t) - defer collr.Cleanup() + defer collr.Cleanup(context.Background()) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -96,20 +97,20 @@ func TestCollector_Check(t *testing.T) { func TestCollector_Charts(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.Charts()) } func TestCollector_Cleanup(t *testing.T) { collr := New() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) m := &mockSupervisorClient{} collr.client = m - collr.Cleanup() + collr.Cleanup(context.Background()) assert.True(t, m.calledCloseIdleConnections) } @@ -167,9 +168,9 @@ func TestCollector_Collect(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { collr := test.prepare(t) - defer collr.Cleanup() + defer collr.Cleanup(context.Background()) - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantCollected, mx) if len(test.wantCollected) > 0 { module.TestMetricsHasAllChartsDims(t, collr.Charts(), mx) @@ -180,21 +181,21 @@ func TestCollector_Collect(t *testing.T) { func prepareSupervisordSuccessOnGetAllProcessInfo(t *testing.T) *Collector { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.client = &mockSupervisorClient{} return collr } func prepareSupervisordZeroProcessesOnGetAllProcessInfo(t *testing.T) *Collector { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.client = &mockSupervisorClient{returnZeroProcesses: true} return collr } func prepareSupervisordErrorOnGetAllProcessInfo(t *testing.T) *Collector { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.client = &mockSupervisorClient{errOnGetAllProcessInfo: true} return collr } diff --git a/src/go/plugin/go.d/collector/systemdunits/collector.go b/src/go/plugin/go.d/collector/systemdunits/collector.go index 24a9c7b021b352..c68dd6e9551de7 100644 --- a/src/go/plugin/go.d/collector/systemdunits/collector.go +++ b/src/go/plugin/go.d/collector/systemdunits/collector.go @@ -5,6 +5,7 @@ package systemdunits import ( + "context" _ "embed" "errors" "fmt" @@ -83,7 +84,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -102,7 +103,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -119,7 +120,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -131,6 +132,6 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { c.closeConnection() } diff --git a/src/go/plugin/go.d/collector/systemdunits/collector_test.go b/src/go/plugin/go.d/collector/systemdunits/collector_test.go index fae68426813252..e9f649d6ebf718 100644 --- a/src/go/plugin/go.d/collector/systemdunits/collector_test.go +++ b/src/go/plugin/go.d/collector/systemdunits/collector_test.go @@ -64,9 +64,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -131,12 +131,12 @@ func TestCollector_Check(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { collr := test.prepare() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -144,7 +144,7 @@ func TestCollector_Check(t *testing.T) { func TestCollector_Charts(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.Charts()) } @@ -154,10 +154,10 @@ func TestCollector_Cleanup(t *testing.T) { client := prepareOKClient(230) collr.client = client - require.NoError(t, collr.Init()) - require.NotNil(t, collr.Collect()) + require.NoError(t, collr.Init(context.Background())) + require.NotNil(t, collr.Collect(context.Background())) conn := collr.conn - collr.Cleanup() + collr.Cleanup(context.Background()) assert.Nil(t, collr.conn) v, _ := conn.(*mockConn) @@ -851,12 +851,12 @@ func TestCollector_Collect(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { collr := test.prepare() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) var mx map[string]int64 for i := 0; i < 10; i++ { - mx = collr.Collect() + mx = collr.Collect(context.Background()) } assert.Equal(t, test.wantCollected, mx) @@ -872,11 +872,11 @@ func TestCollector_connectionReuse(t *testing.T) { collr.Include = []string{"*"} client := prepareOKClient(230) collr.client = client - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) var collected map[string]int64 for i := 0; i < 10; i++ { - collected = collr.Collect() + collected = collr.Collect(context.Background()) } assert.NotEmpty(t, collected) diff --git a/src/go/plugin/go.d/collector/tengine/collector.go b/src/go/plugin/go.d/collector/tengine/collector.go index 2fc41a6c545342..e7fbfc85ab4cea 100644 --- a/src/go/plugin/go.d/collector/tengine/collector.go +++ b/src/go/plugin/go.d/collector/tengine/collector.go @@ -3,6 +3,7 @@ package tengine import ( + "context" _ "embed" "errors" "fmt" @@ -59,7 +60,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.URL == "" { return errors.New("config: url not set") } @@ -76,7 +77,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -93,7 +94,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -107,7 +108,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/tengine/collector_test.go b/src/go/plugin/go.d/collector/tengine/collector_test.go index 8a344758af6c16..83c89d8c34d310 100644 --- a/src/go/plugin/go.d/collector/tengine/collector_test.go +++ b/src/go/plugin/go.d/collector/tengine/collector_test.go @@ -3,6 +3,7 @@ package tengine import ( + "context" "net/http" "net/http/httptest" "os" @@ -36,13 +37,13 @@ func TestCollector_ConfigurationSerialize(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - New().Cleanup() + New().Cleanup(context.Background()) } func TestCollector_Init(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) } func TestCollector_Check(t *testing.T) { @@ -55,16 +56,16 @@ func TestCollector_Check(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) - assert.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.NoError(t, collr.Check(context.Background())) } func TestCollector_CheckNG(t *testing.T) { collr := New() collr.URL = "http://127.0.0.1:38001/us" - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_Charts(t *testing.T) { assert.NotNil(t, New().Charts()) } @@ -79,8 +80,8 @@ func TestCollector_Collect(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) expected := map[string]int64{ "bytes_in": 5944, @@ -114,7 +115,7 @@ func TestCollector_Collect(t *testing.T) { "ups_tries": 268, } - assert.Equal(t, expected, collr.Collect()) + assert.Equal(t, expected, collr.Collect(context.Background())) } func TestCollector_InvalidData(t *testing.T) { @@ -127,8 +128,8 @@ func TestCollector_InvalidData(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_404(t *testing.T) { @@ -141,6 +142,6 @@ func TestCollector_404(t *testing.T) { collr := New() collr.URL = ts.URL - require.NoError(t, collr.Init()) - assert.Error(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + assert.Error(t, collr.Check(context.Background())) } diff --git a/src/go/plugin/go.d/collector/testrandom/collector.go b/src/go/plugin/go.d/collector/testrandom/collector.go index a4de8b3e6bc9ba..41b071d9f9a88a 100644 --- a/src/go/plugin/go.d/collector/testrandom/collector.go +++ b/src/go/plugin/go.d/collector/testrandom/collector.go @@ -3,6 +3,7 @@ package testrandom import ( + "context" _ "embed" "fmt" "math/rand" @@ -72,7 +73,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { err := c.validateConfig() if err != nil { return fmt.Errorf("config validation: %v", err) @@ -86,7 +87,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { return nil } @@ -94,7 +95,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -106,4 +107,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/testrandom/collector_test.go b/src/go/plugin/go.d/collector/testrandom/collector_test.go index 6a85ddae863a29..41875cd8a4d4df 100644 --- a/src/go/plugin/go.d/collector/testrandom/collector_test.go +++ b/src/go/plugin/go.d/collector/testrandom/collector_test.go @@ -3,6 +3,7 @@ package testrandom import ( + "context" "os" "testing" @@ -109,9 +110,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -131,12 +132,12 @@ func TestCollector_Check(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { collr := test.prepare() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -156,7 +157,7 @@ func TestCollector_Charts(t *testing.T) { "initialized collector": { prepare: func(t *testing.T) *Collector { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr }, }, @@ -176,7 +177,7 @@ func TestCollector_Charts(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Collect(t *testing.T) { @@ -253,9 +254,9 @@ func TestCollector_Collect(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { collr := test.prepare() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantCollected, mx) module.TestMetricsHasAllChartsDims(t, collr.Charts(), mx) diff --git a/src/go/plugin/go.d/collector/tomcat/collector.go b/src/go/plugin/go.d/collector/tomcat/collector.go index a4cc008fbb504e..28b5c69b3e1bd0 100644 --- a/src/go/plugin/go.d/collector/tomcat/collector.go +++ b/src/go/plugin/go.d/collector/tomcat/collector.go @@ -3,6 +3,7 @@ package tomcat import ( + "context" _ "embed" "errors" "fmt" @@ -64,7 +65,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -82,7 +83,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -99,7 +100,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -112,7 +113,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/tomcat/collector_test.go b/src/go/plugin/go.d/collector/tomcat/collector_test.go index 2f7c6140c7642a..b1d971c0715408 100644 --- a/src/go/plugin/go.d/collector/tomcat/collector_test.go +++ b/src/go/plugin/go.d/collector/tomcat/collector_test.go @@ -3,6 +3,7 @@ package tomcat import ( + "context" "net/http" "net/http/httptest" "os" @@ -61,9 +62,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -102,9 +103,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -181,7 +182,7 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare(t) defer cleanup() - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) @@ -210,7 +211,7 @@ func prepareCaseSuccess(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -219,7 +220,7 @@ func prepareCaseConnectionRefused(t *testing.T) (*Collector, func()) { t.Helper() collr := New() collr.URL = "http://127.0.0.1:65001" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } @@ -252,7 +253,7 @@ func prepareCaseUnexpectedXMLResponse(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -266,7 +267,7 @@ func prepareCaseInvalidFormatResponse(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } diff --git a/src/go/plugin/go.d/collector/tor/collect.go b/src/go/plugin/go.d/collector/tor/collect.go index 372ce50f72ffca..914c68e3d285f2 100644 --- a/src/go/plugin/go.d/collector/tor/collect.go +++ b/src/go/plugin/go.d/collector/tor/collect.go @@ -5,6 +5,7 @@ package tor import ( "bufio" "bytes" + "context" "fmt" "strconv" "strings" @@ -21,7 +22,7 @@ func (c *Collector) collect() (map[string]int64, error) { mx := make(map[string]int64) if err := c.collectServerInfo(mx); err != nil { - c.Cleanup() + c.Cleanup(context.Background()) return nil, err } diff --git a/src/go/plugin/go.d/collector/tor/collector.go b/src/go/plugin/go.d/collector/tor/collector.go index 6e21b440fdbdc6..48b0d529d87e58 100644 --- a/src/go/plugin/go.d/collector/tor/collector.go +++ b/src/go/plugin/go.d/collector/tor/collector.go @@ -3,6 +3,7 @@ package tor import ( + "context" _ "embed" "errors" "time" @@ -54,7 +55,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.Address == "" { return errors.New("config: address not set") } @@ -62,7 +63,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -79,7 +80,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -92,7 +93,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.conn != nil { c.conn.disconnect() c.conn = nil diff --git a/src/go/plugin/go.d/collector/tor/collector_test.go b/src/go/plugin/go.d/collector/tor/collector_test.go index f8bcc2572b5649..e63a7b03b4e6ab 100644 --- a/src/go/plugin/go.d/collector/tor/collector_test.go +++ b/src/go/plugin/go.d/collector/tor/collector_test.go @@ -4,6 +4,7 @@ package tor import ( "bufio" + "context" "errors" "fmt" "io" @@ -62,9 +63,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -105,15 +106,15 @@ func TestCollector_Check(t *testing.T) { t.Errorf("mock tor daemon start timed out") } - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } - collr.Cleanup() + collr.Cleanup(context.Background()) select { case <-daemon.stopped: @@ -162,9 +163,9 @@ func TestCollector_Collect(t *testing.T) { t.Errorf("mock tor daemon start timed out") } - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) @@ -174,7 +175,7 @@ func TestCollector_Collect(t *testing.T) { module.TestMetricsHasAllChartsDims(t, collr.Charts(), mx) } - collr.Cleanup() + collr.Cleanup(context.Background()) select { case <-daemon.stopped: diff --git a/src/go/plugin/go.d/collector/traefik/collector.go b/src/go/plugin/go.d/collector/traefik/collector.go index d38bfd67a76624..854fc5d6a40173 100644 --- a/src/go/plugin/go.d/collector/traefik/collector.go +++ b/src/go/plugin/go.d/collector/traefik/collector.go @@ -3,6 +3,7 @@ package traefik import ( + "context" _ "embed" "errors" "fmt" @@ -84,7 +85,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -98,7 +99,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -113,7 +114,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -126,4 +127,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/traefik/collector_test.go b/src/go/plugin/go.d/collector/traefik/collector_test.go index e9ccfe51ef695b..32590141d2748f 100644 --- a/src/go/plugin/go.d/collector/traefik/collector_test.go +++ b/src/go/plugin/go.d/collector/traefik/collector_test.go @@ -3,6 +3,7 @@ package traefik import ( + "context" "net/http" "net/http/httptest" "os" @@ -68,9 +69,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -81,7 +82,7 @@ func TestCollector_Charts(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Check(t *testing.T) { @@ -113,9 +114,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -243,7 +244,7 @@ func TestCollector_Collect(t *testing.T) { var mx map[string]int64 for _, want := range test.wantCollected { - mx = collr.Collect() + mx = collr.Collect(context.Background()) assert.Equal(t, want, mx) } if len(test.wantCollected) > 0 { @@ -261,7 +262,7 @@ func prepareCaseTraefikV221Metrics(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -298,7 +299,7 @@ traefik_entrypoint_request_duration_seconds_count{code="300",entrypoint="web",me })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -326,7 +327,7 @@ application_backend_http_responses_total{proxy="infra-vernemq-ws",code="other"} })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -339,7 +340,7 @@ func prepareCase404Response(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -348,7 +349,7 @@ func prepareCaseConnectionRefused(t *testing.T) (*Collector, func()) { t.Helper() collr := New() collr.URL = "http://127.0.0.1:38001" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } diff --git a/src/go/plugin/go.d/collector/typesense/collector.go b/src/go/plugin/go.d/collector/typesense/collector.go index 2199a6dd74e0fb..2379455eed4f07 100644 --- a/src/go/plugin/go.d/collector/typesense/collector.go +++ b/src/go/plugin/go.d/collector/typesense/collector.go @@ -3,6 +3,7 @@ package typesense import ( + "context" _ "embed" "errors" "fmt" @@ -65,7 +66,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.URL == "" { return errors.New("configL: url not configured") } @@ -86,7 +87,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -102,7 +103,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -114,7 +115,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.httpClient != nil { c.httpClient.CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/typesense/collector_test.go b/src/go/plugin/go.d/collector/typesense/collector_test.go index 0bbaab837a633a..8669de61944e8f 100644 --- a/src/go/plugin/go.d/collector/typesense/collector_test.go +++ b/src/go/plugin/go.d/collector/typesense/collector_test.go @@ -3,6 +3,7 @@ package typesense import ( + "context" "net/http" "net/http/httptest" "os" @@ -68,9 +69,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -113,9 +114,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -183,9 +184,9 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare(t) defer cleanup() - _ = collr.Check() + _ = collr.Check(context.Background()) - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) @@ -220,7 +221,7 @@ func caseOk(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL collr.APIKey = testApiKey - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -247,7 +248,7 @@ func caseOkNoApiKey(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL collr.APIKey = "" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -277,7 +278,7 @@ func caseUnexpectedJsonResponse(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -290,7 +291,7 @@ func caseInvalidDataResponse(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -299,7 +300,7 @@ func caseConnectionRefused(t *testing.T) (*Collector, func()) { t.Helper() collr := New() collr.URL = "http://127.0.0.1:65001" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } @@ -312,7 +313,7 @@ func case404(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } diff --git a/src/go/plugin/go.d/collector/unbound/collector.go b/src/go/plugin/go.d/collector/unbound/collector.go index f987abf8d48591..4d0f710634ada6 100644 --- a/src/go/plugin/go.d/collector/unbound/collector.go +++ b/src/go/plugin/go.d/collector/unbound/collector.go @@ -3,6 +3,7 @@ package unbound import ( + "context" _ "embed" "errors" "fmt" @@ -72,7 +73,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if enabled := c.initConfig(); !enabled { return errors.New("remote control is disabled in the configuration file") } @@ -91,7 +92,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -106,7 +107,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -118,7 +119,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.client != nil { _ = c.client.Disconnect() } diff --git a/src/go/plugin/go.d/collector/unbound/collector_test.go b/src/go/plugin/go.d/collector/unbound/collector_test.go index 57e9422d9a34ee..691ea137616d40 100644 --- a/src/go/plugin/go.d/collector/unbound/collector_test.go +++ b/src/go/plugin/go.d/collector/unbound/collector_test.go @@ -5,6 +5,7 @@ package unbound import ( "bufio" "bytes" + "context" "errors" "fmt" "os" @@ -57,7 +58,7 @@ func TestCollector_ConfigurationSerialize(t *testing.T) { func TestCollector_Init(t *testing.T) { collr := prepareNonTLSUnbound() - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } func TestCollector_Init_SetEverythingFromUnboundConf(t *testing.T) { @@ -76,7 +77,7 @@ func TestCollector_Init_SetEverythingFromUnboundConf(t *testing.T) { }, } - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) assert.Equal(t, expectedConfig, collr.Config) } @@ -84,66 +85,66 @@ func TestCollector_Init_DisabledInUnboundConf(t *testing.T) { collr := prepareNonTLSUnbound() collr.ConfPath = "testdata/unbound_disabled.conf" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Init_HandleEmptyConfig(t *testing.T) { collr := prepareNonTLSUnbound() collr.ConfPath = "testdata/unbound_empty.conf" - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } func TestCollector_Init_HandleNonExistentConfig(t *testing.T) { collr := prepareNonTLSUnbound() collr.ConfPath = "testdata/unbound_non_existent.conf" - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } func TestCollector_Check(t *testing.T) { collr := prepareNonTLSUnbound() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.client = mockUnboundClient{data: dataCommonStats, err: false} - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } func TestCollector_Check_ErrorDuringScrapingUnbound(t *testing.T) { collr := prepareNonTLSUnbound() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.client = mockUnboundClient{err: true} - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_Cleanup(t *testing.T) { - New().Cleanup() + New().Cleanup(context.Background()) } func TestCollector_Charts(t *testing.T) { collr := prepareNonTLSUnbound() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.Charts()) } func TestCollector_Collect(t *testing.T) { collr := prepareNonTLSUnbound() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.client = mockUnboundClient{data: dataCommonStats, err: false} - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, expectedCommon, mx) testCharts(t, collr, mx) } func TestCollector_Collect_ExtendedStats(t *testing.T) { collr := prepareNonTLSUnbound() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.client = mockUnboundClient{data: dataExtendedStats, err: false} - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, expectedExtended, mx) testCharts(t, collr, mx) } @@ -160,7 +161,7 @@ func TestCollector_Collect_LifeCycleCumulativeExtendedStats(t *testing.T) { collr := prepareNonTLSUnbound() collr.Cumulative = true - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) ubClient := &mockUnboundClient{err: false} collr.client = ubClient @@ -168,7 +169,7 @@ func TestCollector_Collect_LifeCycleCumulativeExtendedStats(t *testing.T) { for i, test := range tests { t.Run(fmt.Sprintf("run %d", i+1), func(t *testing.T) { ubClient.data = test.input - mx = collr.Collect() + mx = collr.Collect(context.Background()) assert.Equal(t, test.expected, mx) }) } @@ -188,7 +189,7 @@ func TestCollector_Collect_LifeCycleResetExtendedStats(t *testing.T) { collr := prepareNonTLSUnbound() collr.Cumulative = false - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) ubClient := &mockUnboundClient{err: false} collr.client = ubClient @@ -196,7 +197,7 @@ func TestCollector_Collect_LifeCycleResetExtendedStats(t *testing.T) { for i, test := range tests { t.Run(fmt.Sprintf("run %d", i+1), func(t *testing.T) { ubClient.data = test.input - mx = collr.Collect() + mx = collr.Collect(context.Background()) assert.Equal(t, test.expected, mx) }) } @@ -206,35 +207,35 @@ func TestCollector_Collect_LifeCycleResetExtendedStats(t *testing.T) { func TestCollector_Collect_EmptyResponse(t *testing.T) { collr := prepareNonTLSUnbound() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.client = mockUnboundClient{data: []byte{}, err: false} - assert.Nil(t, collr.Collect()) + assert.Nil(t, collr.Collect(context.Background())) } func TestCollector_Collect_ErrorResponse(t *testing.T) { collr := prepareNonTLSUnbound() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.client = mockUnboundClient{data: []byte("error unknown command 'unknown'"), err: false} - assert.Nil(t, collr.Collect()) + assert.Nil(t, collr.Collect(context.Background())) } func TestCollector_Collect_ErrorOnSend(t *testing.T) { collr := prepareNonTLSUnbound() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.client = mockUnboundClient{err: true} - assert.Nil(t, collr.Collect()) + assert.Nil(t, collr.Collect(context.Background())) } func TestCollector_Collect_ErrorOnParseBadSyntax(t *testing.T) { collr := prepareNonTLSUnbound() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) data := strings.Repeat("zk_avg_latency 0\nzk_min_latency 0\nzk_mix_latency 0\n", 10) collr.client = mockUnboundClient{data: []byte(data), err: false} - assert.Nil(t, collr.Collect()) + assert.Nil(t, collr.Collect(context.Background())) } func prepareNonTLSUnbound() *Collector { diff --git a/src/go/plugin/go.d/collector/upsd/collector.go b/src/go/plugin/go.d/collector/upsd/collector.go index 3f7a44ce3775ec..164be2f38d2814 100644 --- a/src/go/plugin/go.d/collector/upsd/collector.go +++ b/src/go/plugin/go.d/collector/upsd/collector.go @@ -3,6 +3,7 @@ package upsd import ( + "context" _ "embed" "errors" "time" @@ -58,7 +59,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.Address == "" { return errors.New("config: 'address' not set") } @@ -66,7 +67,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -81,7 +82,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -93,7 +94,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.conn == nil { return } diff --git a/src/go/plugin/go.d/collector/upsd/collector_test.go b/src/go/plugin/go.d/collector/upsd/collector_test.go index 5c96e9ff6c6e2c..ab043d8ae7c1a9 100644 --- a/src/go/plugin/go.d/collector/upsd/collector_test.go +++ b/src/go/plugin/go.d/collector/upsd/collector_test.go @@ -3,6 +3,7 @@ package upsd import ( + "context" "errors" "fmt" "os" @@ -33,16 +34,16 @@ func TestCollector_ConfigurationSerialize(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - upcollrd := New() + collr := New() - require.NotPanics(t, upcollrd.Cleanup) + require.NotPanics(t, func() { collr.Cleanup(context.Background()) }) mock := prepareMockConnOK() - upcollrd.newUpsdConn = func(Config) upsdConn { return mock } + collr.newUpsdConn = func(Config) upsdConn { return mock } - require.NoError(t, upcollrd.Init()) - _ = upcollrd.Collect() - require.NotPanics(t, upcollrd.Cleanup) + require.NoError(t, collr.Init(context.Background())) + _ = collr.Collect(context.Background()) + require.NotPanics(t, func() { collr.Cleanup(context.Background()) }) assert.True(t, mock.calledDisconnect) } @@ -67,9 +68,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -113,12 +114,12 @@ func TestCollector_Check(t *testing.T) { collr := test.prepareUpsd() collr.newUpsdConn = func(Config) upsdConn { return test.prepareMock() } - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -126,7 +127,7 @@ func TestCollector_Check(t *testing.T) { func TestCollector_Charts(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.Charts()) } @@ -246,12 +247,12 @@ func TestCollector_Collect(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { collr := test.prepareUpsd() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) mock := test.prepareMock() collr.newUpsdConn = func(Config) upsdConn { return mock } - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantCollected, mx) assert.Equalf(t, test.wantCharts, len(*collr.Charts()), "number of charts") diff --git a/src/go/plugin/go.d/collector/uwsgi/collector.go b/src/go/plugin/go.d/collector/uwsgi/collector.go index 88d18574b0c393..8f0ad88b0f2078 100644 --- a/src/go/plugin/go.d/collector/uwsgi/collector.go +++ b/src/go/plugin/go.d/collector/uwsgi/collector.go @@ -3,6 +3,7 @@ package uwsgi import ( + "context" _ "embed" "errors" "time" @@ -54,7 +55,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.Address == "" { return errors.New("config: 'address' not set") } @@ -64,7 +65,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -81,7 +82,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -94,4 +95,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/uwsgi/collector_test.go b/src/go/plugin/go.d/collector/uwsgi/collector_test.go index 996d1938f11c76..2c6ba11666254a 100644 --- a/src/go/plugin/go.d/collector/uwsgi/collector_test.go +++ b/src/go/plugin/go.d/collector/uwsgi/collector_test.go @@ -3,6 +3,7 @@ package uwsgi import ( + "context" "errors" "os" "testing" @@ -61,9 +62,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -82,7 +83,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.conn = prepareMockOk() - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -90,7 +91,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.conn = prepareMockOk() - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -100,7 +101,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -139,9 +140,9 @@ func TestCollector_Check(t *testing.T) { collr.conn = mock if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -239,7 +240,7 @@ func TestCollector_Collect(t *testing.T) { mock := test.prepareMock() collr.conn = mock - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) diff --git a/src/go/plugin/go.d/collector/varnish/collector.go b/src/go/plugin/go.d/collector/varnish/collector.go index 4692de81f5fda4..3be9123fcbef62 100644 --- a/src/go/plugin/go.d/collector/varnish/collector.go +++ b/src/go/plugin/go.d/collector/varnish/collector.go @@ -3,6 +3,7 @@ package varnish import ( + "context" _ "embed" "errors" "fmt" @@ -62,7 +63,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { vs, err := c.initVarnishstatBinary() if err != nil { return fmt.Errorf("init varnishstat exec: %v", err) @@ -72,7 +73,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -89,7 +90,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -102,4 +103,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/varnish/collector_test.go b/src/go/plugin/go.d/collector/varnish/collector_test.go index 80599241d6c055..c6d354d07a9a23 100644 --- a/src/go/plugin/go.d/collector/varnish/collector_test.go +++ b/src/go/plugin/go.d/collector/varnish/collector_test.go @@ -3,6 +3,7 @@ package varnish import ( + "context" "errors" "os" "testing" @@ -51,9 +52,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -72,7 +73,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOkVer71() - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -80,7 +81,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOkVer71() - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -90,7 +91,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -124,9 +125,9 @@ func TestCollector_Check(t *testing.T) { collr.exec = test.prepareMock() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -206,7 +207,7 @@ func TestCollector_Collect(t *testing.T) { collr := New() collr.exec = test.prepareMock() - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantMetrics, mx) diff --git a/src/go/plugin/go.d/collector/vcsa/collector.go b/src/go/plugin/go.d/collector/vcsa/collector.go index 44893b11026fce..2f293516f9c148 100644 --- a/src/go/plugin/go.d/collector/vcsa/collector.go +++ b/src/go/plugin/go.d/collector/vcsa/collector.go @@ -3,6 +3,7 @@ package vcsa import ( + "context" _ "embed" "errors" "fmt" @@ -74,7 +75,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("invalid config: %v", err) } @@ -91,7 +92,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { err := c.client.Login() if err != nil { return err @@ -113,7 +114,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -125,7 +126,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.client == nil { return } diff --git a/src/go/plugin/go.d/collector/vcsa/collector_test.go b/src/go/plugin/go.d/collector/vcsa/collector_test.go index fd3acb96a2b80c..730759246e5860 100644 --- a/src/go/plugin/go.d/collector/vcsa/collector_test.go +++ b/src/go/plugin/go.d/collector/vcsa/collector_test.go @@ -3,6 +3,7 @@ package vcsa import ( + "context" "errors" "os" "testing" @@ -34,57 +35,57 @@ func TestCollector_ConfigurationSerialize(t *testing.T) { func TestCollector_Init(t *testing.T) { collr := prepareVCSA() - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.client) } func TestCollector_InitErrorOnValidatingInitParameters(t *testing.T) { collr := New() - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_InitErrorOnCreatingClient(t *testing.T) { collr := prepareVCSA() collr.ClientConfig.TLSConfig.TLSCA = "testdata/tls" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Check(t *testing.T) { collr := prepareVCSA() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.client = &mockVCenterHealthClient{} - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } func TestCollector_CheckErrorOnLogin(t *testing.T) { collr := prepareVCSA() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.client = &mockVCenterHealthClient{ login: func() error { return errors.New("login mock error") }, } - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_CheckEnsureLoggedIn(t *testing.T) { collr := prepareVCSA() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) mock := &mockVCenterHealthClient{} collr.client = mock - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) assert.True(t, mock.loginCalls == 1) } func TestCollector_Cleanup(t *testing.T) { collr := prepareVCSA() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) mock := &mockVCenterHealthClient{} collr.client = mock - collr.Cleanup() + collr.Cleanup(context.Background()) assert.True(t, mock.logoutCalls == 1) } @@ -92,7 +93,7 @@ func TestCollector_Cleanup(t *testing.T) { func TestCollector_CleanupWithNilClient(t *testing.T) { collr := prepareVCSA() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) } func TestCollector_Charts(t *testing.T) { @@ -101,7 +102,7 @@ func TestCollector_Charts(t *testing.T) { func TestCollector_Collect(t *testing.T) { collr := prepareVCSA() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) mock := &mockVCenterHealthClient{} collr.client = mock @@ -155,33 +156,33 @@ func TestCollector_Collect(t *testing.T) { "system_status_yellow": 0, } - assert.Equal(t, expected, collr.Collect()) + assert.Equal(t, expected, collr.Collect(context.Background())) } func TestCollector_CollectEnsurePingIsCalled(t *testing.T) { collr := prepareVCSA() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) mock := &mockVCenterHealthClient{} collr.client = mock - collr.Collect() + collr.Collect(context.Background()) assert.True(t, mock.pingCalls == 1) } func TestCollector_CollectErrorOnPing(t *testing.T) { collr := prepareVCSA() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) mock := &mockVCenterHealthClient{ ping: func() error { return errors.New("ping mock error") }, } collr.client = mock - assert.Zero(t, collr.Collect()) + assert.Zero(t, collr.Collect(context.Background())) } func TestCollector_CollectErrorOnHealthCalls(t *testing.T) { collr := prepareVCSA() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) mock := &mockVCenterHealthClient{ applMgmt: func() (string, error) { return "", errors.New("applMgmt mock error") }, databaseStorage: func() (string, error) { return "", errors.New("databaseStorage mock error") }, @@ -194,7 +195,7 @@ func TestCollector_CollectErrorOnHealthCalls(t *testing.T) { } collr.client = mock - assert.Zero(t, collr.Collect()) + assert.Zero(t, collr.Collect(context.Background())) } func prepareVCSA() *Collector { diff --git a/src/go/plugin/go.d/collector/vernemq/collector.go b/src/go/plugin/go.d/collector/vernemq/collector.go index 3161821c8ac614..32556e1f39682d 100644 --- a/src/go/plugin/go.d/collector/vernemq/collector.go +++ b/src/go/plugin/go.d/collector/vernemq/collector.go @@ -3,6 +3,7 @@ package vernemq import ( + "context" _ "embed" "errors" "fmt" @@ -67,7 +68,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -81,7 +82,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -98,7 +99,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -111,7 +112,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.prom != nil && c.prom.HTTPClient() != nil { c.prom.HTTPClient().CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/vernemq/collector_test.go b/src/go/plugin/go.d/collector/vernemq/collector_test.go index b8c48b776f5945..31342bc03b7bfb 100644 --- a/src/go/plugin/go.d/collector/vernemq/collector_test.go +++ b/src/go/plugin/go.d/collector/vernemq/collector_test.go @@ -3,6 +3,7 @@ package vernemq import ( + "context" "net/http" "net/http/httptest" "os" @@ -63,9 +64,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -76,7 +77,7 @@ func TestCollector_Charts(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Check(t *testing.T) { @@ -116,9 +117,9 @@ func TestCollector_Check(t *testing.T) { defer cleanup() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -642,7 +643,7 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare(t) defer cleanup() - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, test.wantMetrics, mx) @@ -664,7 +665,7 @@ func caseOkVer201(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -678,7 +679,7 @@ func caseOkVer1101(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -703,7 +704,7 @@ wmi_os_processes_limit 4.294967295e+09 collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -716,7 +717,7 @@ func caseInvalidDataResponse(t *testing.T) (*Collector, func()) { })) collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } @@ -725,7 +726,7 @@ func caseConnectionRefused(t *testing.T) (*Collector, func()) { t.Helper() collr := New() collr.URL = "http://127.0.0.1:65001" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, func() {} } @@ -739,7 +740,7 @@ func case404(t *testing.T) (*Collector, func()) { collr := New() collr.URL = srv.URL - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) return collr, srv.Close } diff --git a/src/go/plugin/go.d/collector/vsphere/collector.go b/src/go/plugin/go.d/collector/vsphere/collector.go index 8b5eb66fbf9a8c..3a197a8477e6fd 100644 --- a/src/go/plugin/go.d/collector/vsphere/collector.go +++ b/src/go/plugin/go.d/collector/vsphere/collector.go @@ -3,6 +3,7 @@ package vsphere import ( + "context" _ "embed" "fmt" "sync" @@ -89,7 +90,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("error on validating config: %v", err) } @@ -114,7 +115,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { return nil } @@ -122,7 +123,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -134,7 +135,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.discoveryTask == nil { return } diff --git a/src/go/plugin/go.d/collector/vsphere/collector_test.go b/src/go/plugin/go.d/collector/vsphere/collector_test.go index 41eaf0b1ee8552..d4fff2edc5a224 100644 --- a/src/go/plugin/go.d/collector/vsphere/collector_test.go +++ b/src/go/plugin/go.d/collector/vsphere/collector_test.go @@ -2,6 +2,7 @@ package vsphere import ( + "context" "crypto/tls" "os" "strings" @@ -42,7 +43,7 @@ func TestCollector_Init(t *testing.T) { collr, _, teardown := prepareVSphereSim(t) defer teardown() - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.discoverer) assert.NotNil(t, collr.scraper) assert.NotNil(t, collr.resources) @@ -55,7 +56,7 @@ func TestCollector_Init_ReturnsFalseIfURLNotSet(t *testing.T) { defer teardown() collr.URL = "" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Init_ReturnsFalseIfUsernameNotSet(t *testing.T) { @@ -63,7 +64,7 @@ func TestCollector_Init_ReturnsFalseIfUsernameNotSet(t *testing.T) { defer teardown() collr.Username = "" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Init_ReturnsFalseIfPasswordNotSet(t *testing.T) { @@ -71,7 +72,7 @@ func TestCollector_Init_ReturnsFalseIfPasswordNotSet(t *testing.T) { defer teardown() collr.Password = "" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Init_ReturnsFalseIfClientWrongTLSCA(t *testing.T) { @@ -79,7 +80,7 @@ func TestCollector_Init_ReturnsFalseIfClientWrongTLSCA(t *testing.T) { defer teardown() collr.ClientConfig.TLSConfig.TLSCA = "testdata/tls" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Init_ReturnsFalseIfConnectionRefused(t *testing.T) { @@ -87,7 +88,7 @@ func TestCollector_Init_ReturnsFalseIfConnectionRefused(t *testing.T) { defer teardown() collr.URL = "http://127.0.0.1:32001" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Init_ReturnsFalseIfInvalidHostVMIncludeFormat(t *testing.T) { @@ -95,16 +96,16 @@ func TestCollector_Init_ReturnsFalseIfInvalidHostVMIncludeFormat(t *testing.T) { defer teardown() collr.HostsInclude = match.HostIncludes{"invalid"} - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) collr.HostsInclude = collr.HostsInclude[:0] collr.VMsInclude = match.VMIncludes{"invalid"} - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Check(t *testing.T) { - assert.NoError(t, New().Check()) + assert.NoError(t, New().Check(context.Background())) } func TestCollector_Charts(t *testing.T) { @@ -115,23 +116,23 @@ func TestCollector_Cleanup(t *testing.T) { collr, _, teardown := prepareVSphereSim(t) defer teardown() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - collr.Cleanup() + collr.Cleanup(context.Background()) time.Sleep(time.Second) assert.True(t, collr.discoveryTask.isStopped()) assert.False(t, collr.discoveryTask.isRunning()) } func TestCollector_Cleanup_NotPanicsIfNotInitialized(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Collect(t *testing.T) { collr, model, teardown := prepareVSphereSim(t) defer teardown() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.scraper = mockScraper{collr.scraper} @@ -330,7 +331,7 @@ func TestCollector_Collect(t *testing.T) { "vm-72_sys.uptime.latest": 200, } - mx := collr.Collect() + mx := collr.Collect(context.Background()) require.Equal(t, expected, mx) @@ -347,8 +348,8 @@ func TestCollector_Collect_RemoveHostsVMsInRuntime(t *testing.T) { collr, _, teardown := prepareVSphereSim(t) defer teardown() - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) okHostID := "host-58" okVMID := "vm-63" @@ -359,7 +360,7 @@ func TestCollector_Collect_RemoveHostsVMsInRuntime(t *testing.T) { numOfRuns := 5 for i := 0; i < numOfRuns; i++ { - collr.Collect() + collr.Collect(context.Background()) } host := collr.resources.Hosts.Get(okHostID) @@ -382,7 +383,7 @@ func TestCollector_Collect_RemoveHostsVMsInRuntime(t *testing.T) { } for i := numOfRuns; i < failedUpdatesLimit; i++ { - collr.Collect() + collr.Collect(context.Background()) } assert.Len(t, collr.discoveredHosts, 1) @@ -403,12 +404,12 @@ func TestCollector_Collect_Run(t *testing.T) { defer teardown() collr.DiscoveryInterval = confopt.Duration(time.Second * 2) - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) runs := 20 for i := 0; i < runs; i++ { - assert.True(t, len(collr.Collect()) > 0) + assert.True(t, len(collr.Collect(context.Background())) > 0) if i < 6 { time.Sleep(time.Second) } @@ -424,7 +425,7 @@ func TestCollector_Collect_Run(t *testing.T) { func prepareVSphereSim(t *testing.T) (collr *Collector, model *simulator.Model, teardown func()) { model, srv := createSim(t) collr = New() - teardown = func() { model.Remove(); srv.Close(); collr.Cleanup() } + teardown = func() { model.Remove(); srv.Close(); collr.Cleanup(context.Background()) } collr.Username = "administrator" collr.Password = "password" diff --git a/src/go/plugin/go.d/collector/w1sensor/collector.go b/src/go/plugin/go.d/collector/w1sensor/collector.go index f5989339fd06cf..78de388caf542d 100644 --- a/src/go/plugin/go.d/collector/w1sensor/collector.go +++ b/src/go/plugin/go.d/collector/w1sensor/collector.go @@ -5,6 +5,7 @@ package w1sensor import ( + "context" _ "embed" "errors" @@ -53,7 +54,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if c.SensorsPath == "" { return errors.New("config: no sensors path specified") } @@ -61,7 +62,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -78,7 +79,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -91,4 +92,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/w1sensor/collector_test.go b/src/go/plugin/go.d/collector/w1sensor/collector_test.go index 8849a0cb2adf8b..24b5992e0f4f67 100644 --- a/src/go/plugin/go.d/collector/w1sensor/collector_test.go +++ b/src/go/plugin/go.d/collector/w1sensor/collector_test.go @@ -5,6 +5,7 @@ package w1sensor import ( + "context" "os" "testing" @@ -51,9 +52,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -71,14 +72,14 @@ func TestAP_Cleanup(t *testing.T) { "after check": { prepare: func() *Collector { collr := prepareCaseOk() - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, "after collect": { prepare: func() *Collector { collr := prepareCaseOk() - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -88,7 +89,7 @@ func TestAP_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -117,9 +118,9 @@ func TestCollector_Check(t *testing.T) { collr := test.prepareMock() if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -151,7 +152,7 @@ func TestW1Sensors_Collect(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepareMock() - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantMetrics, mx) diff --git a/src/go/plugin/go.d/collector/weblog/collector.go b/src/go/plugin/go.d/collector/weblog/collector.go index 43d0659bba941f..f76d78fc87b3c5 100644 --- a/src/go/plugin/go.d/collector/weblog/collector.go +++ b/src/go/plugin/go.d/collector/weblog/collector.go @@ -3,6 +3,7 @@ package weblog import ( + "context" _ "embed" "fmt" @@ -100,7 +101,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.createURLPatterns(); err != nil { return fmt.Errorf("init failed: %v", err) } @@ -123,7 +124,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { // Note: these inits are here to make auto-detection retry working if err := c.createLogReader(); err != nil { return fmt.Errorf("failed to create log reader: %v", err) @@ -144,7 +145,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -156,7 +157,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.file != nil { _ = c.file.Close() } diff --git a/src/go/plugin/go.d/collector/weblog/collector_test.go b/src/go/plugin/go.d/collector/weblog/collector_test.go index e65da9991bb14b..5cb5c124dc031f 100644 --- a/src/go/plugin/go.d/collector/weblog/collector_test.go +++ b/src/go/plugin/go.d/collector/weblog/collector_test.go @@ -4,6 +4,7 @@ package weblog import ( "bytes" + "context" "fmt" "os" "reflect" @@ -51,73 +52,73 @@ func TestCollector_ConfigurationSerialize(t *testing.T) { func TestCollector_Init(t *testing.T) { collr := New() - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } func TestCollector_Init_ErrorOnCreatingURLPatterns(t *testing.T) { collr := New() collr.URLPatterns = []userPattern{{Match: "* !*"}} - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Init_ErrorOnCreatingCustomFields(t *testing.T) { collr := New() collr.CustomFields = []customField{{Patterns: []userPattern{{Name: "p1", Match: "* !*"}}}} - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Check(t *testing.T) { collr := New() - defer collr.Cleanup() + defer collr.Cleanup(context.Background()) collr.Path = "testdata/common.log" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } func TestCollector_Check_ErrorOnCreatingLogReaderNoLogFile(t *testing.T) { collr := New() - defer collr.Cleanup() + defer collr.Cleanup(context.Background()) collr.Path = "testdata/not_exists.log" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_Check_ErrorOnCreatingParserUnknownFormat(t *testing.T) { collr := New() - defer collr.Cleanup() + defer collr.Cleanup(context.Background()) collr.Path = "testdata/custom.log" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_Check_ErrorOnCreatingParserEmptyLine(t *testing.T) { collr := New() - defer collr.Cleanup() + defer collr.Cleanup(context.Background()) collr.Path = "testdata/custom.log" collr.ParserConfig.LogType = logs.TypeCSV collr.ParserConfig.CSV.Format = "$one $two" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_Charts(t *testing.T) { collr := New() - defer collr.Cleanup() + defer collr.Cleanup(context.Background()) collr.Path = "testdata/common.log" - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) assert.NotNil(t, collr.Charts()) } func TestCollector_Cleanup(t *testing.T) { - New().Cleanup() + New().Cleanup(context.Background()) } func TestCollector_Collect(t *testing.T) { @@ -312,7 +313,7 @@ func TestCollector_Collect(t *testing.T) { "url_ptn_org_resp_code_401": 9, } - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, expected, mx) testCharts(t, collr, mx) } @@ -392,7 +393,7 @@ func TestCollector_Collect_CommonLogFormat(t *testing.T) { "upstream_resp_time_sum": 0, } - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, expected, mx) testCharts(t, collr, mx) } @@ -462,7 +463,7 @@ func TestCollector_Collect_CustomLogs(t *testing.T) { "upstream_resp_time_sum": 0, } - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, expected, mx) testCharts(t, collr, mx) } @@ -564,7 +565,7 @@ func TestCollector_Collect_CustomTimeFieldsLogs(t *testing.T) { "upstream_resp_time_sum": 0, } - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, expected, mx) testCharts(t, collr, mx) } @@ -640,7 +641,7 @@ func TestCollector_Collect_CustomNumericFieldsLogs(t *testing.T) { "upstream_resp_time_sum": 0, } - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, expected, mx) testCharts(t, collr, mx) @@ -714,7 +715,7 @@ func TestCollector_IISLogs(t *testing.T) { "upstream_resp_time_sum": 0, } - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, expected, mx) } @@ -1187,9 +1188,9 @@ func prepareWebLogCollectFull(t *testing.T) *Collector { } collr := New() collr.Config = cfg - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) - defer collr.Cleanup() + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) + defer collr.Cleanup(context.Background()) p, err := logs.NewCSVParser(collr.ParserConfig.CSV, bytes.NewReader(dataFullLog)) require.NoError(t, err) @@ -1230,9 +1231,9 @@ func prepareWebLogCollectCommon(t *testing.T) *Collector { collr := New() collr.Config = cfg - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) - defer collr.Cleanup() + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) + defer collr.Cleanup(context.Background()) p, err := logs.NewCSVParser(collr.ParserConfig.CSV, bytes.NewReader(dataCommonLog)) require.NoError(t, err) @@ -1282,9 +1283,9 @@ func prepareWebLogCollectCustom(t *testing.T) *Collector { } collr := New() collr.Config = cfg - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) - defer collr.Cleanup() + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) + defer collr.Cleanup(context.Background()) p, err := logs.NewCSVParser(collr.ParserConfig.CSV, bytes.NewReader(dataCustomLog)) require.NoError(t, err) @@ -1328,9 +1329,9 @@ func prepareWebLogCollectCustomTimeFields(t *testing.T) *Collector { } collr := New() collr.Config = cfg - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) - defer collr.Cleanup() + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) + defer collr.Cleanup(context.Background()) p, err := logs.NewCSVParser(collr.ParserConfig.CSV, bytes.NewReader(dataCustomTimeFieldLog)) require.NoError(t, err) @@ -1374,9 +1375,9 @@ func prepareWebLogCollectCustomNumericFields(t *testing.T) *Collector { } collr := New() collr.Config = cfg - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) - defer collr.Cleanup() + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) + defer collr.Cleanup(context.Background()) p, err := logs.NewCSVParser(collr.ParserConfig.CSV, bytes.NewReader(dataCustomTimeFieldLog)) require.NoError(t, err) @@ -1424,9 +1425,9 @@ func prepareWebLogCollectIISFields(t *testing.T) *Collector { collr := New() collr.Config = cfg - require.NoError(t, collr.Init()) - require.NoError(t, collr.Check()) - defer collr.Cleanup() + require.NoError(t, collr.Init(context.Background())) + require.NoError(t, collr.Check(context.Background())) + defer collr.Cleanup(context.Background()) p, err := logs.NewCSVParser(collr.ParserConfig.CSV, bytes.NewReader(dataIISLog)) require.NoError(t, err) diff --git a/src/go/plugin/go.d/collector/weblog/init.go b/src/go/plugin/go.d/collector/weblog/init.go index 66b52f7523cf08..720dbe91f557e7 100644 --- a/src/go/plugin/go.d/collector/weblog/init.go +++ b/src/go/plugin/go.d/collector/weblog/init.go @@ -3,6 +3,7 @@ package weblog import ( + "context" "errors" "fmt" "strings" @@ -136,7 +137,7 @@ func (c *Collector) createLogLine() { } func (c *Collector) createLogReader() error { - c.Cleanup() + c.Cleanup(context.Background()) c.Debug("starting log reader creating") reader, err := logs.Open(c.Path, c.ExcludePath, c.Logger) diff --git a/src/go/plugin/go.d/collector/whoisquery/collector.go b/src/go/plugin/go.d/collector/whoisquery/collector.go index f805932a79539c..820c2329610a3d 100644 --- a/src/go/plugin/go.d/collector/whoisquery/collector.go +++ b/src/go/plugin/go.d/collector/whoisquery/collector.go @@ -3,6 +3,7 @@ package whoisquery import ( + "context" _ "embed" "errors" "fmt" @@ -57,7 +58,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -73,7 +74,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -88,7 +89,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -100,4 +101,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/whoisquery/collector_test.go b/src/go/plugin/go.d/collector/whoisquery/collector_test.go index 0a1969e22e088a..486b9e053daa92 100644 --- a/src/go/plugin/go.d/collector/whoisquery/collector_test.go +++ b/src/go/plugin/go.d/collector/whoisquery/collector_test.go @@ -3,6 +3,7 @@ package whoisquery import ( + "context" "errors" "os" "testing" @@ -32,13 +33,13 @@ func TestCollector_ConfigurationSerialize(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - New().Cleanup() + New().Cleanup(context.Background()) } func TestCollector_Charts(t *testing.T) { collr := New() collr.Source = "example.com" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.Charts()) } @@ -66,9 +67,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.err { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) var typeOK bool if test.providerType == net { @@ -85,23 +86,23 @@ func TestCollector_Check(t *testing.T) { collr := New() collr.prov = &mockProvider{remTime: 12345.678} - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } func TestCollector_Check_ReturnsFalseOnProviderError(t *testing.T) { collr := New() collr.prov = &mockProvider{err: true} - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_Collect(t *testing.T) { collr := New() collr.Source = "example.com" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.prov = &mockProvider{remTime: 12345} - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "expiry": 12345, @@ -117,10 +118,10 @@ func TestCollector_Collect(t *testing.T) { func TestCollector_Collect_ReturnsNilOnProviderError(t *testing.T) { collr := New() collr.Source = "example.com" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.prov = &mockProvider{err: true} - assert.Nil(t, collr.Collect()) + assert.Nil(t, collr.Collect(context.Background())) } type mockProvider struct { diff --git a/src/go/plugin/go.d/collector/windows/collector.go b/src/go/plugin/go.d/collector/windows/collector.go index 385d10271bd05b..d904cd43a59a0b 100644 --- a/src/go/plugin/go.d/collector/windows/collector.go +++ b/src/go/plugin/go.d/collector/windows/collector.go @@ -3,6 +3,7 @@ package windows import ( + "context" _ "embed" "errors" "fmt" @@ -122,7 +123,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -136,7 +137,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -151,7 +152,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { ms, err := c.collect() if err != nil { c.Error(err) @@ -163,7 +164,7 @@ func (c *Collector) Collect() map[string]int64 { return ms } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.prom != nil && c.prom.HTTPClient() != nil { c.prom.HTTPClient().CloseIdleConnections() } diff --git a/src/go/plugin/go.d/collector/windows/collector_test.go b/src/go/plugin/go.d/collector/windows/collector_test.go index 4759b2560133bf..839f9a0662b41a 100644 --- a/src/go/plugin/go.d/collector/windows/collector_test.go +++ b/src/go/plugin/go.d/collector/windows/collector_test.go @@ -3,6 +3,7 @@ package windows import ( + "context" "fmt" "net/http" "net/http/httptest" @@ -67,9 +68,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -102,12 +103,12 @@ func TestCollector_Check(t *testing.T) { collr, cleanup := test.prepare() defer cleanup() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -118,7 +119,7 @@ func TestCollector_Charts(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Collect(t *testing.T) { @@ -799,9 +800,9 @@ func TestCollector_Collect(t *testing.T) { collr, cleanup := test.prepare() defer cleanup() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) - mx := collr.Collect() + mx := collr.Collect(context.Background()) if mx != nil && test.wantCollected != nil { mx["system_up_time"] = test.wantCollected["system_up_time"] diff --git a/src/go/plugin/go.d/collector/wireguard/collector.go b/src/go/plugin/go.d/collector/wireguard/collector.go index 832d776e75756c..c46b32a3d56bee 100644 --- a/src/go/plugin/go.d/collector/wireguard/collector.go +++ b/src/go/plugin/go.d/collector/wireguard/collector.go @@ -3,6 +3,7 @@ package wireguard import ( + "context" _ "embed" "errors" "time" @@ -63,11 +64,11 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -82,7 +83,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -94,7 +95,7 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() { +func (c *Collector) Cleanup(context.Context) { if c.client == nil { return } diff --git a/src/go/plugin/go.d/collector/wireguard/collector_test.go b/src/go/plugin/go.d/collector/wireguard/collector_test.go index 0f520ce5d6adff..0a21f775c2a67b 100644 --- a/src/go/plugin/go.d/collector/wireguard/collector_test.go +++ b/src/go/plugin/go.d/collector/wireguard/collector_test.go @@ -3,6 +3,7 @@ package wireguard import ( + "context" "errors" "fmt" "os" @@ -36,7 +37,7 @@ func TestCollector_ConfigurationSerialize(t *testing.T) { } func TestCollector_Init(t *testing.T) { - assert.NoError(t, New().Init()) + assert.NoError(t, New().Init(context.Background())) } func TestCollector_Charts(t *testing.T) { @@ -55,15 +56,15 @@ func TestCollector_Cleanup(t *testing.T) { }, "after Init": { wantClose: false, - prepare: func(c *Collector) { _ = c.Init() }, + prepare: func(c *Collector) { _ = c.Init(context.Background()) }, }, "after Check": { wantClose: true, - prepare: func(c *Collector) { _ = c.Init(); _ = c.Check() }, + prepare: func(c *Collector) { _ = c.Init(context.Background()); _ = c.Check(context.Background()) }, }, "after Collect": { wantClose: true, - prepare: func(c *Collector) { _ = c.Init(); _ = c.Collect() }, + prepare: func(c *Collector) { _ = c.Init(context.Background()); _ = c.Collect(context.Background()) }, }, } @@ -75,7 +76,7 @@ func TestCollector_Cleanup(t *testing.T) { test.prepare(collr) - require.NotPanics(t, collr.Cleanup) + require.NotPanics(t, func() { collr.Cleanup(context.Background()) }) if test.wantClose { assert.True(t, m.closeCalled) @@ -133,13 +134,13 @@ func TestCollector_Check(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) test.prepare(collr) if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -158,7 +159,7 @@ func TestCollector_Collect(t *testing.T) { m.devices = append(m.devices, prepareDevice(2)) }, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "device_wg1_peers": 0, @@ -189,7 +190,7 @@ func TestCollector_Collect(t *testing.T) { m.devices = append(m.devices, d2) }, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "device_wg1_peers": 2, @@ -229,7 +230,7 @@ func TestCollector_Collect(t *testing.T) { m.devices = append(m.devices, d1) }, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "device_wg1_peers": 4, @@ -255,7 +256,7 @@ func TestCollector_Collect(t *testing.T) { m.devices = append(m.devices, prepareDevice(1)) }, check: func(t *testing.T, collr *Collector) { - _ = collr.Collect() + _ = collr.Collect(context.Background()) assert.Equal(t, len(deviceChartsTmpl)*1, len(*collr.Charts())) }, }, @@ -264,7 +265,7 @@ func TestCollector_Collect(t *testing.T) { m.devices = append(m.devices, prepareDevice(2)) }, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "device_wg1_peers": 0, @@ -288,7 +289,7 @@ func TestCollector_Collect(t *testing.T) { m.devices = append(m.devices, prepareDevice(2)) }, check: func(t *testing.T, collr *Collector) { - _ = collr.Collect() + _ = collr.Collect(context.Background()) }, }, { @@ -296,7 +297,7 @@ func TestCollector_Collect(t *testing.T) { m.devices = m.devices[:len(m.devices)-1] }, check: func(t *testing.T, collr *Collector) { - _ = collr.Collect() + _ = collr.Collect(context.Background()) assert.Equal(t, len(deviceChartsTmpl)*2, len(*collr.Charts())) assert.Equal(t, 0, calcObsoleteCharts(collr.Charts())) }, @@ -309,7 +310,7 @@ func TestCollector_Collect(t *testing.T) { m.devices = append(m.devices, prepareDevice(2)) }, check: func(t *testing.T, collr *Collector) { - _ = collr.Collect() + _ = collr.Collect(context.Background()) }, }, { @@ -319,7 +320,7 @@ func TestCollector_Collect(t *testing.T) { check: func(t *testing.T, collr *Collector) { collr.cleanupEvery = time.Second time.Sleep(time.Second) - _ = collr.Collect() + _ = collr.Collect(context.Background()) assert.Equal(t, len(deviceChartsTmpl)*2, len(*collr.Charts())) assert.Equal(t, len(deviceChartsTmpl)*1, calcObsoleteCharts(collr.Charts())) }, @@ -331,7 +332,7 @@ func TestCollector_Collect(t *testing.T) { m.devices = append(m.devices, prepareDevice(1)) }, check: func(t *testing.T, collr *Collector) { - _ = collr.Collect() + _ = collr.Collect(context.Background()) assert.Equal(t, len(deviceChartsTmpl)*1, len(*collr.Charts())) }, }, @@ -341,7 +342,7 @@ func TestCollector_Collect(t *testing.T) { d1.Peers = append(d1.Peers, preparePeer("11")) }, check: func(t *testing.T, collr *Collector) { - mx := collr.Collect() + mx := collr.Collect(context.Background()) expected := map[string]int64{ "device_wg1_peers": 1, @@ -366,7 +367,7 @@ func TestCollector_Collect(t *testing.T) { m.devices = append(m.devices, d1) }, check: func(t *testing.T, collr *Collector) { - _ = collr.Collect() + _ = collr.Collect(context.Background()) }, }, { @@ -375,7 +376,7 @@ func TestCollector_Collect(t *testing.T) { d1.Peers = d1.Peers[:len(d1.Peers)-1] }, check: func(t *testing.T, collr *Collector) { - _ = collr.Collect() + _ = collr.Collect(context.Background()) assert.Equal(t, len(deviceChartsTmpl)*1+len(peerChartsTmpl)*2, len(*collr.Charts())) assert.Equal(t, 0, calcObsoleteCharts(collr.Charts())) }, @@ -390,7 +391,7 @@ func TestCollector_Collect(t *testing.T) { m.devices = append(m.devices, d1) }, check: func(t *testing.T, collr *Collector) { - _ = collr.Collect() + _ = collr.Collect(context.Background()) }, }, { @@ -401,7 +402,7 @@ func TestCollector_Collect(t *testing.T) { check: func(t *testing.T, collr *Collector) { collr.cleanupEvery = time.Second time.Sleep(time.Second) - _ = collr.Collect() + _ = collr.Collect(context.Background()) assert.Equal(t, len(deviceChartsTmpl)*1+len(peerChartsTmpl)*2, len(*collr.Charts())) assert.Equal(t, len(peerChartsTmpl)*1, calcObsoleteCharts(collr.Charts())) }, @@ -411,7 +412,7 @@ func TestCollector_Collect(t *testing.T) { { prepareMock: func(m *mockClient) {}, check: func(t *testing.T, collr *Collector) { - assert.Equal(t, map[string]int64(nil), collr.Collect()) + assert.Equal(t, map[string]int64(nil), collr.Collect(context.Background())) }, }, }, @@ -421,7 +422,7 @@ func TestCollector_Collect(t *testing.T) { m.errOnDevices = true }, check: func(t *testing.T, collr *Collector) { - assert.Equal(t, map[string]int64(nil), collr.Collect()) + assert.Equal(t, map[string]int64(nil), collr.Collect(context.Background())) }, }, }, @@ -430,7 +431,7 @@ func TestCollector_Collect(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) m := &mockClient{} collr.client = m diff --git a/src/go/plugin/go.d/collector/x509check/collector.go b/src/go/plugin/go.d/collector/x509check/collector.go index 18df5341e5b18c..87bd51cc33eef7 100644 --- a/src/go/plugin/go.d/collector/x509check/collector.go +++ b/src/go/plugin/go.d/collector/x509check/collector.go @@ -3,6 +3,7 @@ package x509check import ( + "context" _ "embed" "errors" "fmt" @@ -66,7 +67,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %v", err) } @@ -80,7 +81,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -95,7 +96,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -107,4 +108,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/x509check/collector_test.go b/src/go/plugin/go.d/collector/x509check/collector_test.go index f8a8876976c733..0be20666c45b1b 100644 --- a/src/go/plugin/go.d/collector/x509check/collector_test.go +++ b/src/go/plugin/go.d/collector/x509check/collector_test.go @@ -3,6 +3,7 @@ package x509check import ( + "context" "crypto/x509" "errors" "os" @@ -34,13 +35,13 @@ func TestCollector_ConfigurationSerialize(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - assert.NotPanics(t, New().Cleanup) + assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) } func TestCollector_Charts(t *testing.T) { collr := New() collr.Source = "https://example.com" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.Charts()) } @@ -90,9 +91,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.err { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) var typeOK bool switch test.providerType { @@ -114,23 +115,23 @@ func TestCollector_Check(t *testing.T) { collr := New() collr.prov = &mockProvider{certs: []*x509.Certificate{{}}} - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } func TestCollector_Check_ReturnsFalseOnProviderError(t *testing.T) { collr := New() collr.prov = &mockProvider{err: true} - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_Collect(t *testing.T) { collr := New() collr.Source = "https://example.com" - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.prov = &mockProvider{certs: []*x509.Certificate{{}}} - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.NotZero(t, mx) module.TestMetricsHasAllChartsDims(t, collr.Charts(), mx) @@ -140,13 +141,13 @@ func TestCollector_Collect_ReturnsNilOnProviderError(t *testing.T) { collr := New() collr.prov = &mockProvider{err: true} - assert.Nil(t, collr.Collect()) + assert.Nil(t, collr.Collect(context.Background())) } func TestCollector_Collect_ReturnsNilOnZeroCertificates(t *testing.T) { collr := New() collr.prov = &mockProvider{certs: []*x509.Certificate{}} - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Nil(t, mx) } diff --git a/src/go/plugin/go.d/collector/zfspool/collector.go b/src/go/plugin/go.d/collector/zfspool/collector.go index 0007156cbd6e89..34c33f0ad81130 100644 --- a/src/go/plugin/go.d/collector/zfspool/collector.go +++ b/src/go/plugin/go.d/collector/zfspool/collector.go @@ -5,6 +5,7 @@ package zfspool import ( + "context" _ "embed" "errors" "fmt" @@ -62,7 +63,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.validateConfig(); err != nil { return fmt.Errorf("config validation: %s", err) } @@ -76,7 +77,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -93,7 +94,7 @@ func (c *Collector) Charts() *module.Charts { return c.charts } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -106,4 +107,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/zfspool/collector_test.go b/src/go/plugin/go.d/collector/zfspool/collector_test.go index 55c8b66a87fdb6..66e20ed736c1dc 100644 --- a/src/go/plugin/go.d/collector/zfspool/collector_test.go +++ b/src/go/plugin/go.d/collector/zfspool/collector_test.go @@ -5,6 +5,7 @@ package zfspool import ( + "context" "errors" "os" "strings" @@ -68,9 +69,9 @@ func TestCollector_Init(t *testing.T) { collr.Config = test.config if test.wantFail { - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } else { - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) } }) } @@ -89,7 +90,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOk() - _ = collr.Check() + _ = collr.Check(context.Background()) return collr }, }, @@ -97,7 +98,7 @@ func TestCollector_Cleanup(t *testing.T) { prepare: func() *Collector { collr := New() collr.exec = prepareMockOk() - _ = collr.Collect() + _ = collr.Collect(context.Background()) return collr }, }, @@ -107,7 +108,7 @@ func TestCollector_Cleanup(t *testing.T) { t.Run(name, func(t *testing.T) { collr := test.prepare() - assert.NotPanics(t, collr.Cleanup) + assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) }) } } @@ -146,9 +147,9 @@ func TestCollector_Check(t *testing.T) { collr.exec = mock if test.wantFail { - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } else { - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } }) } @@ -381,7 +382,7 @@ func TestCollector_Collect(t *testing.T) { mock := test.prepareMock() collr.exec = mock - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, test.wantMetrics, mx) diff --git a/src/go/plugin/go.d/collector/zookeeper/collector.go b/src/go/plugin/go.d/collector/zookeeper/collector.go index 824480ba6afed8..6c2fc98d15c9ee 100644 --- a/src/go/plugin/go.d/collector/zookeeper/collector.go +++ b/src/go/plugin/go.d/collector/zookeeper/collector.go @@ -3,6 +3,7 @@ package zookeeper import ( + "context" _ "embed" "errors" "fmt" @@ -52,7 +53,7 @@ func (c *Collector) Configuration() any { return c.Config } -func (c *Collector) Init() error { +func (c *Collector) Init(context.Context) error { if err := c.verifyConfig(); err != nil { return fmt.Errorf("invalid config: %v", err) } @@ -66,7 +67,7 @@ func (c *Collector) Init() error { return nil } -func (c *Collector) Check() error { +func (c *Collector) Check(context.Context) error { mx, err := c.collect() if err != nil { return err @@ -81,7 +82,7 @@ func (c *Collector) Charts() *Charts { return charts.Copy() } -func (c *Collector) Collect() map[string]int64 { +func (c *Collector) Collect(context.Context) map[string]int64 { mx, err := c.collect() if err != nil { c.Error(err) @@ -93,4 +94,4 @@ func (c *Collector) Collect() map[string]int64 { return mx } -func (c *Collector) Cleanup() {} +func (c *Collector) Cleanup(context.Context) {} diff --git a/src/go/plugin/go.d/collector/zookeeper/collector_test.go b/src/go/plugin/go.d/collector/zookeeper/collector_test.go index 749e1821540482..9c634551d413c4 100644 --- a/src/go/plugin/go.d/collector/zookeeper/collector_test.go +++ b/src/go/plugin/go.d/collector/zookeeper/collector_test.go @@ -5,6 +5,7 @@ package zookeeper import ( "bufio" "bytes" + "context" "errors" "os" "testing" @@ -41,7 +42,7 @@ func TestCollector_ConfigurationSerialize(t *testing.T) { func TestCollector_Init(t *testing.T) { collr := New() - assert.NoError(t, collr.Init()) + assert.NoError(t, collr.Init(context.Background())) assert.NotNil(t, collr.fetcher) } @@ -50,23 +51,23 @@ func TestCollector_InitErrorOnCreatingTLSConfig(t *testing.T) { collr.UseTLS = true collr.TLSConfig.TLSCA = "testdata/tls" - assert.Error(t, collr.Init()) + assert.Error(t, collr.Init(context.Background())) } func TestCollector_Check(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.fetcher = &mockZookeeperFetcher{data: dataMntrMetrics} - assert.NoError(t, collr.Check()) + assert.NoError(t, collr.Check(context.Background())) } func TestCollector_CheckErrorOnFetch(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.fetcher = &mockZookeeperFetcher{err: true} - assert.Error(t, collr.Check()) + assert.Error(t, collr.Check(context.Background())) } func TestCollector_Charts(t *testing.T) { @@ -74,12 +75,12 @@ func TestCollector_Charts(t *testing.T) { } func TestCollector_Cleanup(t *testing.T) { - New().Cleanup() + New().Cleanup(context.Background()) } func TestCollector_Collect(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.fetcher = &mockZookeeperFetcher{data: dataMntrMetrics} expected := map[string]int64{ @@ -99,7 +100,7 @@ func TestCollector_Collect(t *testing.T) { "znode_count": 5, } - mx := collr.Collect() + mx := collr.Collect(context.Background()) assert.Equal(t, expected, mx) module.TestMetricsHasAllChartsDims(t, collr.Charts(), mx) @@ -107,34 +108,34 @@ func TestCollector_Collect(t *testing.T) { func TestCollector_CollectMntrNotInWhiteList(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.fetcher = &mockZookeeperFetcher{data: dataMntrNotInWhiteListResponse} - assert.Nil(t, collr.Collect()) + assert.Nil(t, collr.Collect(context.Background())) } func TestCollector_CollectMntrEmptyResponse(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.fetcher = &mockZookeeperFetcher{} - assert.Nil(t, collr.Collect()) + assert.Nil(t, collr.Collect(context.Background())) } func TestCollector_CollectMntrInvalidData(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.fetcher = &mockZookeeperFetcher{data: []byte("hello \nand good buy\n")} - assert.Nil(t, collr.Collect()) + assert.Nil(t, collr.Collect(context.Background())) } func TestCollector_CollectMntrReceiveError(t *testing.T) { collr := New() - require.NoError(t, collr.Init()) + require.NoError(t, collr.Init(context.Background())) collr.fetcher = &mockZookeeperFetcher{err: true} - assert.Nil(t, collr.Collect()) + assert.Nil(t, collr.Collect(context.Background())) } type mockZookeeperFetcher struct { diff --git a/src/go/plugin/go.d/examples/simple/main.go b/src/go/plugin/go.d/examples/simple/main.go index b8b7d9bad0b5f3..01f6f28bdf0c59 100644 --- a/src/go/plugin/go.d/examples/simple/main.go +++ b/src/go/plugin/go.d/examples/simple/main.go @@ -3,6 +3,7 @@ package main import ( + "context" "errors" "fmt" "log/slog" @@ -25,11 +26,11 @@ type example struct { module.Base } -func (e *example) Cleanup() {} +func (e *example) Cleanup(context.Context) {} -func (e *example) Init() error { return nil } +func (e *example) Init(context.Context) error { return nil } -func (e *example) Check() error { return nil } +func (e *example) Check(context.Context) error { return nil } func (e *example) Charts() *module.Charts { return &module.Charts{ @@ -45,7 +46,7 @@ func (e *example) Charts() *module.Charts { } func (e *example) Configuration() any { return nil } -func (e *example) Collect() map[string]int64 { +func (e *example) Collect(context.Context) map[string]int64 { return map[string]int64{ "random0": rand.Int63n(100), "random1": rand.Int63n(100),