Skip to content

Commit

Permalink
ref(api): accept logger as func param for core api funcs
Browse files Browse the repository at this point in the history
other than `NewClient()`, that is
  • Loading branch information
tjhop committed Jan 29, 2024
1 parent 2adf648 commit b778c1d
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pkg/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 2 additions & 6 deletions pkg/ns1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
7 changes: 6 additions & 1 deletion pkg/ns1/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/servicediscovery/sd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/servicediscovery/sd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit b778c1d

Please sign in to comment.