-
Notifications
You must be signed in to change notification settings - Fork 639
feat: Add snap_startTrace and snap_endTrace methods for performance tracing
#3519
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
173 changes: 173 additions & 0 deletions
173
packages/snaps-rpc-methods/src/permitted/endTrace.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,173 @@ | ||
| import { JsonRpcEngine } from '@metamask/json-rpc-engine'; | ||
| import type { EndTraceParams, EndTraceResult } from '@metamask/snaps-sdk'; | ||
| import type { JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils'; | ||
|
|
||
| import { endTraceHandler } from './endTrace'; | ||
|
|
||
| describe('snap_endTrace', () => { | ||
| describe('endTraceHandler', () => { | ||
| it('has the expected shape', () => { | ||
| expect(endTraceHandler).toMatchObject({ | ||
| methodNames: ['snap_endTrace'], | ||
| implementation: expect.any(Function), | ||
| hookNames: { | ||
| endTrace: true, | ||
| getSnap: true, | ||
| }, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('implementation', () => { | ||
| it('calls the `endTrace` hook with the provided parameters', async () => { | ||
| const { implementation } = endTraceHandler; | ||
|
|
||
| const endTrace = jest.fn().mockReturnValue(null); | ||
|
|
||
| const getSnap = jest.fn().mockReturnValue({ preinstalled: true }); | ||
| const hooks = { endTrace, getSnap }; | ||
|
|
||
| const engine = new JsonRpcEngine(); | ||
|
|
||
| engine.push((request, response, next, end) => { | ||
| const result = implementation( | ||
| request as JsonRpcRequest<EndTraceParams>, | ||
| response as PendingJsonRpcResponse<EndTraceResult>, | ||
| next, | ||
| end, | ||
| hooks, | ||
| ); | ||
|
|
||
| result?.catch(end); | ||
| }); | ||
|
|
||
| const response = await engine.handle({ | ||
| jsonrpc: '2.0', | ||
| id: 1, | ||
| method: 'snap_endTrace', | ||
| params: { | ||
| id: 'test-id', | ||
| name: 'Test Trace', | ||
| timestamp: 1234567890, | ||
| }, | ||
| }); | ||
|
|
||
| expect(response).toStrictEqual({ | ||
| jsonrpc: '2.0', | ||
| id: 1, | ||
| result: null, | ||
| }); | ||
|
|
||
| expect(endTrace).toHaveBeenCalledWith({ | ||
| id: 'test-id', | ||
| name: 'Test Trace', | ||
| timestamp: 1234567890, | ||
| }); | ||
| }); | ||
|
|
||
| it('throws an error if the Snap is not preinstalled', async () => { | ||
| const { implementation } = endTraceHandler; | ||
|
|
||
| const endTrace = jest.fn(); | ||
| const getSnap = jest.fn().mockReturnValue({ preinstalled: false }); | ||
| const hooks = { endTrace, getSnap }; | ||
|
|
||
| const engine = new JsonRpcEngine(); | ||
|
|
||
| engine.push((request, response, next, end) => { | ||
| const result = implementation( | ||
| request as JsonRpcRequest<EndTraceParams>, | ||
| response as PendingJsonRpcResponse<EndTraceResult>, | ||
| next, | ||
| end, | ||
| hooks, | ||
| ); | ||
|
|
||
| result?.catch(end); | ||
| }); | ||
|
|
||
| const response = await engine.handle({ | ||
| jsonrpc: '2.0', | ||
| id: 1, | ||
| method: 'snap_endTrace', | ||
| params: { | ||
| id: 'test-id', | ||
| name: 'Test Trace', | ||
| }, | ||
| }); | ||
|
|
||
| expect(endTrace).not.toHaveBeenCalled(); | ||
| expect(response).toStrictEqual({ | ||
| jsonrpc: '2.0', | ||
| id: 1, | ||
| error: { | ||
| code: -32601, | ||
| message: 'The method does not exist / is not available.', | ||
| stack: expect.any(String), | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it.each([ | ||
| [ | ||
| { foo: 'bar' }, | ||
| 'Invalid params: At path: name -- Expected a string, but received: undefined.', | ||
| ], | ||
| [ | ||
| { name: undefined }, | ||
| 'Invalid params: At path: name -- Expected a string, but received: undefined.', | ||
| ], | ||
| [ | ||
| { name: 'Test Trace', id: 123 }, | ||
| 'Invalid params: At path: id -- Expected a string, but received: 123.', | ||
| ], | ||
| [ | ||
| { name: 'Test Trace', id: 'test-id', timestamp: 'not-a-number' }, | ||
| 'Invalid params: At path: timestamp -- Expected a number, but received: "not-a-number".', | ||
| ], | ||
| ])( | ||
| 'throws an error if the parameters are invalid', | ||
| async (params, error) => { | ||
| const { implementation } = endTraceHandler; | ||
|
|
||
| const endTrace = jest.fn(); | ||
| const getSnap = jest.fn().mockReturnValue({ preinstalled: true }); | ||
| const hooks = { endTrace, getSnap }; | ||
|
|
||
| const engine = new JsonRpcEngine(); | ||
|
|
||
| engine.push((request, response, next, end) => { | ||
| const result = implementation( | ||
| request as JsonRpcRequest<EndTraceParams>, | ||
| response as PendingJsonRpcResponse<EndTraceResult>, | ||
| next, | ||
| end, | ||
| hooks, | ||
| ); | ||
|
|
||
| result?.catch(end); | ||
| }); | ||
|
|
||
| // @ts-expect-error: Intentionally passing invalid params. | ||
| // eslint-disable-next-line @typescript-eslint/await-thenable | ||
| const response = await engine.handle({ | ||
| jsonrpc: '2.0', | ||
| id: 1, | ||
| method: 'snap_endTrace', | ||
| params, | ||
| }); | ||
|
|
||
| expect(endTrace).not.toHaveBeenCalled(); | ||
| expect(response).toStrictEqual({ | ||
| jsonrpc: '2.0', | ||
| id: 1, | ||
| error: { | ||
| code: -32602, | ||
| message: error, | ||
| stack: expect.any(String), | ||
| }, | ||
| }); | ||
| }, | ||
| ); | ||
| }); | ||
| }); |
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,132 @@ | ||
| import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine'; | ||
| import type { PermittedHandlerExport } from '@metamask/permission-controller'; | ||
| import { rpcErrors } from '@metamask/rpc-errors'; | ||
| import type { | ||
| JsonRpcRequest, | ||
| EndTraceParams, | ||
| EndTraceResult, | ||
| EndTraceRequest, | ||
| } from '@metamask/snaps-sdk'; | ||
| import type { InferMatching, Snap } from '@metamask/snaps-utils'; | ||
| import { | ||
| number, | ||
| create, | ||
| object, | ||
| string, | ||
| StructError, | ||
| exactOptional, | ||
| } from '@metamask/superstruct'; | ||
| import type { PendingJsonRpcResponse } from '@metamask/utils'; | ||
|
|
||
| import type { MethodHooksObject } from '../utils'; | ||
|
|
||
| const hookNames: MethodHooksObject<EndTraceMethodHooks> = { | ||
| endTrace: true, | ||
| getSnap: true, | ||
| }; | ||
|
|
||
| export type EndTraceMethodHooks = { | ||
| /** | ||
| * End a performance trace in Sentry. | ||
| * | ||
| * @param request - The trace request object. | ||
| * @returns The performance trace context. | ||
| */ | ||
| endTrace: (request: EndTraceRequest) => void; | ||
|
|
||
| /** | ||
| * Get Snap metadata. | ||
| * | ||
| * @param snapId - The ID of a Snap. | ||
| */ | ||
| getSnap: (snapId: string) => Snap | undefined; | ||
| }; | ||
|
|
||
| const EndTraceParametersStruct = object({ | ||
| id: exactOptional(string()), | ||
| name: string(), | ||
| timestamp: exactOptional(number()), | ||
| }); | ||
|
|
||
| export type EndTraceParameters = InferMatching< | ||
| typeof EndTraceParametersStruct, | ||
| EndTraceParams | ||
| >; | ||
|
|
||
| /** | ||
| * Handler for the `snap_endTrace` method. | ||
| */ | ||
| export const endTraceHandler: PermittedHandlerExport< | ||
| EndTraceMethodHooks, | ||
| EndTraceParameters, | ||
| EndTraceResult | ||
| > = { | ||
| methodNames: ['snap_endTrace'], | ||
| implementation: getEndTraceImplementation, | ||
| hookNames, | ||
| }; | ||
|
|
||
| /** | ||
| * The `snap_endTrace` method implementation. This method is used to end a | ||
| * performance trace in Sentry. It is only available to preinstalled Snaps. | ||
| * | ||
| * @param request - The JSON-RPC request object. | ||
| * @param response - The JSON-RPC response object. | ||
| * @param _next - The `json-rpc-engine` "next" callback. Not used by this | ||
| * function. | ||
| * @param end - The `json-rpc-engine` "end" callback. | ||
| * @param hooks - The RPC method hooks. | ||
| * @param hooks.endTrace - The hook function to end a performance trace. | ||
| * @param hooks.getSnap - The hook function to get Snap metadata. | ||
| * @returns Nothing. | ||
| */ | ||
| function getEndTraceImplementation( | ||
| request: JsonRpcRequest<EndTraceParameters>, | ||
| response: PendingJsonRpcResponse, | ||
| _next: unknown, | ||
| end: JsonRpcEngineEndCallback, | ||
| { endTrace, getSnap }: EndTraceMethodHooks, | ||
| ): void { | ||
| const snap = getSnap( | ||
| (request as JsonRpcRequest<EndTraceParams> & { origin: string }).origin, | ||
| ); | ||
|
|
||
| if (!snap?.preinstalled) { | ||
| return end(rpcErrors.methodNotFound()); | ||
| } | ||
|
|
||
| const { params } = request; | ||
|
|
||
| try { | ||
| const validatedParams = getValidatedParams(params); | ||
| endTrace(validatedParams); | ||
|
|
||
| response.result = null; | ||
| } catch (error) { | ||
| return end(error); | ||
| } | ||
|
|
||
| return end(); | ||
| } | ||
|
|
||
| /** | ||
| * Validate the parameters for the `snap_endTrace` method. | ||
| * | ||
| * @param params - Parameters to validate. | ||
| * @returns Validated parameters. | ||
| * @throws Throws RPC error if validation fails. | ||
| */ | ||
| function getValidatedParams(params: unknown): EndTraceParameters { | ||
| try { | ||
| return create(params, EndTraceParametersStruct); | ||
| } catch (error) { | ||
| if (error instanceof StructError) { | ||
| throw rpcErrors.invalidParams({ | ||
| message: `Invalid params: ${error.message}.`, | ||
| }); | ||
| } | ||
|
|
||
| /* istanbul ignore next */ | ||
| throw rpcErrors.internal(); | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
Not for this PR, but we should add the metrics method to this Snap too so we can test it all