Skip to content

Commit

Permalink
feat(agent): allow user to define environment id using `--environment…
Browse files Browse the repository at this point in the history
…` flag (#3840)

* agent: update connection request to support env id

* agent: send environment id to authentication endpoint
  • Loading branch information
mathnogueira authored May 3, 2024
1 parent 9c3bbb0 commit 1e7ebca
Show file tree
Hide file tree
Showing 10 changed files with 619 additions and 583 deletions.
12 changes: 7 additions & 5 deletions agent/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ const (
)

type Config struct {
APIKey string
AgentName string
PingPeriod time.Duration
APIKey string
AgentName string
EnvironmentID string
PingPeriod time.Duration
}

type SessionConfig struct {
Expand Down Expand Up @@ -170,8 +171,9 @@ func (c *Client) getConnectionRequest() (*proto.ConnectRequest, error) {
}

request := proto.ConnectRequest{
ApiKey: c.config.APIKey,
Name: name,
ApiKey: c.config.APIKey,
EnvironmentID: c.config.EnvironmentID,
Name: name,
}

return &request, nil
Expand Down
6 changes: 6 additions & 0 deletions agent/client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ func WithAgentName(name string) Option {
}
}

func WithEnvironmentID(id string) Option {
return func(c *Client) {
c.config.EnvironmentID = id
}
}

func WithPingPeriod(period time.Duration) Option {
return func(c *Client) {
c.config.PingPeriod = period
Expand Down
2 changes: 2 additions & 0 deletions agent/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type Config struct {
APIKey string `mapstructure:"api_key"`
Name string `mapstructure:"agent_name"`
EnvironmentID string `mapstructure:"environment_id"`
ServerURL string `mapstructure:"server_url"`
CollectorEndpoint string `mapstructure:"collector_endpoint"`
Mode string `mapstructure:"mode"`
Expand Down Expand Up @@ -46,6 +47,7 @@ func LoadConfig() (Config, error) {

vp.SetDefault("AGENT_NAME", getHostname())
vp.SetDefault("API_KEY", "")
vp.SetDefault("ENVIRONMENT_ID", "")
vp.SetDefault("SERVER_URL", "https://app.tracetest.io")
vp.SetDefault("COLLECTOR_ENDPOINT", "")
vp.SetDefault("MODE", "")
Expand Down
4 changes: 4 additions & 0 deletions agent/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestConfigDefaults(t *testing.T) {

assert.Equal(t, "", cfg.APIKey)
assert.Equal(t, hostname, cfg.Name)
assert.Empty(t, cfg.EnvironmentID)
assert.Equal(t, "https://app.tracetest.io", cfg.ServerURL)
assert.Equal(t, 4317, cfg.OTLPServer.GRPCPort)
assert.Equal(t, 4318, cfg.OTLPServer.HTTPPort)
Expand All @@ -31,10 +32,12 @@ func TestConfigWithEnvs(t *testing.T) {
os.Unsetenv("TRACETEST_SERVER_URL")
os.Unsetenv("TRACETEST_OTLP_SERVER_GRPC_PORT")
os.Unsetenv("TRACETEST_OTLP_SERVER_HTTP_PORT")
os.Unsetenv("TRACETEST_ENVIRONMENT_ID")
})

os.Setenv("TRACETEST_AGENT_NAME", "my-agent-name")
os.Setenv("TRACETEST_API_KEY", "my-agent-api-key")
os.Setenv("TRACETEST_ENVIRONMENT_ID", "123456")
os.Setenv("TRACETEST_DEV_MODE", "true")
os.Setenv("TRACETEST_SERVER_URL", "https://custom.server.com")
os.Setenv("TRACETEST_OTLP_SERVER_GRPC_PORT", "1234")
Expand All @@ -46,6 +49,7 @@ func TestConfigWithEnvs(t *testing.T) {

assert.Equal(t, "my-agent-api-key", cfg.APIKey)
assert.Equal(t, "my-agent-name", cfg.Name)
assert.Equal(t, "123456", cfg.EnvironmentID)
assert.Equal(t, "https://custom.server.com", cfg.ServerURL)
assert.Equal(t, 1234, cfg.OTLPServer.GRPCPort)
assert.Equal(t, 1235, cfg.OTLPServer.HTTPPort)
Expand Down
1,158 changes: 584 additions & 574 deletions agent/proto/orchestrator.pb.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions agent/proto/orchestrator.proto
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ message StopRequest {
message ConnectRequest {
string apiKey = 1;
string name = 2;
string environmentID = 3;
}

// ConnectResponse is the Orchestrator response to the ConnectRequest. It contains
Expand Down
2 changes: 1 addition & 1 deletion agent/proto/orchestrator_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions agent/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Once started, Tracetest Agent exposes OTLP ports 4317 and 4318 to ingest traces

func (s *Runner) onStartAgent(ctx context.Context, cfg config.Config) {
if cfg.AgentApiKey != "" {
err := s.StartAgent(ctx, cfg.AgentEndpoint, cfg.AgentApiKey, cfg.UIEndpoint)
err := s.StartAgent(ctx, cfg.AgentEndpoint, cfg.AgentApiKey, cfg.UIEndpoint, cfg.EnvironmentID)
if err != nil {
s.ui.Error(err.Error())
}
Expand All @@ -106,13 +106,13 @@ func (s *Runner) onStartAgent(ctx context.Context, cfg config.Config) {
return
}

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

func (s *Runner) StartAgent(ctx context.Context, endpoint, agentApiKey, uiEndpoint string) error {
func (s *Runner) StartAgent(ctx context.Context, endpoint, agentApiKey, uiEndpoint, environmentID string) error {
cfg, err := agentConfig.LoadConfig()
s.logger.Debug("Loaded agent config", zap.Any("config", cfg))
if err != nil {
Expand All @@ -132,6 +132,12 @@ func (s *Runner) StartAgent(ctx context.Context, endpoint, agentApiKey, uiEndpoi
}
s.logger.Debug("Agent api key", zap.String("apiKey", cfg.APIKey))

if environmentID != "" {
s.logger.Debug("Overriding agent environment id", zap.String("environment", environmentID))
cfg.EnvironmentID = environmentID
}
s.logger.Debug("Agent environment id", zap.String("environmentID", cfg.EnvironmentID))

if s.mode == agentConfig.Mode_Desktop {
s.logger.Debug("Starting agent in desktop mode")
return s.RunDesktopStrategy(ctx, cfg, uiEndpoint)
Expand Down
1 change: 1 addition & 0 deletions agent/runner/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func newControlPlaneClient(ctx context.Context, config config.Config, traceCache
controlPlaneClient, err := client.Connect(ctx, config.ServerURL,
client.WithAPIKey(config.APIKey),
client.WithAgentName(config.Name),
client.WithEnvironmentID(config.EnvironmentID),
client.WithLogger(logger),
)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions cli/cmd/start_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ var startCmd = &cobra.Command{
flags.Mode = agentConfig.Mode(cfg.Mode)
}

if flags.EnvironmentID != "" {
cfg.EnvironmentID = flags.EnvironmentID
}

err = agentRunner.Run(ctx, cliConfig, flags, verbose)
return "", err
})),
Expand Down

0 comments on commit 1e7ebca

Please sign in to comment.