Skip to content

Commit

Permalink
Default to 0 when search result does not provide a took value (#126701)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
ymao1 and kibanamachine authored Mar 2, 2022
1 parent fc6897c commit d6f211b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ describe('wrapScopedClusterClient', () => {
jest.useRealTimers();
});

afterEach(() => {
jest.resetAllMocks();
});

test('searches with asInternalUser when specified', async () => {
const scopedClusterClient = elasticsearchServiceMock.createScopedClusterClient();
const childClient = elasticsearchServiceMock.createElasticsearchClient();
Expand Down Expand Up @@ -119,6 +123,35 @@ describe('wrapScopedClusterClient', () => {
).rejects.toThrowErrorMatchingInlineSnapshot(`"something went wrong!"`);
});

test('handles empty search result object', async () => {
const scopedClusterClient = elasticsearchServiceMock.createScopedClusterClient();
const childClient = elasticsearchServiceMock.createElasticsearchClient();

(
scopedClusterClient.asInternalUser as unknown as jest.Mocked<ElasticsearchClientWithChild>
).child.mockReturnValue(childClient as unknown as Client);
const asInternalUserWrappedSearchFn = childClient.search;
// @ts-ignore incomplete return type
asInternalUserWrappedSearchFn.mockResolvedValue({});

const wrappedSearchClientFactory = createWrappedScopedClusterClientFactory({
scopedClusterClient,
rule,
logger,
});

const wrappedSearchClient = wrappedSearchClientFactory.client();
await wrappedSearchClient.asInternalUser.search(esQuery);

expect(asInternalUserWrappedSearchFn).toHaveBeenCalledTimes(1);
expect(scopedClusterClient.asInternalUser.search).not.toHaveBeenCalled();
expect(scopedClusterClient.asCurrentUser.search).not.toHaveBeenCalled();

const stats = wrappedSearchClientFactory.getMetrics();
expect(stats.numSearches).toEqual(1);
expect(stats.esSearchDurationMs).toEqual(0);
});

test('keeps track of number of queries', async () => {
const scopedClusterClient = elasticsearchServiceMock.createScopedClusterClient();
const childClient = elasticsearchServiceMock.createElasticsearchClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function getWrappedSearchFn(opts: WrapEsClientOpts) {
took = (result as SearchResponse<TDocument, TAggregations>).took;
}

opts.logMetricsFn({ esSearchDuration: took, totalSearchDuration: durationMs });
opts.logMetricsFn({ esSearchDuration: took ?? 0, totalSearchDuration: durationMs });
return result;
} catch (e) {
throw e;
Expand Down

0 comments on commit d6f211b

Please sign in to comment.