From b778c1ddbd37eb2cc981929ffdfdf3246211ca6b Mon Sep 17 00:00:00 2001 From: TJ Hoplock Date: Mon, 29 Jan 2024 09:04:34 -0500 Subject: [PATCH] ref(api): accept logger as func param for core api funcs other than `NewClient()`, that is --- pkg/exporter/exporter.go | 2 +- pkg/ns1/api.go | 8 ++------ pkg/ns1/api_test.go | 7 ++++++- pkg/servicediscovery/sd.go | 2 +- pkg/servicediscovery/sd_test.go | 2 +- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/pkg/exporter/exporter.go b/pkg/exporter/exporter.go index 93d3315..d656d1f 100644 --- a/pkg/exporter/exporter.go +++ b/pkg/exporter/exporter.go @@ -83,7 +83,7 @@ func (w *Worker) Collect(ch chan<- prometheus.Metric) { // RefreshZoneData updates the data for each of the zones in the worker's zone list by querying the NS1 API, parses the data to structs that serve as internal counterparts to the NS1 API's dns.Record and dns.Zone, and then updating the worker's internal map of zones. This internal map is used as a cache to respond to respond to HTTP requests. func (w *Worker) RefreshZoneData() { getRecords := w.EnableRecordQPS || w.EnableZoneQPS - w.zoneCache = ns1_internal.RefreshZoneData(w.client, getRecords, w.ZoneBlacklist, w.ZoneWhitelist) + w.zoneCache = ns1_internal.RefreshZoneData(w.logger, w.client, getRecords, w.ZoneBlacklist, w.ZoneWhitelist) level.Debug(w.logger).Log("msg", "Worker zone cache updated", "num_zones", len(w.zoneCache)) if getRecords { diff --git a/pkg/ns1/api.go b/pkg/ns1/api.go index 0a2e700..ff04cb0 100644 --- a/pkg/ns1/api.go +++ b/pkg/ns1/api.go @@ -21,18 +21,14 @@ import ( "regexp" "time" + "github.com/go-kit/log" "github.com/go-kit/log/level" - "github.com/prometheus/common/promlog" api "gopkg.in/ns1/ns1-go.v2/rest" "gopkg.in/ns1/ns1-go.v2/rest/model/dns" "github.com/tjhop/ns1_exporter/pkg/metrics" ) -var ( - logger = promlog.New(&promlog.Config{}) -) - type APIConfig struct { Concurrency int Endpoint string @@ -105,7 +101,7 @@ func NewClient(config APIConfig) *api.Client { return c } -func RefreshZoneData(c *api.Client, getRecords bool, zoneBlacklist, zoneWhitelist *regexp.Regexp) map[string]*Zone { +func RefreshZoneData(logger log.Logger, c *api.Client, getRecords bool, zoneBlacklist, zoneWhitelist *regexp.Regexp) map[string]*Zone { zMap := make(map[string]*Zone) zones, _, err := c.Zones.List() diff --git a/pkg/ns1/api_test.go b/pkg/ns1/api_test.go index fafd0db..806f29f 100644 --- a/pkg/ns1/api_test.go +++ b/pkg/ns1/api_test.go @@ -20,12 +20,17 @@ import ( "regexp" "testing" + "github.com/prometheus/common/promlog" "github.com/stretchr/testify/require" "gopkg.in/ns1/ns1-go.v2/mockns1" api "gopkg.in/ns1/ns1-go.v2/rest" "gopkg.in/ns1/ns1-go.v2/rest/model/dns" ) +var ( + mockLogger = promlog.New(&promlog.Config{}) +) + func TestRefreshZoneData(t *testing.T) { mock, doer, err := mockns1.New(t) require.Nil(t, err) @@ -89,7 +94,7 @@ func TestRefreshZoneData(t *testing.T) { getRecords, )) - got := RefreshZoneData(mockClient, getRecords, tc.zoneBlacklist, tc.zoneWhitelist) + got := RefreshZoneData(mockLogger, mockClient, getRecords, tc.zoneBlacklist, tc.zoneWhitelist) require.Equal(t, tc.want, got) require.Equal(t, tc.expectedLen, len(got)) for _, zone := range got { diff --git a/pkg/servicediscovery/sd.go b/pkg/servicediscovery/sd.go index 61a67ec..24a4fd7 100644 --- a/pkg/servicediscovery/sd.go +++ b/pkg/servicediscovery/sd.go @@ -234,7 +234,7 @@ func (w *Worker) RefreshPrometheusTargetData() { } func (w *Worker) RefreshZoneData() { - w.zoneCache = ns1_internal.RefreshZoneData(w.client, true, w.ZoneBlacklist, w.ZoneWhitelist) + w.zoneCache = ns1_internal.RefreshZoneData(w.logger, w.client, true, w.ZoneBlacklist, w.ZoneWhitelist) } func (w *Worker) RefreshRecordData() { diff --git a/pkg/servicediscovery/sd_test.go b/pkg/servicediscovery/sd_test.go index 818b6dc..0e60da6 100644 --- a/pkg/servicediscovery/sd_test.go +++ b/pkg/servicediscovery/sd_test.go @@ -26,10 +26,10 @@ import ( "time" promModel "github.com/prometheus/common/model" + "github.com/prometheus/common/promlog" "github.com/stretchr/testify/require" "gopkg.in/ns1/ns1-go.v2/mockns1" api "gopkg.in/ns1/ns1-go.v2/rest" - "github.com/prometheus/common/promlog" "gopkg.in/ns1/ns1-go.v2/rest/model/data" "gopkg.in/ns1/ns1-go.v2/rest/model/dns" "gopkg.in/ns1/ns1-go.v2/rest/model/filter"