Skip to content

Commit c856d09

Browse files
committed
Handle rate limit error on startup.
1 parent 00cdc0d commit c856d09

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module github.com/jbub/foxesscloud_exporter
33
go 1.23
44

55
require (
6-
github.com/jbub/foxesscloud v0.1.0
6+
github.com/jbub/foxesscloud v0.2.0
7+
github.com/oklog/run v1.1.0
78
github.com/prometheus/client_golang v1.20.4
89
github.com/prometheus/common v0.60.0
910
github.com/urfave/cli/v2 v2.27.4
@@ -16,7 +17,6 @@ require (
1617
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
1718
github.com/klauspost/compress v1.17.9 // indirect
1819
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
19-
github.com/oklog/run v1.1.0 // indirect
2020
github.com/prometheus/client_model v0.6.1 // indirect
2121
github.com/prometheus/procfs v0.15.1 // indirect
2222
github.com/russross/blackfriday/v2 v2.1.0 // indirect

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
88
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
99
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
1010
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
11-
github.com/jbub/foxesscloud v0.1.0 h1:TBHGUliVggEizJvGrMJfKRMLABmBb7SyMHQkzKKctHU=
12-
github.com/jbub/foxesscloud v0.1.0/go.mod h1:Lc1eomdGTxjV8UZ04/rnTwG9nJw3EsV/I95oGK+uHWs=
11+
github.com/jbub/foxesscloud v0.2.0 h1:ZF16lcG7hu6KafDVKQKXbRgePM4mSrsMqszxBnrUjWY=
12+
github.com/jbub/foxesscloud v0.2.0/go.mod h1:Lc1eomdGTxjV8UZ04/rnTwG9nJw3EsV/I95oGK+uHWs=
1313
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
1414
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
1515
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=

internal/collector/exporter.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package collector
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"maps"
78
"strings"
@@ -66,7 +67,7 @@ func New(cfg config.Config, log *zap.Logger, client *foxesscloud.Client) (*Expor
6667

6768
func (e *Exporter) Start() error {
6869
ctx := context.Background()
69-
data, err := e.fetchInverters(ctx)
70+
data, err := e.fetchInvertersInitial(ctx)
7071
if err != nil {
7172
return fmt.Errorf("could not fetch inverter data: %w", err)
7273
}
@@ -133,6 +134,21 @@ func (e *Exporter) buildLabels(inverterSN string) prometheus.Labels {
133134
return labels
134135
}
135136

137+
func (e *Exporter) fetchInvertersInitial(ctx context.Context) ([]metricData, error) {
138+
data, err := e.fetchInverters(ctx)
139+
if err != nil {
140+
// in case initial fetch fails on rate limit, we want the program to continue
141+
// the next tick will retry the fetch instead of exiting the program
142+
var errRate *foxesscloud.RateLimitExceededError
143+
if errors.As(err, &errRate) {
144+
e.log.Error("rate limit exceeded", zap.Error(err))
145+
return data, nil
146+
}
147+
return nil, err
148+
}
149+
return data, nil
150+
}
151+
136152
func (e *Exporter) fetchInverters(ctx context.Context) ([]metricData, error) {
137153
ctx, cancel := context.WithTimeout(ctx, e.timeout)
138154
defer cancel()

0 commit comments

Comments
 (0)