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 934378e commit 7186973
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions common/frame_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,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 @@ -797,10 +802,10 @@ func (fs *FrameSession) onFrameNavigated(frame *cdp.Frame, initial bool) {

// This should only ever be called when a new page is created and it
// navigates to about:blank.
fs.processNavigationSpan(frame.URL, frame.ID)
fs.processNavigationSpan(frame.ID)
}

func (fs *FrameSession) processNavigationSpan(url string, id cdp.FrameID) {
func (fs *FrameSession) processNavigationSpan(id cdp.FrameID) {
newFrame, ok := fs.manager.getFrameByID(id)
if !ok {
return
Expand All @@ -814,11 +819,16 @@ func (fs *FrameSession) processNavigationSpan(url string, id cdp.FrameID) {

// 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(),
)

spanID := fs.mainFrameSpan.SpanContext().SpanID().String()
Expand Down Expand Up @@ -865,10 +875,7 @@ func (fs *FrameSession) onFrameStartedLoading(frameID cdp.FrameID) {
"sid:%v tid:%v fid:%v",
fs.session.ID(), fs.targetID, frameID)

frame, ok := fs.manager.getFrameByID(frameID)
if ok {
fs.processNavigationSpan(frame.URL(), frame.id)
}
fs.processNavigationSpan(frameID)

fs.manager.frameLoadingStarted(frameID)
}
Expand Down

0 comments on commit 7186973

Please sign in to comment.