Skip to content

Commit

Permalink
if fastly_rt_datacenter_info is blocked, do not enable datacentercache
Browse files Browse the repository at this point in the history
The datacentercache is only used for building the
`fastly_rt_datacenter_info` so if that mertic is in the blocklist we can
safely disable the cache and the gatherer
  • Loading branch information
leklund committed Apr 17, 2024
1 parent 624da2b commit 6343ac9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
23 changes: 14 additions & 9 deletions cmd/fastly-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ func main() {

var datacenterCache *api.DatacenterCache
{
datacenterCache = api.NewDatacenterCache(apiClient, token)
enabled := !metricNameFilter.Blocked(prometheus.BuildFQName(namespace, deprecatedSubsystem, "datacenter_info"))
datacenterCache = api.NewDatacenterCache(apiClient, token, enabled)
}

var productCache *api.ProductCache
Expand All @@ -285,12 +286,14 @@ func main() {
}
return nil
})
g.Go(func() error {
if err := datacenterCache.Refresh(context.Background()); err != nil {
level.Warn(logger).Log("during", "initial fetch of datacenters", "err", err, "msg", "datacenter labels unavailable, will retry")
}
return nil
})
if datacenterCache.Enabled() {
g.Go(func() error {
if err := datacenterCache.Refresh(context.Background()); err != nil {
level.Warn(logger).Log("during", "initial fetch of datacenters", "err", err, "msg", "datacenter labels unavailable, will retry")
}
return nil
})
}
g.Go(func() error {
if err := productCache.Refresh(context.Background()); err != nil {
level.Warn(logger).Log("during", "initial fetch of products", "err", err, "msg", "products API unavailable, will retry")
Expand All @@ -302,7 +305,7 @@ func main() {
}

var defaultGatherers prometheus.Gatherers
{
if datacenterCache.Enabled() {
dcs, err := datacenterCache.Gatherer(namespace, deprecatedSubsystem)
if err != nil {
level.Error(apiLogger).Log("during", "create datacenter gatherer", "err", err)
Expand Down Expand Up @@ -331,7 +334,9 @@ func main() {
}

var g run.Group
{
// only setup the ticker if the datacenterCache is enabled.
if datacenterCache.Enabled() {

// Every datacenterRefresh, ask the api.DatacenterCache to refresh
// metadata from the api.fastly.com/datacenters endpoint.
var (
Expand Down
20 changes: 15 additions & 5 deletions pkg/api/datacenter_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,29 @@ type Coördinates struct {
// DatacenterCache polls api.fastly.com/datacenters and maintains a local cache
// of the returned metadata. That information is exposed as Prometheus metrics.
type DatacenterCache struct {
client HTTPClient
token string
client HTTPClient
token string
enabled bool

mtx sync.Mutex
dcs []Datacenter
}

// NewDatacenterCache returns an empty cache of datacenter metadata. Use the
// Refresh method to update the cache.
func NewDatacenterCache(client HTTPClient, token string) *DatacenterCache {
func NewDatacenterCache(client HTTPClient, token string, enabled bool) *DatacenterCache {
return &DatacenterCache{
client: client,
token: token,
client: client,
token: token,
enabled: enabled,
}
}

// Refresh the cache with metadata retreived from the Fastly API.
func (c *DatacenterCache) Refresh(ctx context.Context) error {
if !c.enabled {
return nil
}
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.fastly.com/datacenters", nil)
if err != nil {
return fmt.Errorf("error constructing API datacenters request: %w", err)
Expand Down Expand Up @@ -109,6 +114,11 @@ func (c *DatacenterCache) Gatherer(namespace, subsystem string) (prometheus.Gath
return registry, nil
}

// Enabled returns true if the DatacenterCache is enabled
func (c *DatacenterCache) Enabled() bool {
return c.enabled
}

type datacenterCollector struct {
desc *prometheus.Desc
cache *DatacenterCache
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/datacenter_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestDatacenterCache(t *testing.T) {
var (
ctx = context.Background()
client = testcase.client
cache = api.NewDatacenterCache(client, "irrelevant token")
cache = api.NewDatacenterCache(client, "irrelevant token", true)
)

if want, have := testcase.wantErr, cache.Refresh(ctx); !cmp.Equal(want, have) {
Expand Down

0 comments on commit 6343ac9

Please sign in to comment.