-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracing.go
More file actions
304 lines (285 loc) · 8.12 KB
/
Copy pathtracing.go
File metadata and controls
304 lines (285 loc) · 8.12 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package patchright
import (
"fmt"
"strings"
)
type tracingImpl struct {
channelOwner
includeSources bool
isTracing bool
isLive bool
stacksId string
tracesDir string
harRecorders map[string]harRecordingMetadata
additionalSources map[string]struct{}
}
func (t *tracingImpl) Start(options ...TracingStartOptions) error {
chunkOption := TracingStartChunkOptions{}
if len(options) == 1 {
if options[0].Sources != nil {
t.includeSources = *options[0].Sources
}
t.isLive = options[0].Live != nil && *options[0].Live
chunkOption.Name = options[0].Name
chunkOption.Title = options[0].Title
}
innerStart := func() (any, 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
}
// resetStackCounter clears the in-tracing flag and decrements the connection
// tracing counter, mirroring upstream _resetStackCounter. Called on context
// close so an un-stopped trace doesn't keep the connection collecting stacks.
func (t *tracingImpl) resetStackCounter() {
if t.isTracing {
t.isTracing = false
t.connection.setInTracing(false)
}
}
func (t *tracingImpl) doStopChunk(filePath string) (err error) {
if t.isTracing {
t.isTracing = false
t.connection.setInTracing(false)
}
additionalSources := make([]string, 0, len(t.additionalSources))
for source := range t.additionalSources {
additionalSources = append(additionalSources, source)
}
t.additionalSources = make(map[string]struct{})
if filePath == "" {
// Not interested in artifacts.
_, err = t.channel.Send("tracingStopChunk", map[string]any{
"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]any{
"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.([]any),
StacksId: t.stacksId,
Mode: "write",
IncludeSources: t.includeSources,
AdditionalSources: additionalSources,
})
return err
}
result, err := t.channel.SendReturnAsDict("tracingStopChunk", map[string]any{
"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: []any{},
StacksId: t.stacksId,
Mode: "append",
IncludeSources: t.includeSources,
AdditionalSources: additionalSources,
})
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.isLive, t.tracesDir)
return
}
func (t *tracingImpl) Group(name string, options ...TracingGroupOptions) error {
var option TracingGroupOptions
if len(options) == 1 {
option = options[0]
if option.Location != nil && option.Location.File != "" {
t.additionalSources[option.Location.File] = struct{}{}
}
}
_, err := t.channel.Send("tracingGroup", option, map[string]any{"name": name})
return err
}
func (t *tracingImpl) GroupEnd() error {
_, err := t.channel.Send("tracingGroupEnd")
return err
}
func (t *tracingImpl) StartHar(path string, options ...TracingStartHarOptions) error {
if len(t.harRecorders) > 0 {
return fmt.Errorf("HAR recording has already been started")
}
isZip := strings.HasSuffix(strings.ToLower(path), ".zip")
// Default content matches upstream: attach for .zip output, embed otherwise.
defaultContent := HarContentPolicyEmbed
if isZip {
defaultContent = HarContentPolicyAttach
}
harOptions := recordHarInputOptions{
Path: path,
Content: defaultContent,
Mode: HarModeFull,
}
if len(options) == 1 {
if options[0].ResourcesDir != nil && isZip {
return fmt.Errorf("resourcesDir option is not compatible with a .zip har file")
}
if options[0].Content != nil {
harOptions.Content = options[0].Content
}
if options[0].Mode != nil {
harOptions.Mode = options[0].Mode
}
harOptions.URL = options[0].URLFilter
harOptions.ResourcesDir = options[0].ResourcesDir
}
harId, err := t.channel.Send("harStart", map[string]any{
"options": prepareRecordHarOptions(harOptions),
})
if err != nil {
return err
}
t.harRecorders[harId.(string)] = harRecordingMetadata{
Path: path,
Content: harOptions.Content,
ResourcesDir: harOptions.ResourcesDir,
}
return nil
}
func (t *tracingImpl) StopHar() error {
if len(t.harRecorders) == 0 {
return fmt.Errorf("HAR recording has not been started")
}
return t.exportAllHars()
}
// exportAllHars flushes every active HAR recording to disk. It is invoked by
// StopHar and by APIRequestContext.Dispose so HARs started via the request
// context are written even without an explicit StopHar call.
func (t *tracingImpl) exportAllHars() error {
for harId, harMetaData := range t.harRecorders {
delete(t.harRecorders, harId)
overrides := map[string]any{}
if harId != "" {
overrides["harId"] = harId
}
needCompressed := strings.HasSuffix(strings.ToLower(harMetaData.Path), ".zip")
if !t.connection.isRemote {
overrides["mode"] = "entries"
response, err := t.channel.SendReturnAsDict("harExport", overrides)
if err != nil {
return err
}
if !needCompressed {
continue
}
entries, ok := response["entries"].([]any)
if !ok {
return fmt.Errorf("could not convert HAR entries: %v", response)
}
if _, err = t.connection.LocalUtils().Zip(localUtilsZipOptions{
ZipFile: harMetaData.Path,
Entries: entries,
Mode: "write",
}); err != nil {
return err
}
continue
}
overrides["mode"] = "archive"
response, err := t.channel.SendReturnAsDict("harExport", overrides)
if err != nil {
return err
}
artifact := fromChannel(response["artifact"]).(*artifactImpl)
if needCompressed {
if err := artifact.SaveAs(harMetaData.Path); err != nil {
return err
}
} else {
tmpPath := harMetaData.Path + ".tmp"
if err := artifact.SaveAs(tmpPath); err != nil {
return err
}
if err := t.connection.localUtils.HarUnzip(tmpPath, harMetaData.Path, harMetaData.ResourcesDir); err != nil {
return err
}
}
if err := artifact.Delete(); err != nil {
return err
}
}
return nil
}
func newTracing(parent *channelOwner, objectType string, guid string, initializer map[string]any) *tracingImpl {
bt := &tracingImpl{
harRecorders: make(map[string]harRecordingMetadata),
additionalSources: make(map[string]struct{}),
}
bt.createChannelOwner(bt, parent, objectType, guid, initializer)
bt.markAsInternalType()
return bt
}