Skip to content

Commit 1d3408c

Browse files
authored
fix(go): all actions should be observed (#814)
1 parent 32705f0 commit 1d3408c

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

pkg/service/browser.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -235,19 +235,19 @@ func (s *BrowserService) Render(ctx context.Context, url string, printer Printer
235235

236236
fileChan := make(chan []byte, 1) // buffered: we don't want the browser to stick around while we try to export this value.
237237
actions := []chromedp.Action{
238-
tracingAction("network.Enable", network.Enable()), // required by waitForReady
239-
tracingAction("fetch.Enable", fetch.Enable()), // required by handleNetworkEvents
240-
tracingAction("SetPageScaleFactor", emulation.SetPageScaleFactor(cfg.PageScaleFactor)),
241-
tracingAction("EmulateViewport", chromedp.EmulateViewport(int64(cfg.MinWidth), int64(cfg.MinHeight), orientation)),
242-
setHeaders(browserCtx, cfg.Headers),
243-
setCookies(cfg.Cookies),
244-
tracingAction("Navigate", chromedp.Navigate(url)),
245-
tracingAction("WaitReady(body)", chromedp.WaitReady("body", chromedp.ByQuery)), // wait for a body to exist; this is when the page has started to actually render
246-
scrollForElements(cfg.TimeBetweenScrolls),
247-
waitForDuration(cfg.ReadinessPriorWait),
248-
waitForReady(browserCtx, cfg),
249-
printer.prepare(cfg),
250-
printer.action(fileChan, cfg),
238+
observingAction("network.Enable", network.Enable()), // required by waitForReady
239+
observingAction("fetch.Enable", fetch.Enable()), // required by handleNetworkEvents
240+
observingAction("SetPageScaleFactor", emulation.SetPageScaleFactor(cfg.PageScaleFactor)),
241+
observingAction("EmulateViewport", chromedp.EmulateViewport(int64(cfg.MinWidth), int64(cfg.MinHeight), orientation)),
242+
observingAction("setHeaders", setHeaders(browserCtx, cfg.Headers)),
243+
observingAction("setCookies", setCookies(cfg.Cookies)),
244+
observingAction("Navigate", chromedp.Navigate(url)),
245+
observingAction("WaitReady(body)", chromedp.WaitReady("body", chromedp.ByQuery)), // wait for a body to exist; this is when the page has started to actually render
246+
observingAction("scrollForElements", scrollForElements(cfg.TimeBetweenScrolls)),
247+
observingAction("waitForDuration", waitForDuration(cfg.ReadinessPriorWait)),
248+
observingAction("waitForReady", waitForReady(browserCtx, cfg)),
249+
observingAction("printer.prepare", printer.prepare(cfg)),
250+
observingAction("printer.action", printer.action(fileChan, cfg)),
251251
}
252252
span.AddEvent("actions created")
253253
if err := chromedp.Run(browserCtx, actions...); err != nil {
@@ -312,17 +312,17 @@ func (s *BrowserService) RenderCSV(ctx context.Context, url, renderKey, domain,
312312
s.handleNetworkEvents(browserCtx)
313313

314314
actions := []chromedp.Action{
315-
tracingAction("network.Enable", network.Enable()),
316-
setHeaders(browserCtx, headers),
317-
setCookies([]*network.SetCookieParams{
315+
observingAction("network.Enable", network.Enable()),
316+
observingAction("setHeaders", setHeaders(browserCtx, headers)),
317+
observingAction("setCookies", setCookies([]*network.SetCookieParams{
318318
{
319319
Name: "renderKey",
320320
Value: renderKey,
321321
Domain: domain,
322322
},
323-
}),
324-
tracingAction("SetDownloadBehavior", browser.SetDownloadBehavior(browser.SetDownloadBehaviorBehaviorAllow).WithDownloadPath(tmpDir)),
325-
tracingAction("Navigate", chromedp.Navigate(url)),
323+
})),
324+
observingAction("SetDownloadBehavior", browser.SetDownloadBehavior(browser.SetDownloadBehaviorBehaviorAllow).WithDownloadPath(tmpDir)),
325+
observingAction("Navigate", chromedp.Navigate(url)),
326326
}
327327
if err := chromedp.Run(browserCtx, actions...); err != nil {
328328
return nil, fmt.Errorf("failed to run browser: %w", err)
@@ -1018,7 +1018,12 @@ func waitForDuration(d time.Duration) chromedp.Action {
10181018
})
10191019
}
10201020

1021-
func tracingAction(name string, action chromedp.Action) chromedp.Action {
1021+
// observingAction returns an augmented chromedp.Action which applies observability around the action:
1022+
// - The action has a trace span around it, which will mark and record errors if any is returned.
1023+
// - The action duration is recorded in the MetricBrowserActionDuration histogram, labelled by the action name.
1024+
//
1025+
// This is intended for use on both our own and external actions.
1026+
func observingAction(name string, action chromedp.Action) chromedp.Action {
10221027
return chromedp.ActionFunc(func(ctx context.Context) error {
10231028
tracer := tracer(ctx)
10241029
ctx, span := tracer.Start(ctx, name)

0 commit comments

Comments
 (0)