Skip to content

Commit

Permalink
Merge pull request #6 from Cyb3r-Jak3/expand-health-check
Browse files Browse the repository at this point in the history
Expand health check
  • Loading branch information
Cyb3r-Jak3 authored Aug 4, 2024
2 parents e5f3219 + 8e1cdb5 commit 2714daa
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 24 deletions.
1 change: 1 addition & 0 deletions .github/workflows/golang.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ jobs:

- name: Upload test results
uses: actions/upload-artifact@v4
if: always()
with:
name: test-results
path: |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Required authentication scopes:

To authenticate this way, only set `CF_API_TOKEN` (omit `CF_API_EMAIL` and `CF_API_KEY`)

[Shortcut to create the API token](https://dash.cloudflare.com/profile/api-tokens?permissionGroupKeys=%5B%7B%22key%22%3A%22account_analytics%22%2C%22type%22%3A%22read%22%7D%2C%7B%22key%22%3A%22account_settings%22%2C%22type%22%3A%22read%22%7D%2C%7B%22key%22%3A%22analytics%22%2C%22type%22%3A%22read%22%7D%2C%7B%22key%22%3A%22firewall_services%22%2C%22type%22%3A%22read%22%7D%5D&name=Cloudflare+Exporter&accountId=*&zoneId=all)
[Shortcut to create the API token](https://dash.cloudflare.com/profile/api-tokens?permissionGroupKeys=%5B%7B%22key%22%3A%22account_rulesets%22%2C%22type%22%3A%22read%22%7D%2C%7B%22key%22%3A%22account_settings%22%2C%22type%22%3A%22read%22%7D%2C%7B%22key%22%3A%22analytics%22%2C%22type%22%3A%22read%22%7D%2C%7B%22key%22%3A%22dns_firewall%22%2C%22type%22%3A%22read%22%7D%2C%7B%22key%22%3A%22firewall_services%22%2C%22type%22%3A%22read%22%7D%5D&name=Cloudflare+Exporter&accountId=*&zoneId=all)

### User email + API key
To authenticate with user email + API key, use the `Global API Key` from the Cloudflare dashboard.
Expand Down
17 changes: 7 additions & 10 deletions cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const (
cfGraphQLEndpoint = "https://api.cloudflare.com/client/v4/graphql/"
)

var (
graphqlClient *graphql.Client
)

type cloudflareResponse struct {
Viewer struct {
Zones []zoneResp `json:"zones"`
Expand Down Expand Up @@ -309,9 +313,9 @@ func fetchFirewallRules(zoneID string) map[string]string {
}
for _, rulesetDesc := range listOfRulesets {
if rulesetDesc.Phase == "http_request_firewall_managed" {
ruleset, err := cloudflareAPI.GetRuleset(ctx, cloudflare.ZoneIdentifier(zoneID), rulesetDesc.ID)
if err != nil {
log.Fatalf("Error fetching ruleset: %s", err)
ruleset, ruleGetErr := cloudflareAPI.GetRuleset(ctx, cloudflare.ZoneIdentifier(zoneID), rulesetDesc.ID)
if ruleGetErr != nil {
log.Fatalf("Error fetching ruleset: %s", ruleGetErr)
}
for _, rule := range ruleset.Rules {
firewallRulesMap[rule.ID] = rule.Description
Expand Down Expand Up @@ -447,7 +451,6 @@ query ($zoneIDs: [String!], $mintime: Time!, $maxtime: Time!, $limit: Int!) {
request.Var("zoneIDs", zoneIDs)

ctx := context.Background()
graphqlClient := graphql.NewClient(cfGraphQLEndpoint)

var resp cloudflareResponse
if err := graphqlClient.Run(ctx, request, &resp); err != nil {
Expand Down Expand Up @@ -503,7 +506,6 @@ func fetchColoTotals(zoneIDs []string) (*cloudflareResponseColo, error) {
request.Var("zoneIDs", zoneIDs)

ctx := context.Background()
graphqlClient := graphql.NewClient(cfGraphQLEndpoint)
var resp cloudflareResponseColo
if err := graphqlClient.Run(ctx, request, &resp); err != nil {
log.Error(err)
Expand Down Expand Up @@ -563,7 +565,6 @@ func fetchWorkerTotals(accountID string) (*cloudflareResponseAccts, error) {
request.Var("accountID", accountID)

ctx := context.Background()
graphqlClient := graphql.NewClient(cfGraphQLEndpoint)
var resp cloudflareResponseAccts
if err := graphqlClient.Run(ctx, request, &resp); err != nil {
log.Errorf("Error fetching worker totals: %s", err)
Expand Down Expand Up @@ -640,7 +641,6 @@ func fetchLoadBalancerTotals(zoneIDs []string) (*cloudflareResponseLb, error) {
request.Var("zoneIDs", zoneIDs)

ctx := context.Background()
graphqlClient := graphql.NewClient(cfGraphQLEndpoint)
var resp cloudflareResponseLb
if err := graphqlClient.Run(ctx, request, &resp); err != nil {
log.Errorf("Error fetching load balancer totals: %s", err)
Expand Down Expand Up @@ -692,7 +692,6 @@ func fetchLogpushAccount(accountID string) (*cloudflareResponseLogpushAccount, e
request.Var("mintime", now1mAgo)

ctx := context.Background()
graphqlClient := graphql.NewClient(cfGraphQLEndpoint)
var resp cloudflareResponseLogpushAccount
if err := graphqlClient.Run(ctx, request, &resp); err != nil {
log.Errorf("Error fetching logpush account totals: %s", err)
Expand Down Expand Up @@ -744,7 +743,6 @@ func fetchLogpushZone(zoneIDs []string) (*cloudflareResponseLogpushZone, error)
request.Var("mintime", now1mAgo)

ctx := context.Background()
graphqlClient := graphql.NewClient(cfGraphQLEndpoint)
var resp cloudflareResponseLogpushZone
if err := graphqlClient.Run(ctx, request, &resp); err != nil {
log.Errorf("Error fetching logpush zone totals: %s", err)
Expand Down Expand Up @@ -802,7 +800,6 @@ func fetchR2Account(accountID string) (*cloudflareResponseR2Account, error) {
request.Var("date", now.Format("2006-01-02"))

ctx := context.Background()
graphqlClient := graphql.NewClient(cfGraphQLEndpoint)
var resp cloudflareResponseR2Account
if err := graphqlClient.Run(ctx, request, &resp); err != nil {
log.Errorf("Error fetching R2 account: %s", err)
Expand Down
24 changes: 11 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"fmt"
"net/http"
"runtime/debug"
"slices"
"strings"
"sync"
"time"

"github.com/machinebox/graphql"
"github.com/nelkinda/health-go"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -76,15 +78,6 @@ func filterZones(all []cloudflare.Zone, target []string) []cloudflare.Zone {
return filtered
}

func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}

func filterExcludedZones(all []cloudflare.Zone, exclude []string) []cloudflare.Zone {
var filtered []cloudflare.Zone

Expand All @@ -93,7 +86,7 @@ func filterExcludedZones(all []cloudflare.Zone, exclude []string) []cloudflare.Z
}

for _, z := range all {
if contains(exclude, z.ID) {
if slices.Contains(exclude, z.ID) {
log.Info("Exclude zone: ", z.ID, " ", z.Name)
} else {
filtered = append(filtered, z)
Expand Down Expand Up @@ -163,6 +156,8 @@ func runExporter() {
log.Fatalf("Error creating Cloudflare API client: %s", err)
}

graphqlClient = graphql.NewClient(cfGraphQLEndpoint)

if len(viper.GetString("cf_api_token")) > 0 {
status, err := cloudflareAPI.VerifyAPIToken(context.Background())
if err != nil {
Expand All @@ -179,7 +174,7 @@ func runExporter() {
if err != nil {
log.Fatalf("Error building metrics set: %s", err)
}
log.Debugf("Metrics set: %v", metricsSet)

mustRegisterMetrics(metricsSet)

go func() {
Expand All @@ -200,7 +195,10 @@ func runExporter() {
}

http.Handle(cfgMetricsPath, promhttp.Handler())
h := health.New(health.Health{})
h := health.New(health.Health{
ReleaseID: version,
Version: versionString,
})
http.HandleFunc("/health", h.Handler)

log.Info("Beginning to serve metrics on ", viper.GetString("listen"), cfgMetricsPath)
Expand Down Expand Up @@ -255,7 +253,7 @@ func main() {
viper.BindEnv("cf_exclude_zones")
viper.SetDefault("cf_exclude_zones", "")

flags.Int("scrape_delay", 300, "scrape delay in seconds0")
flags.Int("scrape_delay", 300, "scrape delay in seconds")
viper.BindEnv("scrape_delay")
viper.SetDefault("scrape_delay", 300)

Expand Down
1 change: 1 addition & 0 deletions prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ var (
Name: buildInfoMetricName.String(),
Help: "A metric with a constant '1' value labeled by version, revision, branch, and goversion from which the cloudflare_exporter was built.",
}, []string{"version", "goversion", "revision"})

// Requests
zoneRequestTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: zoneRequestTotalMetricName.String(),
Expand Down

0 comments on commit 2714daa

Please sign in to comment.