Skip to content

Commit

Permalink
chore: ID-3139 Remove events from being sent to metrics API (#2521)
Browse files Browse the repository at this point in the history
Co-authored-by: rodrigo.fournier <[email protected]>
  • Loading branch information
imx-mikhala and rodrigo-fournier-immutable authored Feb 7, 2025
1 parent 6396cbd commit 6d1552e
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 57 deletions.
10 changes: 0 additions & 10 deletions packages/checkout/sdk/src/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable class-methods-use-this */
import { Web3Provider } from '@ethersproject/providers';
import { Environment } from '@imtbl/config';
import { track } from '@imtbl/metrics';
import { Passport } from '@imtbl/passport';
import { ethers } from 'ethers';
import { HttpClient } from './api/http';
Expand Down Expand Up @@ -78,7 +77,6 @@ import * as wallet from './wallet';
import { WidgetConfiguration } from './widgets/definitions/configurations';
import { getWidgetsEsmUrl, loadUnresolvedBundle } from './widgets/load';
import { determineWidgetsVersion, getLatestVersion, validateAndBuildVersion } from './widgets/version';
import { globalPackageVersion } from './env';
import { AssessmentResult, fetchRiskAssessment, isAddressSanctioned } from './riskAssessment';

const SANDBOX_CONFIGURATION = {
Expand Down Expand Up @@ -122,8 +120,6 @@ export class Checkout {

// Initialise injected providers via EIP-6963
InjectedProvidersManager.getInstance().initialise();

track('checkout_sdk', 'initialised');
}

/**
Expand All @@ -150,12 +146,6 @@ export class Checkout {
versionConfig,
);

track('checkout_sdk', 'widgets', {
sdkVersion: globalPackageVersion(),
validatedSdkVersion: validatedBuildVersion,
widgetsVersion,
});

try {
const factory = await this.loadEsModules(init.config, widgetsVersion);
return factory;
Expand Down
7 changes: 4 additions & 3 deletions packages/checkout/widgets-lib/src/lib/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ export const withMetrics = <T>(
return fn(flow);
} catch (error) {
if (error instanceof Error) {
trackError('commerce', flowName, error);
trackError('commerce', flowName, error, { flowId: flow.details.flowId });
} else {
flow.addEvent('errored');
}
flow.addEvent('errored');
throw error;
} finally {
flow.addEvent('End');
Expand All @@ -33,7 +34,7 @@ export const withMetricsAsync = async <T>(
return await fn(flow);
} catch (error:any) {
if (error instanceof Error) {
trackError('commerce', flowName, error);
trackError('commerce', flowName, error, { flowId: flow.details.flowId });
}
if (errorType && errorType(error)) {
flow.addEvent(`errored_${errorType(error)}`);
Expand Down
10 changes: 7 additions & 3 deletions packages/internal/metrics/src/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const getEventName = (flowName: string, eventName: string) => `${flowName}_${cle
const trackFlowFn = (
moduleName: string,
flowName: string,
trackStartEvent: boolean = true,
properties?: AllowedTrackProperties,
): Flow => {
// Track the start of the flow
Expand Down Expand Up @@ -100,8 +101,10 @@ const trackFlowFn = (
previousStepTime = currentTime;
};

// Trigger a Start Event as a record of creating the flow
addEvent('Start');
if (trackStartEvent) {
// Trigger a Start Event as a record of creating the flow
addEvent('Start');
}

return {
details: {
Expand All @@ -119,11 +122,12 @@ const trackFlowFn = (
* Works similarly to `track`
* @param moduleName Name of the module being tracked (for namespacing purposes), e.g. `passport`
* @param flowName Name of the flow, e.g. `performTransaction`
* @param trackStartEvent Whether to track the start event in the flow
* @param properties Other properties to be sent with the event, other than duration
*
* @example
* ```ts
* const flow = trackFlow("passport", "performTransaction", { transationType: "transfer" });
* const flow = trackFlow("passport", "performTransaction", true, { transationType: "transfer" });
* // Do something...
* flow.addEvent("clickItem");
* // Do something...
Expand Down
14 changes: 14 additions & 0 deletions packages/passport/sdk/src/Passport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ describe('Passport', () => {
});
(trackFlow as unknown as jest.Mock).mockImplementation(() => ({
addEvent: jest.fn(),
details: {
flowId: '123',
},
}));
passport = new Passport({
baseConfig: new ImmutableConfiguration({
Expand Down Expand Up @@ -160,6 +163,7 @@ describe('Passport', () => {
'passport',
'connectImx',
e,
{ flowId: '123' },
);
}
});
Expand Down Expand Up @@ -199,6 +203,7 @@ describe('Passport', () => {
'passport',
'connectImxSilent',
e,
{ flowId: '123' },
);
}
});
Expand Down Expand Up @@ -249,6 +254,7 @@ describe('Passport', () => {
'passport',
'connectEvm',
e,
{ flowId: '123' },
);
}
});
Expand All @@ -272,6 +278,7 @@ describe('Passport', () => {
'passport',
'loginCallback',
e,
{ flowId: '123' },
);
}
});
Expand Down Expand Up @@ -311,6 +318,7 @@ describe('Passport', () => {
'passport',
'logout',
e,
{ flowId: '123' },
);
}
});
Expand Down Expand Up @@ -340,6 +348,7 @@ describe('Passport', () => {
'passport',
'logoutDeviceFlow',
e,
{ flowId: '123' },
);
}
});
Expand Down Expand Up @@ -373,6 +382,7 @@ describe('Passport', () => {
'passport',
'getUserInfo',
e,
{ flowId: '123' },
);
}
});
Expand Down Expand Up @@ -406,6 +416,7 @@ describe('Passport', () => {
'passport',
'getIdToken',
e,
{ flowId: '123' },
);
}
});
Expand Down Expand Up @@ -439,6 +450,7 @@ describe('Passport', () => {
'passport',
'getAccessToken',
e,
{ flowId: '123' },
);
}
});
Expand Down Expand Up @@ -479,6 +491,7 @@ describe('Passport', () => {
'passport',
'getLinkedAddresses',
e,
{ flowId: '123' },
);
}
});
Expand Down Expand Up @@ -547,6 +560,7 @@ describe('Passport', () => {
'passport',
'login',
e,
{ flowId: '123' },
);
}
});
Expand Down
19 changes: 10 additions & 9 deletions packages/passport/sdk/src/Passport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ export class Passport {
* `connectImx` instead.
*/
public async connectImxSilent(): Promise<IMXProvider | null> {
return withMetricsAsync(() => this.passportImxProviderFactory.getProviderSilent(), 'connectImxSilent');
return withMetricsAsync(() => this.passportImxProviderFactory.getProviderSilent(), 'connectImxSilent', false);
}

public async connectImx(): Promise<IMXProvider> {
return withMetricsAsync(() => this.passportImxProviderFactory.getProvider(), 'connectImx');
return withMetricsAsync(() => this.passportImxProviderFactory.getProvider(), 'connectImx', false);
}

public connectEvm(options: {
Expand All @@ -167,7 +167,7 @@ export class Passport {
}

return provider;
}, 'connectEvm');
}, 'connectEvm', false);
}

