-
Notifications
You must be signed in to change notification settings - Fork 219
/
logs.go
192 lines (154 loc) · 4.18 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Package logs implements the logs command chain.
package logs
import (
"context"
"errors"
"io"
"time"
"github.com/azazeal/pause"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
fly "github.com/superfly/fly-go"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/logs"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/logger"
"github.com/superfly/flyctl/internal/render"
)
func New() (cmd *cobra.Command) {
const (
long = `View application logs as generated by the application running on
the Fly platform.
Logs can be filtered to a specific instance using the --instance/-i flag or
to all instances running in a specific region using the --region/-r flag.
By default logs are continually streamed until the command is aborted.
Use --no-tail to only fetch the logs in the buffer.
`
short = "View app logs"
)
cmd = command.New("logs", short, long, run,
command.RequireSession,
command.RequireAppName,
)
cmd.Args = cobra.NoArgs
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
flag.Region(),
flag.JSONOutput(),
flag.String{
Name: "instance",
Shorthand: "i",
Description: "Filter by instance ID",
},
flag.Bool{
Name: "no-tail",
Shorthand: "n",
Description: "Do not continually stream logs",
},
)
return
}
func run(ctx context.Context) error {
client := fly.ClientFromContext(ctx)
opts := &logs.LogOptions{
AppName: appconfig.NameFromContext(ctx),
RegionCode: config.FromContext(ctx).Region,
VMID: flag.GetString(ctx, "instance"),
NoTail: flag.GetBool(ctx, "no-tail"),
}
var eg *errgroup.Group
eg, ctx = errgroup.WithContext(ctx)
var streams []<-chan logs.LogEntry
if opts.NoTail {
streams = []<-chan logs.LogEntry{
poll(ctx, eg, client, opts),
}
} else {
pollingCtx, cancelPolling := context.WithCancel(ctx)
streams = []<-chan logs.LogEntry{
poll(pollingCtx, eg, client, opts),
nats(ctx, eg, client, opts, cancelPolling),
}
}
eg.Go(func() error {
return printStreams(ctx, streams...)
})
return eg.Wait()
}
func poll(ctx context.Context, eg *errgroup.Group, client *fly.Client, opts *logs.LogOptions) <-chan logs.LogEntry {
c := make(chan logs.LogEntry)
eg.Go(func() (err error) {
defer close(c)
if err = logs.Poll(ctx, c, client, opts); errors.Is(err, context.Canceled) {
// if the parent context is cancelled then the errorgroup will return
// context.Canceled because nats and/or printStreams will return it.
err = nil
}
return
})
return c
}
func nats(ctx context.Context, eg *errgroup.Group, client *fly.Client, opts *logs.LogOptions, cancelPolling context.CancelFunc) <-chan logs.LogEntry {
c := make(chan logs.LogEntry)
eg.Go(func() error {
defer close(c)
stream, err := logs.NewNatsStream(ctx, client, opts)
if err != nil {
logger := logger.FromContext(ctx)
logger.Debugf("could not connect to wireguard tunnel: %v\n", err)
logger.Debug("falling back to log polling...")
return nil
}
// we wait for 2 seconds before canceling the polling context so that
// we get a few records
pause.For(ctx, 2*time.Second)
cancelPolling()
for entry := range stream.Stream(ctx, opts) {
c <- entry
}
return nil
})
return c
}
func printStreams(ctx context.Context, streams ...<-chan logs.LogEntry) error {
var eg *errgroup.Group
eg, ctx = errgroup.WithContext(ctx)
out := iostreams.FromContext(ctx).Out
json := config.FromContext(ctx).JSONOutput
for _, stream := range streams {
stream := stream
eg.Go(func() error {
return printStream(ctx, out, stream, json)
})
}
return eg.Wait()
}
func printStream(ctx context.Context, w io.Writer, stream <-chan logs.LogEntry, json bool) error {
for {
select {
case <-ctx.Done():
return ctx.Err()
case entry, ok := <-stream:
if !ok {
return nil
}
var err error
if json {
err = render.JSON(w, entry)
} else {
err = render.LogEntry(w, entry,
render.HideAllocID(),
render.RemoveNewlines(),
render.HideRegion(),
)
}
if err != nil {
return err
}
}
}
}