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
21 changes: 21 additions & 0 deletions src/CONST/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1718,6 +1718,8 @@ const CONST = {
SIDEBAR_LOADED: 'sidebar_loaded',
LOAD_SEARCH_OPTIONS: 'load_search_options',
SEND_MESSAGE: 'send_message',
SUBMIT_EXPENSE: 'submit_expense',
NAVIGATE_AFTER_EXPENSE_CREATE: 'navigate_after_expense_create',
OPEN_CREATE_EXPENSE: 'open_create_expense',
OPEN_CREATE_EXPENSE_CONTACT: 'open_create_expense_contact',
OPEN_CREATE_EXPENSE_APPROVE: 'open_create_expense_approve',
Expand Down Expand Up @@ -1777,6 +1779,9 @@ const CONST = {
SPAN_OD_ND_TRANSITION_LOGGED_OUT: 'ManualOdNdTransitionLoggedOut',
SPAN_OPEN_SEARCH_ROUTER: 'ManualOpenSearchRouter',
SPAN_OPEN_CREATE_EXPENSE: 'ManualOpenCreateExpense',
SPAN_SUBMIT_EXPENSE: 'ManualCreateExpenseSubmit',
SPAN_NAVIGATE_AFTER_EXPENSE_CREATE: 'ManualCreateExpenseNavigation',
SPAN_EXPENSE_SERVER_RESPONSE: 'ManualCreateExpenseServerResponse',
SPAN_SEND_MESSAGE: 'ManualSendMessage',
SPAN_NOT_FOUND_PAGE: 'ManualNotFoundPage',
SPAN_SKELETON: 'ManualSkeleton',
Expand Down Expand Up @@ -1817,6 +1822,22 @@ const CONST = {
ATTRIBUTE_MIN_DURATION: 'min_duration',
ATTRIBUTE_FINISHED_MANUALLY: 'finished_manually',
ATTRIBUTE_SKELETON_PREFIX: 'skeleton.',
ATTRIBUTE_SCENARIO: 'scenario',
ATTRIBUTE_HAS_RECEIPT: 'has_receipt',
ATTRIBUTE_IS_FROM_GLOBAL_CREATE: 'is_from_global_create',
ATTRIBUTE_COMMAND: 'command',
ATTRIBUTE_JSON_CODE: 'json_code',
SUBMIT_EXPENSE_SCENARIO: {
REQUEST_MONEY_MANUAL: 'request_money_manual',
REQUEST_MONEY_SCAN: 'request_money_scan',
DISTANCE: 'distance',
TRACK_EXPENSE: 'track_expense',
SPLIT: 'split',
SPLIT_RECEIPT: 'split_receipt',
SPLIT_GLOBAL: 'split_global',
INVOICE: 'invoice',
PER_DIEM: 'per_diem',
},
// Event names
EVENT_SKELETON_ATTRIBUTES_UPDATE: 'skeleton_attributes_updated',
CONFIG: {
Expand Down
5 changes: 5 additions & 0 deletions src/components/Search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import {
shouldShowYear as shouldShowYearUtil,
} from '@libs/SearchUIUtils';
import {cancelSpan, endSpan, getSpan, startSpan} from '@libs/telemetry/activeSpans';
import markNavigateAfterExpenseCreateEnd from '@libs/telemetry/markNavigateAfterExpenseCreateEnd';
import {getOriginalTransactionWithSplitInfo, hasValidModifiedAmount, isOnHold, isTransactionPendingDelete} from '@libs/TransactionUtils';
import Navigation, {navigationRef} from '@navigation/Navigation';
import type {SearchFullscreenNavigatorParamList} from '@navigation/types';
Expand Down Expand Up @@ -1326,6 +1327,7 @@ function Search({
const onLayout = useCallback(() => {
endSpan(CONST.TELEMETRY.SPAN_NAVIGATE_TO_REPORTS_TAB);
endSpan(CONST.TELEMETRY.SPAN_NAVIGATE_TO_REPORTS_TAB_RENDER);
markNavigateAfterExpenseCreateEnd();

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.

Is it fine to call Performance.markEnd without ever starting it?

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.

It won't break anything, but it was running on every Search layout for no reason. Added a getSpan() check so it skips entirely when there's no active span.

handleSelectionListScroll(sortedData, searchListRef.current);
}, [handleSelectionListScroll, sortedData]);

Expand Down Expand Up @@ -1373,12 +1375,14 @@ function Search({
if (searchResults === undefined) {
Log.alert('[Search] Undefined search type');
cancelSpan(CONST.TELEMETRY.SPAN_NAVIGATE_TO_REPORTS_TAB);
cancelSpan(CONST.TELEMETRY.SPAN_NAVIGATE_AFTER_EXPENSE_CREATE);
return <FullPageOfflineBlockingView>{null}</FullPageOfflineBlockingView>;
}

if (hasErrors) {
const isInvalidQuery = searchRequestResponseStatusCode === CONST.JSON_CODE.INVALID_SEARCH_QUERY;
cancelSpan(CONST.TELEMETRY.SPAN_NAVIGATE_TO_REPORTS_TAB);
cancelSpan(CONST.TELEMETRY.SPAN_NAVIGATE_AFTER_EXPENSE_CREATE);
return (
<View style={[shouldUseNarrowLayout ? styles.searchListContentContainerStyles : styles.mt3, styles.flex1]}>
<FullPageErrorView
Expand All @@ -1395,6 +1399,7 @@ function Search({
const visibleDataLength = filteredData.filter((item) => item.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE || isOffline).length;
if (shouldShowEmptyState(isDataLoaded, visibleDataLength, searchDataType)) {
cancelSpan(CONST.TELEMETRY.SPAN_NAVIGATE_TO_REPORTS_TAB);
cancelSpan(CONST.TELEMETRY.SPAN_NAVIGATE_AFTER_EXPENSE_CREATE);
return (
<View style={[shouldUseNarrowLayout ? styles.searchListContentContainerStyles : styles.mt3, styles.flex1]}>
<EmptySearchView
Expand Down
4 changes: 4 additions & 0 deletions src/libs/API/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {AnyRequestMatcher, EnablePolicyFeatureCommand} from '@libs/actions/
import Log from '@libs/Log';
import {handleDeletedAccount, HandleUnusedOptimisticID, Logging, Pagination, Reauthentication, RecheckConnection, SaveResponseInOnyx, SupportalPermission} from '@libs/Middleware';
import FraudMonitoring from '@libs/Middleware/FraudMonitoring';
import SentryServerTiming from '@libs/Middleware/SentryServerTiming';
import {isOffline} from '@libs/Network/NetworkStore';
import {push as pushToSequentialQueue, waitForIdle as waitForSequentialQueueIdle} from '@libs/Network/SequentialQueue';
import Pusher from '@libs/Pusher';
Expand Down Expand Up @@ -42,6 +43,9 @@ addMiddleware(HandleUnusedOptimisticID);

addMiddleware(Pagination);

// SentryServerTiming - Tracks server round-trip time for configured command groups via Sentry spans.
addMiddleware(SentryServerTiming);

// SaveResponseInOnyx - Merges either the successData or failureData (or finallyData, if included in place of the former two values) into Onyx depending on if the call was successful or not. This needs to be the LAST middleware we use, don't add any
// middlewares after this, because the SequentialQueue depends on the result of this middleware to pause the queue (if needed) to bring the app to an up-to-date state.
addMiddleware(SaveResponseInOnyx);
Expand Down
83 changes: 83 additions & 0 deletions src/libs/Middleware/SentryServerTiming.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import {WRITE_COMMANDS} from '@libs/API/types';
import {cancelSpan, endSpan, getSpan, startSpan} from '@libs/telemetry/activeSpans';
import CONST from '@src/CONST';
import type Middleware from './types';

/**
* Configuration for a tracked command group.
* Maps a set of write commands to a Sentry span operation name.
*/
type TrackedCommandGroup = {
/** Set of write commands that should be instrumented */
commands: Set<string>;
/** Sentry span operation name */
spanOp: string;
/** Human-readable span name */
spanName: string;
};

/**
* Registry of command groups to instrument with server round-trip timing.
* Add new entries here to track additional flows without creating new middlewares.
*/
const TRACKED_COMMAND_GROUPS: TrackedCommandGroup[] = [
{
commands: new Set<string>([
WRITE_COMMANDS.REQUEST_MONEY,
WRITE_COMMANDS.CREATE_PER_DIEM_REQUEST,
WRITE_COMMANDS.SPLIT_BILL,
WRITE_COMMANDS.SPLIT_BILL_AND_OPEN_REPORT,
WRITE_COMMANDS.START_SPLIT_BILL,
WRITE_COMMANDS.CREATE_DISTANCE_REQUEST,
WRITE_COMMANDS.TRACK_EXPENSE,
WRITE_COMMANDS.SEND_INVOICE,
]),
spanOp: CONST.TELEMETRY.SPAN_EXPENSE_SERVER_RESPONSE,
spanName: 'expense-server-response',
},
];

/**
* Finds the tracked command group for a given command, if any.
*/
function findTrackedGroup(command: string): TrackedCommandGroup | undefined {
return TRACKED_COMMAND_GROUPS.find((group) => group.commands.has(command));
}

/**
* Middleware that tracks server round-trip time for configured command groups via Sentry spans.
* For non-tracked commands, this is a no-op pass-through.
*
* To add tracking for a new flow, add an entry to TRACKED_COMMAND_GROUPS above.
*/
const SentryServerTiming: Middleware = (response, request) => {
const group = findTrackedGroup(request.command);
if (!group) {
return response;
}

const spanId = `${group.spanOp}_${request.requestID}`;
startSpan(spanId, {
name: group.spanName,
op: group.spanOp,
attributes: {
[CONST.TELEMETRY.ATTRIBUTE_COMMAND]: request.command,
},
});

return response
.then((data) => {
const span = getSpan(spanId);
span?.setAttributes({
[CONST.TELEMETRY.ATTRIBUTE_JSON_CODE]: data?.jsonCode,
});
endSpan(spanId);
return data;
})
.catch((error) => {
cancelSpan(spanId);
throw error;
});
};

export default SentryServerTiming;
7 changes: 7 additions & 0 deletions src/libs/actions/IOU/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@
type StartSplitBilActionParams = {
participants: Participant[];
currentUserLogin: string;
currentUserAccountID: number;

Check warning on line 804 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
comment: string;
receipt: Receipt;
existingSplitChatReportID?: string;
Expand Down Expand Up @@ -894,7 +894,7 @@
}

allTransactions = value;
},

Check warning on line 897 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
});

let allTransactionDrafts: NonNullable<OnyxCollection<OnyxTypes.Transaction>> = {};
Expand All @@ -908,7 +908,7 @@

let allTransactionViolations: NonNullable<OnyxCollection<OnyxTypes.TransactionViolations>> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS,

Check warning on line 911 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
waitForCollectionCallback: true,
callback: (value) => {
if (!value) {
Expand All @@ -917,7 +917,7 @@
}

allTransactionViolations = value;
},

Check warning on line 920 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
});

let allPolicyTags: OnyxCollection<OnyxTypes.PolicyTagLists> = {};
Expand All @@ -931,7 +931,7 @@
}
allPolicyTags = value;
},
});

Check warning on line 934 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function

let allReports: OnyxCollection<OnyxTypes.Report>;
Onyx.connect({
Expand All @@ -944,7 +944,7 @@

let allReportNameValuePairs: OnyxCollection<OnyxTypes.ReportNameValuePairs>;
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS,

Check warning on line 947 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
waitForCollectionCallback: true,
callback: (value) => {
allReportNameValuePairs = value;
Expand All @@ -953,7 +953,7 @@

let userAccountID = -1;
let currentUserEmail = '';
Onyx.connect({

Check warning on line 956 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.SESSION,
callback: (value) => {
currentUserEmail = value?.email ?? '';
Expand All @@ -963,7 +963,7 @@

let deprecatedCurrentUserPersonalDetails: OnyxEntry<OnyxTypes.PersonalDetails>;
Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,

Check warning on line 966 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
callback: (value) => {
deprecatedCurrentUserPersonalDetails = value?.[userAccountID] ?? undefined;
},
Expand All @@ -972,7 +972,7 @@
let allReportActions: OnyxCollection<OnyxTypes.ReportActions>;
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
waitForCollectionCallback: true,

Check warning on line 975 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
callback: (actions) => {
if (!actions) {
return;
Expand All @@ -980,7 +980,7 @@
allReportActions = actions;
},
});

Check warning on line 983 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
let personalDetailsList: OnyxEntry<OnyxTypes.PersonalDetailsList>;
Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
Expand Down Expand Up @@ -1134,6 +1134,13 @@
if (!shouldHandleNavigation) {
return;
}

Performance.markStart(CONST.TIMING.NAVIGATE_AFTER_EXPENSE_CREATE);
startSpan(CONST.TELEMETRY.SPAN_NAVIGATE_AFTER_EXPENSE_CREATE, {
name: 'navigate-after-expense-create',
op: CONST.TELEMETRY.SPAN_NAVIGATE_AFTER_EXPENSE_CREATE,
});

const queryString = buildCannedSearchQuery({type});
Navigation.isNavigationReady().then(() => {
if (getIsNarrowLayout()) {
Expand Down
61 changes: 61 additions & 0 deletions src/libs/telemetry/getSubmitExpenseScenario.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import type {IOUType} from '@src/CONST';
import CONST from '@src/CONST';

type GetSubmitExpenseScenarioParams = {
iouType: IOUType;
isDistanceRequest: boolean;
isMovingTransactionFromTrackExpense: boolean;
isUnreported: boolean;
isCategorizingTrackExpense: boolean;
isSharingTrackExpense: boolean;
isPerDiemRequest: boolean;
isFromGlobalCreate: boolean;
hasReceiptFiles: boolean;
};

/**
* Determines the telemetry scenario string for a submit expense action.
*/
function getSubmitExpenseScenario({
iouType,
isDistanceRequest,
isMovingTransactionFromTrackExpense,
isUnreported,
isCategorizingTrackExpense,
isSharingTrackExpense,
isPerDiemRequest,
isFromGlobalCreate,
hasReceiptFiles,
}: GetSubmitExpenseScenarioParams): string {
const {SUBMIT_EXPENSE_SCENARIO} = CONST.TELEMETRY;

if (iouType !== CONST.IOU.TYPE.TRACK && isDistanceRequest && !isMovingTransactionFromTrackExpense && !isUnreported) {
return SUBMIT_EXPENSE_SCENARIO.DISTANCE;
}
if (iouType === CONST.IOU.TYPE.SPLIT) {
if (hasReceiptFiles) {
return SUBMIT_EXPENSE_SCENARIO.SPLIT_RECEIPT;
}
if (isFromGlobalCreate) {
return SUBMIT_EXPENSE_SCENARIO.SPLIT_GLOBAL;
}
return SUBMIT_EXPENSE_SCENARIO.SPLIT;
}
if (iouType === CONST.IOU.TYPE.INVOICE) {
return SUBMIT_EXPENSE_SCENARIO.INVOICE;
}
if (iouType === CONST.IOU.TYPE.TRACK || isCategorizingTrackExpense || isSharingTrackExpense) {
return SUBMIT_EXPENSE_SCENARIO.TRACK_EXPENSE;
}
if (isPerDiemRequest) {
return SUBMIT_EXPENSE_SCENARIO.PER_DIEM;
}
if (hasReceiptFiles) {
return SUBMIT_EXPENSE_SCENARIO.REQUEST_MONEY_SCAN;
}

return SUBMIT_EXPENSE_SCENARIO.REQUEST_MONEY_MANUAL;
}

export default getSubmitExpenseScenario;
export type {GetSubmitExpenseScenarioParams};
16 changes: 16 additions & 0 deletions src/libs/telemetry/markNavigateAfterExpenseCreateEnd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Performance from '@libs/Performance';
import CONST from '@src/CONST';
import {endSpan, getSpan} from './activeSpans';

/**
* Mark the post-submit navigation telemetry span as finished.
*/
function markNavigateAfterExpenseCreateEnd() {
if (!getSpan(CONST.TELEMETRY.SPAN_NAVIGATE_AFTER_EXPENSE_CREATE)) {
return;
}
endSpan(CONST.TELEMETRY.SPAN_NAVIGATE_AFTER_EXPENSE_CREATE);
Performance.markEnd(CONST.TIMING.NAVIGATE_AFTER_EXPENSE_CREATE);
}

export default markNavigateAfterExpenseCreateEnd;
17 changes: 17 additions & 0 deletions src/libs/telemetry/markSubmitExpenseEnd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Performance from '@libs/Performance';
import CONST from '@src/CONST';
import {endSpan, getSpan} from './activeSpans';

/**
* Mark the submit expense telemetry span as finished.
* Called after all API.write() calls for the expense have been dispatched.
*/
function markSubmitExpenseEnd() {
if (!getSpan(CONST.TELEMETRY.SPAN_SUBMIT_EXPENSE)) {
return;
}
endSpan(CONST.TELEMETRY.SPAN_SUBMIT_EXPENSE);
Performance.markEnd(CONST.TIMING.SUBMIT_EXPENSE);
}

export default markSubmitExpenseEnd;
Loading
Loading