-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Infra] Create typed api client and use it in hosts endpoint (#178214)
closes #175717 closes #175268 ## Summary This PR creates a client to wrap searches against metrics indices. With this change, we are able remove most of the manual ES query typing that uses io-ts in favor of the `InferSearchResponseOf` type. As part of this PR, I refactored the hosts endpoint to use the new client. Other routes will need to be refactored as well. ### How to test - Start a local Kibana instance - Navigate to Infrastructure > Hosts - See if the page loads correctly and sorts by CPU usage desc --------- Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
1 parent
4c4a28e
commit fc7d417
Showing
13 changed files
with
206 additions
and
479 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
53 changes: 53 additions & 0 deletions
53
x-pack/plugins/observability_solution/infra/server/lib/helpers/get_infra_metrics_client.ts
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,53 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import type { ESSearchRequest, InferSearchResponseOf } from '@kbn/es-types'; | ||
import type { KibanaRequest } from '@kbn/core/server'; | ||
import type { InfraPluginRequestHandlerContext } from '../../types'; | ||
import { InfraSources } from '../sources'; | ||
import { KibanaFramework } from '../adapters/framework/kibana_framework_adapter'; | ||
|
||
type RequiredParams = Omit<ESSearchRequest, 'index'> & { | ||
body: { | ||
size: number; | ||
track_total_hits: boolean | number; | ||
}; | ||
}; | ||
|
||
export type InfraMetricsClient = Awaited<ReturnType<typeof getInfraMetricsClient>>; | ||
|
||
export async function getInfraMetricsClient({ | ||
sourceId, | ||
framework, | ||
infraSources, | ||
requestContext, | ||
request, | ||
}: { | ||
sourceId: string; | ||
framework: KibanaFramework; | ||
infraSources: InfraSources; | ||
requestContext: InfraPluginRequestHandlerContext; | ||
request?: KibanaRequest; | ||
}) { | ||
const soClient = (await requestContext.core).savedObjects.getClient(); | ||
const source = await infraSources.getSourceConfiguration(soClient, sourceId); | ||
|
||
return { | ||
search<TDocument, TParams extends RequiredParams>( | ||
searchParams: TParams | ||
): Promise<InferSearchResponseOf<TDocument, TParams>> { | ||
return framework.callWithRequest( | ||
requestContext, | ||
'search', | ||
{ | ||
...searchParams, | ||
index: source.configuration.metricAlias, | ||
}, | ||
request | ||
) as Promise<any>; | ||
}, | ||
}; | ||
} |
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
Oops, something went wrong.