Skip to content

Commit

Permalink
Implement metrics denylist (#77)
Browse files Browse the repository at this point in the history
* Add metrics denylist

* Update README with metrics_denylist flag

* Rename MetricKind alias to MetricName
  • Loading branch information
almirmcunhajr authored Dec 19, 2022
1 parent 8667558 commit 98bf70d
Show file tree
Hide file tree
Showing 3 changed files with 276 additions and 72 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ The exporter can be configured using env variables or command flags.
| `METRICS_PATH` | path for metrics, default `/metrics` |
| `SCRAPE_DELAY` | scrape delay in seconds, default `300` |
| `CF_BATCH_SIZE` | cloudflare request zones batch size (1 - 10), default `10` |
| `METRICS_DENYLIST` | (Optional) cloudflare-exporter metrics to not export, comma delimited list of cloudflare-exporter metrics. If not set, all metrics are exported |
| `ZONE_<NAME>` | `DEPRECATED since 0.0.5` (optional) Zone ID. Add zones you want to scrape by adding env vars in this format. You can find the zone ids in Cloudflare dashboards. |

Corresponding flags:
Expand All @@ -65,6 +66,7 @@ Corresponding flags:
-metrics_path="/metrics": path for metrics, default /metrics
-scrape_delay=300: scrape delay in seconds, defaults to 300
-cf_batch_size=10: cloudflare zones batch size (1-10)
-metrics_denylist="": cloudflare-exporter metrics to not export, comma delimited list
```

Note: `ZONE_<name>` configuration is not supported as flag.
Expand Down
32 changes: 22 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ import (
)

var (
cfgListen = ":8080"
cfgCfAPIKey = ""
cfgCfAPIEmail = ""
cfgCfAPIToken = ""
cfgMetricsPath = "/metrics"
cfgZones = ""
cfgExcludeZones = ""
cfgScrapeDelay = 300
cfgFreeTier = false
cfgBatchSize = 10
cfgListen = ":8080"
cfgCfAPIKey = ""
cfgCfAPIEmail = ""
cfgCfAPIToken = ""
cfgMetricsPath = "/metrics"
cfgZones = ""
cfgExcludeZones = ""
cfgScrapeDelay = 300
cfgFreeTier = false
cfgBatchSize = 10
cfgMetricsDenylist = ""
)

func getTargetZones() []string {
Expand Down Expand Up @@ -139,6 +140,7 @@ func main() {
flag.IntVar(&cfgScrapeDelay, "scrape_delay", cfgScrapeDelay, "scrape delay in seconds, defaults to 300")
flag.IntVar(&cfgBatchSize, "cf_batch_size", cfgBatchSize, "cloudflare zones batch size (1-10), defaults to 10")
flag.BoolVar(&cfgFreeTier, "free_tier", cfgFreeTier, "scrape only metrics included in free plan")
flag.StringVar(&cfgMetricsDenylist, "metrics_denylist", cfgMetricsDenylist, "metrics to not expose, comma delimited list")
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 All @@ -151,6 +153,16 @@ func main() {
log.SetFormatter(customFormatter)
customFormatter.FullTimestamp = true

metricsDenylist := []string{}
if len(cfgMetricsDenylist) > 0 {
metricsDenylist = strings.Split(cfgMetricsDenylist, ",")
}
deniedMetricsSet, err := buildDeniedMetricsSet(metricsDenylist)
if err != nil {
log.Fatal(err)
}
mustRegisterMetrics(deniedMetricsSet)

go func() {
for ; true; <-time.NewTicker(60 * time.Second).C {
go fetchMetrics()
Expand Down
Loading

0 comments on commit 98bf70d

Please sign in to comment.