-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs.go
119 lines (107 loc) · 3.33 KB
/
logs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
"github.com/spf13/cobra"
)
var logsCmd *cobra.Command
func init() {
var ver string
var sinceDur time.Duration
var tail bool
logsCmd = &cobra.Command{
Use: "logs function-name",
Aliases: []string{"log"},
Short: "Print out most recent logs for the function",
Args: cobra.ExactArgs(1),
RunE: func(c *cobra.Command, args []string) error {
since := time.Now().Add(-sinceDur)
fnName := args[0]
ver, err := resolveVersion(fnName, ver)
if err != nil {
return fmt.Errorf("failed to resolve version: %s", err)
}
log.Printf("printing logs for version %d", ver)
var afterToken string
for {
lgs, err := logs(fnName, ver, since, afterToken)
if err != nil {
return err
}
for _, l := range lgs.lines {
fmt.Println(l)
}
if !tail {
return nil
}
afterToken = lgs.afterToken
since = time.Now().Add(-30 * time.Second)
time.Sleep(2 * time.Second)
}
},
}
addVersionFlag(logsCmd.Flags(), &ver)
logsCmd.Flags().BoolVarP(&tail, "tail", "t", false, "wait for new logs and print them as they come in")
logsCmd.Flags().DurationVarP(&sinceDur, "since", "s", time.Minute, "only print logs since this length of time ago")
}
type fnLogs struct {
// Token to pass to logs() to get more recent logs.
afterToken string
// Log lines in chronological order.
lines []string
}
// logs returns the logs for a function at the specified version.
// afterToken is a token to pass to get more recent logs.
// This log retriever is super primitive, thanks to the complexities of AWS.
func logs(fnName string, version int, since time.Time, afterToken string) (fnLogs, error) {
lgs := fnLogs{}
ctx := context.Background()
acfg, err := awsconfig.LoadDefaultConfig(ctx)
if err != nil {
return lgs, fmt.Errorf("failed to load aws config: %s", err)
}
logsCl := cloudwatchlogs.NewFromConfig(acfg)
logGroupName := aws.String(fmt.Sprintf("/aws/lambda/%s", fnName))
// This is a hack to get the logs for a specific version. To cater for
// logstreams that start just before a new date or run for multiple days, we
// look at logstreams a few days into the past first.
const maxDays = 2
prefixDate := since.UTC().AddDate(0, 0, -(maxDays - 1))
now := time.Now()
for prefixDate.Before(now) {
streamPrefix := fmt.Sprintf("%s/[%d]", prefixDate.Format("2006/01/02"), version)
pgr := cloudwatchlogs.NewFilterLogEventsPaginator(logsCl, &cloudwatchlogs.FilterLogEventsInput{
LogGroupName: logGroupName,
StartTime: aws.Int64(since.UnixMilli()),
LogStreamNamePrefix: aws.String(streamPrefix),
Limit: aws.Int32(10000),
})
for pgr.HasMorePages() {
ents, err := pgr.NextPage(ctx)
if err != nil {
if !strings.Contains(err.Error(), "ResourceNotFoundException") {
return lgs, fmt.Errorf("failed to get log events: %s", err)
}
break
}
for _, e := range ents.Events {
if afterToken == "" {
lgs.lines = append(lgs.lines, strings.TrimSuffix(*e.Message, "\n"))
} else {
if *e.EventId == afterToken {
afterToken = ""
}
}
lgs.afterToken = *e.EventId
}
}
prefixDate = prefixDate.AddDate(0, 0, 1)
}
return lgs, nil
}