Skip to content

Commit

Permalink
ID-2416 tracking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenfowler committed Sep 10, 2024
1 parent aa70955 commit 7dbfab7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 40 deletions.
36 changes: 21 additions & 15 deletions packages/passport/sdk/src/magicAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { SDKBase, InstanceWithExtensions } from '@magic-sdk/provider';
import { Magic } from 'magic-sdk';
import { OpenIdExtension } from '@magic-ext/oidc';
import { ethers } from 'ethers';
import { trackDuration } from '@imtbl/metrics';
import { Flow, trackDuration } from '@imtbl/metrics';
import { PassportErrorType, withPassportError } from './errors/passportError';
import { PassportConfiguration } from './config';
import { lazyDocumentReady } from './utils/lazyLoad';
import { withMetricsAsync } from './utils/metrics';

type MagicClient = InstanceWithExtensions<SDKBase, [OpenIdExtension]>;

Expand Down Expand Up @@ -40,23 +41,28 @@ export default class MagicAdapter {
async login(
idToken: string,
): Promise<ethers.providers.ExternalProvider> {
return withPassportError<ethers.providers.ExternalProvider>(async () => {
const startTime = performance.now();
return withPassportError<ethers.providers.ExternalProvider>(async () => (
withMetricsAsync(async (flow: Flow) => {
const startTime = performance.now();

const magicClient = await this.magicClient;
await magicClient.openid.loginWithOIDC({
jwt: idToken,
providerId: this.config.magicProviderId,
});
const magicClient = await this.magicClient;
flow.addEvent('endMagicClientInit');

await magicClient.openid.loginWithOIDC({
jwt: idToken,
providerId: this.config.magicProviderId,
});
flow.addEvent('endLoginWithOIDC');

trackDuration(
'passport',
'magicLogin',
Math.round(performance.now() - startTime),
);
trackDuration(
'passport',
flow.details.flowName,
Math.round(performance.now() - startTime),
);

return magicClient.rpcProvider as unknown as ethers.providers.ExternalProvider;
}, PassportErrorType.WALLET_CONNECTION_ERROR);
return magicClient.rpcProvider as unknown as ethers.providers.ExternalProvider;
}, 'magicLogin')
), PassportErrorType.WALLET_CONNECTION_ERROR);
}

async logout() {
Expand Down
18 changes: 10 additions & 8 deletions packages/passport/sdk/src/utils/lazyLoad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ export const lazyLoad = <T, Y = void>(

export const lazyDocumentReady = <T>(initialiseFunction: () => Promise<T> | T): Promise<T> => {
const documentReadyPromise = () => new Promise<void>((resolve) => {
const onReadyStateChange = () => {
if (window.document.readyState === 'complete') {
resolve();
window.document.removeEventListener('readystatechange', onReadyStateChange);
}
};

// Add a handler before checking `readyState` to ensure that we don't miss the event
window.document.addEventListener('readystatechange', onReadyStateChange);
if (window.document.readyState === 'complete') {
resolve();
} else {
const onReadyStateChange = () => {
if (window.document.readyState === 'complete') {
resolve();
window.document.removeEventListener('readystatechange', onReadyStateChange);
}
};
window.document.addEventListener('readystatechange', onReadyStateChange);
window.document.removeEventListener('readystatechange', onReadyStateChange);
}
});

Expand Down
12 changes: 6 additions & 6 deletions packages/passport/sdk/src/utils/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('passport metrics', () => {
const mockFn = jest.fn();
mockFn.mockReturnValue(returnValue);

expect(withMetrics(mockFn, 'event')).toEqual(returnValue);
expect(withMetrics(mockFn, 'myFlow')).toEqual(returnValue);
});

it('should track and re-throw error', () => {
Expand All @@ -25,7 +25,7 @@ describe('passport metrics', () => {
});

try {
withMetrics(mockFn, 'event');
withMetrics(mockFn, 'myFlow');
} catch (error) {
expect(error).toBeInstanceOf(Error);
expect(error).toMatchObject({
Expand All @@ -34,7 +34,7 @@ describe('passport metrics', () => {
expect(trackFlow).toBeCalledTimes(1);
expect(trackError).toHaveBeenCalledWith(
'passport',
'event',
'myFlow',
error,
);
}
Expand All @@ -47,15 +47,15 @@ describe('passport metrics', () => {
const mockFn = jest.fn();
mockFn.mockResolvedValue(returnValue);

expect(await withMetricsAsync(mockFn, 'event')).toEqual(returnValue);
expect(await withMetricsAsync(mockFn, 'myFlow')).toEqual(returnValue);
});

it('should track and re-throw error', async () => {
const errorFunction = jest.fn();
errorFunction.mockRejectedValue(new Error('error'));

try {
await withMetricsAsync(errorFunction, 'event');
await withMetricsAsync(errorFunction, 'myFlow');
} catch (error) {
expect(error).toBeInstanceOf(Error);
expect(error).toMatchObject({
Expand All @@ -64,7 +64,7 @@ describe('passport metrics', () => {
expect(trackFlow).toBeCalledTimes(1);
expect(trackError).toHaveBeenCalledWith(
'passport',
'event',
'myFlow',
error,
);
}
Expand Down
22 changes: 11 additions & 11 deletions packages/passport/sdk/src/utils/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { trackError, trackFlow } from '@imtbl/metrics';
import { Flow, trackError, trackFlow } from '@imtbl/metrics';

export const withMetrics = <T>(
fn: () => T,
event: string,
fn: (flow: Flow) => T,
flowName: string,
): T => {
const flow = trackFlow('passport', event);
const flow: Flow = trackFlow('passport', flowName);

try {
return fn();
return fn(flow);
} catch (error) {
if (error instanceof Error) {
trackError('passport', event, error);
trackError('passport', flowName, error);
}
flow.addEvent('errored');
throw error;
Expand All @@ -20,16 +20,16 @@ export const withMetrics = <T>(
};

export const withMetricsAsync = async <T>(
fn: () => Promise<T>,
event: string,
fn: (flow: Flow) => Promise<T>,
flowName: string,
): Promise<T> => {
const flow = trackFlow('passport', event);
const flow: Flow = trackFlow('passport', flowName);

try {
return await fn();
return await fn(flow);
} catch (error) {
if (error instanceof Error) {
trackError('passport', event, error);
trackError('passport', flowName, error);
}
flow.addEvent('errored');
throw error;
Expand Down

0 comments on commit 7dbfab7

Please sign in to comment.