-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
tracing.go
164 lines (151 loc) · 4.04 KB
/
tracing.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
package playwright
import "fmt"
type tracingImpl struct {
channelOwner
includeSources bool
isTracing bool
stacksId string
tracesDir string
}
func (t *tracingImpl) Start(options ...TracingStartOptions) error {
chunkOption := TracingStartChunkOptions{}
if len(options) == 1 {
if options[0].Sources != nil {
t.includeSources = *options[0].Sources
}
chunkOption.Name = options[0].Name
chunkOption.Title = options[0].Title
}
innerStart := func() (interface{}, error) {
if _, err := t.channel.Send("tracingStart", options); err != nil {
return "", err
}
return t.channel.Send("tracingStartChunk", chunkOption)
}
name, err := innerStart()
if err != nil {
return err
}
return t.startCollectingStacks(name.(string))
}
func (t *tracingImpl) StartChunk(options ...TracingStartChunkOptions) error {
name, err := t.channel.Send("tracingStartChunk", options)
if err != nil {
return err
}
return t.startCollectingStacks(name.(string))
}
func (t *tracingImpl) StopChunk(path ...string) error {
filePath := ""
if len(path) == 1 {
filePath = path[0]
}
return t.doStopChunk(filePath)
}
func (t *tracingImpl) Stop(path ...string) error {
filePath := ""
if len(path) == 1 {
filePath = path[0]
}
if err := t.doStopChunk(filePath); err != nil {
return err
}
_, err := t.channel.Send("tracingStop")
return err
}
func (t *tracingImpl) doStopChunk(filePath string) (err error) {
if t.isTracing {
t.isTracing = false
t.connection.setInTracing(false)
}
if filePath == "" {
// Not interested in artifacts.
_, err = t.channel.Send("tracingStopChunk", map[string]interface{}{
"mode": "discard",
})
if t.stacksId != "" {
return t.connection.LocalUtils().TraceDiscarded(t.stacksId)
}
return err
}
isLocal := !t.connection.isRemote
if isLocal {
result, err := t.channel.SendReturnAsDict("tracingStopChunk", map[string]interface{}{
"mode": "entries",
})
if err != nil {
return err
}
entries, ok := result["entries"]
if !ok {
return fmt.Errorf("could not convert result to map: %v", result)
}
_, err = t.connection.LocalUtils().Zip(localUtilsZipOptions{
ZipFile: filePath,
Entries: entries.([]interface{}),
StacksId: t.stacksId,
Mode: "write",
IncludeSources: t.includeSources,
})
return err
}
result, err := t.channel.SendReturnAsDict("tracingStopChunk", map[string]interface{}{
"mode": "archive",
})
if err != nil {
return err
}
artifactChannel, ok := result["artifact"]
if !ok {
return fmt.Errorf("could not convert result to map: %v", result)
}
// Save trace to the final local file.
artifact := fromNullableChannel(artifactChannel).(*artifactImpl)
// The artifact may be missing if the browser closed while stopping tracing.
if artifact == nil {
if t.stacksId != "" {
return t.connection.LocalUtils().TraceDiscarded(t.stacksId)
}
return
}
if err := artifact.SaveAs(filePath); err != nil {
return err
}
if err := artifact.Delete(); err != nil {
return err
}
_, err = t.connection.LocalUtils().Zip(localUtilsZipOptions{
ZipFile: filePath,
Entries: []interface{}{},
StacksId: t.stacksId,
Mode: "append",
IncludeSources: t.includeSources,
})
return err
}
func (t *tracingImpl) startCollectingStacks(name string) (err error) {
if !t.isTracing {
t.isTracing = true
t.connection.setInTracing(true)
}
t.stacksId, err = t.connection.LocalUtils().TracingStarted(name, t.tracesDir)
return
}
func (t *tracingImpl) Group(name string, options ...TracingGroupOptions) error {
var option TracingGroupOptions
if len(options) == 1 {
option = options[0]
}
_, err := t.channel.Send("tracingGroup", option, map[string]interface{}{"name": name})
return err
}
func (t *tracingImpl) GroupEnd() error {
_, err := t.channel.Send("tracingGroupEnd")
return err
}
func newTracing(parent *channelOwner, objectType string, guid string, initializer map[string]interface{}) *tracingImpl {
bt := &tracingImpl{}
bt.createChannelOwner(bt, parent, objectType, guid, initializer)
bt.markAsInternalType()
return bt
}