This repository has been archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
trace.go
179 lines (160 loc) · 4.59 KB
/
trace.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
package main
import (
"context"
"fmt"
"io"
"os"
"slices"
"time"
"github.com/stealthrocket/timecraft/internal/debug/tracing"
"github.com/stealthrocket/timecraft/internal/print/human"
"github.com/stealthrocket/timecraft/internal/print/jsonprint"
"github.com/stealthrocket/timecraft/internal/print/textprint"
"github.com/stealthrocket/timecraft/internal/print/yamlprint"
"github.com/stealthrocket/timecraft/internal/stream"
"github.com/stealthrocket/timecraft/internal/timecraft"
"github.com/stealthrocket/timecraft/internal/timemachine"
)
const traceUsage = `
Usage: timecraft trace <layer> <process id> [options]
Options:
-c, --config Path to the timecraft configuration file (overrides TIMECRAFTCONFIG)
-d, --duration duration Duration of the trace (default to the process uptime)
-h, --help Show this usage information
-o, --output format Output format, one of: text, json, yaml
-t, --start-time time Time at which the trace starts (default to 1 minute)
-v, --verbose For text output, display more details about the trace
`
type layer struct {
typ string
alt []string
trace func(io.Writer, outputFormat, string, stream.Reader[tracing.Event]) error
}
var layers = [...]layer{
{
typ: "network",
alt: []string{"net", "nets", "networks"},
trace: traceNetwork,
},
{
typ: "request",
alt: []string{"req", "reqs", "requests"},
trace: traceRequest,
},
}
func findLayer(typ string) (*layer, error) {
for i, l := range layers {
if l.typ == typ || slices.Contains(l.alt, typ) {
return &layers[i], nil
}
}
return nil, fmt.Errorf(`no tracing layers matching '%s'`, typ)
}
func trace(ctx context.Context, args []string) error {
var (
output = outputFormat("text")
startTime = human.Time{}
duration = human.Duration(1 * time.Minute)
verbose = false
)
flagSet := newFlagSet("timecraft trace", traceUsage)
customVar(flagSet, &output, "o", "output")
customVar(flagSet, &duration, "d", "duration")
customVar(flagSet, &startTime, "t", "start-time")
boolVar(flagSet, &verbose, "v", "verbose")
args, err := parseFlags(flagSet, args)
if err != nil {
return err
}
if len(args) != 2 {
perrorf(`Expected trace layer and process id as arguments:
$ timecraft trace network <process id> ...
$ timecraft trace request <process id> ...
`)
return exitCode(2)
}
layer, err := findLayer(args[0])
if err != nil {
perror(err)
return exitCode(2)
}
processID, err := parseProcessID(args[1])
if err != nil {
return err
}
config, err := timecraft.LoadConfig()
if err != nil {
return err
}
registry, err := timecraft.OpenRegistry(config)
if err != nil {
return err
}
manifest, err := registry.LookupLogManifest(ctx, processID)
if err != nil {
return err
}
if startTime.IsZero() {
startTime = human.Time(manifest.StartTime)
}
// timeRange := timemachine.TimeRange{
// Start: time.Time(startTime),
// End: time.Time(startTime).Add(time.Duration(duration)),
// }
logSegment, err := registry.ReadLogSegment(ctx, processID, 0)
if err != nil {
return err
}
defer logSegment.Close()
logReader := timemachine.NewLogReader(logSegment, manifest)
defer logReader.Close()
format := "%v"
if verbose {
format = "%+v"
}
return layer.trace(os.Stdout, output, format, &tracing.EventReader{
Records: timemachine.NewLogRecordReader(logReader),
})
}
func traceNetwork(w io.Writer, output outputFormat, format string, events stream.Reader[tracing.Event]) error {
var writer stream.WriteCloser[tracing.Event]
switch output {
case "json":
writer = jsonprint.NewWriter[tracing.Event](w)
case "yaml":
writer = yamlprint.NewWriter[tracing.Event](w)
default:
writer = textprint.NewWriter[tracing.Event](w,
textprint.Format[tracing.Event](format),
textprint.Separator[tracing.Event](""),
)
}
defer writer.Close()
_, err := stream.Copy[tracing.Event](writer, events)
return err
}
func traceRequest(w io.Writer, output outputFormat, format string, events stream.Reader[tracing.Event]) error {
var writer stream.WriteCloser[tracing.Exchange]
switch output {
case "json":
writer = jsonprint.NewWriter[tracing.Exchange](w)
case "yaml":
writer = yamlprint.NewWriter[tracing.Exchange](w)
default:
writer = textprint.NewWriter[tracing.Exchange](w,
textprint.Format[tracing.Exchange](format),
textprint.Separator[tracing.Exchange]("\n"),
)
defer fmt.Println()
}
defer writer.Close()
_, err := stream.Copy[tracing.Exchange](writer, &tracing.ExchangeReader{
Messages: &tracing.MessageReader{
Events: events,
Protos: []tracing.ConnProtocol{
tracing.HTTP1(),
},
},
})
return err
}