Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/CONST/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,7 @@ const CONST = {
CONTEXT_FULLSTORY: 'Fullstory',
CONTEXT_POLICIES: 'Policies',
TAG_ACTIVE_POLICY: 'active_policy_id',
// Span names
SPAN_OPEN_REPORT: 'ManualOpenReport',
SPAN_APP_STARTUP: 'ManualAppStartup',
SPAN_NAVIGATE_TO_REPORTS_TAB: 'ManualNavigateToReportsTab',
Expand All @@ -1662,11 +1663,14 @@ const CONST = {
SPAN_OPEN_SEARCH_ROUTER: 'ManualOpenSearchRouter',
SPAN_OPEN_CREATE_EXPENSE: 'ManualOpenCreateExpense',
SPAN_SEND_MESSAGE: 'ManualSendMessage',
// Attribute names
ATTRIBUTE_IOU_TYPE: 'iou_type',
ATTRIBUTE_IOU_REQUEST_TYPE: 'iou_request_type',
ATTRIBUTE_REPORT_ID: 'report_id',
ATTRIBUTE_SOURCE_ROUTE: 'source_route',
ATTRIBUTE_MESSAGE_LENGTH: 'message_length',
ATTRIBUTE_CANCELED: 'canceled',
ATTRIBUTE_ROUTE_FROM: 'route_from',
ATTRIBUTE_ROUTE_TO: 'route_to',
},
PRIORITY_MODE: {
GSD: 'gsd',
Expand Down
5 changes: 5 additions & 0 deletions src/components/LHNOptionsList/OptionRowLHN.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {shouldOptionShowTooltip, shouldUseBoldText} from '@libs/OptionsListUtils
import Performance from '@libs/Performance';
import ReportActionComposeFocusManager from '@libs/ReportActionComposeFocusManager';
import {isAdminRoom, isChatUsedForOnboarding as isChatUsedForOnboardingReportUtils, isConciergeChatReport, isGroupChat, isOneOnOneChat, isSystemChat} from '@libs/ReportUtils';
import {startSpan} from '@libs/telemetry/activeSpans';
import TextWithEmojiFragment from '@pages/home/report/comment/TextWithEmojiFragment';
import {showContextMenu} from '@pages/home/report/ContextMenu/ReportActionContextMenu';
import FreeTrial from '@pages/settings/Subscription/FreeTrial';
Expand Down Expand Up @@ -169,6 +170,10 @@ function OptionRowLHN({
const onOptionPress = (event: GestureResponderEvent | KeyboardEvent | undefined) => {
Performance.markStart(CONST.TIMING.OPEN_REPORT);
Timing.start(CONST.TIMING.OPEN_REPORT);
startSpan(`${CONST.TELEMETRY.SPAN_OPEN_REPORT}_${reportID}`, {
name: 'OptionRowLHN',
op: CONST.TELEMETRY.SPAN_OPEN_REPORT,
});

event?.preventDefault();
// Enable Composer to focus on clicking the same chat after opening the context menu.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {PortalHost} from '@gorhom/portal';
import React, {useCallback, useMemo} from 'react';
import React, {useCallback, useEffect, useMemo} from 'react';
// We use Animated for all functionality related to wide RHP to make it easier
// to interact with react-navigation components (e.g., CardContainer, interpolator), which also use Animated.
// eslint-disable-next-line no-restricted-imports
Expand Down Expand Up @@ -28,6 +28,7 @@ import navigationRef from '@libs/Navigation/navigationRef';
import {getFilteredReportActionsForReportView, getOneTransactionThreadReportID, isMoneyRequestAction, isSentMoneyReportAction} from '@libs/ReportActionsUtils';
import {canEditReportAction, getReportOfflinePendingActionAndErrors, isReportTransactionThread} from '@libs/ReportUtils';
import {buildCannedSearchQuery} from '@libs/SearchQueryUtils';
import {cancelSpan} from '@libs/telemetry/activeSpans';
import Navigation from '@navigation/Navigation';
import ReportActionsView from '@pages/home/report/ReportActionsView';
import ReportFooter from '@pages/home/report/ReportFooter';
Expand Down Expand Up @@ -186,6 +187,11 @@ function MoneyRequestReportView({report, policy, reportMetadata, shouldDisplayRe
[backToRoute, isLoadingInitialReportActions, isTransactionThreadView, parentReportAction, policy, report, reportActions, transactionThreadReportID],
);

// We need to cancel telemetry span when user leaves the screen before full report data is loaded
useEffect(() => {
return () => cancelSpan(`${CONST.TELEMETRY.SPAN_OPEN_REPORT}_${reportID}`);
}, [reportID]);

if (!!(isLoadingInitialReportActions && reportActions.length === 0 && !isOffline) || shouldWaitForTransactions) {
return <InitialLoadingSkeleton styles={styles} />;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import {
isWaitingForSubmissionFromCurrentUser as isWaitingForSubmissionFromCurrentUserReportUtils,
} from '@libs/ReportUtils';
import shouldAdjustScroll from '@libs/shouldAdjustScroll';
import {startSpan} from '@libs/telemetry/activeSpans';
import {hasPendingUI, isManagedCardTransaction, isPending} from '@libs/TransactionUtils';
import colors from '@styles/theme/colors';
import variables from '@styles/variables';
Expand Down Expand Up @@ -481,6 +482,10 @@ function MoneyRequestReportPreviewContent({
}
Performance.markStart(CONST.TIMING.OPEN_REPORT_FROM_PREVIEW);
Timing.start(CONST.TIMING.OPEN_REPORT_FROM_PREVIEW);
startSpan(`${CONST.TELEMETRY.SPAN_OPEN_REPORT}_${iouReportID}`, {
name: 'MoneyRequestReportPreviewContent',
op: CONST.TELEMETRY.SPAN_OPEN_REPORT,
});
Navigation.navigate(ROUTES.EXPENSE_REPORT_RHP.getRoute({reportID: iouReportID, backTo: Navigation.getActiveRoute()}));
}, [iouReportID]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import FS from '@libs/Fullstory';
import Performance from '@libs/Performance';
import {getIOUActionForReportID, isSplitBillAction as isSplitBillActionReportActionsUtils, isTrackExpenseAction as isTrackExpenseActionReportActionsUtils} from '@libs/ReportActionsUtils';
import {isIOUReport} from '@libs/ReportUtils';
import {startSpan} from '@libs/telemetry/activeSpans';
import Navigation from '@navigation/Navigation';
import {contextMenuRef} from '@pages/home/report/ContextMenu/ReportActionContextMenu';
import Timing from '@userActions/Timing';
Expand Down Expand Up @@ -106,6 +107,10 @@ function MoneyRequestReportPreview({

Performance.markStart(CONST.TIMING.OPEN_REPORT_FROM_PREVIEW);
Timing.start(CONST.TIMING.OPEN_REPORT_FROM_PREVIEW);
startSpan(`${CONST.TELEMETRY.SPAN_OPEN_REPORT}_${iouReportID}`, {
name: 'MoneyRequestReportPreview',
op: CONST.TELEMETRY.SPAN_OPEN_REPORT,
});
Navigation.navigate(ROUTES.EXPENSE_REPORT_RHP.getRoute({reportID: iouReportID, backTo: Navigation.getActiveRoute()}));
}, [iouReportID]);

Expand Down
6 changes: 5 additions & 1 deletion src/components/Search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import {
shouldShowEmptyState,
shouldShowYear as shouldShowYearUtil,
} from '@libs/SearchUIUtils';
import {endSpan} from '@libs/telemetry/activeSpans';
import {endSpan, startSpan} from '@libs/telemetry/activeSpans';
import {isOnHold, isTransactionPendingDelete, mergeProhibitedViolations, shouldShowViolation} from '@libs/TransactionUtils';
import Navigation, {navigationRef} from '@navigation/Navigation';
import type {SearchFullscreenNavigatorParamList} from '@navigation/types';
Expand Down Expand Up @@ -722,6 +722,10 @@ function Search({

Performance.markStart(CONST.TIMING.OPEN_REPORT_SEARCH);
Timing.start(CONST.TIMING.OPEN_REPORT_SEARCH);
startSpan(`${CONST.TELEMETRY.SPAN_OPEN_REPORT}_${reportID}`, {
name: 'Search',
op: CONST.TELEMETRY.SPAN_OPEN_REPORT,
});

if (isTransactionGroupListItemType(item)) {
const firstTransaction = item.transactions.at(0);
Expand Down
35 changes: 22 additions & 13 deletions src/libs/Navigation/Navigation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {findFocusedRoute, getActionFromState} from '@react-navigation/core';
import type {EventArg, NavigationAction, NavigationContainerEventMap, NavigationState} from '@react-navigation/native';
import {CommonActions, getPathFromState, StackActions} from '@react-navigation/native';
import {Str} from 'expensify-common';
// eslint-disable-next-line you-dont-need-lodash-underscore/omit
import omit from 'lodash/omit';
import {InteractionManager} from 'react-native';
Expand All @@ -10,7 +11,7 @@ import type {Writable} from 'type-fest';
import getIsNarrowLayout from '@libs/getIsNarrowLayout';
import Log from '@libs/Log';
import {shallowCompare} from '@libs/ObjectUtils';
import {startSpan} from '@libs/telemetry/activeSpans';
import {getSpan, startSpan} from '@libs/telemetry/activeSpans';
import CONST from '@src/CONST';
import NAVIGATORS from '@src/NAVIGATORS';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -198,21 +199,29 @@ function navigate(route: Route, options?: LinkToOptions) {

// Start a Sentry span for report navigation
if (route.startsWith('r/') || route.startsWith('search/r/') || route.startsWith('e/')) {
const reportIDMatch = route.match(/^(?:search\/)?(?:r|e)\/(\w+)/);
const routePath = Str.cutAfter(route, '?');
const reportIDMatch = routePath.match(/^(?:search\/)?(?:r|e)\/(\d+)(?:\/\d+)?$/);
const reportID = reportIDMatch?.at(1);
if (reportID) {
let spanName = '/r/*';
if (route.startsWith('search/r/')) {
spanName = '/search/r/*';
} else if (route.startsWith('e/')) {
spanName = '/e/*';
const spanId = `${CONST.TELEMETRY.SPAN_OPEN_REPORT}_${reportID}`;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this was already the case before, but if we set the reportID in the spanId we will never cancel the previous one if someone clicks on a report and clicks on another report before this one loads no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is not catched edge case. I'm currently trying to resolve this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One way to do so would use destructor of ReportActionsView and MoneyRequestReportActionsList (this is where we call markOpenReportEnd) to cancel span

image

let span = getSpan(spanId);

if (!span) {
let spanName = '/r/*';
if (route.startsWith('search/r/')) {
spanName = '/search/r/*';
} else if (route.startsWith('e/')) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR adds e/ urls.

I need to verify if this flow works as this is something new

spanName = '/e/*';
}
span = startSpan(spanId, {
name: spanName,
op: CONST.TELEMETRY.SPAN_OPEN_REPORT,
});
}
startSpan(`${CONST.TELEMETRY.SPAN_OPEN_REPORT}_${reportID}`, {
name: spanName,
op: CONST.TELEMETRY.SPAN_OPEN_REPORT,
attributes: {
reportID,
},
span.setAttributes({
[CONST.TELEMETRY.ATTRIBUTE_REPORT_ID]: reportID,
[CONST.TELEMETRY.ATTRIBUTE_ROUTE_FROM]: getActiveRouteWithoutParams(),
[CONST.TELEMETRY.ATTRIBUTE_ROUTE_TO]: Str.cutAfter(routePath, '?'),
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ function startMoneyRequest(
[CONST.TELEMETRY.ATTRIBUTE_IOU_TYPE]: iouType,
[CONST.TELEMETRY.ATTRIBUTE_IOU_REQUEST_TYPE]: requestType ?? 'unknown',
[CONST.TELEMETRY.ATTRIBUTE_REPORT_ID]: reportID,
[CONST.TELEMETRY.ATTRIBUTE_SOURCE_ROUTE]: sourceRoute || 'unknown',
[CONST.TELEMETRY.ATTRIBUTE_ROUTE_FROM]: sourceRoute || 'unknown',
},
});
clearMoneyRequest(CONST.IOU.OPTIMISTIC_TRANSACTION_ID, skipConfirmation, draftTransactions);
Expand Down
19 changes: 17 additions & 2 deletions src/libs/telemetry/activeSpans.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type {StartSpanOptions} from '@sentry/core';
import * as Sentry from '@sentry/react-native';
import CONST from '@src/CONST';

const activeSpans = new Map<string, ReturnType<typeof Sentry.startInactiveSpan>>();

function startSpan(spanId: string, options: StartSpanOptions) {
// End any existing span for this name
endSpan(spanId);
cancelSpan(spanId);

const span = Sentry.startInactiveSpan(options);

Expand All @@ -22,8 +23,22 @@ function endSpan(spanId: string) {
if (!span) {
return;
}
span.setStatus({code: 1});
span.end();
activeSpans.delete(spanId);
}

export {startSpan, endSpan};
function cancelSpan(spanId: string) {
const span = activeSpans.get(spanId);
span?.setAttribute(CONST.TELEMETRY.ATTRIBUTE_CANCELED, true);
// In Sentry there are only OK or ERROR status codes.
// We treat canceled spans as OK so we have ability to properly track spans that are not finished at all (their status would be different)
span?.setStatus({code: 1});
endSpan(spanId);
}

function getSpan(spanId: string) {
return activeSpans.get(spanId);
}

export {startSpan, endSpan, getSpan, cancelSpan};
4 changes: 4 additions & 0 deletions src/pages/home/ReportScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ import {
isTaskReport,
isValidReportIDFromPath,
} from '@libs/ReportUtils';
import {cancelSpan} from '@libs/telemetry/activeSpans';
import {isNumeric} from '@libs/ValidationUtils';
import type {ReportsSplitNavigatorParamList, SearchReportParamList} from '@navigation/types';
import {setShouldShowComposeInput} from '@userActions/Composer';
Expand Down Expand Up @@ -607,6 +608,9 @@ function ReportScreen({route, navigation}: ReportScreenProps) {

return () => {
skipOpenReportListener.remove();

// We need to cancel telemetry span when user leaves the screen before full report data is loaded
cancelSpan(`${CONST.TELEMETRY.SPAN_OPEN_REPORT}_${reportID}`);
};
}, [reportID]);

Expand Down