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
4 changes: 4 additions & 0 deletions src/CONST/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2208,6 +2208,7 @@ const CONST = {
SPAN_SUBMIT_EXPENSE: 'ManualCreateExpenseSubmit',
SPAN_SUBMIT_TO_DESTINATION_VISIBLE: 'ManualSubmitToDestinationVisible',
SPAN_EXPENSE_SERVER_RESPONSE: 'ManualCreateExpenseServerResponse',
SPAN_RECONNECT_SERVER_RESPONSE: 'ManualReconnectServerResponse',
SPAN_GEOLOCATION_WAIT: 'ManualGeolocationWait',
SPAN_SEND_MESSAGE: 'ManualSendMessage',
SPAN_NOT_FOUND_PAGE: 'ManualNotFoundPage',
Expand Down Expand Up @@ -2278,6 +2279,9 @@ const CONST = {
ATTRIBUTE_FAST_PATH_HANDLER: 'fast_path_handler',
ATTRIBUTE_COMMAND: 'command',
ATTRIBUTE_JSON_CODE: 'json_code',
ATTRIBUTE_UPDATE_ID_FROM: 'update_id_from',
ATTRIBUTE_UPDATE_ID_TO: 'update_id_to',
ATTRIBUTE_RESPONSE_ADVANCED: 'response_advanced',
ATTRIBUTE_COLD_START: 'cold_start',
ATTRIBUTE_TRIGGER: 'trigger',
ATTRIBUTE_PLATFORM: 'platform',
Expand Down
25 changes: 24 additions & 1 deletion src/libs/Middleware/SentryServerTiming.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {WRITE_COMMANDS} from '@libs/API/types';
import {readUpdateIDFrom} from '@libs/actions/RequestConflictUtils';
import {SIDE_EFFECT_REQUEST_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
import {cancelSpan, endSpanWithAttributes, startSpan} from '@libs/telemetry/activeSpans';

import CONST from '@src/CONST';
Expand Down Expand Up @@ -39,6 +40,11 @@ const TRACKED_COMMAND_GROUPS: TrackedCommandGroup[] = [
spanOp: CONST.TELEMETRY.SPAN_EXPENSE_SERVER_RESPONSE,
spanName: 'expense-server-response',
},
{
commands: new Set<string>([SIDE_EFFECT_REQUEST_COMMANDS.RECONNECT_APP, SIDE_EFFECT_REQUEST_COMMANDS.GET_MISSING_ONYX_MESSAGES]),
spanOp: CONST.TELEMETRY.SPAN_RECONNECT_SERVER_RESPONSE,
spanName: 'reconnect-server-response',
},
];

/**
Expand All @@ -48,6 +54,19 @@ function findTrackedGroup(command: string): TrackedCommandGroup | undefined {
return TRACKED_COMMAND_GROUPS.find((group) => group.commands.has(command));
}

function readUpdateID(value: unknown): number | undefined {
const updateID = value === null || value === '' ? Number.NaN : Number(value);
return Number.isFinite(updateID) ? updateID : undefined;
}

function didResponseAdvance(updateIDFrom: number | undefined, lastUpdateID: number | string | undefined): boolean | undefined {
const responseUpdateID = readUpdateID(lastUpdateID);
if (updateIDFrom === undefined || responseUpdateID === undefined) {
return undefined;
}
return responseUpdateID > updateIDFrom;
}

/**
* 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.
Expand All @@ -60,19 +79,23 @@ const SentryServerTiming: Middleware = (response, request) => {
return response;
}

const updateIDFrom = readUpdateIDFrom(request.data);
const spanId = `${group.spanOp}_${request.requestIndex}`;
startSpan(spanId, {
name: group.spanName,
op: group.spanOp,
attributes: {
[CONST.TELEMETRY.ATTRIBUTE_COMMAND]: request.command,
[CONST.TELEMETRY.ATTRIBUTE_UPDATE_ID_FROM]: updateIDFrom,
[CONST.TELEMETRY.ATTRIBUTE_UPDATE_ID_TO]: readUpdateID(request.data?.updateIDTo),
},
});

return response
.then((data) => {
const attributes: SpanAttributes = {
[CONST.TELEMETRY.ATTRIBUTE_JSON_CODE]: data?.jsonCode,
[CONST.TELEMETRY.ATTRIBUTE_RESPONSE_ADVANCED]: didResponseAdvance(updateIDFrom, data?.lastUpdateID),
};
endSpanWithAttributes(spanId, attributes);
return data;
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/OnyxUpdateManager/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function validateAndApplyDeferredUpdates(clientLastUpdateID?: number, previousPa

// Prevent info loops of calls to GetMissingOnyxMessages
if (previousParams?.newLastUpdateIDFromClient === newLastUpdateIDFromClient && previousParams?.latestMissingUpdateID === latestMissingUpdateID) {
Log.info('[DeferredUpdates] Aborting call to GetMissingOnyxMessages, repeated params', false, {lastUpdateIDFromClient, latestMissingUpdateID, previousParams});
Log.alert('[DeferredUpdates] Aborting call to GetMissingOnyxMessages, repeated params', {lastUpdateIDFromClient, latestMissingUpdateID, previousParams});
resolve(undefined);
return;
}
Expand Down
97 changes: 97 additions & 0 deletions tests/unit/SentryServerTimingMiddlewareTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import SentryServerTiming from '@libs/Middleware/SentryServerTiming';
import {endSpanWithAttributes, startSpan} from '@libs/telemetry/activeSpans';

import CONST from '@src/CONST';
import type Request from '@src/types/onyx/Request';
import type Response from '@src/types/onyx/Response';

import type {OnyxKey} from 'react-native-onyx';

jest.mock('@libs/telemetry/activeSpans', () => ({
startSpan: jest.fn(),
endSpanWithAttributes: jest.fn(),
cancelSpan: jest.fn(),
}));

let requestIndex = 0;

function buildRequest(command: string, data: Record<string, unknown>): Request<OnyxKey> {
requestIndex += 1;
return {command, data, requestIndex};
}

function buildResponse(lastUpdateID: number): Response<OnyxKey> {
return {jsonCode: 200, lastUpdateID};
}

describe('SentryServerTiming middleware', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('marks a GetMissingOnyxMessages response as not advanced when its ceiling matches what the request asked from', async () => {
const request = buildRequest('GetMissingOnyxMessages', {updateIDFrom: 8102, updateIDTo: 8102});

await SentryServerTiming(Promise.resolve(buildResponse(8102)), request, false);

expect(endSpanWithAttributes).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({[CONST.TELEMETRY.ATTRIBUTE_RESPONSE_ADVANCED]: false}));
});

it('marks a GetMissingOnyxMessages response as advanced when its ceiling is newer', async () => {
const request = buildRequest('GetMissingOnyxMessages', {updateIDFrom: 8102, updateIDTo: 8200});

await SentryServerTiming(Promise.resolve(buildResponse(8200)), request, false);

expect(endSpanWithAttributes).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({[CONST.TELEMETRY.ATTRIBUTE_RESPONSE_ADVANCED]: true}));
});

it('leaves the verdict off a full ReconnectApp, which fetches everything again and has nothing to advance past', async () => {
const request = buildRequest('ReconnectApp', {});

await SentryServerTiming(Promise.resolve(buildResponse(8200)), request, false);

expect(endSpanWithAttributes).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({[CONST.TELEMETRY.ATTRIBUTE_RESPONSE_ADVANCED]: undefined}));
});

it('leaves the verdict off when the response carries no update ID, so an unreadable answer never reads as a stall', async () => {
const request = buildRequest('ReconnectApp', {updateIDFrom: 8102});

await SentryServerTiming(Promise.resolve({jsonCode: 200}), request, false);

expect(endSpanWithAttributes).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({[CONST.TELEMETRY.ATTRIBUTE_RESPONSE_ADVANCED]: undefined}));
});

it('stamps the range a GetMissingOnyxMessages request asked for, parsing the string form of the target', async () => {
const request = buildRequest('GetMissingOnyxMessages', {updateIDFrom: 8102, updateIDTo: '8200'});

await SentryServerTiming(Promise.resolve(buildResponse(8200)), request, false);

expect(startSpan).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
attributes: expect.objectContaining({
[CONST.TELEMETRY.ATTRIBUTE_UPDATE_ID_FROM]: 8102,
[CONST.TELEMETRY.ATTRIBUTE_UPDATE_ID_TO]: 8200,
}),
}),
);
});

it('leaves the target off a ReconnectApp, which has no update ID to catch up to', async () => {
const request = buildRequest('ReconnectApp', {updateIDFrom: 8102});

await SentryServerTiming(Promise.resolve(buildResponse(8200)), request, false);

expect(startSpan).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({attributes: expect.objectContaining({[CONST.TELEMETRY.ATTRIBUTE_UPDATE_ID_TO]: undefined})}));
});

it('gives overlapping reconnect requests their own span, so one does not cancel the other', async () => {
const first = SentryServerTiming(Promise.resolve(buildResponse(8102)), buildRequest('GetMissingOnyxMessages', {updateIDFrom: 8102}), false);
const second = SentryServerTiming(Promise.resolve(buildResponse(8200)), buildRequest('ReconnectApp', {updateIDFrom: 8102}), false);

await Promise.all([first, second]);

const spanIDs = jest.mocked(startSpan).mock.calls.map(([spanID]) => spanID);
expect(new Set(spanIDs).size).toBe(2);
});
});
Loading