Skip to content

Commit

Permalink
Enable log levels
Browse files Browse the repository at this point in the history
  • Loading branch information
levaitamas committed Nov 26, 2024
1 parent bf61671 commit 07da6a5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.18'
go-version: '1.21'
id: go

- name: Check out code into the Go module directory
Expand Down
56 changes: 28 additions & 28 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"crypto/tls"
"fmt"
"io"
"log"
"log/slog"
"math"
"math/rand"
"net/http"
Expand Down Expand Up @@ -154,19 +154,19 @@ func (client *Client) Do(req Req) (Res, error) {
for attempts := 0; ; attempts++ {
req.HttpReq.Body = io.NopCloser(bytes.NewBuffer(body))
if req.LogPayload {
log.Printf("[DEBUG] HTTP Request: %s, %s, %s", req.HttpReq.Method, req.HttpReq.URL, req.HttpReq.Body)
slog.Debug(fmt.Sprintf("HTTP Request: %s, %s, %s", req.HttpReq.Method, req.HttpReq.URL, req.HttpReq.Body))
} else {
log.Printf("[DEBUG] HTTP Request: %s, %s", req.HttpReq.Method, req.HttpReq.URL)
slog.Debug(fmt.Sprintf("HTTP Request: %s, %s", req.HttpReq.Method, req.HttpReq.URL))
}

httpRes, err := client.HttpClient.Do(req.HttpReq)
if err != nil {
if ok := client.Backoff(attempts); !ok {
log.Printf("[ERROR] HTTP Connection error occured: %+v", err)
log.Printf("[DEBUG] Exit from Do method")
slog.Error(fmt.Sprintf("HTTP Connection error occured: %+v", err))
slog.Debug("Exit from Do method")
return Res{}, err
} else {
log.Printf("[ERROR] HTTP Connection failed: %s, retries: %v", err, attempts)
slog.Error(fmt.Sprintf("HTTP Connection failed: %s, retries: %v", err, attempts))
continue
}
}
Expand All @@ -175,26 +175,26 @@ func (client *Client) Do(req Req) (Res, error) {
bodyBytes, err := io.ReadAll(httpRes.Body)
if err != nil {
if ok := client.Backoff(attempts); !ok {
log.Printf("[ERROR] Cannot decode response body: %+v", err)
log.Printf("[DEBUG] Exit from Do method")
slog.Error(fmt.Sprintf("Cannot decode response body: %+v", err))
slog.Debug("Exit from Do method")
return Res{}, err
} else {
log.Printf("[ERROR] Cannot decode response body: %s, retries: %v", err, attempts)
slog.Error(fmt.Sprintf("Cannot decode response body: %s, retries: %v", err, attempts))
continue
}
}
res = Res(gjson.ParseBytes(bodyBytes))
if req.LogPayload {
log.Printf("[DEBUG] HTTP Response: %s", res.Raw)
slog.Debug(fmt.Sprintf("HTTP Response: %s", res.Raw))
}

if httpRes.StatusCode >= 200 && httpRes.StatusCode <= 299 {
log.Printf("[DEBUG] Exit from Do method")
slog.Debug("Exit from Do method")
break
} else {
if ok := client.Backoff(attempts); !ok {
log.Printf("[ERROR] HTTP Request failed: StatusCode %v", httpRes.StatusCode)
log.Printf("[DEBUG] Exit from Do method")
slog.Error(fmt.Sprintf("HTTP Request failed: StatusCode %v", httpRes.StatusCode))
slog.Debug("Exit from Do method")
return res, fmt.Errorf("HTTP Request failed: StatusCode %v", httpRes.StatusCode)
} else if httpRes.StatusCode == 429 {
retryAfter := httpRes.Header.Get("Retry-After")
Expand All @@ -206,23 +206,23 @@ func (client *Client) Do(req Req) (Res, error) {
} else {
retryAfterDuration = 15 * time.Second
}
log.Printf("[WARNING] HTTP Request rate limited, waiting %v seconds, Retries: %v", retryAfterDuration.Seconds(), attempts)
slog.Warn(fmt.Sprintf("HTTP Request rate limited, waiting %v seconds, Retries: %v", retryAfterDuration.Seconds(), attempts))
time.Sleep(retryAfterDuration)
continue
} else if httpRes.StatusCode == 408 || (httpRes.StatusCode >= 500 && httpRes.StatusCode <= 599) {
log.Printf("[ERROR] HTTP Request failed: StatusCode %v, Retries: %v", httpRes.StatusCode, attempts)
slog.Error(fmt.Sprintf("HTTP Request failed: StatusCode %v, Retries: %v", httpRes.StatusCode, attempts))
continue
} else {
log.Printf("[ERROR] HTTP Request failed: StatusCode %v", httpRes.StatusCode)
log.Printf("[DEBUG] Exit from Do method")
slog.Error(fmt.Sprintf("HTTP Request failed: StatusCode %v", httpRes.StatusCode))
slog.Debug("Exit from Do method")
return res, fmt.Errorf("HTTP Request failed: StatusCode %v", httpRes.StatusCode)
}
}
}

errCode := res.Get("error.code").Str
if errCode != "" {
log.Printf("[ERROR] JSON error: %s", res.Raw)
slog.Error(fmt.Sprintf("JSON error: %s", res.Raw))
return res, fmt.Errorf("JSON error: %s", res.Raw)
}
return res, nil
Expand Down Expand Up @@ -295,17 +295,17 @@ func (client *Client) Login() error {
return err
}
if httpRes.StatusCode != 200 {
log.Printf("[ERROR] Authentication failed: StatusCode %v", httpRes.StatusCode)
slog.Error(fmt.Sprintf("Authentication failed: StatusCode %v", httpRes.StatusCode))
return fmt.Errorf("authentication failed, status code: %v", httpRes.StatusCode)
}
defer httpRes.Body.Close()
bodyBytes, _ := io.ReadAll(httpRes.Body)
if len(bodyBytes) > 0 {
if ok := client.Backoff(attempts); !ok {
log.Printf("[ERROR] Authentication failed: Invalid credentials")
slog.Error("Authentication failed: Invalid credentials")
return fmt.Errorf("authentication failed, invalid credentials")
} else {
log.Printf("[ERROR] Authentication failed: %s, retries: %v", err, attempts)
slog.Error(fmt.Sprintf("Authentication failed: %s, retries: %v", err, attempts))
continue
}
}
Expand All @@ -315,17 +315,17 @@ func (client *Client) Login() error {
return err
}
if httpRes.StatusCode != 200 {
log.Printf("[ERROR] Token retrieval failed: StatusCode %v", httpRes.StatusCode)
slog.Error(fmt.Sprintf("Token retrieval failed: StatusCode %v", httpRes.StatusCode))
return fmt.Errorf("authentication failed, token retrieval, status code: %v", httpRes.StatusCode)
}
defer httpRes.Body.Close()
token, _ := io.ReadAll(httpRes.Body)
if string(token) == "" {
log.Printf("[ERROR] Token retrieval failed: no token in payload")
slog.Error("Token retrieval failed: no token in payload")
return fmt.Errorf("authentication failed, no token in payload")
}
client.Token = string(token)
log.Printf("[DEBUG] Authentication successful")
slog.Debug("Authentication successful")
return nil
}
}
Expand All @@ -343,9 +343,9 @@ func (client *Client) Authenticate() error {

// Backoff waits following an exponential backoff algorithm
func (client *Client) Backoff(attempts int) bool {
log.Printf("[DEBUG] Begining backoff method: attempts %v on %v", attempts, client.MaxRetries)
slog.Debug(fmt.Sprintf("Begining backoff method: attempts %v on %v", attempts, client.MaxRetries))
if attempts >= client.MaxRetries {
log.Printf("[DEBUG] Exit from backoff method with return value false")
slog.Debug("Exit from backoff method with return value false")
return false
}

Expand All @@ -359,8 +359,8 @@ func (client *Client) Backoff(attempts int) bool {
}
backoff = (rand.Float64()/2+0.5)*(backoff-min) + min
backoffDuration := time.Duration(backoff)
log.Printf("[TRACE] Starting sleeping for %v", backoffDuration.Round(time.Second))
slog.Debug(fmt.Sprintf("Starting sleeping for %v", backoffDuration.Round(time.Second)))
time.Sleep(backoffDuration)
log.Printf("[DEBUG] Exit from backoff method with return value true")
slog.Debug("Exit from backoff method with return value true")
return true
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/netascode/go-sdwan

go 1.18
go 1.21

require (
github.com/stretchr/testify v1.9.0
Expand Down

0 comments on commit 07da6a5

Please sign in to comment.