1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

#4205: activate event emission (#4240)

This PR activates the event emission that was prepared for in
https://github.com/Unleash/unleash/pull/4239.

It emits events (behind a flag) when something is marked as potentially
stale or the opposite.
It takes the features returned from the store and creates events out of
them.

The events only contain data, no preData. This is because the preData
can easily be inferred and because it gives a nicer event in the event
log.

Here is an image of the difference. The top event uses only data, so it
shows the name of the feature and the new potentiallyStale status. The
bottom event uses both preData and data, so it only shows the new
potentiallyStale status and not the feature name (unless you show the
raw event):


![image](https://github.com/Unleash/unleash/assets/17786332/5ec0fbef-f4cf-4dc6-9af6-9203fca30e5d)


Should not be merged before #4239. Merge that and then rebase this off
main or cherry the commit.

## Discussion 


### `preData`

Should we also use preData or is it enough to use only data? It seems
unnecessary in this event, but I'm open to hearing your thoughts.

### event author: `createdBy`

I've set `unleash-system` as the `createdBy` property on these events
because they are generated by the system. I found the same string used
some other places. However, it may be that there we want to use a
different author.
This commit is contained in:
Thomas Heartman 2023-07-17 10:10:15 +02:00 committed by GitHub
parent 333c0c0db1
commit 11f77a21de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 1 deletions

View File

@ -0,0 +1,52 @@
import { IUnleashConfig, IUnleashStores } from '../types';
import { createTestConfig } from '../../test/config/test-config';
import FeatureToggleService from './feature-toggle-service';
import { AccessService } from './access-service';
import { IChangeRequestAccessReadModel } from 'lib/features/change-request-access-service/change-request-access-read-model';
import { ISegmentService } from 'lib/segments/segment-service-interface';
test('Should store events', async () => {
expect.assertions(4);
const featureUpdates = [
{ name: 'feature1', potentiallyStale: true },
{ name: 'feature2', potentiallyStale: false },
];
const config = createTestConfig();
const featureToggleService = new FeatureToggleService(
{
featureToggleStore: {
updatePotentiallyStaleFeatures: () => featureUpdates,
},
eventStore: {
batchStore: ([event1, event2]) => {
// expect 'feature1'
expect(event1.data).toMatchObject({
name: 'feature1',
potentiallyStale: true,
});
expect(event1.preData).toBeUndefined();
// expect 'feature1'
expect(event2.data).toMatchObject({
name: 'feature2',
potentiallyStale: false,
});
expect(event2.preData).toBeUndefined();
},
},
} as unknown as IUnleashStores,
{
...config,
flagResolver: { isEnabled: () => true },
experimental: {
...(config.experimental ?? {}),
emitPotentiallyStaleEvents: true,
},
} as unknown as IUnleashConfig,
{} as ISegmentService,
{} as AccessService,
{} as IChangeRequestAccessReadModel,
);
await featureToggleService.updatePotentiallyStaleFeatures();
});

View File

@ -39,6 +39,7 @@ import {
SKIP_CHANGE_REQUEST, SKIP_CHANGE_REQUEST,
Unsaved, Unsaved,
WeightType, WeightType,
FEATURE_POTENTIALLY_STALE_UPDATED,
} from '../types'; } from '../types';
import { Logger } from '../logger'; import { Logger } from '../logger';
import BadDataError from '../error/bad-data-error'; import BadDataError from '../error/bad-data-error';
@ -1984,7 +1985,24 @@ class FeatureToggleService {
} }
async updatePotentiallyStaleFeatures(): Promise<void> { async updatePotentiallyStaleFeatures(): Promise<void> {
const potentiallyStaleFeatures =
await this.featureToggleStore.updatePotentiallyStaleFeatures(); await this.featureToggleStore.updatePotentiallyStaleFeatures();
if (this.flagResolver.isEnabled('emitPotentiallyStaleEvents')) {
if (potentiallyStaleFeatures.length > 0) {
return this.eventStore.batchStore(
potentiallyStaleFeatures.map(
({ name, potentiallyStale }) => ({
type: FEATURE_POTENTIALLY_STALE_UPDATED,
createdBy: 'unleash-system',
data: {
name,
potentiallyStale,
},
}),
),
);
}
}
} }
} }