Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[APM] Create infraMetricsClient #141735

Merged

Conversation

MiriamAparicio
Copy link
Contributor

Closes #140924

start: number;
end: number;
}) => {
const response = await esClient.search<unknown, { hostNames: Aggs }>({
index: [index],
const response = await infraMetricsClient.search<
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type of the response failed because of the type of response from the infraMetricsClient
Screenshot 2022-09-26 at 09 31 01
Screenshot 2022-09-26 at 09 30 30

Screenshot 2022-09-26 at 09 30 44

Comment on lines 79 to 82
infraMetricsClient: await createInfraMetricsClient({
infraPlugin: plugins.infra,
context,
}),
Copy link
Member

@sorenlouv sorenlouv Sep 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand why you added it here but the idea is to split up setupRequest in separate parts, so we don't have to initialise every client when we just need one of them.

So please remove it from here and invoke it separately

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I completely forgot about that issue, it's almost a year old, should we prioritize it and added to 8.6?
I will add a different setupRequest for the infraMetricsClient for now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love to see it split up. Your are welcome to take a stab at it after the test plan is done.

context: ApmPluginRequestHandlerContext;
}) {
return {
search: async (opts: ESSearchRequest) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

index is required by default, so you need to remove it:

Suggested change
search: async (opts: ESSearchRequest) => {
search: async <TParams extends Omit<ESSearchRequest, 'index'>>(
opts: TParams

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dgieselaar What else is needed to ensure that aggregations are inferred correctly? Do we need to set a manual return type like InferSearchResponseOf<TDocument, TParams> ?

Copy link
Contributor Author

@MiriamAparicio MiriamAparicio Sep 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the params type, but I still have the same errors, my errors are in the response type from the client

@MiriamAparicio MiriamAparicio changed the title Create infraMetricsClient [APM] Create infraMetricsClient Sep 27, 2022
}),
};
});
}
Copy link
Member

@sorenlouv sorenlouv Sep 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this function? Why not call createInfraMetricsClient directly?

