mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
[HTTP] Apply the same behaviour to all 500 errors (except from custom
responses) (#85541)
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
parent
4304cb9e62
commit
5342877a32
162 changed files with 1030 additions and 1438 deletions
|
@ -19,7 +19,6 @@ kibanaResponseFactory: {
|
|||
forbidden: (options?: ErrorHttpResponseOptions) => KibanaResponse<ResponseError>;
|
||||
notFound: (options?: ErrorHttpResponseOptions) => KibanaResponse<ResponseError>;
|
||||
conflict: (options?: ErrorHttpResponseOptions) => KibanaResponse<ResponseError>;
|
||||
internalError: (options?: ErrorHttpResponseOptions) => KibanaResponse<ResponseError>;
|
||||
customError: (options: CustomHttpResponseOptions<ResponseError>) => KibanaResponse<ResponseError>;
|
||||
redirected: (options: RedirectResponseOptions) => KibanaResponse<string | Record<string, any> | Buffer | Stream>;
|
||||
ok: (options?: HttpResponseOptions) => KibanaResponse<string | Record<string, any> | Buffer | Stream>;
|
||||
|
|
|
@ -283,7 +283,7 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
|
|||
| [PluginOpaqueId](./kibana-plugin-core-server.pluginopaqueid.md) | |
|
||||
| [PublicUiSettingsParams](./kibana-plugin-core-server.publicuisettingsparams.md) | A sub-set of [UiSettingsParams](./kibana-plugin-core-server.uisettingsparams.md) exposed to the client-side. |
|
||||
| [RedirectResponseOptions](./kibana-plugin-core-server.redirectresponseoptions.md) | HTTP response parameters for redirection response |
|
||||
| [RequestHandler](./kibana-plugin-core-server.requesthandler.md) | A function executed when route path matched requested resource path. Request handler is expected to return a result of one of [KibanaResponseFactory](./kibana-plugin-core-server.kibanaresponsefactory.md) functions. |
|
||||
| [RequestHandler](./kibana-plugin-core-server.requesthandler.md) | A function executed when route path matched requested resource path. Request handler is expected to return a result of one of [KibanaResponseFactory](./kibana-plugin-core-server.kibanaresponsefactory.md) functions. If anything else is returned, or an error is thrown, the HTTP service will automatically log the error and respond <code>500 - Internal Server Error</code>. |
|
||||
| [RequestHandlerContextContainer](./kibana-plugin-core-server.requesthandlercontextcontainer.md) | An object that handles registration of http request context providers. |
|
||||
| [RequestHandlerContextProvider](./kibana-plugin-core-server.requesthandlercontextprovider.md) | Context provider for request handler. Extends request context object with provided functionality or data. |
|
||||
| [RequestHandlerWrapper](./kibana-plugin-core-server.requesthandlerwrapper.md) | Type-safe wrapper for [RequestHandler](./kibana-plugin-core-server.requesthandler.md) function. |
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
## RequestHandler type
|
||||
|
||||
A function executed when route path matched requested resource path. Request handler is expected to return a result of one of [KibanaResponseFactory](./kibana-plugin-core-server.kibanaresponsefactory.md) functions.
|
||||
A function executed when route path matched requested resource path. Request handler is expected to return a result of one of [KibanaResponseFactory](./kibana-plugin-core-server.kibanaresponsefactory.md) functions. If anything else is returned, or an error is thrown, the HTTP service will automatically log the error and respond `500 - Internal Server Error`<!-- -->.
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
|
|
|
@ -705,12 +705,8 @@ describe('BasePathProxyServer', () => {
|
|||
options: { body: { output: 'stream' } },
|
||||
},
|
||||
(_, req, res) => {
|
||||
try {
|
||||
expect(req.body).toBeInstanceOf(Readable);
|
||||
return res.ok({ body: req.route.options.body });
|
||||
} catch (err) {
|
||||
return res.internalError({ body: err.message });
|
||||
}
|
||||
expect(req.body).toBeInstanceOf(Readable);
|
||||
return res.ok({ body: req.route.options.body });
|
||||
}
|
||||
);
|
||||
registerRouter(router);
|
||||
|
@ -740,15 +736,11 @@ describe('BasePathProxyServer', () => {
|
|||
},
|
||||
},
|
||||
(_, req, res) => {
|
||||
try {
|
||||
return res.ok({
|
||||
body: {
|
||||
timeout: req.route.options.timeout,
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
return res.internalError({ body: err.message });
|
||||
}
|
||||
return res.ok({
|
||||
body: {
|
||||
timeout: req.route.options.timeout,
|
||||
},
|
||||
});
|
||||
}
|
||||
);
|
||||
registerRouter(router);
|
||||
|
@ -779,15 +771,11 @@ describe('BasePathProxyServer', () => {
|
|||
},
|
||||
},
|
||||
(context, req, res) => {
|
||||
try {
|
||||
return res.ok({
|
||||
body: {
|
||||
timeout: req.route.options.timeout,
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
return res.internalError({ body: err.message });
|
||||
}
|
||||
return res.ok({
|
||||
body: {
|
||||
timeout: req.route.options.timeout,
|
||||
},
|
||||
});
|
||||
}
|
||||
);
|
||||
registerRouter(router);
|
||||
|
@ -815,15 +803,11 @@ describe('BasePathProxyServer', () => {
|
|||
},
|
||||
},
|
||||
(_, req, res) => {
|
||||
try {
|
||||
return res.ok({
|
||||
body: {
|
||||
timeout: req.route.options.timeout,
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
return res.internalError({ body: err.message });
|
||||
}
|
||||
return res.ok({
|
||||
body: {
|
||||
timeout: req.route.options.timeout,
|
||||
},
|
||||
});
|
||||
}
|
||||
);
|
||||
registerRouter(router);
|
||||
|
@ -851,15 +835,11 @@ describe('BasePathProxyServer', () => {
|
|||
},
|
||||
},
|
||||
(_, req, res) => {
|
||||
try {
|
||||
return res.ok({
|
||||
body: {
|
||||
timeout: req.route.options.timeout,
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
return res.internalError({ body: err.message });
|
||||
}
|
||||
return res.ok({
|
||||
body: {
|
||||
timeout: req.route.options.timeout,
|
||||
},
|
||||
});
|
||||
}
|
||||
);
|
||||
registerRouter(router);
|
||||
|
|
|
@ -151,7 +151,6 @@ const createResponseFactoryMock = (): jest.Mocked<KibanaResponseFactory> => ({
|
|||
forbidden: jest.fn(),
|
||||
notFound: jest.fn(),
|
||||
conflict: jest.fn(),
|
||||
internalError: jest.fn(),
|
||||
customError: jest.fn(),
|
||||
});
|
||||
|
||||
|
@ -162,7 +161,6 @@ const createLifecycleResponseFactoryMock = (): jest.Mocked<LifecycleResponseFact
|
|||
forbidden: jest.fn(),
|
||||
notFound: jest.fn(),
|
||||
conflict: jest.fn(),
|
||||
internalError: jest.fn(),
|
||||
customError: jest.fn(),
|
||||
});
|
||||
|
||||
|
|
|
@ -1016,13 +1016,9 @@ describe('body options', () => {
|
|||
options: { body: { parse: false } },
|
||||
},
|
||||
(context, req, res) => {
|
||||
try {
|
||||
expect(req.body).toBeInstanceOf(Buffer);
|
||||
expect(req.body.toString()).toBe(JSON.stringify({ test: 1 }));
|
||||
return res.ok({ body: req.route.options.body });
|
||||
} catch (err) {
|
||||
return res.internalError({ body: err.message });
|
||||
}
|
||||
expect(req.body).toBeInstanceOf(Buffer);
|
||||
expect(req.body.toString()).toBe(JSON.stringify({ test: 1 }));
|
||||
return res.ok({ body: req.route.options.body });
|
||||
}
|
||||
);
|
||||
registerRouter(router);
|
||||
|
@ -1053,15 +1049,11 @@ describe('timeout options', () => {
|
|||
},
|
||||
},
|
||||
(context, req, res) => {
|
||||
try {
|
||||
return res.ok({
|
||||
body: {
|
||||
timeout: req.route.options.timeout,
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
return res.internalError({ body: err.message });
|
||||
}
|
||||
return res.ok({
|
||||
body: {
|
||||
timeout: req.route.options.timeout,
|
||||
},
|
||||
});
|
||||
}
|
||||
);
|
||||
registerRouter(router);
|
||||
|
@ -1091,15 +1083,11 @@ describe('timeout options', () => {
|
|||
},
|
||||
},
|
||||
(context, req, res) => {
|
||||
try {
|
||||
return res.ok({
|
||||
body: {
|
||||
timeout: req.route.options.timeout,
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
return res.internalError({ body: err.message });
|
||||
}
|
||||
return res.ok({
|
||||
body: {
|
||||
timeout: req.route.options.timeout,
|
||||
},
|
||||
});
|
||||
}
|
||||
);
|
||||
registerRouter(router);
|
||||
|
@ -1128,15 +1116,11 @@ describe('timeout options', () => {
|
|||
},
|
||||
},
|
||||
(context, req, res) => {
|
||||
try {
|
||||
return res.ok({
|
||||
body: {
|
||||
timeout: req.route.options.timeout,
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
return res.internalError({ body: err.message });
|
||||
}
|
||||
return res.ok({
|
||||
body: {
|
||||
timeout: req.route.options.timeout,
|
||||
},
|
||||
});
|
||||
}
|
||||
);
|
||||
registerRouter(router);
|
||||
|
@ -1165,15 +1149,11 @@ describe('timeout options', () => {
|
|||
},
|
||||
},
|
||||
(context, req, res) => {
|
||||
try {
|
||||
return res.ok({
|
||||
body: {
|
||||
timeout: req.route.options.timeout,
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
return res.internalError({ body: err.message });
|
||||
}
|
||||
return res.ok({
|
||||
body: {
|
||||
timeout: req.route.options.timeout,
|
||||
},
|
||||
});
|
||||
}
|
||||
);
|
||||
registerRouter(router);
|
||||
|
@ -1294,12 +1274,8 @@ test('should return a stream in the body', async () => {
|
|||
options: { body: { output: 'stream' } },
|
||||
},
|
||||
(context, req, res) => {
|
||||
try {
|
||||
expect(req.body).toBeInstanceOf(Readable);
|
||||
return res.ok({ body: req.route.options.body });
|
||||
} catch (err) {
|
||||
return res.internalError({ body: err.message });
|
||||
}
|
||||
expect(req.body).toBeInstanceOf(Readable);
|
||||
return res.ok({ body: req.route.options.body });
|
||||
}
|
||||
);
|
||||
registerRouter(router);
|
||||
|
|
|
@ -1672,7 +1672,11 @@ describe('Response factory', () => {
|
|||
|
||||
const result = await supertest(innerServer.listener).get('/').expect(500);
|
||||
|
||||
expect(result.body.message).toBe('reason');
|
||||
expect(result.body).toEqual({
|
||||
error: 'Internal Server Error',
|
||||
message: 'reason',
|
||||
statusCode: 500,
|
||||
});
|
||||
expect(loggingSystemMock.collect(logger).error).toHaveLength(0);
|
||||
});
|
||||
|
||||
|
|
|
@ -177,15 +177,6 @@ const errorResponseFactory = {
|
|||
conflict: (options: ErrorHttpResponseOptions = {}) =>
|
||||
new KibanaResponse(409, options.body || 'Conflict', options),
|
||||
|
||||
// Server error
|
||||
/**
|
||||
* The server encountered an unexpected condition that prevented it from fulfilling the request.
|
||||
* Status code: `500`.
|
||||
* @param options - {@link HttpResponseOptions} configures HTTP response headers, error message and other error details to pass to the client
|
||||
*/
|
||||
internalError: (options: ErrorHttpResponseOptions = {}) =>
|
||||
new KibanaResponse(500, options.body || 'Internal Error', options),
|
||||
|
||||
/**
|
||||
* Creates an error response with defined status code and payload.
|
||||
* @param options - {@link CustomHttpResponseOptions} configures HTTP response headers, error message and other error details to pass to the client
|
||||
|
|
|
@ -314,6 +314,8 @@ type RequestHandlerEnhanced<P, Q, B, Method extends RouteMethod> = WithoutHeadAr
|
|||
/**
|
||||
* A function executed when route path matched requested resource path.
|
||||
* Request handler is expected to return a result of one of {@link KibanaResponseFactory} functions.
|
||||
* If anything else is returned, or an error is thrown, the HTTP service will automatically log the error
|
||||
* and respond `500 - Internal Server Error`.
|
||||
* @param context {@link RequestHandlerContext} - the core context exposed for this request.
|
||||
* @param request {@link KibanaRequest} - object containing information about requested resource,
|
||||
* such as path, method, headers, parameters, query, body, etc.
|
||||
|
|
|
@ -1276,7 +1276,6 @@ export const kibanaResponseFactory: {
|
|||
forbidden: (options?: ErrorHttpResponseOptions) => KibanaResponse<ResponseError>;
|
||||
notFound: (options?: ErrorHttpResponseOptions) => KibanaResponse<ResponseError>;
|
||||
conflict: (options?: ErrorHttpResponseOptions) => KibanaResponse<ResponseError>;
|
||||
internalError: (options?: ErrorHttpResponseOptions) => KibanaResponse<ResponseError>;
|
||||
customError: (options: CustomHttpResponseOptions<ResponseError>) => KibanaResponse<ResponseError>;
|
||||
redirected: (options: RedirectResponseOptions) => KibanaResponse<string | Record<string, any> | Buffer | Stream>;
|
||||
ok: (options?: HttpResponseOptions) => KibanaResponse<string | Record<string, any> | Buffer | Stream>;
|
||||
|
@ -3197,7 +3196,7 @@ export const validBodyOutput: readonly ["data", "stream"];
|
|||
|
||||
// Warnings were encountered during analysis:
|
||||
//
|
||||
// src/core/server/http/router/response.ts:306:3 - (ae-forgotten-export) The symbol "KibanaResponse" needs to be exported by the entry point index.d.ts
|
||||
// src/core/server/http/router/response.ts:297:3 - (ae-forgotten-export) The symbol "KibanaResponse" needs to be exported by the entry point index.d.ts
|
||||
// src/core/server/plugins/types.ts:280:3 - (ae-forgotten-export) The symbol "KibanaConfigType" needs to be exported by the entry point index.d.ts
|
||||
// src/core/server/plugins/types.ts:280:3 - (ae-forgotten-export) The symbol "SharedGlobalConfigKeys" needs to be exported by the entry point index.d.ts
|
||||
// src/core/server/plugins/types.ts:283:3 - (ae-forgotten-export) The symbol "SavedObjectsConfigType" needs to be exported by the entry point index.d.ts
|
||||
|
|
|
@ -57,17 +57,13 @@ export function registerValueSuggestionsRoute(
|
|||
const field = indexPattern && getFieldByName(fieldName, indexPattern);
|
||||
const body = await getBody(autocompleteSearchOptions, field || fieldName, query, filters);
|
||||
|
||||
try {
|
||||
const result = await client.callAsCurrentUser('search', { index, body }, { signal });
|
||||
const result = await client.callAsCurrentUser('search', { index, body }, { signal });
|
||||
|
||||
const buckets: any[] =
|
||||
get(result, 'aggregations.suggestions.buckets') ||
|
||||
get(result, 'aggregations.nestedSuggestions.suggestions.buckets');
|
||||
const buckets: any[] =
|
||||
get(result, 'aggregations.suggestions.buckets') ||
|
||||
get(result, 'aggregations.nestedSuggestions.suggestions.buckets');
|
||||
|
||||
return response.ok({ body: map(buckets || [], 'key') });
|
||||
} catch (error) {
|
||||
return response.internalError({ body: error });
|
||||
}
|
||||
return response.ok({ body: map(buckets || [], 'key') });
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -38,5 +38,5 @@ export const handleEsError = ({
|
|||
});
|
||||
}
|
||||
// Case: default
|
||||
return response.internalError({ body: error });
|
||||
throw error;
|
||||
};
|
||||
|
|
|
@ -136,8 +136,7 @@ export function createInstallRoute(
|
|||
(counts as any)[index] = count;
|
||||
} catch (err) {
|
||||
const errMsg = `sample_data install errors while loading data. Error: ${err}`;
|
||||
logger.warn(errMsg);
|
||||
return res.internalError({ body: errMsg });
|
||||
throw new Error(errMsg);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -157,8 +156,7 @@ export function createInstallRoute(
|
|||
);
|
||||
} catch (err) {
|
||||
const errMsg = `bulkCreate failed, error: ${err.message}`;
|
||||
logger.warn(errMsg);
|
||||
return res.internalError({ body: errMsg });
|
||||
throw new Error(errMsg);
|
||||
}
|
||||
const errors = createResults.saved_objects.filter((savedObjectCreateResult) => {
|
||||
return Boolean(savedObjectCreateResult.error);
|
||||
|
|
|
@ -77,46 +77,32 @@ export function runRoute(
|
|||
},
|
||||
},
|
||||
router.handleLegacyErrors(async (context, request, response) => {
|
||||
try {
|
||||
const [, { data }] = await core.getStartServices();
|
||||
const uiSettings = await context.core.uiSettings.client.getAll();
|
||||
const indexPatternsService = await data.indexPatterns.indexPatternsServiceFactory(
|
||||
context.core.savedObjects.client,
|
||||
context.core.elasticsearch.client.asCurrentUser
|
||||
);
|
||||
const [, { data }] = await core.getStartServices();
|
||||
const uiSettings = await context.core.uiSettings.client.getAll();
|
||||
const indexPatternsService = await data.indexPatterns.indexPatternsServiceFactory(
|
||||
context.core.savedObjects.client,
|
||||
context.core.elasticsearch.client.asCurrentUser
|
||||
);
|
||||
|
||||
const tlConfig = getTlConfig({
|
||||
context,
|
||||
request,
|
||||
settings: _.defaults(uiSettings, timelionDefaults), // Just in case they delete some setting.
|
||||
getFunction,
|
||||
getIndexPatternsService: () => indexPatternsService,
|
||||
getStartServices: core.getStartServices,
|
||||
allowedGraphiteUrls: configManager.getGraphiteUrls(),
|
||||
esShardTimeout: configManager.getEsShardTimeout(),
|
||||
});
|
||||
const chainRunner = chainRunnerFn(tlConfig);
|
||||
const sheet = await Bluebird.all(chainRunner.processRequest(request.body));
|
||||
const tlConfig = getTlConfig({
|
||||
context,
|
||||
request,
|
||||
settings: _.defaults(uiSettings, timelionDefaults), // Just in case they delete some setting.
|
||||
getFunction,
|
||||
getIndexPatternsService: () => indexPatternsService,
|
||||
getStartServices: core.getStartServices,
|
||||
allowedGraphiteUrls: configManager.getGraphiteUrls(),
|
||||
esShardTimeout: configManager.getEsShardTimeout(),
|
||||
});
|
||||
const chainRunner = chainRunnerFn(tlConfig);
|
||||
const sheet = await Bluebird.all(chainRunner.processRequest(request.body));
|
||||
|
||||
return response.ok({
|
||||
body: {
|
||||
sheet,
|
||||
stats: chainRunner.getStats(),
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error(`${err.toString()}: ${err.stack}`);
|
||||
// TODO Maybe we should just replace everywhere we throw with Boom? Probably.
|
||||
if (err.isBoom) {
|
||||
throw err;
|
||||
} else {
|
||||
return response.internalError({
|
||||
body: {
|
||||
message: err.toString(),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
return response.ok({
|
||||
body: {
|
||||
sheet,
|
||||
stats: chainRunner.getStats(),
|
||||
},
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
|
|
|
@ -42,18 +42,12 @@ export const visDataRoutes = (router: VisTypeTimeseriesRouter, framework: Framew
|
|||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const results = await getVisData(
|
||||
requestContext,
|
||||
request as KibanaRequest<{}, {}, GetVisDataOptions>,
|
||||
framework
|
||||
);
|
||||
return response.ok({ body: results });
|
||||
} catch (error) {
|
||||
return response.internalError({
|
||||
body: error.message,
|
||||
});
|
||||
}
|
||||
const results = await getVisData(
|
||||
requestContext,
|
||||
request as KibanaRequest<{}, {}, GetVisDataOptions>,
|
||||
framework
|
||||
);
|
||||
return response.ok({ body: results });
|
||||
}
|
||||
);
|
||||
};
|
||||
|
|
|
@ -34,7 +34,7 @@ export class NewsFeedSimulatorPlugin implements Plugin {
|
|||
options: { authRequired: false },
|
||||
},
|
||||
(context, req, res) => {
|
||||
return res.internalError({ body: new Error('Internal server error') });
|
||||
throw new Error('Internal server error');
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ export class CorePluginBPlugin implements Plugin {
|
|||
public setup(core: CoreSetup, deps: {}) {
|
||||
const router = core.http.createRouter<PluginBContext>();
|
||||
router.get({ path: '/core_plugin_b', validate: false }, async (context, req, res) => {
|
||||
if (!context.pluginA) return res.internalError({ body: 'pluginA is disabled' });
|
||||
if (!context.pluginA) throw new Error('pluginA is disabled');
|
||||
const response = await context.pluginA.ping();
|
||||
return res.ok({ body: `Pong via plugin A: ${response}` });
|
||||
});
|
||||
|
|
|
@ -50,11 +50,7 @@ export const registerCreateTokenRoute = (router: BeatsManagementRouter) => {
|
|||
});
|
||||
} catch (err) {
|
||||
beatsManagement.framework.log(err.message);
|
||||
return response.internalError({
|
||||
body: {
|
||||
message: 'An error occurred, please check your Kibana logs',
|
||||
},
|
||||
});
|
||||
throw new Error('An error occurred, please check your Kibana logs');
|
||||
}
|
||||
}
|
||||
)
|
||||
|
|
|
@ -20,7 +20,7 @@ export const catchErrorHandler: <P, Q, B>(
|
|||
statusCode: error.output.statusCode,
|
||||
});
|
||||
}
|
||||
return response.internalError({ body: error });
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
|
@ -147,8 +147,8 @@ describe('Retrieve ES Fields', () => {
|
|||
|
||||
callAsCurrentUserMock.mockRejectedValueOnce(new Error('Index not found'));
|
||||
|
||||
const response = await routeHandler(mockRouteContext, request, kibanaResponseFactory);
|
||||
|
||||
expect(response.status).toBe(500);
|
||||
await expect(
|
||||
routeHandler(mockRouteContext, request, kibanaResponseFactory)
|
||||
).rejects.toThrowError('Index not found');
|
||||
});
|
||||
});
|
||||
|
|
|
@ -55,7 +55,7 @@ export const registerCreateRoute = ({
|
|||
return response.customError(formatEsError(err));
|
||||
}
|
||||
// Case: default
|
||||
return response.internalError({ body: err });
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -71,7 +71,7 @@ export const registerCreateRoute = ({
|
|||
return response.customError(formatEsError(err));
|
||||
}
|
||||
// Case: default
|
||||
return response.internalError({ body: err });
|
||||
throw err;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -41,7 +41,7 @@ export const registerDeleteRoute = ({
|
|||
return response.customError(formatEsError(err));
|
||||
}
|
||||
// Case: default
|
||||
return response.internalError({ body: err });
|
||||
return response.customError({ statusCode: 500, body: err });
|
||||
};
|
||||
|
||||
await Promise.all(
|
||||
|
|
|
@ -37,7 +37,7 @@ export const registerFetchRoute = ({
|
|||
return response.customError(formatEsError(err));
|
||||
}
|
||||
// Case: default
|
||||
return response.internalError({ body: err });
|
||||
throw err;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -48,7 +48,7 @@ export const registerGetRoute = ({
|
|||
return response.customError(formatEsError(err));
|
||||
}
|
||||
// Case: default
|
||||
return response.internalError({ body: err });
|
||||
throw err;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -40,7 +40,7 @@ export const registerPauseRoute = ({
|
|||
return response.customError(formatEsError(err));
|
||||
}
|
||||
// Case: default
|
||||
return response.internalError({ body: err });
|
||||
return response.customError({ statusCode: 500, body: err });
|
||||
};
|
||||
|
||||
await Promise.all(
|
||||
|
|
|
@ -40,7 +40,7 @@ export const registerResumeRoute = ({
|
|||
return response.customError(formatEsError(err));
|
||||
}
|
||||
// Case: default
|
||||
return response.internalError({ body: err });
|
||||
return response.customError({ statusCode: 500, body: err });
|
||||
};
|
||||
|
||||
await Promise.all(
|
||||
|
|
|
@ -54,7 +54,7 @@ export const registerUpdateRoute = ({
|
|||
return response.customError(formatEsError(err));
|
||||
}
|
||||
// Case: default
|
||||
return response.internalError({ body: err });
|
||||
throw err;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -64,7 +64,7 @@ export const registerPermissionsRoute = ({
|
|||
return response.customError(formatEsError(err));
|
||||
}
|
||||
// Case: default
|
||||
return response.internalError({ body: err });
|
||||
throw err;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -36,7 +36,7 @@ export const registerStatsRoute = ({
|
|||
return response.customError(formatEsError(err));
|
||||
}
|
||||
// Case: default
|
||||
return response.internalError({ body: err });
|
||||
throw err;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -59,7 +59,7 @@ export const registerCreateRoute = ({
|
|||
return response.customError(formatEsError(err));
|
||||
}
|
||||
// Case: default
|
||||
return response.internalError({ body: err });
|
||||
throw err;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -56,7 +56,7 @@ export const registerFetchRoute = ({
|
|||
return response.customError(formatEsError(err));
|
||||
}
|
||||
// Case: default
|
||||
return response.internalError({ body: err });
|
||||
throw err;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -72,7 +72,7 @@ export const registerGetRoute = ({
|
|||
return response.customError(formatEsError(err));
|
||||
}
|
||||
// Case: default
|
||||
return response.internalError({ body: err });
|
||||
throw err;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -38,7 +38,7 @@ export const registerPauseRoute = ({
|
|||
return response.customError(formatEsError(err));
|
||||
}
|
||||
// Case: default
|
||||
return response.internalError({ body: err });
|
||||
return response.customError({ statusCode: 500, body: err });
|
||||
};
|
||||
|
||||
await Promise.all(
|
||||
|
|
|
@ -38,7 +38,7 @@ export const registerResumeRoute = ({
|
|||
return response.customError(formatEsError(err));
|
||||
}
|
||||
// Case: default
|
||||
return response.internalError({ body: err });
|
||||
return response.customError({ statusCode: 500, body: err });
|
||||
};
|
||||
|
||||
await Promise.all(
|
||||
|
|
|
@ -39,7 +39,7 @@ export const registerUnfollowRoute = ({
|
|||
return response.customError(formatEsError(err));
|
||||
}
|
||||
// Case: default
|
||||
return response.internalError({ body: err });
|
||||
return response.customError({ statusCode: 500, body: err });
|
||||
};
|
||||
|
||||
await Promise.all(
|
||||
|
|
|
@ -87,7 +87,7 @@ export const registerUpdateRoute = ({
|
|||
return response.customError(formatEsError(err));
|
||||
}
|
||||
// Case: default
|
||||
return response.internalError({ body: err });
|
||||
throw err;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
import { MockRouter, mockLogger, mockDependencies } from '../../__mocks__';
|
||||
|
||||
import { loggingSystemMock, savedObjectsServiceMock } from 'src/core/server/mocks';
|
||||
import { savedObjectsServiceMock } from 'src/core/server/mocks';
|
||||
|
||||
jest.mock('../../collectors/lib/telemetry', () => ({
|
||||
incrementUICounter: jest.fn(),
|
||||
|
@ -84,17 +84,17 @@ describe('Enterprise Search Telemetry API', () => {
|
|||
it('throws an error when incrementing fails', async () => {
|
||||
(incrementUICounter as jest.Mock).mockImplementation(jest.fn(() => Promise.reject('Failed')));
|
||||
|
||||
await mockRouter.callRoute({
|
||||
body: {
|
||||
product: 'enterprise_search',
|
||||
action: 'error',
|
||||
metric: 'error',
|
||||
},
|
||||
});
|
||||
await expect(
|
||||
mockRouter.callRoute({
|
||||
body: {
|
||||
product: 'enterprise_search',
|
||||
action: 'error',
|
||||
metric: 'error',
|
||||
},
|
||||
})
|
||||
).rejects.toEqual('Failed');
|
||||
|
||||
expect(incrementUICounter).toHaveBeenCalled();
|
||||
expect(mockLogger.error).toHaveBeenCalled();
|
||||
expect(mockRouter.response.internalError).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('throws an error if the Saved Objects service is unavailable', async () => {
|
||||
|
@ -104,16 +104,9 @@ describe('Enterprise Search Telemetry API', () => {
|
|||
getSavedObjectsService: null,
|
||||
log: mockLogger,
|
||||
} as any);
|
||||
await mockRouter.callRoute({});
|
||||
await expect(mockRouter.callRoute({})).rejects.toThrow();
|
||||
|
||||
expect(incrementUICounter).not.toHaveBeenCalled();
|
||||
expect(mockLogger.error).toHaveBeenCalled();
|
||||
expect(mockRouter.response.internalError).toHaveBeenCalled();
|
||||
expect(loggingSystemMock.collect(mockLogger).error[0][0]).toEqual(
|
||||
expect.stringContaining(
|
||||
'Enterprise Search UI telemetry error: Error: Could not find Saved Objects service'
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
describe('validates', () => {
|
||||
|
|
|
@ -20,7 +20,7 @@ const productToTelemetryMap = {
|
|||
workplace_search: WS_TELEMETRY_NAME,
|
||||
};
|
||||
|
||||
export function registerTelemetryRoute({ router, getSavedObjectsService, log }: RouteDependencies) {
|
||||
export function registerTelemetryRoute({ router, getSavedObjectsService }: RouteDependencies) {
|
||||
router.put(
|
||||
{
|
||||
path: '/api/enterprise_search/stats',
|
||||
|
@ -43,23 +43,16 @@ export function registerTelemetryRoute({ router, getSavedObjectsService, log }:
|
|||
async (ctx, request, response) => {
|
||||
const { product, action, metric } = request.body;
|
||||
|
||||
try {
|
||||
if (!getSavedObjectsService) throw new Error('Could not find Saved Objects service');
|
||||
if (!getSavedObjectsService) throw new Error('Could not find Saved Objects service');
|
||||
|
||||
return response.ok({
|
||||
body: await incrementUICounter({
|
||||
id: productToTelemetryMap[product],
|
||||
savedObjects: getSavedObjectsService(),
|
||||
uiAction: `ui_${action}`,
|
||||
metric,
|
||||
}),
|
||||
});
|
||||
} catch (e) {
|
||||
log.error(
|
||||
`Enterprise Search UI telemetry error: ${e instanceof Error ? e.stack : e.toString()}`
|
||||
);
|
||||
return response.internalError({ body: 'Enterprise Search UI telemetry failed' });
|
||||
}
|
||||
return response.ok({
|
||||
body: await incrementUICounter({
|
||||
id: productToTelemetryMap[product],
|
||||
savedObjects: getSavedObjectsService(),
|
||||
uiAction: `ui_${action}`,
|
||||
metric,
|
||||
}),
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -76,11 +76,7 @@ export function registerExploreRoute({
|
|||
}
|
||||
}
|
||||
|
||||
return response.internalError({
|
||||
body: {
|
||||
message: error.message,
|
||||
},
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
)
|
||||
|
|
|
@ -71,7 +71,7 @@ export const registerCreateRoute = ({
|
|||
});
|
||||
}
|
||||
|
||||
return res.internalError({ body: error });
|
||||
throw error;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -54,7 +54,7 @@ export function registerGetAllRoute({ router, license, lib: { isEsError } }: Rou
|
|||
});
|
||||
}
|
||||
|
||||
return res.internalError({ body: error });
|
||||
throw error;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
@ -94,7 +94,7 @@ export function registerGetAllRoute({ router, license, lib: { isEsError } }: Rou
|
|||
});
|
||||
}
|
||||
|
||||
return res.internalError({ body: error });
|
||||
throw error;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -64,7 +64,7 @@ export const registerPrivilegesRoute = ({ license, router, config }: RouteDepend
|
|||
|
||||
return res.ok({ body: privilegesResult });
|
||||
} catch (e) {
|
||||
return res.internalError({ body: e });
|
||||
throw e;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -55,7 +55,7 @@ export const registerUpdateRoute = ({
|
|||
});
|
||||
}
|
||||
|
||||
return res.internalError({ body: error });
|
||||
throw error;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -38,7 +38,7 @@ export function registerClearCacheRoute({ router, license, lib }: RouteDependenc
|
|||
});
|
||||
}
|
||||
// Case: default
|
||||
return res.internalError({ body: e });
|
||||
throw e;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -38,7 +38,7 @@ export function registerCloseRoute({ router, license, lib }: RouteDependencies)
|
|||
});
|
||||
}
|
||||
// Case: default
|
||||
return res.internalError({ body: e });
|
||||
throw e;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -38,7 +38,7 @@ export function registerDeleteRoute({ router, license, lib }: RouteDependencies)
|
|||
});
|
||||
}
|
||||
// Case: default
|
||||
return res.internalError({ body: e });
|
||||
throw e;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -38,7 +38,7 @@ export function registerFlushRoute({ router, license, lib }: RouteDependencies)
|
|||
});
|
||||
}
|
||||
// Case: default
|
||||
return res.internalError({ body: e });
|
||||
throw e;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -45,7 +45,7 @@ export function registerForcemergeRoute({ router, license, lib }: RouteDependenc
|
|||
});
|
||||
}
|
||||
// Case: default
|
||||
return res.internalError({ body: e });
|
||||
throw e;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -40,7 +40,7 @@ export function registerFreezeRoute({ router, license, lib }: RouteDependencies)
|
|||
});
|
||||
}
|
||||
// Case: default
|
||||
return res.internalError({ body: e });
|
||||
throw e;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -27,7 +27,7 @@ export function registerListRoute({ router, license, indexDataEnricher, lib }: R
|
|||
});
|
||||
}
|
||||
// Case: default
|
||||
return res.internalError({ body: e });
|
||||
throw e;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -38,7 +38,7 @@ export function registerOpenRoute({ router, license, lib }: RouteDependencies) {
|
|||
});
|
||||
}
|
||||
// Case: default
|
||||
return res.internalError({ body: e });
|
||||
throw e;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -38,7 +38,7 @@ export function registerRefreshRoute({ router, license, lib }: RouteDependencies
|
|||
});
|
||||
}
|
||||
// Case: default
|
||||
return res.internalError({ body: e });
|
||||
throw e;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -43,7 +43,7 @@ export function registerReloadRoute({
|
|||
});
|
||||
}
|
||||
// Case: default
|
||||
return res.internalError({ body: e });
|
||||
throw e;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -35,7 +35,7 @@ export function registerUnfreezeRoute({ router, license, lib }: RouteDependencie
|
|||
});
|
||||
}
|
||||
// Case: default
|
||||
return res.internalError({ body: e });
|
||||
throw e;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -46,7 +46,7 @@ export function registerMappingRoute({ router, license, lib }: RouteDependencies
|
|||
});
|
||||
}
|
||||
// Case: default
|
||||
return res.internalError({ body: e });
|
||||
throw e;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -48,7 +48,7 @@ export function registerLoadRoute({ router, license, lib }: RouteDependencies) {
|
|||
});
|
||||
}
|
||||
// Case: default
|
||||
return res.internalError({ body: e });
|
||||
throw e;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -46,7 +46,7 @@ export function registerUpdateRoute({ router, license, lib }: RouteDependencies)
|
|||
});
|
||||
}
|
||||
// Case: default
|
||||
return res.internalError({ body: e });
|
||||
throw e;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -47,7 +47,7 @@ export function registerStatsRoute({ router, license, lib }: RouteDependencies)
|
|||
});
|
||||
}
|
||||
// Case: default
|
||||
return res.internalError({ body: e });
|
||||
throw e;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -62,7 +62,7 @@ export function registerCreateRoute({ router, license, lib }: RouteDependencies)
|
|||
});
|
||||
}
|
||||
// Case: default
|
||||
return res.internalError({ body: e });
|
||||
throw e;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -104,7 +104,7 @@ export function registerGetOneRoute({ router, license, lib }: RouteDependencies)
|
|||
});
|
||||
}
|
||||
// Case: default
|
||||
return res.internalError({ body: e });
|
||||
throw e;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -40,7 +40,7 @@ export function registerSimulateRoute({ router, license, lib }: RouteDependencie
|
|||
});
|
||||
}
|
||||
// Case: default
|
||||
return res.internalError({ body: e });
|
||||
throw e;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -56,7 +56,7 @@ export function registerUpdateRoute({ router, license, lib }: RouteDependencies)
|
|||
});
|
||||
}
|
||||
// Case: default
|
||||
return res.internalError({ body: e });
|
||||
throw e;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -33,33 +33,27 @@ export const initInventoryMetaRoute = (libs: InfraBackendLibs) => {
|
|||
},
|
||||
},
|
||||
async (requestContext, request, response) => {
|
||||
try {
|
||||
const { sourceId, nodeType, currentTime } = pipe(
|
||||
InventoryMetaRequestRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
const { sourceId, nodeType, currentTime } = pipe(
|
||||
InventoryMetaRequestRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
|
||||
const { configuration } = await libs.sources.getSourceConfiguration(
|
||||
requestContext.core.savedObjects.client,
|
||||
sourceId
|
||||
);
|
||||
const { configuration } = await libs.sources.getSourceConfiguration(
|
||||
requestContext.core.savedObjects.client,
|
||||
sourceId
|
||||
);
|
||||
|
||||
const awsMetadata = await getCloudMetadata(
|
||||
framework,
|
||||
requestContext,
|
||||
configuration,
|
||||
nodeType,
|
||||
currentTime
|
||||
);
|
||||
const awsMetadata = await getCloudMetadata(
|
||||
framework,
|
||||
requestContext,
|
||||
configuration,
|
||||
nodeType,
|
||||
currentTime
|
||||
);
|
||||
|
||||
return response.ok({
|
||||
body: InventoryMetaResponseRT.encode(awsMetadata),
|
||||
});
|
||||
} catch (error) {
|
||||
return response.internalError({
|
||||
body: error.message,
|
||||
});
|
||||
}
|
||||
return response.ok({
|
||||
body: InventoryMetaResponseRT.encode(awsMetadata),
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
||||
|
|
|
@ -31,65 +31,59 @@ export const initValidateLogAnalysisIndicesRoute = ({ framework }: InfraBackendL
|
|||
validate: { body: escapeHatch },
|
||||
},
|
||||
async (requestContext, request, response) => {
|
||||
try {
|
||||
const payload = pipe(
|
||||
validationIndicesRequestPayloadRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
const payload = pipe(
|
||||
validationIndicesRequestPayloadRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
|
||||
const { fields, indices } = payload.data;
|
||||
const errors: ValidationIndicesError[] = [];
|
||||
const { fields, indices } = payload.data;
|
||||
const errors: ValidationIndicesError[] = [];
|
||||
|
||||
// Query each pattern individually, to map correctly the errors
|
||||
await Promise.all(
|
||||
indices.map(async (index) => {
|
||||
const fieldCaps = await framework.callWithRequest(requestContext, 'fieldCaps', {
|
||||
allow_no_indices: true,
|
||||
fields: fields.map((field) => field.name),
|
||||
ignore_unavailable: true,
|
||||
// Query each pattern individually, to map correctly the errors
|
||||
await Promise.all(
|
||||
indices.map(async (index) => {
|
||||
const fieldCaps = await framework.callWithRequest(requestContext, 'fieldCaps', {
|
||||
allow_no_indices: true,
|
||||
fields: fields.map((field) => field.name),
|
||||
ignore_unavailable: true,
|
||||
index,
|
||||
});
|
||||
|
||||
if (fieldCaps.indices.length === 0) {
|
||||
errors.push({
|
||||
error: 'INDEX_NOT_FOUND',
|
||||
index,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (fieldCaps.indices.length === 0) {
|
||||
fields.forEach(({ name: fieldName, validTypes }) => {
|
||||
const fieldMetadata = fieldCaps.fields[fieldName];
|
||||
|
||||
if (fieldMetadata === undefined) {
|
||||
errors.push({
|
||||
error: 'INDEX_NOT_FOUND',
|
||||
error: 'FIELD_NOT_FOUND',
|
||||
index,
|
||||
field: fieldName,
|
||||
});
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
const fieldTypes = Object.keys(fieldMetadata);
|
||||
|
||||
fields.forEach(({ name: fieldName, validTypes }) => {
|
||||
const fieldMetadata = fieldCaps.fields[fieldName];
|
||||
|
||||
if (fieldMetadata === undefined) {
|
||||
if (!fieldTypes.every((fieldType) => validTypes.includes(fieldType))) {
|
||||
errors.push({
|
||||
error: 'FIELD_NOT_FOUND',
|
||||
error: `FIELD_NOT_VALID`,
|
||||
index,
|
||||
field: fieldName,
|
||||
});
|
||||
} else {
|
||||
const fieldTypes = Object.keys(fieldMetadata);
|
||||
|
||||
if (!fieldTypes.every((fieldType) => validTypes.includes(fieldType))) {
|
||||
errors.push({
|
||||
error: `FIELD_NOT_VALID`,
|
||||
index,
|
||||
field: fieldName,
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
return response.ok({
|
||||
body: validationIndicesResponsePayloadRT.encode({ data: { errors } }),
|
||||
});
|
||||
} catch (error) {
|
||||
return response.internalError({
|
||||
body: error.message,
|
||||
});
|
||||
}
|
||||
return response.ok({
|
||||
body: validationIndicesResponsePayloadRT.encode({ data: { errors } }),
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
||||
|
|
|
@ -33,75 +33,69 @@ export const initLogEntriesHighlightsRoute = ({ framework, logEntries }: InfraBa
|
|||
validate: { body: escapeHatch },
|
||||
},
|
||||
async (requestContext, request, response) => {
|
||||
try {
|
||||
const payload = pipe(
|
||||
logEntriesHighlightsRequestRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
const payload = pipe(
|
||||
logEntriesHighlightsRequestRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
|
||||
const { startTimestamp, endTimestamp, sourceId, query, size, highlightTerms } = payload;
|
||||
|
||||
let entriesPerHighlightTerm;
|
||||
|
||||
if ('center' in payload) {
|
||||
entriesPerHighlightTerm = await Promise.all(
|
||||
highlightTerms.map((highlightTerm) =>
|
||||
logEntries.getLogEntriesAround(requestContext, sourceId, {
|
||||
startTimestamp,
|
||||
endTimestamp,
|
||||
query: parseFilterQuery(query),
|
||||
center: payload.center,
|
||||
size,
|
||||
highlightTerm,
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
const { startTimestamp, endTimestamp, sourceId, query, size, highlightTerms } = payload;
|
||||
|
||||
let entriesPerHighlightTerm;
|
||||
|
||||
if ('center' in payload) {
|
||||
entriesPerHighlightTerm = await Promise.all(
|
||||
highlightTerms.map((highlightTerm) =>
|
||||
logEntries.getLogEntriesAround(requestContext, sourceId, {
|
||||
startTimestamp,
|
||||
endTimestamp,
|
||||
query: parseFilterQuery(query),
|
||||
center: payload.center,
|
||||
size,
|
||||
highlightTerm,
|
||||
})
|
||||
)
|
||||
);
|
||||
} else {
|
||||
let cursor: LogEntriesParams['cursor'];
|
||||
if ('before' in payload) {
|
||||
cursor = { before: payload.before };
|
||||
} else if ('after' in payload) {
|
||||
cursor = { after: payload.after };
|
||||
}
|
||||
|
||||
entriesPerHighlightTerm = await Promise.all(
|
||||
highlightTerms.map((highlightTerm) =>
|
||||
logEntries.getLogEntries(requestContext, sourceId, {
|
||||
startTimestamp,
|
||||
endTimestamp,
|
||||
query: parseFilterQuery(query),
|
||||
cursor,
|
||||
size,
|
||||
highlightTerm,
|
||||
})
|
||||
)
|
||||
);
|
||||
} else {
|
||||
let cursor: LogEntriesParams['cursor'];
|
||||
if ('before' in payload) {
|
||||
cursor = { before: payload.before };
|
||||
} else if ('after' in payload) {
|
||||
cursor = { after: payload.after };
|
||||
}
|
||||
|
||||
return response.ok({
|
||||
body: logEntriesHighlightsResponseRT.encode({
|
||||
data: entriesPerHighlightTerm.map(({ entries }) => {
|
||||
if (entries.length > 0) {
|
||||
return {
|
||||
entries,
|
||||
topCursor: entries[0].cursor,
|
||||
bottomCursor: entries[entries.length - 1].cursor,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
entries,
|
||||
topCursor: null,
|
||||
bottomCursor: null,
|
||||
};
|
||||
}
|
||||
}),
|
||||
}),
|
||||
});
|
||||
} catch (error) {
|
||||
return response.internalError({
|
||||
body: error.message,
|
||||
});
|
||||
entriesPerHighlightTerm = await Promise.all(
|
||||
highlightTerms.map((highlightTerm) =>
|
||||
logEntries.getLogEntries(requestContext, sourceId, {
|
||||
startTimestamp,
|
||||
endTimestamp,
|
||||
query: parseFilterQuery(query),
|
||||
cursor,
|
||||
size,
|
||||
highlightTerm,
|
||||
})
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return response.ok({
|
||||
body: logEntriesHighlightsResponseRT.encode({
|
||||
data: entriesPerHighlightTerm.map(({ entries }) => {
|
||||
if (entries.length > 0) {
|
||||
return {
|
||||
entries,
|
||||
topCursor: entries[0].cursor,
|
||||
bottomCursor: entries[entries.length - 1].cursor,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
entries,
|
||||
topCursor: null,
|
||||
bottomCursor: null,
|
||||
};
|
||||
}
|
||||
}),
|
||||
}),
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
||||
|
|
|
@ -33,38 +33,32 @@ export const initLogEntriesSummaryRoute = ({ framework, logEntries }: InfraBacke
|
|||
validate: { body: escapeHatch },
|
||||
},
|
||||
async (requestContext, request, response) => {
|
||||
try {
|
||||
const payload = pipe(
|
||||
logEntriesSummaryRequestRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
const { sourceId, startTimestamp, endTimestamp, bucketSize, query } = payload;
|
||||
const payload = pipe(
|
||||
logEntriesSummaryRequestRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
const { sourceId, startTimestamp, endTimestamp, bucketSize, query } = payload;
|
||||
|
||||
const buckets = await logEntries.getLogSummaryBucketsBetween(
|
||||
requestContext,
|
||||
sourceId,
|
||||
startTimestamp,
|
||||
endTimestamp,
|
||||
bucketSize,
|
||||
parseFilterQuery(query)
|
||||
);
|
||||
const buckets = await logEntries.getLogSummaryBucketsBetween(
|
||||
requestContext,
|
||||
sourceId,
|
||||
startTimestamp,
|
||||
endTimestamp,
|
||||
bucketSize,
|
||||
parseFilterQuery(query)
|
||||
);
|
||||
|
||||
UsageCollector.countLogs();
|
||||
UsageCollector.countLogs();
|
||||
|
||||
return response.ok({
|
||||
body: logEntriesSummaryResponseRT.encode({
|
||||
data: {
|
||||
start: startTimestamp,
|
||||
end: endTimestamp,
|
||||
buckets,
|
||||
},
|
||||
}),
|
||||
});
|
||||
} catch (error) {
|
||||
return response.internalError({
|
||||
body: error.message,
|
||||
});
|
||||
}
|
||||
return response.ok({
|
||||
body: logEntriesSummaryResponseRT.encode({
|
||||
data: {
|
||||
start: startTimestamp,
|
||||
end: endTimestamp,
|
||||
buckets,
|
||||
},
|
||||
}),
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
||||
|
|
|
@ -35,44 +35,31 @@ export const initLogEntriesSummaryHighlightsRoute = ({
|
|||
validate: { body: escapeHatch },
|
||||
},
|
||||
async (requestContext, request, response) => {
|
||||
try {
|
||||
const payload = pipe(
|
||||
logEntriesSummaryHighlightsRequestRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
const {
|
||||
sourceId,
|
||||
startTimestamp,
|
||||
endTimestamp,
|
||||
bucketSize,
|
||||
query,
|
||||
highlightTerms,
|
||||
} = payload;
|
||||
const payload = pipe(
|
||||
logEntriesSummaryHighlightsRequestRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
const { sourceId, startTimestamp, endTimestamp, bucketSize, query, highlightTerms } = payload;
|
||||
|
||||
const bucketsPerHighlightTerm = await logEntries.getLogSummaryHighlightBucketsBetween(
|
||||
requestContext,
|
||||
sourceId,
|
||||
startTimestamp,
|
||||
endTimestamp,
|
||||
bucketSize,
|
||||
highlightTerms,
|
||||
parseFilterQuery(query)
|
||||
);
|
||||
const bucketsPerHighlightTerm = await logEntries.getLogSummaryHighlightBucketsBetween(
|
||||
requestContext,
|
||||
sourceId,
|
||||
startTimestamp,
|
||||
endTimestamp,
|
||||
bucketSize,
|
||||
highlightTerms,
|
||||
parseFilterQuery(query)
|
||||
);
|
||||
|
||||
return response.ok({
|
||||
body: logEntriesSummaryHighlightsResponseRT.encode({
|
||||
data: bucketsPerHighlightTerm.map((buckets) => ({
|
||||
start: startTimestamp,
|
||||
end: endTimestamp,
|
||||
buckets,
|
||||
})),
|
||||
}),
|
||||
});
|
||||
} catch (error) {
|
||||
return response.internalError({
|
||||
body: error.message,
|
||||
});
|
||||
}
|
||||
return response.ok({
|
||||
body: logEntriesSummaryHighlightsResponseRT.encode({
|
||||
data: bucketsPerHighlightTerm.map((buckets) => ({
|
||||
start: startTimestamp,
|
||||
end: endTimestamp,
|
||||
buckets,
|
||||
})),
|
||||
}),
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
||||
|
|
|
@ -37,65 +37,57 @@ export const initMetadataRoute = (libs: InfraBackendLibs) => {
|
|||
},
|
||||
},
|
||||
async (requestContext, request, response) => {
|
||||
try {
|
||||
const { nodeId, nodeType, sourceId, timeRange } = pipe(
|
||||
InfraMetadataRequestRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
const { nodeId, nodeType, sourceId, timeRange } = pipe(
|
||||
InfraMetadataRequestRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
|
||||
const { configuration } = await libs.sources.getSourceConfiguration(
|
||||
requestContext.core.savedObjects.client,
|
||||
sourceId
|
||||
);
|
||||
const metricsMetadata = await getMetricMetadata(
|
||||
framework,
|
||||
requestContext,
|
||||
configuration,
|
||||
nodeId,
|
||||
nodeType,
|
||||
timeRange
|
||||
);
|
||||
const metricFeatures = pickFeatureName(metricsMetadata.buckets).map(
|
||||
nameToFeature('metrics')
|
||||
);
|
||||
const { configuration } = await libs.sources.getSourceConfiguration(
|
||||
requestContext.core.savedObjects.client,
|
||||
sourceId
|
||||
);
|
||||
const metricsMetadata = await getMetricMetadata(
|
||||
framework,
|
||||
requestContext,
|
||||
configuration,
|
||||
nodeId,
|
||||
nodeType,
|
||||
timeRange
|
||||
);
|
||||
const metricFeatures = pickFeatureName(metricsMetadata.buckets).map(nameToFeature('metrics'));
|
||||
|
||||
const info = await getNodeInfo(
|
||||
framework,
|
||||
requestContext,
|
||||
configuration,
|
||||
nodeId,
|
||||
nodeType,
|
||||
timeRange
|
||||
);
|
||||
const cloudInstanceId = get(info, 'cloud.instance.id');
|
||||
const info = await getNodeInfo(
|
||||
framework,
|
||||
requestContext,
|
||||
configuration,
|
||||
nodeId,
|
||||
nodeType,
|
||||
timeRange
|
||||
);
|
||||
const cloudInstanceId = get(info, 'cloud.instance.id');
|
||||
|
||||
const cloudMetricsMetadata = cloudInstanceId
|
||||
? await getCloudMetricsMetadata(
|
||||
framework,
|
||||
requestContext,
|
||||
configuration,
|
||||
cloudInstanceId,
|
||||
timeRange
|
||||
)
|
||||
: { buckets: [] };
|
||||
const cloudMetricsFeatures = pickFeatureName(cloudMetricsMetadata.buckets).map(
|
||||
nameToFeature('metrics')
|
||||
);
|
||||
const id = metricsMetadata.id;
|
||||
const name = metricsMetadata.name || id;
|
||||
return response.ok({
|
||||
body: InfraMetadataRT.encode({
|
||||
id,
|
||||
name,
|
||||
features: [...metricFeatures, ...cloudMetricsFeatures],
|
||||
info,
|
||||
}),
|
||||
});
|
||||
} catch (error) {
|
||||
return response.internalError({
|
||||
body: error.message,
|
||||
});
|
||||
}
|
||||
const cloudMetricsMetadata = cloudInstanceId
|
||||
? await getCloudMetricsMetadata(
|
||||
framework,
|
||||
requestContext,
|
||||
configuration,
|
||||
cloudInstanceId,
|
||||
timeRange
|
||||
)
|
||||
: { buckets: [] };
|
||||
const cloudMetricsFeatures = pickFeatureName(cloudMetricsMetadata.buckets).map(
|
||||
nameToFeature('metrics')
|
||||
);
|
||||
const id = metricsMetadata.id;
|
||||
const name = metricsMetadata.name || id;
|
||||
return response.ok({
|
||||
body: InfraMetadataRT.encode({
|
||||
id,
|
||||
name,
|
||||
features: [...metricFeatures, ...cloudMetricsFeatures],
|
||||
info,
|
||||
}),
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
||||
|
|
|
@ -29,23 +29,17 @@ export const initMetricsAPIRoute = (libs: InfraBackendLibs) => {
|
|||
},
|
||||
},
|
||||
async (requestContext, request, response) => {
|
||||
try {
|
||||
const options = pipe(
|
||||
MetricsAPIRequestRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
const options = pipe(
|
||||
MetricsAPIRequestRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
|
||||
const client = createSearchClient(requestContext, framework);
|
||||
const metricsApiResponse = await query(client, options);
|
||||
const client = createSearchClient(requestContext, framework);
|
||||
const metricsApiResponse = await query(client, options);
|
||||
|
||||
return response.ok({
|
||||
body: MetricsAPIResponseRT.encode(metricsApiResponse),
|
||||
});
|
||||
} catch (error) {
|
||||
return response.internalError({
|
||||
body: error.message,
|
||||
});
|
||||
}
|
||||
return response.ok({
|
||||
body: MetricsAPIResponseRT.encode(metricsApiResponse),
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
||||
|
|
|
@ -37,55 +37,49 @@ export const initMetricExplorerRoute = (libs: InfraBackendLibs) => {
|
|||
},
|
||||
},
|
||||
async (requestContext, request, response) => {
|
||||
try {
|
||||
const options = pipe(
|
||||
metricsExplorerRequestBodyRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
const options = pipe(
|
||||
metricsExplorerRequestBodyRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
|
||||
const client = createSearchClient(requestContext, framework);
|
||||
const interval = await findIntervalForMetrics(client, options);
|
||||
const client = createSearchClient(requestContext, framework);
|
||||
const interval = await findIntervalForMetrics(client, options);
|
||||
|
||||
const optionsWithInterval = options.forceInterval
|
||||
? options
|
||||
: {
|
||||
...options,
|
||||
timerange: {
|
||||
...options.timerange,
|
||||
interval: interval ? `>=${interval}s` : options.timerange.interval,
|
||||
},
|
||||
};
|
||||
const optionsWithInterval = options.forceInterval
|
||||
? options
|
||||
: {
|
||||
...options,
|
||||
timerange: {
|
||||
...options.timerange,
|
||||
interval: interval ? `>=${interval}s` : options.timerange.interval,
|
||||
},
|
||||
};
|
||||
|
||||
const metricsApiOptions = convertRequestToMetricsAPIOptions(optionsWithInterval);
|
||||
const metricsApiResponse = await query(client, metricsApiOptions);
|
||||
const totalGroupings = await queryTotalGroupings(client, metricsApiOptions);
|
||||
const hasGroupBy =
|
||||
Array.isArray(metricsApiOptions.groupBy) && metricsApiOptions.groupBy.length > 0;
|
||||
const metricsApiOptions = convertRequestToMetricsAPIOptions(optionsWithInterval);
|
||||
const metricsApiResponse = await query(client, metricsApiOptions);
|
||||
const totalGroupings = await queryTotalGroupings(client, metricsApiOptions);
|
||||
const hasGroupBy =
|
||||
Array.isArray(metricsApiOptions.groupBy) && metricsApiOptions.groupBy.length > 0;
|
||||
|
||||
const pageInfo: MetricsExplorerPageInfo = {
|
||||
total: totalGroupings,
|
||||
afterKey: null,
|
||||
};
|
||||
const pageInfo: MetricsExplorerPageInfo = {
|
||||
total: totalGroupings,
|
||||
afterKey: null,
|
||||
};
|
||||
|
||||
if (metricsApiResponse.info.afterKey) {
|
||||
pageInfo.afterKey = metricsApiResponse.info.afterKey;
|
||||
}
|
||||
|
||||
// If we have a groupBy but there are ZERO groupings returned then we need to
|
||||
// return an empty array. Otherwise we transform the series to match the current schema.
|
||||
const series =
|
||||
hasGroupBy && totalGroupings === 0
|
||||
? []
|
||||
: metricsApiResponse.series.map(transformSeries(hasGroupBy));
|
||||
|
||||
return response.ok({
|
||||
body: metricsExplorerResponseRT.encode({ series, pageInfo }),
|
||||
});
|
||||
} catch (error) {
|
||||
return response.internalError({
|
||||
body: error.message,
|
||||
});
|
||||
if (metricsApiResponse.info.afterKey) {
|
||||
pageInfo.afterKey = metricsApiResponse.info.afterKey;
|
||||
}
|
||||
|
||||
// If we have a groupBy but there are ZERO groupings returned then we need to
|
||||
// return an empty array. Otherwise we transform the series to match the current schema.
|
||||
const series =
|
||||
hasGroupBy && totalGroupings === 0
|
||||
? []
|
||||
: metricsApiResponse.series.map(transformSeries(hasGroupBy));
|
||||
|
||||
return response.ok({
|
||||
body: metricsExplorerResponseRT.encode({ series, pageInfo }),
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
||||
|
|
|
@ -34,38 +34,32 @@ export const initNodeDetailsRoute = (libs: InfraBackendLibs) => {
|
|||
},
|
||||
},
|
||||
async (requestContext, request, response) => {
|
||||
try {
|
||||
const { nodeId, cloudId, nodeType, metrics, timerange, sourceId } = pipe(
|
||||
NodeDetailsRequestRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
const source = await libs.sources.getSourceConfiguration(
|
||||
requestContext.core.savedObjects.client,
|
||||
sourceId
|
||||
);
|
||||
const { nodeId, cloudId, nodeType, metrics, timerange, sourceId } = pipe(
|
||||
NodeDetailsRequestRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
const source = await libs.sources.getSourceConfiguration(
|
||||
requestContext.core.savedObjects.client,
|
||||
sourceId
|
||||
);
|
||||
|
||||
UsageCollector.countNode(nodeType);
|
||||
UsageCollector.countNode(nodeType);
|
||||
|
||||
const options: InfraMetricsRequestOptions = {
|
||||
nodeIds: {
|
||||
nodeId,
|
||||
cloudId,
|
||||
},
|
||||
nodeType,
|
||||
sourceConfiguration: source.configuration,
|
||||
metrics,
|
||||
timerange,
|
||||
};
|
||||
return response.ok({
|
||||
body: NodeDetailsMetricDataResponseRT.encode({
|
||||
metrics: await libs.metrics.getMetrics(requestContext, options, request),
|
||||
}),
|
||||
});
|
||||
} catch (error) {
|
||||
return response.internalError({
|
||||
body: error.message,
|
||||
});
|
||||
}
|
||||
const options: InfraMetricsRequestOptions = {
|
||||
nodeIds: {
|
||||
nodeId,
|
||||
cloudId,
|
||||
},
|
||||
nodeType,
|
||||
sourceConfiguration: source.configuration,
|
||||
metrics,
|
||||
timerange,
|
||||
};
|
||||
return response.ok({
|
||||
body: NodeDetailsMetricDataResponseRT.encode({
|
||||
metrics: await libs.metrics.getMetrics(requestContext, options, request),
|
||||
}),
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
||||
|
|
|
@ -36,77 +36,71 @@ export const initOverviewRoute = (libs: InfraBackendLibs) => {
|
|||
},
|
||||
},
|
||||
async (requestContext, request, response) => {
|
||||
try {
|
||||
const overviewRequest = pipe(
|
||||
OverviewRequestRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
const overviewRequest = pipe(
|
||||
OverviewRequestRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
|
||||
const client = createSearchClient(requestContext, framework);
|
||||
const source = await libs.sources.getSourceConfiguration(
|
||||
requestContext.core.savedObjects.client,
|
||||
overviewRequest.sourceId
|
||||
);
|
||||
const client = createSearchClient(requestContext, framework);
|
||||
const source = await libs.sources.getSourceConfiguration(
|
||||
requestContext.core.savedObjects.client,
|
||||
overviewRequest.sourceId
|
||||
);
|
||||
|
||||
const inventoryModelFields = findInventoryFields('host', source.configuration.fields);
|
||||
const inventoryModelFields = findInventoryFields('host', source.configuration.fields);
|
||||
|
||||
const params = {
|
||||
index: source.configuration.metricAlias,
|
||||
body: {
|
||||
query: {
|
||||
range: {
|
||||
[source.configuration.fields.timestamp]: {
|
||||
gte: overviewRequest.timerange.from,
|
||||
lte: overviewRequest.timerange.to,
|
||||
format: 'epoch_millis',
|
||||
},
|
||||
},
|
||||
},
|
||||
aggs: {
|
||||
hosts: {
|
||||
cardinality: {
|
||||
field: inventoryModelFields.id,
|
||||
},
|
||||
},
|
||||
cpu: {
|
||||
avg: {
|
||||
field: 'system.cpu.total.norm.pct',
|
||||
},
|
||||
},
|
||||
memory: {
|
||||
avg: {
|
||||
field: 'system.memory.actual.used.pct',
|
||||
},
|
||||
const params = {
|
||||
index: source.configuration.metricAlias,
|
||||
body: {
|
||||
query: {
|
||||
range: {
|
||||
[source.configuration.fields.timestamp]: {
|
||||
gte: overviewRequest.timerange.from,
|
||||
lte: overviewRequest.timerange.to,
|
||||
format: 'epoch_millis',
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const esResponse = await client<{}, OverviewESAggResponse>(params);
|
||||
|
||||
return response.ok({
|
||||
body: {
|
||||
stats: {
|
||||
hosts: {
|
||||
type: 'number',
|
||||
value: esResponse.aggregations?.hosts.value ?? 0,
|
||||
aggs: {
|
||||
hosts: {
|
||||
cardinality: {
|
||||
field: inventoryModelFields.id,
|
||||
},
|
||||
cpu: {
|
||||
type: 'percent',
|
||||
value: esResponse.aggregations?.cpu.value ?? 0,
|
||||
},
|
||||
cpu: {
|
||||
avg: {
|
||||
field: 'system.cpu.total.norm.pct',
|
||||
},
|
||||
memory: {
|
||||
type: 'percent',
|
||||
value: esResponse.aggregations?.memory.value ?? 0,
|
||||
},
|
||||
memory: {
|
||||
avg: {
|
||||
field: 'system.memory.actual.used.pct',
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
return response.internalError({
|
||||
body: error.message,
|
||||
});
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
const esResponse = await client<{}, OverviewESAggResponse>(params);
|
||||
|
||||
return response.ok({
|
||||
body: {
|
||||
stats: {
|
||||
hosts: {
|
||||
type: 'number',
|
||||
value: esResponse.aggregations?.hosts.value ?? 0,
|
||||
},
|
||||
cpu: {
|
||||
type: 'percent',
|
||||
value: esResponse.aggregations?.cpu.value ?? 0,
|
||||
},
|
||||
memory: {
|
||||
type: 'percent',
|
||||
value: esResponse.aggregations?.memory.value ?? 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
||||
|
|
|
@ -35,23 +35,17 @@ export const initProcessListRoute = (libs: InfraBackendLibs) => {
|
|||
},
|
||||
},
|
||||
async (requestContext, request, response) => {
|
||||
try {
|
||||
const options = pipe(
|
||||
ProcessListAPIRequestRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
const options = pipe(
|
||||
ProcessListAPIRequestRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
|
||||
const client = createSearchClient(requestContext, framework);
|
||||
const processListResponse = await getProcessList(client, options);
|
||||
const client = createSearchClient(requestContext, framework);
|
||||
const processListResponse = await getProcessList(client, options);
|
||||
|
||||
return response.ok({
|
||||
body: ProcessListAPIResponseRT.encode(processListResponse),
|
||||
});
|
||||
} catch (error) {
|
||||
return response.internalError({
|
||||
body: error.message,
|
||||
});
|
||||
}
|
||||
return response.ok({
|
||||
body: ProcessListAPIResponseRT.encode(processListResponse),
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
|
@ -64,23 +58,17 @@ export const initProcessListRoute = (libs: InfraBackendLibs) => {
|
|||
},
|
||||
},
|
||||
async (requestContext, request, response) => {
|
||||
try {
|
||||
const options = pipe(
|
||||
ProcessListAPIChartRequestRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
const options = pipe(
|
||||
ProcessListAPIChartRequestRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
|
||||
const client = createSearchClient(requestContext, framework);
|
||||
const processListResponse = await getProcessListChart(client, options);
|
||||
const client = createSearchClient(requestContext, framework);
|
||||
const processListResponse = await getProcessListChart(client, options);
|
||||
|
||||
return response.ok({
|
||||
body: ProcessListAPIChartResponseRT.encode(processListResponse),
|
||||
});
|
||||
} catch (error) {
|
||||
return response.internalError({
|
||||
body: error.message,
|
||||
});
|
||||
}
|
||||
return response.ok({
|
||||
body: ProcessListAPIChartResponseRT.encode(processListResponse),
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
||||
|
|
|
@ -31,29 +31,23 @@ export const initSnapshotRoute = (libs: InfraBackendLibs) => {
|
|||
},
|
||||
},
|
||||
async (requestContext, request, response) => {
|
||||
try {
|
||||
const snapshotRequest = pipe(
|
||||
SnapshotRequestRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
const snapshotRequest = pipe(
|
||||
SnapshotRequestRT.decode(request.body),
|
||||
fold(throwErrors(Boom.badRequest), identity)
|
||||
);
|
||||
|
||||
const source = await libs.sources.getSourceConfiguration(
|
||||
requestContext.core.savedObjects.client,
|
||||
snapshotRequest.sourceId
|
||||
);
|
||||
const source = await libs.sources.getSourceConfiguration(
|
||||
requestContext.core.savedObjects.client,
|
||||
snapshotRequest.sourceId
|
||||
);
|
||||
|
||||
UsageCollector.countNode(snapshotRequest.nodeType);
|
||||
const client = createSearchClient(requestContext, framework);
|
||||
const snapshotResponse = await getNodes(client, snapshotRequest, source);
|
||||
UsageCollector.countNode(snapshotRequest.nodeType);
|
||||
const client = createSearchClient(requestContext, framework);
|
||||
const snapshotResponse = await getNodes(client, snapshotRequest, source);
|
||||
|
||||
return response.ok({
|
||||
body: SnapshotNodeResponseRT.encode(snapshotResponse),
|
||||
});
|
||||
} catch (error) {
|
||||
return response.internalError({
|
||||
body: error.message,
|
||||
});
|
||||
}
|
||||
return response.ok({
|
||||
body: SnapshotNodeResponseRT.encode(snapshotResponse),
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
||||
|
|
|
@ -44,34 +44,28 @@ export const initSourceRoute = (libs: InfraBackendLibs) => {
|
|||
},
|
||||
},
|
||||
async (requestContext, request, response) => {
|
||||
try {
|
||||
const { type, sourceId } = request.params;
|
||||
const { type, sourceId } = request.params;
|
||||
|
||||
const [source, logIndexStatus, metricIndicesExist, indexFields] = await Promise.all([
|
||||
libs.sources.getSourceConfiguration(requestContext.core.savedObjects.client, sourceId),
|
||||
libs.sourceStatus.getLogIndexStatus(requestContext, sourceId),
|
||||
libs.sourceStatus.hasMetricIndices(requestContext, sourceId),
|
||||
libs.fields.getFields(requestContext, sourceId, typeToInfraIndexType(type)),
|
||||
]);
|
||||
const [source, logIndexStatus, metricIndicesExist, indexFields] = await Promise.all([
|
||||
libs.sources.getSourceConfiguration(requestContext.core.savedObjects.client, sourceId),
|
||||
libs.sourceStatus.getLogIndexStatus(requestContext, sourceId),
|
||||
libs.sourceStatus.hasMetricIndices(requestContext, sourceId),
|
||||
libs.fields.getFields(requestContext, sourceId, typeToInfraIndexType(type)),
|
||||
]);
|
||||
|
||||
if (!source) {
|
||||
return response.notFound();
|
||||
}
|
||||
|
||||
const status: InfraSourceStatus = {
|
||||
logIndicesExist: logIndexStatus !== 'missing',
|
||||
metricIndicesExist,
|
||||
indexFields,
|
||||
};
|
||||
|
||||
return response.ok({
|
||||
body: SourceResponseRuntimeType.encode({ source: { ...source, status } }),
|
||||
});
|
||||
} catch (error) {
|
||||
return response.internalError({
|
||||
body: error.message,
|
||||
});
|
||||
if (!source) {
|
||||
return response.notFound();
|
||||
}
|
||||
|
||||
const status: InfraSourceStatus = {
|
||||
logIndicesExist: logIndexStatus !== 'missing',
|
||||
metricIndicesExist,
|
||||
indexFields,
|
||||
};
|
||||
|
||||
return response.ok({
|
||||
body: SourceResponseRuntimeType.encode({ source: { ...source, status } }),
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
|
@ -169,26 +163,20 @@ export const initSourceRoute = (libs: InfraBackendLibs) => {
|
|||
},
|
||||
},
|
||||
async (requestContext, request, response) => {
|
||||
try {
|
||||
const { type, sourceId } = request.params;
|
||||
const { type, sourceId } = request.params;
|
||||
|
||||
const client = createSearchClient(requestContext, framework);
|
||||
const source = await libs.sources.getSourceConfiguration(
|
||||
requestContext.core.savedObjects.client,
|
||||
sourceId
|
||||
);
|
||||
const indexPattern =
|
||||
type === 'metrics' ? source.configuration.metricAlias : source.configuration.logAlias;
|
||||
const results = await hasData(indexPattern, client);
|
||||
const client = createSearchClient(requestContext, framework);
|
||||
const source = await libs.sources.getSourceConfiguration(
|
||||
requestContext.core.savedObjects.client,
|
||||
sourceId
|
||||
);
|
||||
const indexPattern =
|
||||
type === 'metrics' ? source.configuration.metricAlias : source.configuration.logAlias;
|
||||
const results = await hasData(indexPattern, client);
|
||||
|
||||
return response.ok({
|
||||
body: { hasData: results },
|
||||
});
|
||||
} catch (error) {
|
||||
return response.internalError({
|
||||
body: error.message,
|
||||
});
|
||||
}
|
||||
return response.ok({
|
||||
body: { hasData: results },
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
||||
|
|
|
@ -83,7 +83,7 @@ export const registerCreateRoute = ({
|
|||
});
|
||||
}
|
||||
|
||||
return res.internalError({ body: error });
|
||||
throw error;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -51,7 +51,7 @@ export const registerDocumentsRoute = ({
|
|||
});
|
||||
}
|
||||
|
||||
return res.internalError({ body: error });
|
||||
throw error;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -44,7 +44,7 @@ export const registerGetRoutes = ({
|
|||
});
|
||||
}
|
||||
|
||||
return res.internalError({ body: error });
|
||||
throw error;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
@ -78,7 +78,7 @@ export const registerGetRoutes = ({
|
|||
});
|
||||
}
|
||||
|
||||
return res.internalError({ body: error });
|
||||
throw error;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -44,28 +44,24 @@ export const registerPrivilegesRoute = ({ license, router, config }: RouteDepend
|
|||
},
|
||||
} = ctx;
|
||||
|
||||
try {
|
||||
const { has_all_requested: hasAllPrivileges, cluster } = await client.callAsCurrentUser(
|
||||
'transport.request',
|
||||
{
|
||||
path: '/_security/user/_has_privileges',
|
||||
method: 'POST',
|
||||
body: {
|
||||
cluster: APP_CLUSTER_REQUIRED_PRIVILEGES,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
if (!hasAllPrivileges) {
|
||||
privilegesResult.missingPrivileges.cluster = extractMissingPrivileges(cluster);
|
||||
const { has_all_requested: hasAllPrivileges, cluster } = await client.callAsCurrentUser(
|
||||
'transport.request',
|
||||
{
|
||||
path: '/_security/user/_has_privileges',
|
||||
method: 'POST',
|
||||
body: {
|
||||
cluster: APP_CLUSTER_REQUIRED_PRIVILEGES,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
privilegesResult.hasAllPrivileges = hasAllPrivileges;
|
||||
|
||||
return res.ok({ body: privilegesResult });
|
||||
} catch (e) {
|
||||
return res.internalError({ body: e });
|
||||
if (!hasAllPrivileges) {
|
||||
privilegesResult.missingPrivileges.cluster = extractMissingPrivileges(cluster);
|
||||
}
|
||||
|
||||
privilegesResult.hasAllPrivileges = hasAllPrivileges;
|
||||
|
||||
return res.ok({ body: privilegesResult });
|
||||
})
|
||||
);
|
||||
};
|
||||
|
|
|
@ -52,7 +52,7 @@ export const registerSimulateRoute = ({
|
|||
});
|
||||
}
|
||||
|
||||
return res.internalError({ body: error });
|
||||
throw error;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -65,7 +65,7 @@ export const registerUpdateRoute = ({
|
|||
});
|
||||
}
|
||||
|
||||
return res.internalError({ body: error });
|
||||
throw error;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -78,11 +78,9 @@ export async function existingFieldsRoute(setup: CoreSetup<PluginStartContract>,
|
|||
if (e.output.statusCode === 404) {
|
||||
return res.notFound({ body: e.output.payload.message });
|
||||
}
|
||||
return res.internalError({ body: e.output.payload.message });
|
||||
throw new Error(e.output.payload.message);
|
||||
} else {
|
||||
return res.internalError({
|
||||
body: Boom.internal(e.message || e.name),
|
||||
});
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
import Boom from '@hapi/boom';
|
||||
import { errors } from '@elastic/elasticsearch';
|
||||
import DateMath from '@elastic/datemath';
|
||||
import { schema } from '@kbn/config-schema';
|
||||
|
@ -122,11 +121,9 @@ export async function initFieldsRoute(setup: CoreSetup<PluginStartContract>) {
|
|||
if (e.output.statusCode === 404) {
|
||||
return res.notFound();
|
||||
}
|
||||
return res.internalError(e.output.message);
|
||||
throw new Error(e.output.message);
|
||||
} else {
|
||||
return res.internalError({
|
||||
body: Boom.internal(e.message || e.name),
|
||||
});
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
import Boom from '@hapi/boom';
|
||||
import { errors } from '@elastic/elasticsearch';
|
||||
import { CoreSetup } from 'src/core/server';
|
||||
import { schema } from '@kbn/config-schema';
|
||||
|
@ -84,11 +83,9 @@ export async function initLensUsageRoute(setup: CoreSetup<PluginStartContract>)
|
|||
if (e.output.statusCode === 404) {
|
||||
return res.notFound();
|
||||
}
|
||||
return res.internalError(e.output.message);
|
||||
throw new Error(e.output.message);
|
||||
} else {
|
||||
return res.internalError({
|
||||
body: Boom.internal(e.message || e.name),
|
||||
});
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,18 +23,14 @@ export function registerLicenseRoute({ router, plugins: { licensing } }: RouteDe
|
|||
},
|
||||
async (ctx, req, res) => {
|
||||
const { callAsCurrentUser } = ctx.core.elasticsearch.legacy.client;
|
||||
try {
|
||||
return res.ok({
|
||||
body: await putLicense({
|
||||
acknowledge: Boolean(req.query.acknowledge),
|
||||
callAsCurrentUser,
|
||||
licensing,
|
||||
license: req.body,
|
||||
}),
|
||||
});
|
||||
} catch (e) {
|
||||
return res.internalError({ body: e });
|
||||
}
|
||||
return res.ok({
|
||||
body: await putLicense({
|
||||
acknowledge: Boolean(req.query.acknowledge),
|
||||
callAsCurrentUser,
|
||||
licensing,
|
||||
license: req.body,
|
||||
}),
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -16,12 +16,8 @@ export function registerPermissionsRoute({
|
|||
router.post({ path: addBasePath('/permissions'), validate: false }, async (ctx, req, res) => {
|
||||
const { callAsCurrentUser } = ctx.core.elasticsearch.legacy.client;
|
||||
|
||||
try {
|
||||
return res.ok({
|
||||
body: await getPermissions({ callAsCurrentUser, isSecurityEnabled }),
|
||||
});
|
||||
} catch (e) {
|
||||
return res.internalError({ body: e });
|
||||
}
|
||||
return res.ok({
|
||||
body: await getPermissions({ callAsCurrentUser, isSecurityEnabled }),
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -18,17 +18,13 @@ export function registerStartBasicRoute({ router, plugins: { licensing } }: Rout
|
|||
},
|
||||
async (ctx, req, res) => {
|
||||
const { callAsCurrentUser } = ctx.core.elasticsearch.legacy.client;
|
||||
try {
|
||||
return res.ok({
|
||||
body: await startBasic({
|
||||
acknowledge: Boolean(req.query.acknowledge),
|
||||
callAsCurrentUser,
|
||||
licensing,
|
||||
}),
|
||||
});
|
||||
} catch (e) {
|
||||
return res.internalError({ body: e });
|
||||
}
|
||||
return res.ok({
|
||||
body: await startBasic({
|
||||
acknowledge: Boolean(req.query.acknowledge),
|
||||
callAsCurrentUser,
|
||||
licensing,
|
||||
}),
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -12,21 +12,13 @@ import { addBasePath } from '../../helpers';
|
|||
export function registerStartTrialRoutes({ router, plugins: { licensing } }: RouteDependencies) {
|
||||
router.get({ path: addBasePath('/start_trial'), validate: false }, async (ctx, req, res) => {
|
||||
const { callAsCurrentUser } = ctx.core.elasticsearch.legacy.client;
|
||||
try {
|
||||
return res.ok({ body: await canStartTrial(callAsCurrentUser) });
|
||||
} catch (e) {
|
||||
return res.internalError({ body: e });
|
||||
}
|
||||
return res.ok({ body: await canStartTrial(callAsCurrentUser) });
|
||||
});
|
||||
|
||||
router.post({ path: addBasePath('/start_trial'), validate: false }, async (ctx, req, res) => {
|
||||
const { callAsCurrentUser } = ctx.core.elasticsearch.legacy.client;
|
||||
try {
|
||||
return res.ok({
|
||||
body: await startTrial({ callAsCurrentUser, licensing }),
|
||||
});
|
||||
} catch (e) {
|
||||
return res.internalError({ body: e });
|
||||
}
|
||||
return res.ok({
|
||||
body: await startTrial({ callAsCurrentUser, licensing }),
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ export function registerClusterLoadRoute(router: LogstashPluginRouter) {
|
|||
if (err.status === 403) {
|
||||
return response.ok();
|
||||
}
|
||||
return response.internalError();
|
||||
throw err;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -368,7 +368,7 @@ export class MonitoringPlugin
|
|||
if (Boom.isBoom(err) || statusCode !== 500) {
|
||||
return res.customError({ statusCode, body: err });
|
||||
}
|
||||
return res.internalError(wrapError(err));
|
||||
throw wrapError(err).body;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -88,7 +88,7 @@ export const register = (deps: RouteDependencies): void => {
|
|||
if (isEsError(error)) {
|
||||
return response.customError({ statusCode: error.statusCode, body: error });
|
||||
}
|
||||
return response.internalError({ body: error });
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
deps.router.post(
|
||||
|
|
|
@ -95,7 +95,7 @@ export const register = (deps: RouteDependencies): void => {
|
|||
if (isEsError(error)) {
|
||||
return response.customError({ statusCode: error.statusCode, body: error });
|
||||
}
|
||||
return response.internalError({ body: error });
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -132,7 +132,7 @@ export const register = (deps: RouteDependencies): void => {
|
|||
if (isEsError(error)) {
|
||||
return response.customError({ statusCode: error.statusCode, body: error });
|
||||
}
|
||||
return response.internalError({ body: error });
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -78,10 +78,16 @@ describe('GET remote clusters', () => {
|
|||
const mockContext = xpackMocks.createRequestHandlerContext();
|
||||
mockContext.core.elasticsearch.legacy.client = mockScopedClusterClient;
|
||||
|
||||
const response = await handler(mockContext, mockRequest, kibanaResponseFactory);
|
||||
if (asserts.statusCode === 500) {
|
||||
await expect(handler(mockContext, mockRequest, kibanaResponseFactory)).rejects.toThrowError(
|
||||
asserts.result as Error
|
||||
);
|
||||
} else {
|
||||
const response = await handler(mockContext, mockRequest, kibanaResponseFactory);
|
||||
|
||||
expect(response.status).toBe(asserts.statusCode);
|
||||
expect(response.payload).toEqual(asserts.result);
|
||||
expect(response.status).toBe(asserts.statusCode);
|
||||
expect(response.payload).toEqual(asserts.result);
|
||||
}
|
||||
|
||||
if (Array.isArray(asserts.apiArguments)) {
|
||||
for (const apiArguments of asserts.apiArguments) {
|
||||
|
|
|
@ -63,7 +63,7 @@ export const register = (deps: RouteDependencies): void => {
|
|||
if (isEsError(error)) {
|
||||
return response.customError({ statusCode: error.statusCode, body: error });
|
||||
}
|
||||
return response.internalError({ body: error });
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -100,7 +100,7 @@ export const register = (deps: RouteDependencies): void => {
|
|||
if (isEsError(error)) {
|
||||
return response.customError({ statusCode: error.statusCode, body: error });
|
||||
}
|
||||
return response.internalError({ body: error });
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -34,7 +34,7 @@ export const registerGetRoute = ({
|
|||
if (isEsError(err)) {
|
||||
return response.customError({ statusCode: err.statusCode, body: err });
|
||||
}
|
||||
return response.internalError({ body: err });
|
||||
throw err;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -136,7 +136,7 @@ export const registerValidateIndexPatternRoute = ({
|
|||
return response.customError({ statusCode: err.statusCode, body: err });
|
||||
}
|
||||
|
||||
return response.internalError({ body: err });
|
||||
throw err;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -43,7 +43,7 @@ export const registerCreateRoute = ({
|
|||
if (isEsError(err)) {
|
||||
return response.customError({ statusCode: err.statusCode, body: err });
|
||||
}
|
||||
return response.internalError({ body: err });
|
||||
throw err;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -45,7 +45,7 @@ export const registerDeleteRoute = ({
|
|||
if (isEsError(err)) {
|
||||
return response.customError({ statusCode: err.statusCode, body: err });
|
||||
}
|
||||
return response.internalError({ body: err });
|
||||
throw err;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -26,7 +26,7 @@ export const registerGetRoute = ({
|
|||
if (isEsError(err)) {
|
||||
return response.customError({ statusCode: err.statusCode, body: err });
|
||||
}
|
||||
return response.internalError({ body: err });
|
||||
throw err;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue