Skip to content

Commit

Permalink
Merge pull request #17238 from mozilla/bye-switch-auth
Browse files Browse the repository at this point in the history
chore(metrics): remove the switch to call event metrics in auth-server
  • Loading branch information
chenba authored Jul 18, 2024
2 parents 6c37b21 + a28d6a2 commit b58a560
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 127 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ commands:
steps:
- run:
name: Linting
command: npx nx affected --base=main --head=$CIRCLE_SHA1 --parallel=2 -t lint
command: npx nx affected --base=main --head=$CIRCLE_SHA1 --parallel=1 -t lint

compile:
steps:
Expand Down
187 changes: 61 additions & 126 deletions packages/fxa-auth-server/lib/metrics/glean/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ type MetricsData = {
oauthClientId?: string;
};

type AdditionalMetricsCallback = (
metrics: Record<string, any>
) => Record<string, any>;

type GleanEventFnOptions = {
// for certain events, passing in the "client" ip address isn't helpful since
// the client in question is a service from an RP
skipClientIp?: boolean;

// a callback to allow the caller to pass in additional metrics
additionalMetrics?: AdditionalMetricsCallback;
};

type ErrorLoggerFnParams = {
Expand Down Expand Up @@ -87,6 +94,27 @@ const findOauthClientId = async (
return clientId || (await clientIdInService()) || '';
};

const getMetricMethod = (eventName: string) => {
const uppercaseWords = eventName
.split('_')
.map((word) => `${word[0].toUpperCase()}${word.slice(1)}`)
.join('');
const methodName = `record${uppercaseWords}`;

if (
!gleanServerEventLogger[methodName as keyof typeof gleanServerEventLogger]
) {
process.stderr.write(
`Method ${methodName} not found in gleanServerEventLogger`
);
process.exit(1);
}

return gleanServerEventLogger[
methodName as keyof typeof gleanServerEventLogger
] as (args: any) => void;
};

const createEventFn =
// On MetricsData: for an event like successful login, the uid isn't known at
// the time of request since the request itself isn't authenticated. We'll
Expand Down Expand Up @@ -138,135 +166,26 @@ const createEventFn =
}

// new style Glean events with event metric type
switch (eventName) {
case 'reg_acc_created':
gleanServerEventLogger.recordRegAccCreated(commonMetrics);
break;
case 'reg_acc_verified':
gleanServerEventLogger.recordRegAccVerified(commonMetrics);
break;
case 'reg_complete':
gleanServerEventLogger.recordRegComplete(commonMetrics);
break;
case 'reg_email_sent':
gleanServerEventLogger.recordRegEmailSent(commonMetrics);
break;
case 'reg_submit_error':
gleanServerEventLogger.recordRegSubmitError({
...commonMetrics,
reason: eventReason,
});
break;
case 'login_success':
gleanServerEventLogger.recordLoginSuccess(commonMetrics);
break;
case 'login_submit_backend_error':
gleanServerEventLogger.recordLoginSubmitBackendError({
...commonMetrics,
reason: eventReason,
});
break;
case 'login_totp_code_success':
gleanServerEventLogger.recordLoginTotpCodeSuccess(commonMetrics);
break;
case 'login_totp_code_failure':
gleanServerEventLogger.recordLoginTotpCodeFailure(commonMetrics);
break;
case 'login_backup_code_success':
gleanServerEventLogger.recordLoginBackupCodeSuccess(commonMetrics);
break;
case 'login_email_confirmation_sent':
gleanServerEventLogger.recordLoginEmailConfirmationSent(
commonMetrics
);
break;
case 'login_email_confirmation_success':
gleanServerEventLogger.recordLoginEmailConfirmationSuccess(
commonMetrics
);
break;
case 'login_complete':
gleanServerEventLogger.recordLoginComplete(commonMetrics);
break;
case 'password_reset_email_sent':
gleanServerEventLogger.recordPasswordResetEmailSent(commonMetrics);
break;
case 'password_reset_create_new_success':
gleanServerEventLogger.recordPasswordResetCreateNewSuccess(
commonMetrics
);
break;
case 'account_password_reset':
gleanServerEventLogger.recordAccountPasswordReset(commonMetrics);
break;
case 'password_reset_recovery_key_success':
gleanServerEventLogger.recordPasswordResetRecoveryKeySuccess(
commonMetrics
);
break;
case 'password_reset_recovery_key_create_success':
gleanServerEventLogger.recordPasswordResetRecoveryKeyCreateSuccess(
commonMetrics
);
break;
case 'password_reset_email_confirmation_sent':
gleanServerEventLogger.recordPasswordResetEmailConfirmationSent(
commonMetrics
);
break;
case 'password_reset_email_confirmation_success':
gleanServerEventLogger.recordPasswordResetEmailConfirmationSuccess(
commonMetrics
);
break;
case 'access_token_created':
gleanServerEventLogger.recordAccessTokenCreated({
const method = getMetricMethod(eventName);
const moreMetrics = options?.additionalMetrics
? options.additionalMetrics({
...commonMetrics,
reason: eventReason,
});
break;
case 'access_token_checked':
gleanServerEventLogger.recordAccessTokenChecked(commonMetrics);
break;
case 'third_party_auth_google_login_complete':
gleanServerEventLogger.recordThirdPartyAuthGoogleLoginComplete({
...commonMetrics,
linking: metricsData?.reason === 'linking',
});
break;
case 'third_party_auth_apple_login_complete':
gleanServerEventLogger.recordThirdPartyAuthAppleLoginComplete({
...commonMetrics,
linking: metricsData?.reason === 'linking',
});
break;
case 'third_party_auth_google_reg_complete':
gleanServerEventLogger.recordThirdPartyAuthGoogleRegComplete(
commonMetrics
);
break;
case 'third_party_auth_apple_reg_complete':
gleanServerEventLogger.recordThirdPartyAuthAppleRegComplete(
commonMetrics
);
break;
case 'third_party_auth_set_password_complete':
gleanServerEventLogger.recordThirdPartyAuthSetPasswordComplete(
commonMetrics
);
break;
case 'account_delete_complete':
gleanServerEventLogger.recordAccountDeleteComplete(commonMetrics);
break;
}
...(metricsData || {}),
})
: {};
method.call(gleanServerEventLogger, { ...commonMetrics, ...moreMetrics });