@@ -29,12 +30,9 @@ const infrastructureRoute = createApmServerRoute({
podNames: string[];
}> => {
const setup = await setupRequest(resources);
const setupInfraMetrics = await setupInfraMetricsRequest(resources);
Copy link
Member

@sorenlouv sorenlouv Sep 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we not calling createInfraMetricsClient directly?

Suggested change
const setupInfraMetrics = await setupInfraMetricsRequest(resources);
const infraMetricsClient = await createInfraMetricsClient(resources);

Comment on lines 42 to 44
if (!infraMetricsClient) {
return undefined;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this check necessary? Isn't infraMetricsClient required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it was checking for the index before, but if the client is created we don't need to check it

...opts,
};

return esClient.asCurrentUser.search(searchParams);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try this (credits goes to @dgieselaar)

Suggested change
return esClient.asCurrentUser.search(searchParams);
const res = esClient.asCurrentUser.search(
searchParams
) as unknown as Promise<{
body: any;
}>;
return unwrapEsResponse(res);

Comment on lines 28 to 30
search: async <TParams extends Omit<ESSearchRequest, 'index'>>(
opts: TParams
) => {
Copy link
Member

@sorenlouv sorenlouv Sep 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
search: async <TParams extends Omit<ESSearchRequest, 'index'>>(
opts: TParams
) => {
async search<TDocument, TParams extends InfraMetricsSearchParams>(
opts: TParams
): Promise<InferSearchResponseOf<TDocument, TParams>> {

Where InfraMetricsSearchParams is:

type InfraMetricsSearchParams = Omit<ESSearchRequest, 'index'> & {
  body: {
    size: number;
    track_total_hits: boolean | number;
  };
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InfraMetricsSearchParams is needed to make size and track_total_hits required.

Comment on lines 64 to 67
const response = await infraMetricsClient.search<
unknown,
ResponseAggregations
>({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be needed

Suggested change
const response = await infraMetricsClient.search<
unknown,
ResponseAggregations
>({
const response = await infraMetricsClient.search({

Comment on lines 68 to 69
_source: [KUBERNETES, CONTAINER],
size: 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: We are not using source (size: 0) so there's no need to specify _source: [KUBERNETES, CONTAINER].


const {
const { plugins, context } = resources;
const infraMetricsClient = await createInfraMetricsClient({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to await createInfraMetricsClient?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const infraMetricsClient = await createInfraMetricsClient({
const infraMetricsClient = createInfraMetricsClient({


export type InfraClient = Awaited<ReturnType<typeof createInfraMetricsClient>>;

export async function createInfraMetricsClient({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be async afaict

Suggested change
export async function createInfraMetricsClient({
export function createInfraMetricsClient({

@@ -19,6 +17,7 @@ import {
KUBERNETES_REPLICASET_NAME,
KUBERNETES_DEPLOYMENT_NAME,
} from '../../../common/elasticsearch_fieldnames';
import { InfraClient } from '../../lib/helpers/create_es_client/create_infra_metrics_client/create_infra_metrics_client';

type ServiceOverviewContainerMetadataDetails =
| {
Copy link
Member

@sorenlouv sorenlouv Sep 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be inferred from the query and not explicitly typed

@@ -19,6 +17,7 @@ import {
KUBERNETES_REPLICASET_NAME,
KUBERNETES_DEPLOYMENT_NAME,
} from '../../../common/elasticsearch_fieldnames';
import { InfraClient } from '../../lib/helpers/create_es_client/create_infra_metrics_client/create_infra_metrics_client';
Copy link
Member

@sorenlouv sorenlouv Sep 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please rename this.

Suggested change
import { InfraClient } from '../../lib/helpers/create_es_client/create_infra_metrics_client/create_infra_metrics_client';
import { InfraMetricsClient } from '../../lib/helpers/create_es_client/create_infra_metrics_client/create_infra_metrics_client';

@MiriamAparicio MiriamAparicio added Team:APM - DEPRECATED Use Team:obs-ux-infra_services. release_note:skip Skip the PR/issue when compiling release notes backport:skip This commit does not require backporting labels Sep 28, 2022
@MiriamAparicio MiriamAparicio marked this pull request as ready for review September 28, 2022 14:23
@MiriamAparicio MiriamAparicio requested a review from a team as a code owner September 28, 2022 14:23
@elasticmachine
Copy link
Contributor

Pinging @elastic/apm-ui (Team:APM)

Comment on lines 278 to 282
const { plugins, context } = resources;
const infraMetricsClient = await createInfraMetricsClient({
infraPlugin: plugins.infra,
context,
});
Copy link
Member

@sorenlouv sorenlouv Sep 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can simplify this a bit if we just pass resources as arg:

Suggested change
const { plugins, context } = resources;
const infraMetricsClient = await createInfraMetricsClient({
infraPlugin: plugins.infra,
context,
});
const infraMetricsClient = await createInfraMetricsClient(resources);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I leave like this, infraPlugin is not part of the resources, so I will need to extracted somewhere else

Copy link
Member

@sorenlouv sorenlouv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. Thanks @MiriamAparicio !

@MiriamAparicio MiriamAparicio force-pushed the 140924-infra-metrics-client branch from bd3de94 to f942e61 Compare September 29, 2022 09:19
@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

✅ unchanged

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@MiriamAparicio MiriamAparicio merged commit 4bab191 into elastic:main Sep 29, 2022
@MiriamAparicio MiriamAparicio deleted the 140924-infra-metrics-client branch September 29, 2022 10:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting release_note:skip Skip the PR/issue when compiling release notes Team:APM - DEPRECATED Use Team:obs-ux-infra_services. v8.6.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[APM] Create infraMetricsClient to fetch infra metrics data
4 participants