Skip to content

Commit

Permalink
🌱 added --debug-hcloud-api-calls (#1332)
Browse files Browse the repository at this point in the history
  • Loading branch information
guettli authored Jun 20, 2024
1 parent 4343d94 commit f7b1343
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func main() {
fs.StringVar(&logLevel, "log-level", "info", "Specifies log level. Options are 'debug', 'info' and 'error'")
fs.DurationVar(&syncPeriod, "sync-period", 3*time.Minute, "The minimum interval at which watched resources are reconciled (e.g. 3m)")
fs.DurationVar(&rateLimitWaitTime, "rate-limit", 5*time.Minute, "The rate limiting for HCloud controller (e.g. 5m)")
fs.BoolVar(&hcloudclient.DebugAPICalls, "debug-hcloud-api-calls", false, "Debug all calls to the hcloud API.")

pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
Expand Down
42 changes: 41 additions & 1 deletion pkg/services/hcloud/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ import (
"context"
"fmt"
"net"
"net/http"
"regexp"
"runtime/debug"
"strings"

"github.com/hetznercloud/hcloud-go/v2/hcloud"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/metrics"

caphversion "github.com/syself/cluster-api-provider-hetzner/pkg/version"
Expand Down Expand Up @@ -77,13 +81,49 @@ type Factory interface {
NewClient(hcloudToken string) Client
}

// LoggingTransport is a struct for creating new logger for hcloud API.
type LoggingTransport struct {
roundTripper http.RoundTripper
hcloudToken string
}

var replaceHex = regexp.MustCompile(`0x[0123456789abcdef]+`)

// RoundTrip is used for logging api calls to hcloud API.
func (lt *LoggingTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
stack := replaceHex.ReplaceAllString(string(debug.Stack()), "0xX")
resp, err = lt.roundTripper.RoundTrip(req)
token := lt.hcloudToken[:5] + "..."
logger := ctrl.LoggerFrom(req.Context()).WithName("hcloud-api")
req.Context()
if err != nil {
logger.V(1).Info("hcloud API called. Error.", "err", err, "method", req.Method, "url", req.URL, "hcloud_token", token, "stack", stack)
return resp, err
}
logger.V(1).Info("hcloud API called", "statusCode", resp.StatusCode, "method", req.Method, "url", req.URL, "hcloud_token", token, "stack", stack)
return resp, nil
}

// DebugAPICalls loggs all hcloud API calls if true.
var DebugAPICalls bool

// NewClient creates new HCloud clients.
func (f *factory) NewClient(hcloudToken string) Client {
return &realClient{client: hcloud.NewClient(
httpClient := &http.Client{}

hcloudClient := realClient{client: hcloud.NewClient(
hcloud.WithToken(hcloudToken),
hcloud.WithApplication("cluster-api-provider-hetzner", caphversion.Get().String()),
hcloud.WithInstrumentation(metrics.Registry),
hcloud.WithHTTPClient(httpClient),
)}
if DebugAPICalls {
httpClient.Transport = &LoggingTransport{
roundTripper: httpClient.Transport,
hcloudToken: hcloudToken,
}
}
return &hcloudClient
}

type factory struct{}
Expand Down

0 comments on commit f7b1343

Please sign in to comment.