/
logs.go
168 lines (153 loc) · 4.37 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package handler
import (
"github.com/TylerBrock/saw/blade"
sawconfig "github.com/TylerBrock/saw/config"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go/aws"
"github.com/common-fate/common-fate/pkg/cliconfig"
"github.com/common-fate/clio"
"github.com/common-fate/common-fate/pkg/cfaws"
"github.com/common-fate/common-fate/pkg/client"
"github.com/common-fate/common-fate/pkg/prompt"
"github.com/urfave/cli/v2"
)
var LogsCommand = cli.Command{
Name: "logs",
Description: "View log groups for a handler",
Usage: "View log groups for a handler",
Subcommands: []*cli.Command{
&WatchCommand,
&GetCommand,
},
}
var WatchCommand = cli.Command{
Name: "watch",
Description: "Stream logs for a handler",
Flags: []cli.Flag{
&cli.StringFlag{Name: "id"},
},
Action: func(c *cli.Context) error {
ctx := c.Context
cfg, err := cfaws.ConfigFromContextOrDefault(ctx)
if err != nil {
return err
}
id := c.String("id")
if id == "" {
cfg, err := cliconfig.Load()
if err != nil {
return err
}
cf, err := client.FromConfig(ctx, cfg)
if err != nil {
return err
}
h, err := prompt.Handler(ctx, cf)
if err != nil {
return err
}
id = h.Id
}
// logGroup for handlers uses the id of the handler as its name
// this is defined in the cloudformation template
logGroup := "/aws/lambda/" + id
clio.Infof("Starting to watch logs for log group id: %s", logGroup)
watchEvents(logGroup, cfg.Region, c.String("filter"))
return nil
},
}
func watchEvents(group string, region string, filter string) {
sawcfg := sawconfig.Configuration{
Group: group,
Filter: filter,
}
outputcfg := sawconfig.OutputConfiguration{
Pretty: true,
}
// The Blade api from saw is not very configurable
// The most we can do is pass in a Region
b := blade.NewBlade(&sawcfg, &sawconfig.AWSConfiguration{Region: region}, &outputcfg)
b.StreamEvents()
}
var GetCommand = cli.Command{
Name: "get",
Description: "Get logs for a handler",
Flags: []cli.Flag{
&cli.StringFlag{Name: "id"},
&cli.StringFlag{Name: "start", Usage: "Start time", Value: "-5m", Required: false},
&cli.StringFlag{Name: "end", Usage: "End time", Value: "now", Required: false},
},
Action: func(c *cli.Context) error {
ctx := c.Context
cfg, err := cfaws.ConfigFromContextOrDefault(ctx)
if err != nil {
return err
}
id := c.String("id")
if id == "" {
cfg, err := cliconfig.Load()
if err != nil {
return err
}
cf, err := client.FromConfig(ctx, cfg)
if err != nil {
return err
}
h, err := prompt.Handler(ctx, cf)
if err != nil {
return err
}
id = h.Id
}
logGroup := "/aws/lambda/" + id
start := c.String("start")
end := c.String("end")
clio.Info("Starting to get logs for Health check lambda, log group id: %s", logGroup)
hasLogs := true
cwClient := cloudwatchlogs.NewFromConfig(cfg)
// Because the saw library emits its own errors and os.exits.
// We first check whether logs exist for the log group.
// if they dont, emit a warning rather than terminating the command
o, _ := cwClient.DescribeLogGroups(ctx, &cloudwatchlogs.DescribeLogGroupsInput{
LogGroupNamePrefix: &logGroup,
})
if o != nil && len(o.LogGroups) == 1 {
lo, err := cwClient.DescribeLogStreams(ctx, &cloudwatchlogs.DescribeLogStreamsInput{
LogGroupName: o.LogGroups[0].LogGroupName,
Limit: aws.Int32(1),
})
_ = err
if lo != nil && len(lo.LogStreams) != 0 {
hasLogs = true
}
}
if hasLogs {
getEvents(GetEventsOpts{Group: logGroup, Start: start, End: end}, cfg.Region, c.String("filter"))
} else {
clio.Warnf("The handler may not have been invoked yet. Log group id: %s", logGroup)
}
return nil
},
}
type GetEventsOpts struct {
Group string
Start string
End string
}
func getEvents(opts GetEventsOpts, region string, filter string) {
sawcfg := sawconfig.Configuration{
Group: opts.Group,
Start: opts.Start,
End: opts.End,
Filter: filter,
}
outputcfg := sawconfig.OutputConfiguration{
Pretty: true,
}
b := blade.NewBlade(&sawcfg, &sawconfig.AWSConfiguration{Region: region}, &outputcfg)
// The blade package will OS.Exit if the loggroup is not found
// logroup will not be found possible if no logs have been created yet for the lambda
// resulting in
// Error ResourceNotFoundException: The specified log group does not exist.
b.GetEvents()
}