Skip to content

Commit

Permalink
Make sure getOpsMetrics$ only replays lastest value
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdover committed Sep 16, 2020
1 parent 9b6edc5 commit 467d14f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/core/server/metrics/metrics_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,25 @@ describe('MetricsService', () => {
`"#setup() needs to be run first"`
);
});

it('emits the last value on each getOpsMetrics$ call', async () => {
const firstMetrics = { metric: 'first' };
const secondMetrics = { metric: 'second' };
mockOpsCollector.collect
.mockResolvedValueOnce(firstMetrics)
.mockResolvedValueOnce(secondMetrics);

await metricsService.setup({ http: httpMock });
const { getOpsMetrics$ } = await metricsService.start();

const firstEmission = getOpsMetrics$().pipe(take(1)).toPromise();
jest.advanceTimersByTime(testInterval);
expect(await firstEmission).toEqual({ metric: 'first' });

const secondEmission = getOpsMetrics$().pipe(take(1)).toPromise();
jest.advanceTimersByTime(testInterval);
expect(await secondEmission).toEqual({ metric: 'second' });
});
});

describe('#stop', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/metrics/metrics_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class MetricsService
private readonly logger: Logger;
private metricsCollector?: OpsMetricsCollector;
private collectInterval?: NodeJS.Timeout;
private metrics$ = new ReplaySubject<OpsMetrics>();
private metrics$ = new ReplaySubject<OpsMetrics>(1);
private service?: InternalMetricsServiceSetup;

constructor(private readonly coreContext: CoreContext) {
Expand Down

0 comments on commit 467d14f

Please sign in to comment.