diff --git a/agent/initialization/start.go b/agent/initialization/start.go index 6b876f36cd..44dbe3810d 100644 --- a/agent/initialization/start.go +++ b/agent/initialization/start.go @@ -2,6 +2,7 @@ package initialization import ( "context" + "errors" "fmt" "github.com/kubeshop/tracetest/agent/client" @@ -13,6 +14,8 @@ import ( "go.opentelemetry.io/otel/trace" ) +var ErrOtlpServerStart = errors.New("OTLP server start error") + func NewClient(ctx context.Context, config config.Config, traceCache collector.TraceCache) (*client.Client, error) { client, err := client.Connect(ctx, config.ServerURL, client.WithAPIKey(config.APIKey), @@ -72,7 +75,7 @@ func StartCollector(ctx context.Context, config config.Config, traceCache collec _, err := collector.Start(ctx, collectorConfig, noopTracer, collector.WithTraceCache(traceCache), collector.WithStartRemoteServer(false)) if err != nil { - return err + return ErrOtlpServerStart } return nil diff --git a/cli/pkg/starter/starter.go b/cli/pkg/starter/starter.go index dd8bef56d6..dcc89060bf 100644 --- a/cli/pkg/starter/starter.go +++ b/cli/pkg/starter/starter.go @@ -3,6 +3,7 @@ package starter import ( "context" "encoding/json" + "errors" "fmt" "github.com/golang-jwt/jwt/v4" @@ -106,11 +107,29 @@ func (s *Starter) StartAgent(ctx context.Context, endpoint, agentApiKey, uiEndpo cfg.APIKey = agentApiKey } - s.ui.Println(fmt.Sprintf(` -Starting Agent with name %s...`, cfg.Name)) - session, err := initialization.Start(ctx, cfg) - if err != nil { - return err + s.ui.Info(fmt.Sprintf("Starting Agent with name %s...", cfg.Name)) + + isStarted := false + session := &initialization.Session{} + for !isStarted { + session, err = initialization.Start(ctx, cfg) + if err != nil && errors.Is(err, initialization.ErrOtlpServerStart) { + s.ui.Error("Tracetest Agent binds to the OpenTelemetry ports 4317 and 4318 which are used to receive trace information from your system. The agent tried to bind to these ports, but failed.") + shouldRetry := s.ui.Enter("Please stop the process currently listening on these ports and press enter to try again.") + + if !shouldRetry { + s.ui.Finish() + return err + } + + continue + } + + if err != nil { + return err + } + + isStarted = true } claims, err := s.getTokenClaims(session.Token)