-
-
Notifications
You must be signed in to change notification settings - Fork 280
refactor: dedupe sentry trace #8994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gambinish
wants to merge
1
commit into
main
Choose a base branch
from
perps/dedupe-sentry-trace
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+268
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
192 changes: 192 additions & 0 deletions
192
packages/perps-controller/tests/src/services/TradingService.placeOrder.timeout.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| /* eslint-disable */ | ||
| import { PERPS_CONSTANTS } from '../../../src/constants/perpsConfig'; | ||
| import { TradingService } from '../../../src/services/TradingService'; | ||
| import type { | ||
| OrderParams, | ||
| OrderResult, | ||
| PerpsProvider, | ||
| PerpsPlatformDependencies, | ||
| } from '../../../src/types'; | ||
| import { createMockHyperLiquidProvider } from '../../helpers/providerMocks'; | ||
| import { | ||
| createMockInfrastructure, | ||
| createMockServiceContext, | ||
| createMockPerpsControllerState, | ||
| } from '../../helpers/serviceMocks'; | ||
|
|
||
| jest.mock('uuid', () => ({ v4: () => 'mock-trace-id' })); | ||
|
|
||
| describe('TradingService.placeOrder — order submission timeout', () => { | ||
| let tradingService: TradingService; | ||
| let mockDeps: jest.Mocked<PerpsPlatformDependencies>; | ||
| let mockProvider: jest.Mocked<PerpsProvider>; | ||
| let mockRewardsService: { calculateUserFeeDiscount: jest.Mock }; | ||
| let mockContext: ReturnType<typeof createMockServiceContext>; | ||
| let mockReportOrderToDataLake: jest.Mock; | ||
|
|
||
| const baseOrderParams: OrderParams = { | ||
| symbol: 'BTC', | ||
| isBuy: true, | ||
| size: '0.1', | ||
| orderType: 'market', | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| jest.useFakeTimers(); | ||
| mockDeps = createMockInfrastructure(); | ||
| tradingService = new TradingService(mockDeps); | ||
| mockRewardsService = { | ||
| calculateUserFeeDiscount: jest.fn().mockResolvedValue(undefined), | ||
| }; | ||
| tradingService.setControllerDependencies({ | ||
| rewardsIntegrationService: mockRewardsService as never, | ||
| }); | ||
| mockProvider = | ||
| createMockHyperLiquidProvider() as unknown as jest.Mocked<PerpsProvider>; | ||
| mockContext = createMockServiceContext({ | ||
| errorContext: { controller: 'TradingService', method: 'test' }, | ||
| stateManager: { | ||
| update: jest.fn(), | ||
| getState: jest.fn(() => createMockPerpsControllerState()), | ||
| }, | ||
| }); | ||
| mockReportOrderToDataLake = jest.fn().mockResolvedValue({ success: true }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.useRealTimers(); | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('emits no threshold breadcrumb and leaves reason undefined when provider resolves before threshold', async () => { | ||
| const mockResult: OrderResult = { | ||
| success: true, | ||
| orderId: 'order-123', | ||
| filledSize: '0.1', | ||
| averagePrice: '50000', | ||
| }; | ||
| mockProvider.placeOrder.mockResolvedValue(mockResult); | ||
|
|
||
| await tradingService.placeOrder({ | ||
| provider: mockProvider, | ||
| params: baseOrderParams, | ||
| context: mockContext, | ||
| reportOrderToDataLake: mockReportOrderToDataLake, | ||
| }); | ||
|
|
||
| expect(mockDeps.tracer.addBreadcrumb).not.toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| message: 'Order submission exceeded threshold (still pending)', | ||
| }), | ||
| ); | ||
|
|
||
| const endTraceArgs = (mockDeps.tracer.endTrace as jest.Mock).mock | ||
| .calls[0][0]; | ||
| expect(endTraceArgs.data?.reason).toBeUndefined(); | ||
| expect(jest.getTimerCount()).toBe(0); | ||
| }); | ||
|
|
||
| it('emits breadcrumb exactly once and sets reason: late_success when provider resolves after threshold', async () => { | ||
| let resolveOrder!: (result: OrderResult) => void; | ||
| const slowOrder = new Promise<OrderResult>((resolve) => { | ||
| resolveOrder = resolve; | ||
| }); | ||
| mockProvider.placeOrder.mockReturnValue(slowOrder); | ||
|
|
||
| const placeOrderPromise = tradingService.placeOrder({ | ||
| provider: mockProvider, | ||
| params: baseOrderParams, | ||
| context: mockContext, | ||
| reportOrderToDataLake: mockReportOrderToDataLake, | ||
| }); | ||
|
|
||
| // Advance past the threshold, allowing microtasks (fee discount await) to run first | ||
| await jest.advanceTimersByTimeAsync( | ||
| PERPS_CONSTANTS.PlaceOrderTimeoutMs + 1, | ||
| ); | ||
|
|
||
| expect(mockDeps.tracer.addBreadcrumb).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| message: 'Order submission exceeded threshold (still pending)', | ||
| level: 'warning', | ||
| category: 'perps', | ||
| data: expect.objectContaining({ | ||
| thresholdMs: PERPS_CONSTANTS.PlaceOrderTimeoutMs, | ||
| }), | ||
| }), | ||
| ); | ||
| // Exactly one threshold breadcrumb (plus the 'Order execution started' breadcrumb = 2 total) | ||
| const thresholdCalls = ( | ||
| mockDeps.tracer.addBreadcrumb as jest.Mock | ||
| ).mock.calls.filter( | ||
| ([args]: [{ message: string }]) => | ||
| args.message === 'Order submission exceeded threshold (still pending)', | ||
| ); | ||
| expect(thresholdCalls).toHaveLength(1); | ||
|
|
||
| // Resolve the provider after the threshold fired | ||
| resolveOrder({ | ||
| success: true, | ||
| orderId: 'order-456', | ||
| filledSize: '0.1', | ||
| averagePrice: '50000', | ||
| }); | ||
| await placeOrderPromise; | ||
|
|
||
| const endTraceArgs = (mockDeps.tracer.endTrace as jest.Mock).mock | ||
| .calls[0][0]; | ||
| expect(endTraceArgs.data?.reason).toBe('late_success'); | ||
| expect(jest.getTimerCount()).toBe(0); | ||
| }); | ||
|
|
||
| it('sets reason: late_error and rethrows the original error when provider rejects after threshold', async () => { | ||
| let rejectOrder!: (error: Error) => void; | ||
| const slowOrder = new Promise<OrderResult>((_, reject) => { | ||
| rejectOrder = reject; | ||
| }); | ||
| mockProvider.placeOrder.mockReturnValue(slowOrder); | ||
|
|
||
| const placeOrderPromise = tradingService.placeOrder({ | ||
| provider: mockProvider, | ||
| params: baseOrderParams, | ||
| context: mockContext, | ||
| reportOrderToDataLake: mockReportOrderToDataLake, | ||
| }); | ||
|
|
||
| await jest.advanceTimersByTimeAsync( | ||
| PERPS_CONSTANTS.PlaceOrderTimeoutMs + 1, | ||
| ); | ||
|
|
||
| const originalError = new Error('Provider connection timed out'); | ||
| rejectOrder(originalError); | ||
|
|
||
| await expect(placeOrderPromise).rejects.toThrow( | ||
| 'Provider connection timed out', | ||
| ); | ||
|
|
||
| const endTraceArgs = (mockDeps.tracer.endTrace as jest.Mock).mock | ||
| .calls[0][0]; | ||
| expect(endTraceArgs.data?.reason).toBe('late_error'); | ||
| expect(endTraceArgs.data?.success).toBe(false); | ||
| expect(jest.getTimerCount()).toBe(0); | ||
| }); | ||
|
|
||
| it('leaves no pending timers when the provider rejects before the threshold', async () => { | ||
| mockProvider.placeOrder.mockRejectedValue(new Error('immediate failure')); | ||
|
|
||
| await expect( | ||
| tradingService.placeOrder({ | ||
| provider: mockProvider, | ||
| params: baseOrderParams, | ||
| context: mockContext, | ||
| reportOrderToDataLake: mockReportOrderToDataLake, | ||
| }), | ||
| ).rejects.toThrow('immediate failure'); | ||
|
|
||
| expect(jest.getTimerCount()).toBe(0); | ||
|
|
||
| const endTraceArgs = (mockDeps.tracer.endTrace as jest.Mock).mock | ||
| .calls[0][0]; | ||
| expect(endTraceArgs.data?.reason).toBe('error'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Late error mislabels post-provider failures
Medium Severity
The
catchpath sets tracereasontolate_errorwheneverdidExceedOrderSubmissionThresholdis true, including whenprovider.placeOrderalready finished successfully and a later step (e.g.#handleOrderSuccessor#trackOrderResult) throws. That labels a slow-but-successful submission as a late provider error in Sentry.Reviewed by Cursor Bugbot for commit a4f810d. Configure here.