Skip to content

Commit

Permalink
feat(payments-metrics): add glean enabled env var
Browse files Browse the repository at this point in the history
Because:

- Need to be able to enable or disable glean recording of events.

This commit:

- Adds enabled value to payments metrics glean config
- Updates payments metrics glean manager to respect enabled config value

Closes #FXA-10564
  • Loading branch information
StaberindeZA committed Oct 29, 2024
1 parent ffd4e0d commit 8054aeb
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 21 deletions.
20 changes: 19 additions & 1 deletion libs/payments/metrics/src/lib/glean/glean.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { IsEnum, IsString } from 'class-validator';
import { faker } from '@faker-js/faker';
import { Provider } from '@nestjs/common';
import { IsBoolean, IsEnum, IsString } from 'class-validator';

enum GleanChannel {
Development = 'development',
Expand All @@ -10,6 +12,9 @@ enum GleanChannel {
}

export class PaymentsGleanConfig {
@IsBoolean()
enabled!: boolean;

@IsString()
applicationId!: string;

Expand All @@ -22,3 +27,16 @@ export class PaymentsGleanConfig {
@IsString()
loggerAppName!: string;
}

export const MockPaymentsGleanConfig = {
enabled: true,
applicationId: faker.string.uuid(),
version: '0.0.1',
channel: GleanChannel.Development,
loggerAppName: 'payments-next-test',
} satisfies PaymentsGleanConfig;

export const MockPaymentsGleanConfigProvider = {
provide: PaymentsGleanConfig,
useValue: MockPaymentsGleanConfig,
} satisfies Provider<PaymentsGleanConfig>;
76 changes: 75 additions & 1 deletion libs/payments/metrics/src/lib/glean/glean.manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import {
} from './glean.factory';
import { PaymentsGleanProvider } from './glean.types';
import { MockPaymentsGleanFactory } from './glean.test-provider';
import {
MockPaymentsGleanConfigProvider,
PaymentsGleanConfig,
} from './glean.config';

const mockCommonMetricsData = {
commonMetricsData: CommonMetricsFactory(),
Expand All @@ -26,7 +30,11 @@ describe('PaymentsGleanManager', () => {

beforeEach(async () => {
const moduleRef = await Test.createTestingModule({
providers: [MockPaymentsGleanFactory, PaymentsGleanManager],
providers: [
MockPaymentsGleanFactory,
PaymentsGleanManager,
MockPaymentsGleanConfigProvider,
],
}).compile();

paymentsGleanManager = moduleRef.get(PaymentsGleanManager);
Expand Down Expand Up @@ -156,4 +164,70 @@ describe('PaymentsGleanManager', () => {
});
});
});

describe('enabled is false', () => {
{
let paymentsGleanManager: PaymentsGleanManager;
let paymentsGleanServerEventsLogger: any;

beforeEach(async () => {
const moduleRef = await Test.createTestingModule({
providers: [
MockPaymentsGleanFactory,
PaymentsGleanManager,
MockPaymentsGleanConfigProvider,
{
provide: PaymentsGleanConfig,
useValue: {
enabled: false,
},
},
],
}).compile();

paymentsGleanManager = moduleRef.get(PaymentsGleanManager);
paymentsGleanServerEventsLogger = moduleRef.get(PaymentsGleanProvider);
jest.spyOn(paymentsGleanServerEventsLogger, 'recordPaySetupView');
jest.spyOn(paymentsGleanServerEventsLogger, 'recordPaySetupEngage');
jest.spyOn(paymentsGleanServerEventsLogger, 'recordPaySetupSubmit');
jest.spyOn(paymentsGleanServerEventsLogger, 'recordPaySetupSuccess');
jest.spyOn(paymentsGleanServerEventsLogger, 'recordPaySetupFail');
});

it('recordFxaPaySetupView', () => {
paymentsGleanManager.recordFxaPaySetupView(mockCommonMetricsData);
expect(
paymentsGleanServerEventsLogger.recordPaySetupView
).not.toHaveBeenCalled();
});

it('recordFxaPaySetupEngage', () => {
paymentsGleanManager.recordFxaPaySetupEngage(mockCommonMetricsData);
expect(
paymentsGleanServerEventsLogger.recordPaySetupEngage
).not.toHaveBeenCalled();
});

it('recordFxaPaySetupSubmit', () => {
paymentsGleanManager.recordFxaPaySetupSubmit(mockCommonMetricsData);
expect(
paymentsGleanServerEventsLogger.recordPaySetupSubmit
).not.toHaveBeenCalled();
});

it('recordFxaPaySetupSuccess', () => {
paymentsGleanManager.recordFxaPaySetupSuccess(mockCommonMetricsData);
expect(
paymentsGleanServerEventsLogger.recordPaySetupSuccess
).not.toHaveBeenCalled();
});

it('recordFxaPaySetupFail', () => {
paymentsGleanManager.recordFxaPaySetupFail(mockCommonMetricsData);
expect(
paymentsGleanServerEventsLogger.recordPaySetupFail
).not.toHaveBeenCalled();
});
}
});
});
53 changes: 34 additions & 19 deletions libs/payments/metrics/src/lib/glean/glean.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,38 @@ import { mapUtm } from './utils/mapUtm';
import { mapSubscription } from './utils/mapSubscription';
import { mapRelyingParty } from './utils/mapRelyingParty';
import { normalizeGleanFalsyValues } from './utils/normalizeGleanFalsyValues';
import { PaymentsGleanConfig } from './glean.config';

@Injectable()
export class PaymentsGleanManager {
constructor(
@Inject(PaymentsGleanProvider)
private paymentsGleanServerEventsLogger: PaymentsGleanServerEventsLogger
private paymentsGleanServerEventsLogger: PaymentsGleanServerEventsLogger,
private paymentsGleanConfig: PaymentsGleanConfig
) {}

recordFxaPaySetupView(metrics: {
commonMetricsData: CommonMetrics;
cartMetricsData: CartMetrics;
cmsMetricsData: CmsMetricsData;
}) {
this.paymentsGleanServerEventsLogger.recordPaySetupView(
this.populateCommonMetrics(metrics)
);
if (this.paymentsGleanConfig.enabled) {
this.paymentsGleanServerEventsLogger.recordPaySetupView(
this.populateCommonMetrics(metrics)
);
}
}

recordFxaPaySetupEngage(metrics: {
commonMetricsData: CommonMetrics;
cartMetricsData: CartMetrics;
cmsMetricsData: CmsMetricsData;
}) {
this.paymentsGleanServerEventsLogger.recordPaySetupEngage(
this.populateCommonMetrics(metrics)
);
if (this.paymentsGleanConfig.enabled) {
this.paymentsGleanServerEventsLogger.recordPaySetupEngage(
this.populateCommonMetrics(metrics)
);
}
}

recordFxaPaySetupSubmit(
Expand All @@ -51,10 +57,13 @@ export class PaymentsGleanManager {
},
paymentProvider?: PaymentProvidersType
) {
this.paymentsGleanServerEventsLogger.recordPaySetupSubmit({
...this.populateCommonMetrics(metrics),
subscription_payment_provider: normalizeGleanFalsyValues(paymentProvider),
});
if (this.paymentsGleanConfig.enabled) {
this.paymentsGleanServerEventsLogger.recordPaySetupSubmit({
...this.populateCommonMetrics(metrics),
subscription_payment_provider:
normalizeGleanFalsyValues(paymentProvider),
});
}
}

recordFxaPaySetupSuccess(
Expand All @@ -67,10 +76,13 @@ export class PaymentsGleanManager {
) {
const commonMetrics = this.populateCommonMetrics(metrics);

this.paymentsGleanServerEventsLogger.recordPaySetupSuccess({
...commonMetrics,
subscription_payment_provider: normalizeGleanFalsyValues(paymentProvider),
});
if (this.paymentsGleanConfig.enabled) {
this.paymentsGleanServerEventsLogger.recordPaySetupSuccess({
...commonMetrics,
subscription_payment_provider:
normalizeGleanFalsyValues(paymentProvider),
});
}
}

recordFxaPaySetupFail(
Expand All @@ -83,10 +95,13 @@ export class PaymentsGleanManager {
) {
const commonMetrics = this.populateCommonMetrics(metrics);

this.paymentsGleanServerEventsLogger.recordPaySetupFail({
...commonMetrics,
subscription_payment_provider: normalizeGleanFalsyValues(paymentProvider),
});
if (this.paymentsGleanConfig.enabled) {
this.paymentsGleanServerEventsLogger.recordPaySetupFail({
...commonMetrics,
subscription_payment_provider:
normalizeGleanFalsyValues(paymentProvider),
});
}
}

private populateCommonMetrics(metrics: {
Expand Down

0 comments on commit 8054aeb

Please sign in to comment.