Skip to content

Commit

Permalink
Fix url attribute on navigate and navigation span
Browse files Browse the repository at this point in the history
When the navigation span is started, the new URL is not available to us
yet. We have to defer the adding of the url attribute to the span to
the end of the navigation span.
  • Loading branch information
ankur22 committed Sep 10, 2024
1 parent b20938f commit 792270b
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions common/frame_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ func (fs *FrameSession) initEvents() {
// If there is an active span for main frame,
// end it before exiting so it can be flushed
if fs.mainFrameSpan != nil {
// The url needs to be added here instead of at the start of the span
// because at the start of the span we don't know the correct url for
// the page we're navigating to. At the end of the span we do have this
// information.
fs.mainFrameSpan.SetAttributes(attribute.String("navigation.url", fs.manager.MainFrame().URL()))
fs.mainFrameSpan.End()
fs.mainFrameSpan = nil
}
Expand Down Expand Up @@ -795,10 +800,10 @@ func (fs *FrameSession) onFrameNavigated(frame *cdp.Frame, initial bool) {
return
}

fs.processNavigationSpan(frame.ParentID == "", frame.URL, frame.ID)
fs.processNavigationSpan(frame.ParentID == "", frame.ID)
}

func (fs *FrameSession) processNavigationSpan(isMainFrame bool, url string, id cdp.FrameID) {
func (fs *FrameSession) processNavigationSpan(isMainFrame bool, id cdp.FrameID) {
// Trace navigation only for the main frame.
// TODO: How will this affect sub frames such as iframes?
if !isMainFrame {
Expand All @@ -807,11 +812,16 @@ func (fs *FrameSession) processNavigationSpan(isMainFrame bool, url string, id c

// End the navigation span if it is non-nil
if fs.mainFrameSpan != nil {
// The url needs to be added here instead of at the start of the span
// because at the start of the span we don't know the correct url for
// the page we're navigating to. At the end of the span we do have this
// information.
fs.mainFrameSpan.SetAttributes(attribute.String("navigation.url", fs.manager.MainFrame().URL()))
fs.mainFrameSpan.End()
}

_, fs.mainFrameSpan = TraceNavigation(
fs.ctx, fs.targetID.String(), trace.WithAttributes(attribute.String("navigation.url", url)),
fs.ctx, fs.targetID.String(),
)

var (
Expand Down Expand Up @@ -875,7 +885,7 @@ func (fs *FrameSession) onFrameStartedLoading(frameID cdp.FrameID) {
frame, ok := fs.manager.getFrameByID(frameID)
if !fs.navCaught && ok {
fs.navCaught = true
fs.processNavigationSpan(frame.page.frameManager.MainFrame() == frame, frame.URL(), frame.id)
fs.processNavigationSpan(frame.page.frameManager.MainFrame() == frame, frame.id)
}

fs.manager.frameLoadingStarted(frameID)
Expand Down

0 comments on commit 792270b

Please sign in to comment.