From 8a2b977ac095684772ad43a4d76844a248999ac6 Mon Sep 17 00:00:00 2001 From: Jaanus Sellin Date: Fri, 10 May 2024 11:50:47 +0300 Subject: [PATCH] fix: fix prometheus metrics for lifecycle (#7030) getAll was not properly tested, added test and fixed query. Now metrics should come up. --- .../feature-lifecycle-store.ts | 5 ++- src/lib/metrics.test.ts | 32 ++++++++++++++++--- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/lib/features/feature-lifecycle/feature-lifecycle-store.ts b/src/lib/features/feature-lifecycle/feature-lifecycle-store.ts index 4ec14b7e85..925e47af08 100644 --- a/src/lib/features/feature-lifecycle/feature-lifecycle-store.ts +++ b/src/lib/features/feature-lifecycle/feature-lifecycle-store.ts @@ -42,6 +42,9 @@ export class FeatureLifecycleStore implements IFeatureLifecycleStore { existingFeaturesSet.has(stage.feature), ); + if (validStages.length === 0) { + return; + } await this.db('feature_lifecycles') .insert( validStages.map((stage) => ({ @@ -70,7 +73,7 @@ export class FeatureLifecycleStore implements IFeatureLifecycleStore { async getAll(): Promise { const results = await this.db('feature_lifecycles as flc') .select('flc.feature', 'flc.stage', 'flc.created_at', 'f.project') - .leftJoin('features f', 'f.name', 'flc.feature') + .leftJoin('features as f', 'f.name', 'flc.feature') .orderBy('created_at', 'asc'); return results.map( diff --git a/src/lib/metrics.test.ts b/src/lib/metrics.test.ts index 74e73e0299..351caeada8 100644 --- a/src/lib/metrics.test.ts +++ b/src/lib/metrics.test.ts @@ -20,7 +20,10 @@ import type { IEnvironmentStore, IUnleashStores } from './types'; import FakeEnvironmentStore from './features/project-environments/fake-environment-store'; import { SchedulerService } from './services'; import noLogger from '../test/fixtures/no-logger'; -import { createFakeFeatureLifecycleService } from './features'; +import { createFeatureLifecycleService } from './features'; +import dbInit, { type ITestDb } from '../test/e2e/helpers/database-init'; +import getLogger from '../test/fixtures/no-logger'; +import type { FeatureLifecycleStore } from './features/feature-lifecycle/feature-lifecycle-store'; const monitor = createMetricsMonitor(); const eventBus = new EventEmitter(); @@ -30,6 +33,8 @@ let environmentStore: IEnvironmentStore; let statsService: InstanceStatsService; let stores: IUnleashStores; let schedulerService: SchedulerService; +let featureLifeCycleStore: FeatureLifecycleStore; +let db: ITestDb; beforeAll(async () => { const config = createTestConfig({ @@ -52,8 +57,11 @@ beforeAll(async () => { createFakeGetActiveUsers(), createFakeGetProductionChanges(), ); - const { featureLifecycleService } = - createFakeFeatureLifecycleService(config); + db = await dbInit('metrics_test', getLogger); + + const { featureLifecycleService, featureLifecycleStore } = + createFeatureLifecycleService(db.rawDatabase, config); + statsService = new InstanceStatsService( stores, config, @@ -62,6 +70,7 @@ beforeAll(async () => { createFakeGetProductionChanges(), featureLifecycleService, ); + featureLifeCycleStore = featureLifecycleStore; schedulerService = new SchedulerService( noLogger, @@ -71,7 +80,7 @@ beforeAll(async () => { eventBus, ); - const db = { + const metricsDbConf = { client: { pool: { min: 0, @@ -92,7 +101,7 @@ beforeAll(async () => { statsService, schedulerService, // @ts-ignore - We don't want a full knex implementation for our tests, it's enough that it actually yields the numbers we want. - db, + metricsDbConf, ); }); @@ -287,6 +296,19 @@ test('should collect metrics for project disabled numbers', async () => { }); test('should collect metrics for lifecycle', async () => { + await db.stores.featureToggleStore.create('default', { + name: 'my_feature_b', + createdByUserId: 9999, + }); + await featureLifeCycleStore.insert([ + { + feature: 'my_feature_b', + stage: 'initial', + }, + ]); + const { featureLifeCycles } = await statsService.getStats(); + expect(featureLifeCycles).toHaveLength(1); + const metrics = await prometheusRegister.metrics(); expect(metrics).toMatch(/feature_lifecycle_stage_duration/); });