Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: report app names only if below a threshold (#2737)
## About the changes Introduce a snapshot version of instanceStats inside instance-stats-service to provide a cached state of the statistics without compromising the DB. ### Important notes Some rule-of-thumb applied in the PR that can be changed: 1. The snapshot refresh time 2. The threshold to report appName with the metrics ## Discussion points 1. The snapshot could be limited to just the information needed (things like `hasOIDC` don't change until there's a restart), to optimize the memory usage 3. metrics.ts (used to expose Prometheus metrics) has a [refresh interval of 2hs](https://github.com/Unleash/unleash/blob/2d16730cc24ca9d62bd7eaff451853e58cb83482/src/lib/metrics.ts#L189-L195), but with this implementation, we could remove that background task and rely on the snapshot 4. We could additionally update the snapshot every time someone queries the DB to fetch stats (`getStats()` method), but it may increase complexity without a significant benefit Co-authored-by: Mateusz Kwasniewski <kwasniewski.mateusz@gmail.com> Co-authored-by: Simon Hornby <liquidwicked64@gmail.com>
- Loading branch information
1 parent
ea31154
commit ce815e5
Showing
7 changed files
with
126 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { createTestConfig } from '../../test/config/test-config'; | ||
import { InstanceStatsService } from './instance-stats-service'; | ||
import createStores from '../../test/fixtures/store'; | ||
import VersionService from './version-service'; | ||
|
||
let instanceStatsService: InstanceStatsService; | ||
let versionService: VersionService; | ||
|
||
beforeEach(() => { | ||
const config = createTestConfig(); | ||
const stores = createStores(); | ||
versionService = new VersionService(stores, config); | ||
instanceStatsService = new InstanceStatsService( | ||
stores, | ||
config, | ||
versionService, | ||
); | ||
|
||
jest.spyOn(instanceStatsService, 'refreshStatsSnapshot'); | ||
jest.spyOn(instanceStatsService, 'getStats'); | ||
|
||
// validate initial state without calls to these methods | ||
expect(instanceStatsService.refreshStatsSnapshot).toBeCalledTimes(0); | ||
expect(instanceStatsService.getStats).toBeCalledTimes(0); | ||
}); | ||
|
||
test('get snapshot should not call getStats', async () => { | ||
await instanceStatsService.refreshStatsSnapshot(); | ||
expect(instanceStatsService.getStats).toBeCalledTimes(1); | ||
|
||
// subsequent calls to getStatsSnapshot don't call getStats | ||
for (let i = 0; i < 3; i++) { | ||
const stats = instanceStatsService.getStatsSnapshot(); | ||
expect(stats.clientApps).toStrictEqual([ | ||
{ range: 'allTime', count: 0 }, | ||
{ range: '30d', count: 0 }, | ||
{ range: '7d', count: 0 }, | ||
]); | ||
} | ||
// after querying the stats snapshot no call to getStats should be issued | ||
expect(instanceStatsService.getStats).toBeCalledTimes(1); | ||
}); | ||
|
||
test('before the snapshot is refreshed we can still get the appCount', async () => { | ||
expect(instanceStatsService.refreshStatsSnapshot).toBeCalledTimes(0); | ||
expect(instanceStatsService.getAppCountSnapshot('7d')).toBeUndefined(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters