Skip to content

Make it more palatable for ECS environments #29

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ sudo dpkg -i <the_deb_name>
# Watch production log group streams for api
saw watch production --prefix api

# Watch recently changed streams for the production log group
saw watch production --recent

# Watch production log group streams for api and filter for "error"
saw watch production --prefix api --filter error
```
Expand Down
20 changes: 20 additions & 0 deletions blade/blade.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ func (b *Blade) GetLogStreams() []*cloudwatchlogs.LogStream {
return streams
}

// GetTopLogStreams gets the recent log streams from AWS without prefix and limited to 100
func (b *Blade) GetTopLogStreams() []*cloudwatchlogs.LogStream {
input := b.config.DescribeRecentLogStreamsInput()
streams := make([]*cloudwatchlogs.LogStream, 0)
b.cwl.DescribeLogStreamsPages(input, func(
out *cloudwatchlogs.DescribeLogStreamsOutput,
lastPage bool,
) bool {
for _, stream := range out.LogStreams {
streams = append(streams, stream)
}
if lastPage || len(streams) >= 100 {
return false
}
return true
Comment on lines +103 to +106

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might return the bool directly here.
Maybe:

return !lastPage && len(streams) < 100

})
return streams
}

// GetEvents gets events from AWS given the blade configuration
func (b *Blade) GetEvents() {
formatter := b.output.Formatter()
Expand Down Expand Up @@ -150,6 +169,7 @@ func (b *Blade) StreamEvents() {
}
return !lastPage
}
fmt.Printf("starting to tail logs from %d streams ...\n", len(input.LogStreamNames))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind removing this bit? As part of the design I like there to be no other output than data or errors (this helps with things downstream if you pipe the output of saw into other programs)


for {
err := b.cwl.FilterLogEventsPages(input, handlePage)
Expand Down
12 changes: 10 additions & 2 deletions cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import (

"github.com/TylerBrock/saw/blade"
"github.com/TylerBrock/saw/config"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/spf13/cobra"
)

var watchConfig config.Configuration

var watchOutputConfig config.OutputConfiguration

var useRecent bool
var watchCommand = &cobra.Command{
Use: "watch <log group>",
Short: "Continuously stream log events",
Expand All @@ -27,8 +29,13 @@ var watchCommand = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
watchConfig.Group = args[0]
b := blade.NewBlade(&watchConfig, &awsConfig, &watchOutputConfig)
if watchConfig.Prefix != "" {
streams := b.GetLogStreams()
if watchConfig.Prefix != "" || useRecent {
var streams []*cloudwatchlogs.LogStream
if useRecent {
streams = b.GetTopLogStreams()
} else {
streams = b.GetLogStreams()
}
if len(streams) == 0 {
fmt.Printf("No streams found in %s with prefix %s\n", watchConfig.Group, watchConfig.Prefix)
fmt.Printf("To view available streams: `saw streams %s`\n", watchConfig.Group)
Expand All @@ -41,6 +48,7 @@ var watchCommand = &cobra.Command{
}

func init() {
watchCommand.Flags().BoolVar(&useRecent, "recent", true, "Tails from recently changed streams")
watchCommand.Flags().StringVar(&watchConfig.Prefix, "prefix", "", "log stream prefix filter")
watchCommand.Flags().StringVar(&watchConfig.Filter, "filter", "", "event filter pattern")
watchCommand.Flags().BoolVar(&watchOutputConfig.Raw, "raw", false, "print raw log event without timestamp or stream prefix")
Expand Down
8 changes: 8 additions & 0 deletions config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ func (c *Configuration) DescribeLogStreamsInput() *cloudwatchlogs.DescribeLogStr
return &input
}

func (c *Configuration) DescribeRecentLogStreamsInput() *cloudwatchlogs.DescribeLogStreamsInput {
input := cloudwatchlogs.DescribeLogStreamsInput{}
input.SetLogGroupName(c.Group)
input.SetDescending(true)
input.SetOrderBy("LastEventTime")
return &input
}

func (c *Configuration) FilterLogEventsInput() *cloudwatchlogs.FilterLogEventsInput {
input := cloudwatchlogs.FilterLogEventsInput{}
input.SetInterleaved(true)
Expand Down