Skip to content

Commit

Permalink
feat(replay): Calculate hydration diff timestamps based on related hy…
Browse files Browse the repository at this point in the history
…dration breadcrumbs (#73095)

This is the 2nd try at #72561
The original was reverted in
986296c
because of some errors that popped up:
-
https://sentry.sentry.io/issues/5505399172/?project=11276&referrer=github-pr-bot
-
https://sentry.sentry.io/issues/5505401256/?project=11276&referrer=github-pr-bot
-
https://sentry.sentry.io/issues/5505436186/?project=11276&referrer=github-pr-bot
-
https://sentry.sentry.io/issues/5505439753/?project=11276&referrer=github-pr-bot
-
https://sentry.sentry.io/issues/5505459069/?project=11276&referrer=github-pr-bot

The difference now is that I've improved the types to include
`data.mutations.next`. The real but though was that before, in
`breadcrumbItem.tsx`, we were doing the left/right timestamp math only
for crumbs that have that mutations.next field...

That problem is fixed now because we're checking the crumb type first,
then defer to the new `<CrumbHydrationButton>` which will get the
offsets and render all at once.
Without that fix, we were basically trying to get the left/right offsets
for any breadcrumb type, which would easily explode.

Related to #70199
  • Loading branch information
ryan953 authored Jun 24, 2024
1 parent 8a16854 commit 50fdd0e
Show file tree
Hide file tree
Showing 13 changed files with 394 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {ReplayGroupContextProvider} from 'sentry/components/replays/replayGroupC
import {t} from 'sentry/locale';
import type {Event} from 'sentry/types/event';
import type {Group} from 'sentry/types/group';
import {getReplayDiffOffsetsFromEvent} from 'sentry/utils/replays/getDiffTimestamps';
import useReplayReader from 'sentry/utils/replays/hooks/useReplayReader';

interface Props {
Expand All @@ -31,11 +32,7 @@ export default function ReplayDiffContent({event, group, orgSlug, replaySlug}: P
return null;
}

// TODO: base the event timestamp off the replay data itself.
const startTimestampMS =
'startTimestamp' in event ? event.startTimestamp * 1000 : undefined;
const timeOfEvent = event.dateCreated ?? startTimestampMS ?? event.dateReceived;
const eventTimestampMs = timeOfEvent ? Math.floor(new Date(timeOfEvent).getTime()) : 0;
const {leftOffsetMs, rightOffsetMs} = getReplayDiffOffsetsFromEvent(replay, event);

return (
<EventDataSection
Expand All @@ -44,9 +41,9 @@ export default function ReplayDiffContent({event, group, orgSlug, replaySlug}: P
actions={
<OpenReplayComparisonButton
key="open-modal-button"
leftTimestamp={0}
leftOffsetMs={leftOffsetMs}
replay={replay}
rightTimestamp={eventTimestampMs}
rightOffsetMs={rightOffsetMs}
size="xs"
>
{t('Open Diff Viewer')}
Expand All @@ -57,9 +54,9 @@ export default function ReplayDiffContent({event, group, orgSlug, replaySlug}: P
<ReplayGroupContextProvider groupId={group?.id} eventId={event.id}>
<ReplayDiff
defaultTab={DiffType.VISUAL}
leftTimestamp={0}
leftOffsetMs={leftOffsetMs}
replay={replay}
rightTimestamp={eventTimestampMs}
rightOffsetMs={rightOffsetMs}
/>
</ReplayGroupContextProvider>
</ErrorBoundary>
Expand Down
57 changes: 40 additions & 17 deletions static/app/components/replays/breadcrumbs/breadcrumbItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,21 @@ import {Tooltip} from 'sentry/components/tooltip';
import {t} from 'sentry/locale';
import {space} from 'sentry/styles/space';
import type {Extraction} from 'sentry/utils/replays/extractDomNodes';
import {getReplayDiffOffsetsFromFrame} from 'sentry/utils/replays/getDiffTimestamps';
import getFrameDetails from 'sentry/utils/replays/getFrameDetails';
import type {ErrorFrame, FeedbackFrame, ReplayFrame} from 'sentry/utils/replays/types';
import {isErrorFrame, isFeedbackFrame} from 'sentry/utils/replays/types';
import type ReplayReader from 'sentry/utils/replays/replayReader';
import type {
ErrorFrame,
FeedbackFrame,
HydrationErrorFrame,
ReplayFrame,
} from 'sentry/utils/replays/types';
import {
isBreadcrumbFrame,
isErrorFrame,
isFeedbackFrame,
isHydrationErrorFrame,
} from 'sentry/utils/replays/types';
import useOrganization from 'sentry/utils/useOrganization';
import useProjectFromSlug from 'sentry/utils/useProjectFromSlug';
import IconWrapper from 'sentry/views/replays/detail/iconWrapper';
Expand Down Expand Up @@ -88,22 +100,10 @@ function BreadcrumbItem({
}, [description, expandPaths, onInspectorExpanded]);

const renderComparisonButton = useCallback(() => {
return frame?.data && 'mutations' in frame.data ? (
<div>
<OpenReplayComparisonButton
replay={replay}
leftTimestamp={frame.offsetMs}
rightTimestamp={
(frame.data.mutations.next?.timestamp ?? 0) -
(replay?.getReplay().started_at.getTime() ?? 0)
}
size="xs"
>
{t('Open Hydration Diff')}
</OpenReplayComparisonButton>
</div>
return isBreadcrumbFrame(frame) && isHydrationErrorFrame(frame) ? (
<CrumbHydrationButton replay={replay} frame={frame} />
) : null;
}, [frame?.data, frame.offsetMs, replay]);
}, [frame, replay]);

const renderCodeSnippet = useCallback(() => {
return extraction?.html ? (
Expand Down Expand Up @@ -187,6 +187,29 @@ function BreadcrumbItem({
);
}

function CrumbHydrationButton({
replay,
frame,
}: {
frame: HydrationErrorFrame;
replay: ReplayReader | null;
}) {
const {leftOffsetMs, rightOffsetMs} = getReplayDiffOffsetsFromFrame(replay, frame);

return (
<div>
<OpenReplayComparisonButton
replay={replay}
leftOffsetMs={leftOffsetMs}
rightOffsetMs={rightOffsetMs}
size="xs"
>
{t('Open Hydration Diff')}
</OpenReplayComparisonButton>
</div>
);
}

function CrumbErrorIssue({frame}: {frame: FeedbackFrame | ErrorFrame}) {
const organization = useOrganization();
const project = useProjectFromSlug({organization, projectSlug: frame.data.projectSlug});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ const LazyComparisonModal = lazy(

interface Props {
children: ReactNode;
leftTimestamp: number;
leftOffsetMs: number;
replay: null | ReplayReader;
rightTimestamp: number;
rightOffsetMs: number;
size?: ButtonProps['size'];
}

export function OpenReplayComparisonButton({
children,
leftTimestamp,
leftOffsetMs,
replay,
rightTimestamp,
rightOffsetMs,
size,
}: Props) {
const organization = useOrganization();
Expand Down Expand Up @@ -59,8 +59,8 @@ export function OpenReplayComparisonButton({
<LazyComparisonModal
replay={replay}
organization={organization}
leftTimestamp={leftTimestamp}
rightTimestamp={rightTimestamp}
leftOffsetMs={leftOffsetMs}
rightOffsetMs={rightOffsetMs}
{...deps}
/>
</Suspense>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import type ReplayReader from 'sentry/utils/replays/replayReader';
import {OrganizationContext} from 'sentry/views/organizationContext';

interface Props extends ModalRenderProps {
leftTimestamp: number;
leftOffsetMs: number;
organization: Organization;
replay: null | ReplayReader;
rightTimestamp: number;
rightOffsetMs: number;
}

export default function ReplayComparisonModal({
Body,
Header,
leftTimestamp,
leftOffsetMs,
organization,
replay,
rightTimestamp,
rightOffsetMs,
}: Props) {
return (
<OrganizationContext.Provider value={organization}>
Expand Down Expand Up @@ -55,8 +55,8 @@ export default function ReplayComparisonModal({
</StyledParagraph>
<ReplayDiff
replay={replay}
leftTimestamp={leftTimestamp}
rightTimestamp={rightTimestamp}
leftOffsetMs={leftOffsetMs}
rightOffsetMs={rightOffsetMs}
/>
</Body>
</OrganizationContext.Provider>
Expand Down
16 changes: 8 additions & 8 deletions static/app/components/replays/replayDiff.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import type ReplayReader from 'sentry/utils/replays/replayReader';
const MAX_CLAMP_TO_START = 2000;

interface Props {
leftTimestamp: number;
leftOffsetMs: number;
replay: null | ReplayReader;
rightTimestamp: number;
rightOffsetMs: number;
defaultTab?: DiffType;
}

Expand All @@ -33,16 +33,16 @@ export enum DiffType {

export default function ReplayDiff({
defaultTab = DiffType.VISUAL,
leftTimestamp,
leftOffsetMs,
replay,
rightTimestamp,
rightOffsetMs,
}: Props) {
const fetching = false;

const [leftBody, setLeftBody] = useState(null);
const [rightBody, setRightBody] = useState(null);

let startOffset = leftTimestamp - 1;
let startOffset = leftOffsetMs - 1;
// If the error occurs close to the start of the replay, clamp the start offset to 1
// to help compare with the html provided by the server, This helps with some errors on localhost.
if (startOffset < MAX_CLAMP_TO_START) {
Expand Down Expand Up @@ -96,16 +96,16 @@ export default function ReplayDiff({
</ReplayContextProvider>
<ReplayContextProvider
analyticsContext="replay_comparison_modal_right"
initialTimeOffsetMs={{offsetMs: rightTimestamp + 1}}
initialTimeOffsetMs={{offsetMs: rightOffsetMs + 1}}
isFetching={fetching}
prefsStrategy={StaticReplayPreferences}
replay={replay}
>
<ComparisonSideWrapper id="rightSide">
{rightTimestamp > 0 ? (
{rightOffsetMs > 0 ? (
<ReplaySide
selector="#rightSide iframe"
expectedTime={rightTimestamp + 1}
expectedTime={rightOffsetMs + 1}
onLoad={setRightBody}
/>
) : (
Expand Down
Loading

0 comments on commit 50fdd0e

Please sign in to comment.