From bb4e6290d5565c73bf8a67cfe7f19fb4b299e958 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20Conradi=20=C3=98sthus?= Date: Thu, 9 Dec 2021 21:39:00 +0100 Subject: [PATCH] fix: move toggle-counters to metrics service --- .../client-metrics/client-instance-service.ts | 23 +++++------------ .../client-metrics-service-v2.ts | 25 ++++++++++++++++--- .../client-metrics/client-metrics.test.ts | 9 +++---- .../client-metrics-service.e2e.test.ts | 4 +-- 4 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/lib/services/client-metrics/client-instance-service.ts b/src/lib/services/client-metrics/client-instance-service.ts index b2e9b94352..e7ac507728 100644 --- a/src/lib/services/client-metrics/client-instance-service.ts +++ b/src/lib/services/client-metrics/client-instance-service.ts @@ -1,6 +1,5 @@ import { applicationSchema } from './metrics-schema'; -import { clientMetricsSchema } from './client-metrics-schema'; -import { APPLICATION_CREATED, CLIENT_METRICS } from '../../types/events'; +import { APPLICATION_CREATED } from '../../types/events'; import { IApplication } from './models'; import { IUnleashStores } from '../../types/stores'; import { IUnleashConfig } from '../../types/option'; @@ -17,8 +16,8 @@ import { IClientApp } from '../../types/model'; import { clientRegisterSchema } from './register-schema'; import { minutesToMilliseconds, secondsToMilliseconds } from 'date-fns'; -import EventEmitter from 'events'; import { IClientMetricsStoreV2 } from '../../types/stores/client-metrics-store-v2'; +import { clientMetricsSchema } from './client-metrics-schema'; export default class ClientInstanceService { apps = {}; @@ -45,8 +44,6 @@ export default class ClientInstanceService { private announcementInterval: number; - private eventBus: EventEmitter; - constructor( { clientMetricsStoreV2, @@ -64,7 +61,7 @@ export default class ClientInstanceService { | 'clientInstanceStore' | 'eventStore' >, - { getLogger, eventBus }: Pick, + { getLogger }: Pick, bulkInterval = secondsToMilliseconds(5), announcementInterval = minutesToMilliseconds(5), ) { @@ -74,9 +71,10 @@ export default class ClientInstanceService { this.clientApplicationsStore = clientApplicationsStore; this.clientInstanceStore = clientInstanceStore; this.eventStore = eventStore; - this.eventBus = eventBus; - this.logger = getLogger('/services/client-metrics/index.ts'); + this.logger = getLogger( + '/services/client-metrics/client-instance-service.ts', + ); this.bulkInterval = bulkInterval; this.announcementInterval = announcementInterval; @@ -96,20 +94,11 @@ export default class ClientInstanceService { clientIp: string, ): Promise { const value = await clientMetricsSchema.validateAsync(data); - await this.clientInstanceStore.insert({ appName: value.appName, instanceId: value.instanceId, clientIp, }); - - // TODO: move to new service - const toggleNames = Object.keys(value.bucket.toggles); - if (toggleNames.length > 0) { - await this.featureToggleStore.setLastSeen(toggleNames); - } - - this.eventBus.emit(CLIENT_METRICS, value); } public async registerClient( diff --git a/src/lib/services/client-metrics/client-metrics-service-v2.ts b/src/lib/services/client-metrics/client-metrics-service-v2.ts index 6f49011b99..27b84f43db 100644 --- a/src/lib/services/client-metrics/client-metrics-service-v2.ts +++ b/src/lib/services/client-metrics/client-metrics-service-v2.ts @@ -9,24 +9,37 @@ import { } from '../../types/stores/client-metrics-store-v2'; import { clientMetricsSchema } from './client-metrics-schema'; import { hoursToMilliseconds, minutesToMilliseconds } from 'date-fns'; +import { IFeatureToggleStore } from '../../types/stores/feature-toggle-store'; +import EventEmitter from 'events'; +import { CLIENT_METRICS } from '../../types/events'; export default class ClientMetricsServiceV2 { private timer: NodeJS.Timeout; private clientMetricsStoreV2: IClientMetricsStoreV2; + private featureToggleStore: IFeatureToggleStore; + + private eventBus: EventEmitter; + private logger: Logger; private bulkInterval: number; constructor( - { clientMetricsStoreV2 }: Pick, - { getLogger }: Pick, + { + featureToggleStore, + clientMetricsStoreV2, + }: Pick, + { eventBus, getLogger }: Pick, bulkInterval = minutesToMilliseconds(5), ) { + this.featureToggleStore = featureToggleStore; this.clientMetricsStoreV2 = clientMetricsStoreV2; - - this.logger = getLogger('/services/client-metrics/index.ts'); + this.eventBus = eventBus; + this.logger = getLogger( + '/services/client-metrics/client-metrics-service-v2.ts', + ); this.bulkInterval = bulkInterval; this.timer = setInterval(async () => { @@ -41,6 +54,9 @@ export default class ClientMetricsServiceV2 { ): Promise { const value = await clientMetricsSchema.validateAsync(data); const toggleNames = Object.keys(value.bucket.toggles); + if (toggleNames.length > 0) { + await this.featureToggleStore.setLastSeen(toggleNames); + } this.logger.debug(`got metrics from ${clientIp}`); @@ -57,6 +73,7 @@ export default class ClientMetricsServiceV2 { // TODO: should we aggregate for a few minutes (bulkInterval) before pushing to DB? await this.clientMetricsStoreV2.batchInsertMetrics(clientMetrics); + this.eventBus.emit(CLIENT_METRICS, value); } // Overview over usage last "hour" bucket and all applications using the toggle diff --git a/src/lib/services/client-metrics/client-metrics.test.ts b/src/lib/services/client-metrics/client-metrics.test.ts index c37db8b2c6..e75c3d8ffb 100644 --- a/src/lib/services/client-metrics/client-metrics.test.ts +++ b/src/lib/services/client-metrics/client-metrics.test.ts @@ -1,4 +1,3 @@ -import EventEmitter from 'events'; import ClientInstanceService from './client-instance-service'; import getLogger from '../../../test/fixtures/no-logger'; import { IClientApp } from '../../types/model'; @@ -57,7 +56,7 @@ test('Multiple registrations of same appname and instanceid within same time per clientInstanceStore, eventStore: null, }, - { getLogger, eventBus: new EventEmitter() }, + { getLogger }, ); const client1: IClientApp = { appName: 'test_app', @@ -107,7 +106,7 @@ test('Multiple unique clients causes multiple registrations', async () => { clientInstanceStore, eventStore: null, }, - { getLogger, eventBus: new EventEmitter() }, + { getLogger }, ); const client1 = { appName: 'test_app', @@ -160,7 +159,7 @@ test('Same client registered outside of dedup interval will be registered twice' clientInstanceStore, eventStore: null, }, - { getLogger, eventBus: new EventEmitter() }, + { getLogger }, bulkInterval, ); const client1 = { @@ -213,7 +212,7 @@ test('No registrations during a time period will not call stores', async () => { clientInstanceStore, eventStore: null, }, - { getLogger, eventBus: new EventEmitter() }, + { getLogger }, ); jest.advanceTimersByTime(6000); expect(appStoreSpy).toHaveBeenCalledTimes(0); diff --git a/src/test/e2e/services/client-metrics-service.e2e.test.ts b/src/test/e2e/services/client-metrics-service.e2e.test.ts index 9e0ebafdc3..d441f6c0d6 100644 --- a/src/test/e2e/services/client-metrics-service.e2e.test.ts +++ b/src/test/e2e/services/client-metrics-service.e2e.test.ts @@ -1,7 +1,6 @@ import ClientInstanceService from '../../../lib/services/client-metrics/client-instance-service'; import { IClientApp } from '../../../lib/types/model'; import { secondsToMilliseconds } from 'date-fns'; -import EventEmitter from 'events'; const faker = require('faker'); const dbInit = require('../helpers/database-init'); @@ -15,14 +14,13 @@ let clientInstanceService; beforeAll(async () => { db = await dbInit('client_metrics_service_serial', getLogger); stores = db.stores; - const eventBus = new EventEmitter(); const bulkInterval = secondsToMilliseconds(0.5); const announcementInterval = secondsToMilliseconds(2); clientInstanceService = new ClientInstanceService( stores, - { getLogger, eventBus }, + { getLogger }, bulkInterval, announcementInterval, );