Skip to content

Commit

Permalink
Add support for debug logging (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgarber-akamai authored Jan 22, 2024
1 parent 1bd6512 commit 0c5e7be
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 9 deletions.
31 changes: 31 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const (
APIVersion = "v1"
)

type Logger = resty.Logger

// Client represents an instance of a Linode Metadata Service client.
type Client struct {
managedTokenExpiry time.Time
Expand Down Expand Up @@ -91,6 +93,11 @@ func NewClient(ctx context.Context, opts ...ClientOption) (*Client, error) {
result.UseToken(clientOpts.StartingToken)
}

result.resty.SetDebug(clientOpts.Debug)
if clientOpts.DebugLogger != nil {
result.resty.SetLogger(clientOpts.DebugLogger)
}

result.resty.OnBeforeRequest(result.middlewareTokenRefresh)

return &result, nil
Expand Down Expand Up @@ -184,6 +191,9 @@ type clientCreateConfig struct {
ManagedTokenOpts []TokenOption

StartingToken string

Debug bool
DebugLogger Logger
}

// ClientOption is an option that can be used
Expand Down Expand Up @@ -252,3 +262,24 @@ func ClientWithToken(token string) ClientOption {
options.StartingToken = token
}
}

// ClientWithDebug enables debug mode for the metadata client.
// If this option is specified, all metadata service API requests
// and responses will be written to the client logger (default: stderr).
//
// To override the client logger, refer to ClientWithLogger.
func ClientWithDebug() ClientOption {
return func(options *clientCreateConfig) {
options.Debug = true
}
}

// ClientWithLogger specifies the logger that should be used
// when outputting debug logs. The logger argument should implement
// the Logger interface.
// Default: stderr
func ClientWithLogger(logger Logger) ClientOption {
return func(options *clientCreateConfig) {
options.DebugLogger = logger
}
}
3 changes: 2 additions & 1 deletion examples/instancewatcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

import (
"context"
metadata "github.com/linode/go-metadata"
"log"
"time"

metadata "github.com/linode/go-metadata"
)

func main() {
Expand Down
3 changes: 2 additions & 1 deletion examples/networkwatcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

import (
"context"
metadata "github.com/linode/go-metadata"
"log"
"time"

metadata "github.com/linode/go-metadata"
)

func main() {
Expand Down
26 changes: 24 additions & 2 deletions test/integration/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package integration

import (
"context"
"github.com/linode/go-metadata"
"github.com/stretchr/testify/assert"
"testing"
"time"

"github.com/linode/go-metadata"
"github.com/stretchr/testify/assert"
)

func TestClient_UnmanagedTokenExpired(t *testing.T) {
Expand Down Expand Up @@ -42,3 +43,24 @@ func TestClient_ManagedTokenRefresh(t *testing.T) {
_, err = mdClient.GetInstance(context.Background())
assert.NoError(t, err)
}

func TestClient_DebugLogs(t *testing.T) {
var logger testLogger

mdClient, err := metadata.NewClient(
context.Background(),
metadata.ClientWithDebug(),
metadata.ClientWithLogger(&logger),
)
assert.NoError(t, err)

_, err = mdClient.GetInstance(context.Background())
assert.NoError(t, err)

debugOutput := logger.Data.String()

// Validate a few arbitrary bits of the debug output
assert.Contains(t, debugOutput, "User-Agent: go-metadata")
assert.Contains(t, debugOutput, "/v1/token")
assert.Contains(t, debugOutput, "/v1/instance")
}
8 changes: 5 additions & 3 deletions test/integration/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
"github.com/linode/linodego"
)

var testToken = os.Getenv("LINODE_TOKEN")
var metadataClient *metadata.Client
var linodeClient *linodego.Client
var (
testToken = os.Getenv("LINODE_TOKEN")
metadataClient *metadata.Client
linodeClient *linodego.Client
)

func init() {
if testToken == "" {
Expand Down
3 changes: 2 additions & 1 deletion test/integration/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package integration

import (
"context"
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetInstance(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion test/integration/userdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package integration

import (
"context"
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetUserData(t *testing.T) {
Expand Down
22 changes: 22 additions & 0 deletions test/integration/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package integration

import (
"bytes"
"fmt"
)

type testLogger struct {
Data bytes.Buffer
}

func (l *testLogger) Errorf(format string, v ...interface{}) {
l.Data.WriteString("[ERROR] " + fmt.Sprintf(format, v...))
}

func (l *testLogger) Warnf(format string, v ...interface{}) {
l.Data.WriteString("[WARN] " + fmt.Sprintf(format, v...))
}

func (l *testLogger) Debugf(format string, v ...interface{}) {
l.Data.WriteString("[DEBUG] " + fmt.Sprintf(format, v...))
}

0 comments on commit 0c5e7be

Please sign in to comment.