Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve agent startup #3191

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion agent/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ var StartCmd = cobra.Command{

log.Printf("starting agent [%s] connecting to %s", cfg.Name, cfg.ServerURL)

err = initialization.Start(ctx, cfg)
session, err := initialization.Start(ctx, cfg)
if err != nil {
fmt.Fprintln(os.Stderr, err)
ExitCLI(1)
}

session.WaitUntilDisconnected()
session.Close()
},
}

Expand Down
16 changes: 16 additions & 0 deletions agent/initialization/session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package initialization

import "github.com/kubeshop/tracetest/agent/client"

type Session struct {
Token string
client *client.Client
}

func (s *Session) Close() {
s.client.Close()
}

func (s *Session) WaitUntilDisconnected() {
s.client.WaitUntilDisconnected()
}
14 changes: 8 additions & 6 deletions agent/initialization/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,27 @@ func NewClient(ctx context.Context, config config.Config, traceCache collector.T
}

// Start the agent with given configuration
func Start(ctx context.Context, config config.Config) error {
func Start(ctx context.Context, config config.Config) (*Session, error) {
traceCache := collector.NewTraceCache()
client, err := NewClient(ctx, config, traceCache)
if err != nil {
return err
return nil, err
}

err = client.Start(ctx)
if err != nil {
return err
return nil, err
}

err = StartCollector(ctx, config, traceCache)
if err != nil {
return err
return nil, err
}

client.WaitUntilDisconnected()
return nil
return &Session{
client: client,
Token: client.SessionConfiguration().AgentIdentification.Token,
}, nil
}

func StartCollector(ctx context.Context, config config.Config, traceCache collector.TraceCache) error {
Expand Down
12 changes: 11 additions & 1 deletion cli/cmd/start_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"

agentConfig "github.com/kubeshop/tracetest/agent/config"
"github.com/kubeshop/tracetest/cli/config"
"github.com/kubeshop/tracetest/cli/pkg/starter"
"github.com/spf13/cobra"
Expand All @@ -29,7 +30,16 @@ var startCmd = &cobra.Command{
AgentApiKey: saveParams.agentApiKey,
}

err := start.Run(ctx, cliConfig, flags)
cfg, err := agentConfig.LoadConfig()
if err != nil {
return "", err
}

if cfg.APIKey != "" {
flags.AgentApiKey = cfg.APIKey
}

err = start.Run(ctx, cliConfig, flags)
return "", err
})),
PostRun: teardownCommand,
Expand Down
37 changes: 14 additions & 23 deletions cli/pkg/starter/starter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"

"github.com/golang-jwt/jwt/v4"
"github.com/kubeshop/tracetest/agent/collector"
agentConfig "github.com/kubeshop/tracetest/agent/config"
"github.com/kubeshop/tracetest/agent/initialization"

Expand Down Expand Up @@ -34,7 +33,7 @@ func (s *Starter) Run(ctx context.Context, cfg config.Config, flags config.Confi

func (s *Starter) onStartAgent(ctx context.Context, cfg config.Config) {
if cfg.AgentApiKey != "" {
err := s.StartAgent(ctx, cfg.AgentEndpoint, "local", cfg.AgentApiKey, cfg.UIEndpoint)
err := s.StartAgent(ctx, cfg.AgentEndpoint, cfg.AgentApiKey, cfg.UIEndpoint)
if err != nil {
s.ui.Error(err.Error())
}
Expand All @@ -51,7 +50,7 @@ func (s *Starter) onStartAgent(ctx context.Context, cfg config.Config) {
Connecting Agent with name %s to Organization %s and Environment %s
`, "local", cfg.OrganizationID, env.Name))

err = s.StartAgent(ctx, cfg.AgentEndpoint, "local", env.AgentApiKey, cfg.UIEndpoint)
err = s.StartAgent(ctx, cfg.AgentEndpoint, env.AgentApiKey, cfg.UIEndpoint)
if err != nil {
s.ui.Error(err.Error())
}
Expand Down Expand Up @@ -95,35 +94,27 @@ func (s *Starter) getEnvironment(ctx context.Context, cfg config.Config) (enviro
return env, nil
}

func (s *Starter) StartAgent(ctx context.Context, endpoint, name, agentApiKey, uiEndpoint string) error {
cfg := agentConfig.Config{
ServerURL: endpoint,
APIKey: agentApiKey,
Name: name,
OTLPServer: agentConfig.OtlpServer{
GRPCPort: 4317,
HTTPPort: 4318,
},
}

s.ui.Info(fmt.Sprintf("Starting Agent with name %s...", name))
cache := collector.NewTraceCache()
client, err := initialization.NewClient(ctx, cfg, cache)
func (s *Starter) StartAgent(ctx context.Context, endpoint, agentApiKey, uiEndpoint string) error {
cfg, err := agentConfig.LoadConfig()
if err != nil {
return err
}

err = client.Start(ctx)
if err != nil {
return err
if endpoint != "" {
cfg.ServerURL = endpoint
}

if agentApiKey != "" {
cfg.APIKey = agentApiKey
}

err = initialization.StartCollector(ctx, cfg, cache)
s.ui.Info(fmt.Sprintf("Starting Agent with name %s...", cfg.Name))
session, err := initialization.Start(ctx, cfg)
if err != nil {
return err
}

claims, err := s.getTokenClaims(client.SessionConfiguration().AgentIdentification.Token)
claims, err := s.getTokenClaims(session.Token)
if err != nil {
return err
}
Expand All @@ -140,7 +131,7 @@ func (s *Starter) StartAgent(ctx context.Context, endpoint, name, agentApiKey, u
Text: "Stop this agent",
Fn: func(_ ui.UI) {
isOpen = false
go client.Close()
session.Close()
},
}}

Expand Down