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

Add more logs to tail and service status monitor #518

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
33 changes: 17 additions & 16 deletions src/cmd/cli/command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"regexp"
"slices"
"strings"
"sync"
"time"

"github.com/AlecAivazis/survey/v2"
Expand Down Expand Up @@ -827,26 +826,28 @@ var composeUpCmd = &cobra.Command{

tailCtx, cancelTail := context.WithCancel(ctx)

var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
// show users the current streaming logs
if err := startTailing(tailCtx, deploy.Etag, since); err != nil {
var cerr *cli.CancelError
if !errors.As(err, &cerr) {
term.Debugf("failed to start tailing: %v", err)
go func() { // Cancel the tailing if the service is ready
if err := waitServiceStatus(ctx, cli.ServiceStarted, serviceInfos); err != nil {
if !errors.Is(err, context.Canceled) &&
!errors.Is(err, cli.ErrDryRun) &&
!errors.As(err, new(cliClient.ErrNotImplemented)) {
term.Warnf("failed to wait for service status: %v", err)
} else {
term.Debugf("failed to wait for service status: %v", err)
}
term.Info("Service status monitoring failed, we will continue tailing the logs. Press Ctrl+C to detach.")
return // Do not cancel the tailing if we are unable to wait for the service status
}
cancelTail()
}()
if err := waitServiceStatus(ctx, cli.ServiceStarted, serviceInfos); err != nil && !errors.Is(err, context.Canceled) {
if !errors.Is(err, cli.ErrDryRun) && !errors.As(err, new(cliClient.ErrNotImplemented)) {
term.Warnf("failed to wait for service status: %v", err)

// show users the current streaming logs
if err := startTailing(tailCtx, deploy.Etag, since); err != nil {
edwardrf marked this conversation as resolved.
Show resolved Hide resolved
var cerr *cli.CancelError
if !errors.As(err, &cerr) {
term.Debugf("failed to start tailing: %v", err)
}
wg.Wait() // Wait until ctrl+c is pressed
}
cancelTail()
wg.Wait() // Wait for tail to finish

printEndpoints(serviceInfos)
term.Info("Done.")
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/cli/command/servicemonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ func waitServiceStatus(ctx context.Context, targetStatus cli.ServiceStatus, serv
for _, serviceInfo := range serviceInfos {
serviceList = append(serviceList, serviceInfo.Service.Name)
}
term.Debugf("Waiting for services %v to reach state: %s", serviceList, targetStatus)

// set up service status subscription (non-blocking)
subscribeServiceStatusChan, err := cli.Subscribe(ctx, client, serviceList)
if err != nil {
term.Debugf("error subscribing to service status: %v", err)
return err
return fmt.Errorf("error subscribing to service status: %w", err)
}

serviceStatus := make(map[string]string, len(serviceList))
Expand Down
8 changes: 8 additions & 0 deletions src/pkg/clouds/aws/ecs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/DefangLabs/defang/src/pkg/clouds/aws"
"github.com/DefangLabs/defang/src/pkg/clouds/aws/region"
"github.com/DefangLabs/defang/src/pkg/term"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types"
"github.com/aws/aws-sdk-go-v2/service/ecs"
Expand Down Expand Up @@ -140,7 +141,13 @@ type LogGroupInput struct {
LogEventFilterPattern string
}

func (l LogGroupInput) String() string {
return fmt.Sprintf("LogGroupARN: %s, LogStreamNames: %v, LogStreamNamePrefix: %s, LogEventFilterPattern: %s",
l.LogGroupARN, l.LogStreamNames, l.LogStreamNamePrefix, l.LogEventFilterPattern)
}

func TailLogGroup(ctx context.Context, input LogGroupInput) (EventStream, error) {
term.Debug("Trying to tail log, getting event stream for:", input)
var pattern *string
if input.LogEventFilterPattern != "" {
pattern = &input.LogEventFilterPattern
Expand Down Expand Up @@ -318,6 +325,7 @@ type collectionStream struct {
}

func (c *collectionStream) addAndStart(s EventStream, since time.Time, lgi LogGroupInput) {
term.Debug("Started to tail log event stream:", lgi)
c.lock.Lock()
defer c.lock.Unlock()
c.streams = append(c.streams, s)
Expand Down