Skip to content

Commit

Permalink
Add more logs to tail and service status monitor
Browse files Browse the repository at this point in the history
Slight restructure of the service monitor and tail code to improve
readbablity
  • Loading branch information
edw-defang committed Jul 1, 2024
1 parent 5690b4a commit d1f940b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
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 {
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
3 changes: 1 addition & 2 deletions src/cmd/cli/command/servicemonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ func waitServiceStatus(ctx context.Context, targetStatus cli.ServiceStatus, serv
// 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

0 comments on commit d1f940b

Please sign in to comment.