Skip to content

Commit 40cadff

Browse files
authored
support regional log pulls for cloud agents (#695)
* support regional log pulls for cloud agents * update proto version * fix log call when agent doesn't exist * update var name
1 parent ff37ae4 commit 40cadff

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

cmd/lk/agent.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ func createAgent(ctx context.Context, cmd *cli.Command) error {
572572
return err
573573
} else if viewLogs {
574574
fmt.Println("Tailing runtime logs...safe to exit at any time")
575-
return agentsClient.StreamLogs(ctx, "deploy", lkConfig.Agent.ID, os.Stdout)
575+
return agentsClient.StreamLogs(ctx, "deploy", lkConfig.Agent.ID, os.Stdout, resp.ServerRegions[0])
576576
}
577577
}
578578
return nil
@@ -859,7 +859,19 @@ func getLogs(ctx context.Context, cmd *cli.Command) error {
859859
if err != nil {
860860
return err
861861
}
862-
return agentsClient.StreamLogs(ctx, cmd.String("log-type"), agentID, os.Stdout)
862+
863+
response, err := agentsClient.ListAgents(ctx, &lkproto.ListAgentsRequest{
864+
AgentId: agentID,
865+
})
866+
if err != nil {
867+
return err
868+
}
869+
870+
if len(response.Agents) == 0 {
871+
return fmt.Errorf("no agent deployments found")
872+
}
873+
874+
return agentsClient.StreamLogs(ctx, cmd.String("log-type"), agentID, os.Stdout, response.Agents[0].AgentDeployments[0].ServerRegion)
863875
}
864876

865877
func deleteAgent(ctx context.Context, cmd *cli.Command) error {

pkg/agentfs/client.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ import (
2323
"regexp"
2424
"strings"
2525

26+
"github.com/twitchtv/twirp"
27+
2628
livekitcli "github.com/livekit/livekit-cli/v2"
2729
"github.com/livekit/protocol/auth"
2830
lkproto "github.com/livekit/protocol/livekit"
2931
"github.com/livekit/protocol/logger"
3032
lksdk "github.com/livekit/server-sdk-go/v2"
31-
"github.com/twitchtv/twirp"
3233
)
3334

3435
// Client is a wrapper around the lksdk.AgentClient that provides a simpler interface for creating and deploying agents.
@@ -63,7 +64,7 @@ func New(opts ...ClientOption) (*Client, error) {
6364
return nil, err
6465
}
6566
client.AgentClient = agentClient
66-
client.agentsURL = client.getAgentsURL()
67+
client.agentsURL = client.getAgentsURL("")
6768
if client.httpClient == nil {
6869
client.httpClient = &http.Client{}
6970
}
@@ -172,7 +173,7 @@ func (c *Client) uploadAndBuild(
172173
return nil
173174
}
174175

175-
func (c *Client) getAgentsURL() string {
176+
func (c *Client) getAgentsURL(serverRegion string) string {
176177
agentsURL := c.projectURL
177178
if strings.HasPrefix(agentsURL, "ws") {
178179
agentsURL = strings.Replace(agentsURL, "ws", "http", 1)
@@ -182,7 +183,10 @@ func (c *Client) getAgentsURL() string {
182183
} else if !strings.Contains(agentsURL, "localhost") && !strings.Contains(agentsURL, "127.0.0.1") {
183184
pattern := `^https://[a-zA-Z0-9\-]+\.`
184185
re := regexp.MustCompile(pattern)
185-
agentsURL = re.ReplaceAllString(agentsURL, "https://agents.")
186+
if serverRegion != "" {
187+
serverRegion = fmt.Sprintf("%s.", serverRegion)
188+
}
189+
agentsURL = re.ReplaceAllString(agentsURL, fmt.Sprintf("https://%sagents.", serverRegion))
186190
}
187191
return agentsURL
188192
}

pkg/agentfs/logs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ type APIError struct {
3333
}
3434

3535
// StreamLogs streams the logs for the given agent.
36-
func (c *Client) StreamLogs(ctx context.Context, logType, agentID string, writer io.Writer) error {
36+
func (c *Client) StreamLogs(ctx context.Context, logType, agentID string, writer io.Writer, serverRegion string) error {
3737
logger := c.logger.WithName("StreamLogs")
3838
if logType == "" {
3939
logType = "deploy"
4040
}
4141
params := url.Values{}
4242
params.Add("agent_id", agentID)
4343
params.Add("log_type", logType)
44-
fullUrl := fmt.Sprintf("%s/logs?%s", c.agentsURL, params.Encode())
44+
fullUrl := fmt.Sprintf("%s/logs?%s", c.getAgentsURL(serverRegion), params.Encode())
4545
req, err := c.newRequest("GET", fullUrl, nil)
4646
if err != nil {
4747
return err

0 commit comments

Comments
 (0)