[Search Profiler] Migrate server to new es-js client (#88725)

This commit is contained in:
Alison Goryachev 2021-01-25 11:27:59 -05:00 committed by GitHub
parent 4bc5f01428
commit 43db7e365f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 80 additions and 10 deletions

View file

@ -27,10 +27,6 @@ export const register = ({ router, getLicenseStatus, log }: RouteDependencies) =
});
}
const {
core: { elasticsearch },
} = ctx;
const {
body: { query, index },
} = request;
@ -46,21 +42,25 @@ export const register = ({ router, getLicenseStatus, log }: RouteDependencies) =
body: JSON.stringify(parsed, null, 2),
};
try {
const resp = await elasticsearch.legacy.client.callAsCurrentUser('search', body);
const client = ctx.core.elasticsearch.client.asCurrentUser;
const resp = await client.search(body);
return response.ok({
body: {
ok: true,
resp,
resp: resp.body,
},
});
} catch (err) {
log.error(err);
const { statusCode, body: errorBody } = err;
return response.customError({
statusCode: err.status || 500,
body: err.body
statusCode: statusCode || 500,
body: errorBody
? {
message: err.message,
attributes: err.body,
message: errorBody.error?.reason,
attributes: errorBody,
}
: err,
});

View file

@ -33,5 +33,6 @@ export default function ({ loadTestFile }: FtrProviderContext) {
loadTestFile(require.resolve('./transform'));
loadTestFile(require.resolve('./lists'));
loadTestFile(require.resolve('./upgrade_assistant'));
loadTestFile(require.resolve('./searchprofiler'));
});
}

View file

@ -0,0 +1,13 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { FtrProviderContext } from '../../ftr_provider_context';
export default function ({ loadTestFile }: FtrProviderContext) {
describe('Search Profiler', () => {
loadTestFile(require.resolve('./searchprofiler'));
});
}

View file

@ -0,0 +1,56 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import expect from '@kbn/expect';
import { FtrProviderContext } from '../../ftr_provider_context';
const API_BASE_PATH = '/api/searchprofiler';
export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
describe('Profile', () => {
it('should return profile results for a valid index', async () => {
const payload = {
index: '_all',
query: {
query: {
match_all: {},
},
},
};
const { body } = await supertest
.post(`${API_BASE_PATH}/profile`)
.set('kbn-xsrf', 'xxx')
.set('Content-Type', 'application/json;charset=UTF-8')
.send(payload)
.expect(200);
expect(body.ok).to.eql(true);
});
it('should return error for invalid index', async () => {
const payloadWithInvalidIndex = {
index: 'index_does_not_exist',
query: {
query: {
match_all: {},
},
},
};
const { body } = await supertest
.post(`${API_BASE_PATH}/execute`)
.set('kbn-xsrf', 'xxx')
.set('Content-Type', 'application/json;charset=UTF-8')
.send(payloadWithInvalidIndex)
.expect(404);
expect(body.error).to.eql('Not Found');
});
});
}