Skip to content

Commit

Permalink
feat(google): Set refresh interval for compute module
Browse files Browse the repository at this point in the history
This alters the `collector.Collect` method on the compute module to
also check if the pricing map needs to be refreshed. This is a
configurable paramater so that folks that want a tighter refresh
interval are easily able to do it.

- closes #56
  • Loading branch information
Pokom committed Dec 18, 2023
1 parent ce66897 commit 66e36ee
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
15 changes: 11 additions & 4 deletions pkg/google/compute/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"regexp"
"strings"
"time"

"cloud.google.com/go/billing/apiv1/billingpb"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -37,7 +38,8 @@ var (
)

type Config struct {
Projects string
Projects string
ScrapeInterval time.Duration
}

// Collector implements the Collector interface for compute services in GKE.
Expand All @@ -47,6 +49,7 @@ type Collector struct {
PricingMap *StructuredPricingMap
config *Config
Projects []string
NextScrape time.Time
}

// New is a helper method to properly setup a compute.Collector struct.
Expand Down Expand Up @@ -225,9 +228,10 @@ func (c *Collector) Register(registry provider.Registry) error {
}

func (c *Collector) Collect() error {
log.Println("Collecting GKE metrics")
// TODO: Consider adding a timer to this so we can refresh after a certain period of time
if c.PricingMap == nil {
start := time.Now()
log.Printf("Collecting %s metrics", c.Name())
if c.PricingMap == nil || time.Now().After(c.NextScrape) {
log.Println("Refreshing pricing map")
serviceName, err := c.GetServiceName()
if err != nil {
return err
Expand All @@ -238,6 +242,8 @@ func (c *Collector) Collect() error {
return err
}
c.PricingMap = pricingMap
c.NextScrape = time.Now().Add(c.config.ScrapeInterval)
log.Printf("Finished refreshing pricing map in %s", time.Since(start))
}
for _, project := range c.Projects {
instances, err := c.ListInstances(project)
Expand Down Expand Up @@ -270,6 +276,7 @@ func (c *Collector) Collect() error {
}).Set(ramCost)
}
}
log.Printf("Finished collecting GKE metrics in %s", time.Since(start))

return nil
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/google/compute/pricing_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package compute
import (
"errors"
"fmt"
"log"
"regexp"
"strings"

Expand Down Expand Up @@ -106,15 +105,15 @@ func GeneratePricingMap(skus []*billingpb.Sku) (*StructuredPricingMap, error) {
rawData, err := getDataFromSku(sku)

if errors.Is(err, SkuNotRelevant) {
log.Println(fmt.Errorf("%w: %s", SkuNotRelevant, sku.Description))
fmt.Errorf("%w: %s", SkuNotRelevant, sku.Description)
continue
}
if errors.Is(err, PricingDataIsOff) {
log.Println(fmt.Errorf("%w: %s", PricingDataIsOff, sku.Description))
fmt.Errorf("%w: %s", PricingDataIsOff, sku.Description)
continue
}
if errors.Is(err, SkuNotParsable) {
log.Println(fmt.Errorf("%w: %s", SkuNotParsable, sku.Description))
fmt.Errorf("%w: %s", SkuNotParsable, sku.Description)
continue
}
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/google/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ func New(config *Config) (*GCP, error) {
}
case "GKE":
collector = compute.New(&compute.Config{
Projects: config.Projects,
Projects: config.Projects,
ScrapeInterval: config.ScrapeInterval,
}, computeService, cloudCatalogClient)
default:
log.Printf("Unknown service %s", service)
Expand Down

0 comments on commit 66e36ee

Please sign in to comment.