Skip to content

Commit

Permalink
fix(cli): fix logger setup
Browse files Browse the repository at this point in the history
  • Loading branch information
schoren committed Sep 2, 2024
1 parent f1014f8 commit cbad736
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 35 deletions.
29 changes: 1 addition & 28 deletions agent/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package runner
import (
"context"
"errors"
"fmt"
"os"

"github.com/golang-jwt/jwt"
Expand All @@ -16,7 +15,6 @@ import (
"github.com/kubeshop/tracetest/cli/pkg/resourcemanager"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

type Runner struct {
Expand All @@ -39,7 +37,7 @@ func NewRunner(configurator config.Configurator, resources *resourcemanager.Regi
}
}

func (s *Runner) Run(ctx context.Context, cfg config.Config, flags agentConfig.Flags, verbose bool) error {
func (s *Runner) Run(ctx context.Context, logger *zap.Logger, cfg config.Config, flags agentConfig.Flags, verbose bool) error {
s.ui.Banner(config.Version)
s.ui.Println(`Tracetest start launches a lightweight agent. It enables you to run tests and collect traces with Tracetest.
Once started, Tracetest Agent exposes OTLP ports 4317 and 4318 to ingest traces via gRCP and HTTP.`)
Expand All @@ -52,31 +50,6 @@ Once started, Tracetest Agent exposes OTLP ports 4317 and 4318 to ingest traces
s.mode = flags.Mode
s.ui.Infof("Running in %s mode...", s.mode)

logger := zap.NewNop()

if enableLogging(flags.LogLevel) {
var err error
atom := zap.NewAtomicLevel()
if verbose {
atom.SetLevel(zapcore.DebugLevel)
}

logger, err = zap.NewDevelopment()
if err != nil {
return fmt.Errorf("could not create logger: %w", err)
}

logger = logger.WithOptions(zap.WrapCore(func(c zapcore.Core) zapcore.Core {
return zapcore.NewCore(
zapcore.NewJSONEncoder(zap.NewDevelopmentEncoderConfig()),
zapcore.Lock(os.Stdout),
atom,
)
}))

s.loggerLevel = &atom
}

s.logger = logger
s.configurator = s.configurator.WithLogger(logger)
oauth.SetLogger(logger)
Expand Down
2 changes: 2 additions & 0 deletions cli/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/kubeshop/tracetest/cli/config"
"github.com/kubeshop/tracetest/cli/formatters"
"github.com/kubeshop/tracetest/cli/openapi"
"github.com/kubeshop/tracetest/cli/pkg/oauth"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -132,6 +133,7 @@ func validateConfig(cmd *cobra.Command, args []string) {
func setupLogger(cmd *cobra.Command, args []string) {
l := cmdutil.GetLogger(cmdutil.WithVerbose(verbose))
*cliLogger = *l
oauth.SetLogger(l)
}

func teardownCommand(cmd *cobra.Command, args []string) {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/start_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ var startCmd = &cobra.Command{
cfg.EnvironmentID = flags.EnvironmentID
}

err = agentRunner.Run(ctx, cliConfig, flags, verbose)
err = agentRunner.Run(ctx, cliLogger, cliConfig, flags, verbose)
return "", err
})),
PostRun: teardownCommand,
Expand Down
33 changes: 27 additions & 6 deletions cli/pkg/oauth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ func SetLogger(l *zap.Logger) {
logger = l
}

type oauthError struct {
err error
msg string
}

func (e oauthError) Error() string {
return e.err.Error()
}

func (e oauthError) Message() string {
return e.msg
}

func ExchangeToken(endpoint string, token string) (string, error) {
logger.Debug("Exchanging token", zap.String("endpoint", endpoint), zap.String("token", token))
req, err := http.NewRequest("GET", fmt.Sprintf("%s/tokens/%s/exchange", endpoint, token), nil)
Expand All @@ -95,16 +108,24 @@ func ExchangeToken(endpoint string, token string) (string, error) {

res, err := http.DefaultClient.Do(req)
if err != nil {
logger.Debug("Failed to exchange token", zap.Error(err))
return "", fmt.Errorf("failed to exchange token: %w", err)
logger.Debug("Cannot create exchange token request", zap.Error(err))
return "", fmt.Errorf("cannot create exchange token request: %w", err)
}
defer res.Body.Close()

if res.StatusCode != http.StatusCreated {
logger.Debug("Failed to exchange token", zap.String("status", res.Status))
return "", fmt.Errorf("failed to exchange token: %s", res.Status)
switch res.StatusCode {
case http.StatusNotFound:
return "", oauthError{err: fmt.Errorf("token not found"), msg: "Token not found"}
case http.StatusUnauthorized:
return "", oauthError{err: fmt.Errorf("token expired"), msg: "Token has expired"}
case http.StatusCreated:
logger.Debug("Token exchanged")
default:
b, _ := io.ReadAll(res.Body)
logger.Debug("Failed to exchange token", zap.String("status", res.Status), zap.String("response", string(b)))
return "", oauthError{err: fmt.Errorf("failed to exchange token: %s", res.Status), msg: "Unexpected error exchanging token"}
}

defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
logger.Debug("Failed to read response body", zap.Error(err))
Expand Down

0 comments on commit cbad736

Please sign in to comment.