Skip to content

Commit 68e6929

Browse files
authored
Merge pull request #19537 from mozilla/pay-3202-cms-statsd-improve
feat(strapi): move statsd logging to strapi client
2 parents 0385afa + c8f255b commit 68e6929

6 files changed

Lines changed: 20 additions & 48 deletions

libs/shared/cms/src/lib/product-configuration.manager.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ describe('productConfigurationManager', () => {
9494
beforeEach(async () => {
9595
const module = await Test.createTestingModule({
9696
providers: [
97-
{ provide: StatsDService, useValue: mockStatsd },
9897
MockStrapiClientConfigProvider,
9998
MockFirestoreProvider,
10099
MockStatsDProvider,

libs/shared/cms/src/lib/product-configuration.manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import {
5353
ServicesWithCapabilitiesResultUtil,
5454
servicesWithCapabilitiesQuery,
5555
} from './queries/services-with-capabilities';
56-
import { StrapiClient, StrapiClientEventResponse } from './strapi.client';
56+
import { StrapiClient, type StrapiClientEventResponse } from './strapi.client';
5757
import { DeepNonNullable } from './types';
5858
import {
5959
iapOfferingsByStoreIDsQuery,

libs/shared/cms/src/lib/strapi.client.spec.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ jest.useFakeTimers();
6161

6262
describe('StrapiClient', () => {
6363
let strapiClient: StrapiClient;
64-
const onCallback = jest.fn();
6564

6665
beforeEach(async () => {
6766
const module = await Test.createTestingModule({
@@ -73,11 +72,6 @@ describe('StrapiClient', () => {
7372
}).compile();
7473

7574
strapiClient = module.get(StrapiClient);
76-
strapiClient.on('response', onCallback);
77-
});
78-
79-
afterEach(() => {
80-
onCallback.mockClear();
8175
});
8276

8377
describe('query', () => {
@@ -122,14 +116,6 @@ describe('StrapiClient', () => {
122116
await expect(() =>
123117
strapiClient.query(offeringQuery, queryArgs)
124118
).rejects.toThrow(new StrapiQueryError(offeringQuery, queryArgs, error));
125-
126-
expect(onCallback).toHaveBeenCalledWith(
127-
expect.objectContaining({
128-
method: 'query',
129-
cache: false,
130-
error: expect.anything(),
131-
})
132-
);
133119
});
134120
});
135121

libs/shared/cms/src/lib/strapi.client.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export interface StrapiClientEventResponse {
4141
requestEndTime: number;
4242
elapsed: number;
4343
cache: boolean;
44-
cacheType: 'method' | 'memory' | 'stale' | 'fallback';
44+
cacheType: 'method' | 'memory' | 'stale' | 'fallback' | 'fallbackFailed';
4545
query?: TypedDocumentNode;
4646
variables?: string;
4747
error?: Error;
@@ -141,6 +141,7 @@ export class StrapiClient {
141141
requestStartTime: startTime,
142142
requestEndTime: endTime,
143143
elapsed: endTime - startTime,
144+
error: result === 'fallback' || result === 'fallbackFailed',
144145
cache: result === 'stale' || result === 'fallback',
145146
cacheType: result,
146147
});
@@ -155,26 +156,12 @@ export class StrapiClient {
155156
query: TypedDocumentNode<Result, Variables>,
156157
variables: Variables
157158
): Promise<Result> {
158-
const requestStartTime = Date.now();
159-
160159
try {
161160
return await this.client.request<Result, any>({
162161
document: query,
163162
variables,
164163
});
165164
} catch (e) {
166-
const requestEndTime = Date.now();
167-
this.emitter.emit('response', {
168-
method: 'query',
169-
query,
170-
variables: JSON.stringify(variables),
171-
requestStartTime,
172-
requestEndTime,
173-
elapsed: requestEndTime - requestStartTime,
174-
error: e,
175-
cache: false,
176-
});
177-
178165
throw new StrapiQueryError(query, variables, e);
179166
}
180167
}

libs/shared/db/type-cacheable/src/lib/type-cachable-stale-while-revalidate-with-fallback.spec.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ describe('StaleWhileRevalidateWithFallbackStrategy', () => {
5151
};
5252
await cacheClient.set(cacheKey, cacheValue);
5353

54-
const result = await staleWhileRevalidateWithFallbackStrategy.handle(
55-
context
56-
);
54+
const result =
55+
await staleWhileRevalidateWithFallbackStrategy.handle(context);
5756

5857
expect(result).toEqual(cacheValue.value);
5958

@@ -80,9 +79,8 @@ describe('StaleWhileRevalidateWithFallbackStrategy', () => {
8079
isCacheable: () => true,
8180
} satisfies CacheStrategyContext;
8281

83-
const result = await staleWhileRevalidateWithFallbackStrategy.handle(
84-
context
85-
);
82+
const result =
83+
await staleWhileRevalidateWithFallbackStrategy.handle(context);
8684

8785
expect(result).toEqual(methodValue);
8886

@@ -117,9 +115,8 @@ describe('StaleWhileRevalidateWithFallbackStrategy', () => {
117115
};
118116
await cacheClient.set(cacheKey, cacheValue);
119117

120-
const result = await staleWhileRevalidateWithFallbackStrategy.handle(
121-
context
122-
);
118+
const result =
119+
await staleWhileRevalidateWithFallbackStrategy.handle(context);
123120

124121
expect(result).toEqual(cacheValue.value);
125122

@@ -179,9 +176,8 @@ describe('StaleWhileRevalidateWithFallbackStrategy', () => {
179176
.spyOn(cacheClient, 'get')
180177
.mockRejectedValue(new Error('cache get error'));
181178

182-
const result = await staleWhileRevalidateWithFallbackStrategy.handle(
183-
context
184-
);
179+
const result =
180+
await staleWhileRevalidateWithFallbackStrategy.handle(context);
185181

186182
expect(result).toEqual(methodValue);
187183

@@ -216,9 +212,8 @@ describe('StaleWhileRevalidateWithFallbackStrategy', () => {
216212
};
217213
await cacheClient.set(cacheKey, cacheValue);
218214

219-
const result = await staleWhileRevalidateWithFallbackStrategy.handle(
220-
context
221-
);
215+
const result =
216+
await staleWhileRevalidateWithFallbackStrategy.handle(context);
222217

223218
expect(result).toEqual(cacheValue.value);
224219

@@ -252,7 +247,11 @@ describe('StaleWhileRevalidateWithFallbackStrategy', () => {
252247
).rejects.toThrowError('method error');
253248

254249
expect(targetMethod).toHaveBeenCalled();
255-
expect(onRequestFinished).not.toHaveBeenCalled();
250+
expect(onRequestFinished).toHaveBeenCalledWith(
251+
expect.any(Number),
252+
expect.any(Number),
253+
'fallbackFailed'
254+
);
256255
expect(onAsyncCacheWriteFailure).not.toHaveBeenCalled();
257256
});
258257
});

libs/shared/db/type-cacheable/src/lib/type-cachable-stale-while-revalidate-with-fallback.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface CacheEntry {
1919
* - method: There was no fresh enough value in cache, so the method was called.
2020
* - fallback: The method failed, so a very old (older than stale) value was returned.
2121
*/
22-
type CacheResult = 'stale' | 'method' | 'fallback';
22+
type CacheResult = 'stale' | 'method' | 'fallback' | 'fallbackFailed';
2323

2424
export class StaleWhileRevalidateWithFallbackStrategy implements CacheStrategy {
2525
private pendingCacheRequestMap = new Map<
@@ -149,6 +149,7 @@ export class StaleWhileRevalidateWithFallbackStrategy implements CacheStrategy {
149149
this.onRequestFinshed?.(startTime, Date.now(), 'fallback');
150150
return oldResult.value;
151151
} else {
152+
this.onRequestFinshed?.(startTime, Date.now(), 'fallbackFailed');
152153
throw err;
153154
}
154155
}

0 commit comments

Comments
 (0)