/**
Expand Down Expand Up @@ -305,21 +305,21 @@ export class Passport {
return withMetricsAsync(async () => {
const user = await this.authManager.getUser();
return user?.profile;
}, 'getUserInfo');
}, 'getUserInfo', false);
}

public async getIdToken(): Promise<string | undefined> {
return withMetricsAsync(async () => {
const user = await this.authManager.getUser();
return user?.idToken;
}, 'getIdToken');
}, 'getIdToken', false);
}

public async getAccessToken(): Promise<string | undefined> {
return withMetricsAsync(async () => {
const user = await this.authManager.getUser();
return user?.accessToken;
}, 'getAccessToken');
}, 'getAccessToken', false, false);
}

public async getLinkedAddresses(): Promise<string[]> {
Expand All @@ -331,11 +331,11 @@ export class Passport {
const headers = { Authorization: `Bearer ${user.accessToken}` };
const getUserInfoResult = await this.multiRollupApiClients.passportProfileApi.getUserInfo({ headers });
return getUserInfoResult.data.linked_addresses;
}, 'getLinkedAddresses');
}, 'getLinkedAddresses', false);
}

public async linkExternalWallet(params: LinkWalletParams): Promise<LinkedWallet> {
const flow = trackFlow('passport', 'linkExternalWallet');
const flow = trackFlow('passport', 'linkExternalWallet', false);

const user = await this.authManager.getUser();
if (!user) {
Expand Down Expand Up @@ -363,8 +363,9 @@ export class Passport {
} catch (error) {
if (error instanceof Error) {
trackError('passport', 'linkExternalWallet', error);
} else {
flow.addEvent('errored');
}
flow.addEvent('errored');

if (isAxiosError(error) && error.response) {
if (error.response.data && isAPIError(error.response.data)) {
Expand Down
10 changes: 10 additions & 0 deletions packages/passport/sdk/src/starkEx/passportImxProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ describe('PassportImxProvider', () => {
// Metrics
(trackFlow as unknown as jest.Mock).mockImplementation(() => ({
addEvent: jest.fn(),
details: {
flowId: '123',
},
}));

// Signers
Expand Down Expand Up @@ -135,6 +138,9 @@ describe('PassportImxProvider', () => {
// Metrics
(trackFlow as unknown as jest.Mock).mockImplementation(() => ({
addEvent: jest.fn(),
details: {
flowId: '123',
},
}));

const pp = new PassportImxProvider({
Expand Down Expand Up @@ -402,11 +408,13 @@ describe('PassportImxProvider', () => {
expect(trackFlow).toHaveBeenCalledWith(
'passport',
eventName,
true,
);
expect(trackError).toHaveBeenCalledWith(
'passport',
eventName,
error,
{ flowId: '123' },
);
}
});
Expand Down Expand Up @@ -444,11 +452,13 @@ describe('PassportImxProvider', () => {
expect(trackFlow).toHaveBeenCalledWith(
'passport',
eventName,
true,
);
expect(trackError).toHaveBeenCalledWith(
'passport',
eventName,
error,
{ flowId: '123' },
);
}
});
Expand Down
5 changes: 5 additions & 0 deletions packages/passport/sdk/src/utils/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ describe('passport metrics', () => {
beforeEach(() => {
(trackFlow as unknown as jest.Mock).mockImplementation(() => ({
addEvent: jest.fn(),
details: {
flowId: '123',
},
}));
});

Expand Down Expand Up @@ -36,6 +39,7 @@ describe('passport metrics', () => {
'passport',
'myFlow',
error,
{ flowId: '123' },
);
}
});
Expand Down Expand Up @@ -66,6 +70,7 @@ describe('passport metrics', () => {
'passport',
'myFlow',
error,
{ flowId: '123' },
);
}
});
Expand Down
Loading

0 comments on commit 6d1552e

Please sign in to comment.