Skip to content

Commit 47568c7

Browse files
committed
feat: use a single graphql client
1 parent e5f3219 commit 47568c7

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed

cloudflare.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ const (
1515
cfGraphQLEndpoint = "https://api.cloudflare.com/client/v4/graphql/"
1616
)
1717

18+
var (
19+
graphqlClient *graphql.Client
20+
)
21+
1822
type cloudflareResponse struct {
1923
Viewer struct {
2024
Zones []zoneResp `json:"zones"`
@@ -309,9 +313,9 @@ func fetchFirewallRules(zoneID string) map[string]string {
309313
}
310314
for _, rulesetDesc := range listOfRulesets {
311315
if rulesetDesc.Phase == "http_request_firewall_managed" {
312-
ruleset, err := cloudflareAPI.GetRuleset(ctx, cloudflare.ZoneIdentifier(zoneID), rulesetDesc.ID)
313-
if err != nil {
314-
log.Fatalf("Error fetching ruleset: %s", err)
316+
ruleset, ruleGetErr := cloudflareAPI.GetRuleset(ctx, cloudflare.ZoneIdentifier(zoneID), rulesetDesc.ID)
317+
if ruleGetErr != nil {
318+
log.Fatalf("Error fetching ruleset: %s", ruleGetErr)
315319
}
316320
for _, rule := range ruleset.Rules {
317321
firewallRulesMap[rule.ID] = rule.Description
@@ -447,7 +451,6 @@ query ($zoneIDs: [String!], $mintime: Time!, $maxtime: Time!, $limit: Int!) {
447451
request.Var("zoneIDs", zoneIDs)
448452

449453
ctx := context.Background()
450-
graphqlClient := graphql.NewClient(cfGraphQLEndpoint)
451454

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

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

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

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

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

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

804802
ctx := context.Background()
805-
graphqlClient := graphql.NewClient(cfGraphQLEndpoint)
806803
var resp cloudflareResponseR2Account
807804
if err := graphqlClient.Run(ctx, request, &resp); err != nil {
808805
log.Errorf("Error fetching R2 account: %s", err)

main.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import (
55
"fmt"
66
"net/http"
77
"runtime/debug"
8+
"slices"
89
"strings"
910
"sync"
1011
"time"
1112

13+
"github.com/machinebox/graphql"
1214
"github.com/nelkinda/health-go"
1315
"github.com/prometheus/client_golang/prometheus/promhttp"
1416
"github.com/spf13/cobra"
@@ -76,15 +78,6 @@ func filterZones(all []cloudflare.Zone, target []string) []cloudflare.Zone {
7678
return filtered
7779
}
7880

79-
func contains(s []string, e string) bool {
80-
for _, a := range s {
81-
if a == e {
82-
return true
83-
}
84-
}
85-
return false
86-
}
87-
8881
func filterExcludedZones(all []cloudflare.Zone, exclude []string) []cloudflare.Zone {
8982
var filtered []cloudflare.Zone
9083

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

9588
for _, z := range all {
96-
if contains(exclude, z.ID) {
89+
if slices.Contains(exclude, z.ID) {
9790
log.Info("Exclude zone: ", z.ID, " ", z.Name)
9891
} else {
9992
filtered = append(filtered, z)
@@ -163,6 +156,8 @@ func runExporter() {
163156
log.Fatalf("Error creating Cloudflare API client: %s", err)
164157
}
165158

159+
graphqlClient = graphql.NewClient(cfGraphQLEndpoint)
160+
166161
if len(viper.GetString("cf_api_token")) > 0 {
167162
status, err := cloudflareAPI.VerifyAPIToken(context.Background())
168163
if err != nil {
@@ -179,7 +174,7 @@ func runExporter() {
179174
if err != nil {
180175
log.Fatalf("Error building metrics set: %s", err)
181176
}
182-
log.Debugf("Metrics set: %v", metricsSet)
177+
//log.Debugf("Metrics set: %v", metricsSet)
183178
mustRegisterMetrics(metricsSet)
184179

185180
go func() {
@@ -200,7 +195,10 @@ func runExporter() {
200195
}
201196

202197
http.Handle(cfgMetricsPath, promhttp.Handler())
203-
h := health.New(health.Health{})
198+
h := health.New(health.Health{
199+
ReleaseID: version,
200+
Version: versionString,
201+
})
204202
http.HandleFunc("/health", h.Handler)
205203

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

258-
flags.Int("scrape_delay", 300, "scrape delay in seconds0")
256+
flags.Int("scrape_delay", 300, "scrape delay in seconds")
259257
viper.BindEnv("scrape_delay")
260258
viper.SetDefault("scrape_delay", 300)
261259

prometheus.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ var (
7272
Name: buildInfoMetricName.String(),
7373
Help: "A metric with a constant '1' value labeled by version, revision, branch, and goversion from which the cloudflare_exporter was built.",
7474
}, []string{"version", "goversion", "revision"})
75+
7576
// Requests
7677
zoneRequestTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
7778
Name: zoneRequestTotalMetricName.String(),

0 commit comments

Comments
 (0)