await gleanEventLogger.record({
gleanEventLogger.record({
...commonMetrics,
event_name: eventName,
event_reason: eventReason,
});
};

const extraKeyReasonCb = (metrics: Record<string, any>) => ({
reason: metrics.reason,
});

export function gleanMetrics(config: ConfigType) {
appConfig = config;
gleanEventLogger = createAccountsEventsEvent({
Expand All @@ -288,12 +207,16 @@ export function gleanMetrics(config: ConfigType) {
confirmationEmailSent: createEventFn('reg_email_sent'),
accountVerified: createEventFn('reg_acc_verified'),
complete: createEventFn('reg_complete'),
error: createEventFn('reg_submit_error'),
error: createEventFn('reg_submit_error', {
additionalMetrics: extraKeyReasonCb,
}),
},

login: {
success: createEventFn('login_success'),
error: createEventFn('login_submit_backend_error'),
error: createEventFn('login_submit_backend_error', {
additionalMetrics: extraKeyReasonCb,
}),
totpSuccess: createEventFn('login_totp_code_success'),
totpFailure: createEventFn('login_totp_code_failure'),
recoveryCodeSuccess: createEventFn('login_backup_code_success'),
Expand All @@ -317,18 +240,30 @@ export function gleanMetrics(config: ConfigType) {
},

oauth: {
tokenCreated: createEventFn('access_token_created'),
tokenCreated: createEventFn('access_token_created', {
additionalMetrics: extraKeyReasonCb,
}),
tokenChecked: createEventFn('access_token_checked', {
skipClientIp: true,
}),
},

thirdPartyAuth: {
googleLoginComplete: createEventFn(
'third_party_auth_google_login_complete'
'third_party_auth_google_login_complete',
{
additionalMetrics: (metrics) => ({
linking: metrics.reason === 'linking',
}),
}
),
appleLoginComplete: createEventFn(
'third_party_auth_apple_login_complete'
'third_party_auth_apple_login_complete',
{
additionalMetrics: (metrics) => ({
linking: metrics.reason === 'linking',
}),
}
),
googleRegComplete: createEventFn('third_party_auth_google_reg_complete'),
appleRegComplete: createEventFn('third_party_auth_apple_reg_complete'),
Expand Down

0 comments on commit b58a560

Please sign in to comment.