1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

fix: fix prometheus metrics for lifecycle (#7030)

getAll was not properly tested, added test and fixed query. Now metrics
should come up.
This commit is contained in:
Jaanus Sellin 2024-05-10 11:50:47 +03:00 committed by GitHub
parent 9fbb041bfd
commit 8a2b977ac0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 6 deletions

View File

@ -42,6 +42,9 @@ export class FeatureLifecycleStore implements IFeatureLifecycleStore {
existingFeaturesSet.has(stage.feature), existingFeaturesSet.has(stage.feature),
); );
if (validStages.length === 0) {
return;
}
await this.db('feature_lifecycles') await this.db('feature_lifecycles')
.insert( .insert(
validStages.map((stage) => ({ validStages.map((stage) => ({
@ -70,7 +73,7 @@ export class FeatureLifecycleStore implements IFeatureLifecycleStore {
async getAll(): Promise<FeatureLifecycleProjectItem[]> { async getAll(): Promise<FeatureLifecycleProjectItem[]> {
const results = await this.db('feature_lifecycles as flc') const results = await this.db('feature_lifecycles as flc')
.select('flc.feature', 'flc.stage', 'flc.created_at', 'f.project') .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'); .orderBy('created_at', 'asc');
return results.map( return results.map(

View File

@ -20,7 +20,10 @@ import type { IEnvironmentStore, IUnleashStores } from './types';
import FakeEnvironmentStore from './features/project-environments/fake-environment-store'; import FakeEnvironmentStore from './features/project-environments/fake-environment-store';
import { SchedulerService } from './services'; import { SchedulerService } from './services';
import noLogger from '../test/fixtures/no-logger'; 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 monitor = createMetricsMonitor();
const eventBus = new EventEmitter(); const eventBus = new EventEmitter();
@ -30,6 +33,8 @@ let environmentStore: IEnvironmentStore;
let statsService: InstanceStatsService; let statsService: InstanceStatsService;
let stores: IUnleashStores; let stores: IUnleashStores;
let schedulerService: SchedulerService; let schedulerService: SchedulerService;
let featureLifeCycleStore: FeatureLifecycleStore;
let db: ITestDb;
beforeAll(async () => { beforeAll(async () => {
const config = createTestConfig({ const config = createTestConfig({
@ -52,8 +57,11 @@ beforeAll(async () => {
createFakeGetActiveUsers(), createFakeGetActiveUsers(),
createFakeGetProductionChanges(), createFakeGetProductionChanges(),
); );
const { featureLifecycleService } = db = await dbInit('metrics_test', getLogger);
createFakeFeatureLifecycleService(config);
const { featureLifecycleService, featureLifecycleStore } =
createFeatureLifecycleService(db.rawDatabase, config);
statsService = new InstanceStatsService( statsService = new InstanceStatsService(
stores, stores,
config, config,
@ -62,6 +70,7 @@ beforeAll(async () => {
createFakeGetProductionChanges(), createFakeGetProductionChanges(),
featureLifecycleService, featureLifecycleService,
); );
featureLifeCycleStore = featureLifecycleStore;
schedulerService = new SchedulerService( schedulerService = new SchedulerService(
noLogger, noLogger,
@ -71,7 +80,7 @@ beforeAll(async () => {
eventBus, eventBus,
); );
const db = { const metricsDbConf = {
client: { client: {
pool: { pool: {
min: 0, min: 0,
@ -92,7 +101,7 @@ beforeAll(async () => {
statsService, statsService,
schedulerService, 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. // @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 () => { 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(); const metrics = await prometheusRegister.metrics();
expect(metrics).toMatch(/feature_lifecycle_stage_duration/); expect(metrics).toMatch(/feature_lifecycle_stage_duration/);
}); });