Skip to content

Commit 509a7e8

Browse files
committed
Instrument healthChecker with Prometheus metrics
1 parent b323b34 commit 509a7e8

File tree

3 files changed

+59
-49
lines changed

3 files changed

+59
-49
lines changed

cloud/linode/client/client_with_metrics.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cloud/linode/cloud.go

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,9 @@ func init() {
6868
})
6969
}
7070

71-
func initLinodeClient() (client.Client, error) {
72-
// Read environment variables (from secrets)
73-
apiToken := os.Getenv(accessTokenEnv)
74-
if apiToken == "" {
75-
return nil, fmt.Errorf("%s must be set in the environment (use a k8s secret)", accessTokenEnv)
76-
}
77-
78-
// set timeout used by linodeclient for API calls
79-
timeout := client.DefaultClientTimeout
80-
if raw, ok := os.LookupEnv("LINODE_REQUEST_TIMEOUT_SECONDS"); ok {
81-
if t, err := strconv.Atoi(raw); err == nil && t > 0 {
82-
timeout = time.Duration(t) * time.Second
83-
}
84-
}
85-
71+
// newLinodeClientWithPrometheus creates a new client kept in its own local
72+
// scope and returns an instrumented one that should be used and passed around
73+
func newLinodeClientWithPrometheus(apiToken string, timeout time.Duration) (client.Client, error) {
8674
linodeClient, err := client.New(apiToken, timeout)
8775
if err != nil {
8876
return nil, fmt.Errorf("client was not created succesfully: %w", err)
@@ -92,33 +80,6 @@ func initLinodeClient() (client.Client, error) {
9280
linodeClient.SetDebug(true)
9381
}
9482

95-
var healthChecker *healthChecker
96-
97-
if Options.EnableTokenHealthChecker {
98-
authenticated, err := client.CheckClientAuthenticated(context.TODO(), linodeClient)
99-
if err != nil {
100-
return nil, fmt.Errorf("linode client authenticated connection error: %w", err)
101-
}
102-
103-
if !authenticated {
104-
return nil, fmt.Errorf("linode api token %q is invalid", accessTokenEnv)
105-
}
106-
107-
healthChecker, err = newHealthChecker(apiToken, timeout, tokenHealthCheckPeriod, Options.GlobalStopChannel)
108-
if err != nil {
109-
return nil, fmt.Errorf("unable to initialize healthchecker: %w", err)
110-
}
111-
}
112-
113-
if Options.VPCName != "" && Options.VPCNames != "" {
114-
return nil, fmt.Errorf("cannot have both vpc-name and vpc-names set")
115-
}
116-
117-
if Options.VPCName != "" {
118-
klog.Warningf("vpc-name flag is deprecated. Use vpc-names instead")
119-
Options.VPCNames = Options.VPCName
120-
}
121-
12283
return client.NewClientWithPrometheus(linodeClient), nil
12384
}
12485

@@ -128,7 +89,21 @@ func newCloud() (cloudprovider.Interface, error) {
12889
return nil, fmt.Errorf("%s must be set in the environment (use a k8s secret)", regionEnv)
12990
}
13091

131-
linodeClient, err := initLinodeClient()
92+
// Read environment variables (from secrets)
93+
apiToken := os.Getenv(accessTokenEnv)
94+
if apiToken == "" {
95+
return nil, fmt.Errorf("%s must be set in the environment (use a k8s secret)", accessTokenEnv)
96+
}
97+
98+
// set timeout used by linodeclient for API calls
99+
timeout := client.DefaultClientTimeout
100+
if raw, ok := os.LookupEnv("LINODE_REQUEST_TIMEOUT_SECONDS"); ok {
101+
if t, err := strconv.Atoi(raw); err == nil && t > 0 {
102+
timeout = time.Duration(t) * time.Second
103+
}
104+
}
105+
106+
linodeClient, err := newLinodeClientWithPrometheus(apiToken, timeout)
132107
if err != nil {
133108
return nil, err
134109
}
@@ -157,6 +132,33 @@ func newCloud() (cloudprovider.Interface, error) {
157132
return nil, fmt.Errorf("%s", msg)
158133
}
159134

135+
var healthChecker *healthChecker
136+
137+
if Options.EnableTokenHealthChecker {
138+
authenticated, err := client.CheckClientAuthenticated(context.TODO(), linodeClient)
139+
if err != nil {
140+
return nil, fmt.Errorf("linode client authenticated connection error: %w", err)
141+
}
142+
143+
if !authenticated {
144+
return nil, fmt.Errorf("linode api token %q is invalid", accessTokenEnv)
145+
}
146+
147+
healthChecker, err = newHealthChecker(linodeClient, timeout, tokenHealthCheckPeriod, Options.GlobalStopChannel)
148+
if err != nil {
149+
return nil, fmt.Errorf("unable to initialize healthchecker: %w", err)
150+
}
151+
}
152+
153+
if Options.VPCName != "" && Options.VPCNames != "" {
154+
return nil, fmt.Errorf("cannot have both vpc-name and vpc-names set")
155+
}
156+
157+
if Options.VPCName != "" {
158+
klog.Warningf("vpc-name flag is deprecated. Use vpc-names instead")
159+
Options.VPCNames = Options.VPCName
160+
}
161+
160162
// create struct that satisfies cloudprovider.Interface
161163
lcloud := &linodeCloud{
162164
client: linodeClient,

cloud/linode/health_check.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@ type healthChecker struct {
1515
stopCh chan<- struct{}
1616
}
1717

18-
func newHealthChecker(apiToken string, timeout time.Duration, period time.Duration, stopCh chan<- struct{}) (*healthChecker, error) {
19-
client, err := client.New(apiToken, timeout)
20-
if err != nil {
21-
return nil, err
22-
}
23-
18+
func newHealthChecker(client client.Client, timeout time.Duration, period time.Duration, stopCh chan<- struct{}) (*healthChecker, error) {
2419
return &healthChecker{
2520
period: period,
2621
linodeClient: client,

0 commit comments

Comments
 (0)