Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: filter empty metrics before we collect last seen toggles. #2172

Merged
merged 6 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/lib/services/client-metrics/metrics-service-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,27 @@ export default class ClientMetricsServiceV2 {
clientIp: string,
): Promise<void> {
const value = await clientMetricsSchema.validateAsync(data);
const toggleNames = Object.keys(value.bucket.toggles);
const toggleNames = Object.keys(value.bucket.toggles).filter(
(name) =>
!(
value.bucket.toggles[name].yes === 0 &&
value.bucket.toggles[name].no === 0
),
);
if (toggleNames.length > 0) {
await this.featureToggleStore.setLastSeen(toggleNames);
ivarconr marked this conversation as resolved.
Show resolved Hide resolved
}

this.logger.debug(`got metrics from ${clientIp}`);

const clientMetrics: IClientMetricsEnv[] = toggleNames
.map((name) => ({
featureName: name,
appName: value.appName,
environment: value.environment,
timestamp: value.bucket.start, //we might need to approximate between start/stop...
yes: value.bucket.toggles[name].yes,
no: value.bucket.toggles[name].no,
}))
.filter((item) => !(item.yes === 0 && item.no === 0));
const clientMetrics: IClientMetricsEnv[] = toggleNames.map((name) => ({
featureName: name,
appName: value.appName,
environment: value.environment,
timestamp: value.bucket.start, //we might need to approximate between start/stop...
yes: value.bucket.toggles[name].yes,
no: value.bucket.toggles[name].no,
}));

if (this.config.flagResolver.isEnabled('batchMetrics')) {
this.unsavedMetrics = collapseHourlyMetrics([
Expand Down
49 changes: 49 additions & 0 deletions src/test/e2e/api/client/metricsV2.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,52 @@ test('should pick up environment from token', async () => {
expect(metrics[0].environment).toBe('test');
expect(metrics[0].appName).toBe('some-fancy-app');
});

test('should set lastSeen for toggles with metrics', async () => {
const start = Date.now();
await app.services.featureToggleServiceV2.createFeatureToggle(
'default',
{ name: 't1' },
'tester',
);
await app.services.featureToggleServiceV2.createFeatureToggle(
'default',
{ name: 't2' },
'tester',
);
const token = await app.services.apiTokenService.createApiToken({
type: ApiTokenType.CLIENT,
project: 'default',
environment: 'default',
username: 'tester',
});

await app.request
.post('/api/client/metrics')
.set('Authorization', token.secret)
.send({
appName: 'some-fancy-app',
instanceId: '1',
bucket: {
start: Date.now(),
stop: Date.now(),
toggles: {
t1: {
yes: 100,
no: 50,
},
t2: {
yes: 0,
no: 0,
},
},
},
})
.expect(202);

await app.services.clientMetricsServiceV2.bulkAdd();
const t1 = await db.stores.featureToggleStore.get('t1');
const t2 = await db.stores.featureToggleStore.get('t2');
expect(t1.lastSeenAt.getTime()).toBeGreaterThanOrEqual(start);
expect(t2.lastSeenAt).toBeDefined();
});