Skip to content

Commit

Permalink
chore: Migrate start actions from CLI to agent (#3378)
Browse files Browse the repository at this point in the history
* wip

* chore: starting to decouple agent and cli

* fix agent tests

* update starter
  • Loading branch information
danielbdias committed Nov 20, 2023
1 parent 54bef84 commit d0e3702
Show file tree
Hide file tree
Showing 17 changed files with 547 additions and 339 deletions.
42 changes: 0 additions & 42 deletions agent/cmd/root.go

This file was deleted.

46 changes: 0 additions & 46 deletions agent/cmd/start.go

This file was deleted.

18 changes: 18 additions & 0 deletions agent/config/flags.go
@@ -0,0 +1,18 @@
package config

type Mode string

const (
Mode_Desktop Mode = "desktop"
Mode_Verbose Mode = "verbose"
)

type Flags struct {
Endpoint string
OrganizationID string
EnvironmentID string
CI bool
AgentApiKey string
Token string
Mode Mode
}
16 changes: 0 additions & 16 deletions agent/initialization/session.go

This file was deleted.

9 changes: 0 additions & 9 deletions agent/main.go

This file was deleted.

48 changes: 48 additions & 0 deletions agent/runner/environment.go
@@ -0,0 +1,48 @@
package runner

import (
"context"
"encoding/json"
"fmt"

"github.com/kubeshop/tracetest/cli/config"
"github.com/kubeshop/tracetest/cli/pkg/resourcemanager"
)

type environment struct {
ID string `json:"id"`
Name string `json:"name"`
AgentApiKey string `json:"agentApiKey"`
OrganizationID string `json:"organizationID"`
}

func (s *Runner) getEnvironment(ctx context.Context, cfg config.Config) (environment, error) {
resource, err := s.resources.Get("env")
if err != nil {
return environment{}, err
}

resource = resource.
WithHttpClient(config.SetupHttpClient(cfg)).
WithOptions(resourcemanager.WithPrefixGetter(func() string {
return fmt.Sprintf("/organizations/%s/", cfg.OrganizationID)
}))

resultFormat, err := resourcemanager.Formats.GetWithFallback("json", "json")
if err != nil {
return environment{}, err
}

raw, err := resource.Get(ctx, cfg.EnvironmentID, resultFormat)
if err != nil {
return environment{}, err
}

var env environment
err = json.Unmarshal([]byte(raw), &env)
if err != nil {
return environment{}, err
}

return env, nil
}
86 changes: 86 additions & 0 deletions agent/runner/runner.go
@@ -0,0 +1,86 @@
package runner

import (
"context"

agentConfig "github.com/kubeshop/tracetest/agent/config"
"github.com/kubeshop/tracetest/agent/ui"

"github.com/kubeshop/tracetest/cli/config"
"github.com/kubeshop/tracetest/cli/pkg/resourcemanager"
)

type Runner struct {
configurator config.Configurator
resources *resourcemanager.Registry
ui ui.ConsoleUI
mode agentConfig.Mode
}

func NewRunner(configurator config.Configurator, resources *resourcemanager.Registry, ui ui.ConsoleUI) *Runner {
return &Runner{
configurator: configurator,
resources: resources,
ui: ui,
mode: agentConfig.Mode_Desktop,
}
}

func (s *Runner) Run(ctx context.Context, cfg config.Config, flags agentConfig.Flags) 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.`)

if flags.Token == "" || flags.AgentApiKey != "" {
s.configurator = s.configurator.WithOnFinish(s.onStartAgent)
}

s.ui.Infof("Running in %s mode...", s.mode)

s.mode = flags.Mode

return s.configurator.Start(ctx, cfg, flags)
}

func (s *Runner) onStartAgent(ctx context.Context, cfg config.Config) {
if cfg.AgentApiKey != "" {
err := s.StartAgent(ctx, cfg.AgentEndpoint, cfg.AgentApiKey, cfg.UIEndpoint)
if err != nil {
s.ui.Error(err.Error())
}

return
}

env, err := s.getEnvironment(ctx, cfg)
if err != nil {
s.ui.Error(err.Error())
}

err = s.StartAgent(ctx, cfg.AgentEndpoint, env.AgentApiKey, cfg.UIEndpoint)
if err != nil {
s.ui.Error(err.Error())
}
}

func (s *Runner) StartAgent(ctx context.Context, endpoint, agentApiKey, uiEndpoint string) error {
cfg, err := agentConfig.LoadConfig()
if err != nil {
return err
}

if endpoint != "" {
cfg.ServerURL = endpoint
}

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

if s.mode == agentConfig.Mode_Desktop {
return RunDesktopStrategy(ctx, cfg, s.ui, uiEndpoint)
}

// TODO: Add verbose strategy
return nil
}
70 changes: 70 additions & 0 deletions agent/runner/runstrategy_desktop.go
@@ -0,0 +1,70 @@
package runner

import (
"context"
"errors"
"fmt"

agentConfig "github.com/kubeshop/tracetest/agent/config"
"github.com/kubeshop/tracetest/cli/config"

consoleUI "github.com/kubeshop/tracetest/agent/ui"
)

func RunDesktopStrategy(ctx context.Context, cfg agentConfig.Config, ui consoleUI.ConsoleUI, uiEndpoint string) error {
ui.Infof("Starting Agent with name %s...", cfg.Name)

isStarted := false
session := &Session{}

var err error

for !isStarted {
session, err = StartSession(ctx, cfg)
if err != nil && errors.Is(err, ErrOtlpServerStart) {
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 := ui.Enter("Please stop the process currently listening on these ports and press enter to try again.")

if !shouldRetry {
ui.Finish()
return err
}

continue
}

if err != nil {
return err
}

isStarted = true
}

claims, err := config.GetTokenClaims(session.Token)
if err != nil {
return err
}

isOpen := true
message := `Agent is started! Leave the terminal open so tests can be run and traces gathered from this environment.
You can`
options := []consoleUI.Option{{
Text: "Open Tracetest in a browser to this environment",
Fn: func(_ consoleUI.ConsoleUI) {
ui.OpenBrowser(fmt.Sprintf("%sorganizations/%s/environments/%s/dashboard", uiEndpoint, claims["organization_id"], claims["environment_id"]))
},
}, {
Text: "Stop this agent",
Fn: func(_ consoleUI.ConsoleUI) {
isOpen = false
session.Close()
ui.Finish()
},
}}

for isOpen {
selected := ui.Select(message, options, 0)
selected.Fn(ui)
}
return nil
}

0 comments on commit d0e3702

Please sign in to comment.