Skip to content

Commit

Permalink
Merge pull request #43 from m0ar/master
Browse files Browse the repository at this point in the history
Add `CpuTime` and `Duration` quantiles to worker metrics
  • Loading branch information
martinhaus authored Oct 15, 2021
2 parents 12cb860 + 7854a9e commit 96e6d4d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ The original method of zone filtering by using env variables `ZONE_<name>` is no
## List of available metrics

```
# HELP cloudflare_worker_cpu_time CPU time quantiles by script name
# HELP cloudflare_worker_duration Duration quantiles by script name (GB*s)
# HELP cloudflare_worker_errors_count Number of errors by script name
# HELP cloudflare_worker_requests_count Number of requests sent to worker by script name
# HELP cloudflare_zone_bandwidth_cached Cached bandwidth per zone in bytes
Expand Down
24 changes: 23 additions & 1 deletion cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"time"

cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/cloudflare/cloudflare-go"
"github.com/machinebox/graphql"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -43,6 +43,17 @@ type accountResp struct {
Errors uint64 `json:"errors"`
Duration float64 `json:"duration"`
} `json:"sum"`

Quantiles struct {
CpuTimeP50 float32 `json:"cpuTimeP50"`
CpuTimeP75 float32 `json:"cpuTimeP75"`
CpuTimeP99 float32 `json:"cpuTimeP99"`
CpuTimeP999 float32 `json:"cpuTimeP999"`
DurationP50 float32 `json:"durationP50"`
DurationP75 float32 `json:"durationP75"`
DurationP99 float32 `json:"durationP99"`
DurationP999 float32 `json:"durationP999"`
} `json:"quantiles"`
} `json:"workersInvocationsAdaptive"`
}

Expand Down Expand Up @@ -402,6 +413,17 @@ func fetchWorkerTotals(accountID string) (*cloudflareResponseAccts, error) {
errors
duration
}
quantiles {
cpuTimeP50
cpuTimeP75
cpuTimeP99
cpuTimeP999
durationP50
durationP75
durationP99
durationP999
}
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sync"
"time"

cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/cloudflare/cloudflare-go"
"github.com/namsral/flag"
"github.com/nelkinda/health-go"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand All @@ -22,6 +22,7 @@ var (
cfgMetricsPath = "/metrics"
cfgZones = ""
cfgScrapeDelay = 300
cfgFreeTier = false
)

func getTargetZones() []string {
Expand All @@ -30,7 +31,7 @@ func getTargetZones() []string {
if len(cfgZones) > 0 {
zoneIDs = strings.Split(cfgZones, ",")
} else {
//depricated
// deprecated
for _, e := range os.Environ() {
if strings.HasPrefix(e, "ZONE_") {
split := strings.SplitN(e, "=", 2)
Expand Down Expand Up @@ -64,7 +65,6 @@ func fetchMetrics() {
var wg sync.WaitGroup
zones := fetchZones()
accounts := fetchAccounts()

filteredZones := filterZones(zones, getTargetZones())

for _, a := range accounts {
Expand Down Expand Up @@ -96,7 +96,8 @@ func main() {
flag.StringVar(&cfgCfAPIEmail, "cf_api_email", cfgCfAPIEmail, "cloudflare api email, works with api_key flag")
flag.StringVar(&cfgCfAPIToken, "cf_api_token", cfgCfAPIToken, "cloudflare api token (preferred)")
flag.StringVar(&cfgZones, "cf_zones", cfgZones, "cloudflare zones to export, comma delimited list")
flag.IntVar(&cfgScrapeDelay, "scrape_delay", cfgScrapeDelay , "scrape delay in seconds, defaults to 300")
flag.IntVar(&cfgScrapeDelay, "scrape_delay", cfgScrapeDelay, "scrape delay in seconds, defaults to 300")
flag.BoolVar(&cfgFreeTier, "free_tier", cfgFreeTier, "scrape only metrics included in free plan")
flag.Parse()
if !(len(cfgCfAPIToken) > 0 || (len(cfgCfAPIEmail) > 0 && len(cfgCfAPIKey) > 0)) {
log.Fatal("Please provide CF_API_KEY+CF_API_EMAIL or CF_API_TOKEN")
Expand Down
30 changes: 30 additions & 0 deletions prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ var (
Help: "Number of errors by script name",
}, []string{"script_name"},
)

workerCpuTime = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "cloudflare_worker_cpu_time",
Help: "CPU time quantiles by script name",
}, []string{"script_name", "quantile"},
)

workerDuration = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "cloudflare_worker_duration",
Help: "Duration quantiles by script name (GB*s)",
}, []string{"script_name", "quantile"},
)
)

func fetchWorkerAnalytics(account cloudflare.Account, wg *sync.WaitGroup) {
Expand All @@ -200,13 +212,26 @@ func fetchWorkerAnalytics(account cloudflare.Account, wg *sync.WaitGroup) {
for _, w := range a.WorkersInvocationsAdaptive {
workerRequests.With(prometheus.Labels{"script_name": w.Dimensions.ScriptName}).Add(float64(w.Sum.Requests))
workerErrors.With(prometheus.Labels{"script_name": w.Dimensions.ScriptName}).Add(float64(w.Sum.Errors))
workerCpuTime.With(prometheus.Labels{"script_name": w.Dimensions.ScriptName, "quantile": "P50"}).Set(float64(w.Quantiles.CpuTimeP50))
workerCpuTime.With(prometheus.Labels{"script_name": w.Dimensions.ScriptName, "quantile": "P75"}).Set(float64(w.Quantiles.CpuTimeP75))
workerCpuTime.With(prometheus.Labels{"script_name": w.Dimensions.ScriptName, "quantile": "P99"}).Set(float64(w.Quantiles.CpuTimeP99))
workerCpuTime.With(prometheus.Labels{"script_name": w.Dimensions.ScriptName, "quantile": "P999"}).Set(float64(w.Quantiles.CpuTimeP999))
workerDuration.With(prometheus.Labels{"script_name": w.Dimensions.ScriptName, "quantile": "P50"}).Set(float64(w.Quantiles.DurationP50))
workerDuration.With(prometheus.Labels{"script_name": w.Dimensions.ScriptName, "quantile": "P75"}).Set(float64(w.Quantiles.DurationP75))
workerDuration.With(prometheus.Labels{"script_name": w.Dimensions.ScriptName, "quantile": "P99"}).Set(float64(w.Quantiles.DurationP99))
workerDuration.With(prometheus.Labels{"script_name": w.Dimensions.ScriptName, "quantile": "P999"}).Set(float64(w.Quantiles.DurationP999))
}
}
}

func fetchZoneColocationAnalytics(zones []cloudflare.Zone, wg *sync.WaitGroup) {
wg.Add(1)
defer wg.Done()

// Colocation metrics are not available in non-enterprise zones
if cfgFreeTier {
return
}
zoneIDs := extractZoneIDs(zones)

r, err := fetchColoTotals(zoneIDs)
Expand All @@ -229,6 +254,11 @@ func fetchZoneAnalytics(zones []cloudflare.Zone, wg *sync.WaitGroup) {
wg.Add(1)
defer wg.Done()

// None of the below referenced metrics are available in the free tier
if cfgFreeTier {
return
}

zoneIDs := extractZoneIDs(zones)

r, err := fetchZoneTotals(zoneIDs)
Expand Down

0 comments on commit 96e6d4d

Please sign in to comment.