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

Elasticsearch client: no longer default to using meta: true #124488

Merged
merged 87 commits into from
Feb 12, 2022

Conversation

pgayvallet
Copy link
Contributor

@pgayvallet pgayvallet commented Feb 3, 2022

This is not a failed rebase

Summary

Fix #116095

  • Remove usages of the KibanaClient interface in favor of the vanilla Client
  • Stop forcing the meta: true option in our client's transport
  • Adapt all the usages of the client in the whole codebase to handle the new signature and returns
  • Adapt associated tests

What is this about?

The 8.0 version of @elastic/elasticsearch changed the behavior of the client to only return the body by default, unless the meta: true option is specified, in which case it returns a { body, statusCode, headers, warnings } struct as in did in 7.x. This was a very significant breaking change.

Which is why, when we migrated to this version of the client in #113950, we decided to force the old behavior by injecting meta: true to all ES client calls and using our own KibanaClient interface shim-ing the old signatures to reduce the impact of this library upgrade in the initial PR.

This PR is the direct follow up of #113950, and get rid of the KibanaClient interface, switch to using the behavior of a vanilla Client, and adapt the client usages across the whole codebase to properly use the changed API.

See #116095 for more context

New ElasticsearchClient mock

To ease the testing of the ES API, especially the behavior where the return of the client's API differs depending on the meta option, this PR adapts core's ES client mock to add additional mocking functions to ElasticsearchClientMock:

  • mockResponse
  • mockResponseOnce
  • mockResponseImplementation
  • mockResponseImplementationOnce

These methods handle the meta option internally, to either only return the body or the whole response depending on the provided meta option's value.

To codeowners

All usages of the client were, in theory, adapted in this PR.

Note that if some features are not properly covered by FTR tests, some usages may have been missed, as some practices can shallow typescript errors:

  • Record<> based ES responses causing response.body to pass validation
  • Usages of lodash get such as _.get(response, 'body.X.Y')
  • @ts-ignore-error usage on the line of the API calls
  • Custom interfaces on top of Client or ElasticsearchClient (e.g the APM client or ML client)
  • Client calls from javascript (.js) files
  • ...

If you are aware that some of your owned features are not properly test-covered, please focus on those during the review and double check that no client call was missed.

Regarding the test changes, I tried as much as possible to use the new client mock functions to make sure that the returns were correctly of the body or response shapes, but in some cases, such as manual mocks not using core's ES client mock, I couldn't, and had to just change the return values of the mock to return the body instead of the whole response. Improving the tests to properly use core's ES client mock is considered out of scope of this PR and should eventually be performed by the individual code owners as follow-ups, if they think it's necessary.

Checklist

Risk Matrix

Before closing this PR, invite QA, stakeholders, and other developers to identify risks that should be tested prior to the change/feature release.

When forming the risk matrix, consider some of the following examples and how they may potentially impact the change:

Risk Probability Severity Mitigation/Notes
Features relying on direct ES calls and not properly covered by functional tests may be broken Medium High Typescript validation detects most of the signature mismatches, but some specific scenarios can lead to undetected 'failures' by the validation. This PR aims to be merged early in the 8.2 dev cycle to allow detecting such potential problems during the development phase of 8.2

Copy link
Member

@jgowdyelastic jgowdyelastic left a comment

Choose a reason for hiding this comment

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

ML, Transforms and File Upload changes LGTM

): this;
}

const createMockedApi = <
Copy link
Contributor

Choose a reason for hiding this comment

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

We need tests for the mocks now 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, that's right 😅 . will add them

@@ -28,7 +28,7 @@ const methods = [

type MethodName = typeof methods[number];

export type RepositoryEsClient = Pick<ElasticsearchClient, MethodName>;
export type RepositoryEsClient = Pick<ElasticsearchClient, MethodName | 'transport'>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need to provide it in the RepositoryEsClient? Is it possible to migrate from transport to an ES method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We just need it because of the client API definition (unfortunately), we're not using it.

see #124488 (comment)

Removing it causes errors such as

 proc [tsc] src/core/server/saved_objects/service/lib/repository.ts:2210:49 - error TS2769: No overload matches this call.
 proc [tsc]   Overload 1 of 3, '(this: That, params: import("/kibana/node_modules/@elastic/elasticsearch/lib/api/types").GetRequest | import("/kibana/node_modules/@elastic/elasticsearch/lib/api/typesWithBodyKey").GetRequest, options?: TransportRequestOptionsWithOutMeta | undefined): Promise<...>', gave the following error.
 proc [tsc]     The 'this' context of type 'RepositoryEsClient' is not assignable to method's 'this' of type 'That'.
 proc [tsc]   Overload 2 of 3, '(this: That, params: import("/kibana/node_modules/@elastic/elasticsearch/lib/api/types").GetRequest | import("/kibana/node_modules/@elastic/elasticsearch/lib/api/typesWithBodyKey").GetRequest, options?: TransportRequestOptionsWithMeta | undefined): Promise<...>', gave the following error.
 proc [tsc]     The 'this' context of type 'RepositoryEsClient' is not assignable to method's 'this' of type 'That'.
 proc [tsc]   Overload 3 of 3, '(this: That, params: import("/kibana/node_modules/@elastic/elasticsearch/lib/api/types").GetRequest | import("/kibana/node_modules/@elastic/elasticsearch/lib/api/typesWithBodyKey").GetRequest, options?: TransportRequestOptions | undefined): Promise<...>', gave the following error.
 proc [tsc]     The 'this' context of type 'RepositoryEsClient' is not assignable to method's 'this' of type 'That'.
 proc [tsc]
 proc [tsc] 2210     const { body, statusCode, headers } = await this.client.get<SavedObjectsRawDocSource>

Copy link
Contributor

@yuliacech yuliacech left a comment

Choose a reason for hiding this comment

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

Thanks a lot, @pgayvallet!
Deployment Management changes LGTM 🚀
(Logged an unrelated search profiler issue here)

Copy link
Contributor

@joshdover joshdover left a comment

Choose a reason for hiding this comment

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

Fleet changes LGTM. One nit suggestion.

Nice removal of a little boilerplate, and I think it's a good breaking change 🎉 . Wonder if we should rename the asResponse option we have on our public http client in Core to meta to match?

Copy link
Contributor

@FrankHassanabad FrankHassanabad left a comment

Choose a reason for hiding this comment

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

Went through the changes owned by me. 👍 , nothing bad I could find. Thanks.

Copy link
Member

@nickpeihl nickpeihl left a comment

Choose a reason for hiding this comment

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

file_upload and geo_containment changes lgtm!

Copy link
Member

@tsullivan tsullivan left a comment

Choose a reason for hiding this comment

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

Reporting changes LGTM

@tsullivan tsullivan requested a review from a team February 9, 2022 21:05
@weltenwort weltenwort self-requested a review February 10, 2022 10:32
Copy link
Member

@weltenwort weltenwort left a comment

Choose a reason for hiding this comment

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

infra and monitoring changes LGTM, thank you!

Copy link
Contributor

@crob611 crob611 left a comment

Choose a reason for hiding this comment

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

Tested changes to Presentation Team and everything looks and works as expected ✅

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Public APIs missing comments

Total count of every public API that lacks a comment. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats comments for more detailed information.

id before after diff
@kbn/test 208 205 -3

Public APIs missing exports

Total count of every type that is part of your API that should be exported but is not. This will cause broken links in the API documentation system. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats exports for more detailed information.

id before after diff
kibana 942 302 -640
Unknown metric groups

API count

id before after diff
@kbn/test 244 241 -3

ESLint disabled line counts

id before after diff
actions 25 22 -3
apm 86 83 -3
infra 127 126 -1
monitoring 39 31 -8
securitySolution 446 442 -4
total -19

Total ESLint disabled count

id before after diff
actions 30 27 -3
apm 101 98 -3
infra 134 133 -1
monitoring 46 38 -8
securitySolution 513 509 -4
total -19

History

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

@pgayvallet pgayvallet merged commit 6627bd8 into elastic:main Feb 12, 2022
@pgayvallet
Copy link
Contributor Author

A planned, merged on green CI despite having some missing reviews.

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 Feature:elasticsearch release_note:skip Skip the PR/issue when compiling release notes Team:APM All issues that need APM UI Team support Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc Team:Fleet Team label for Observability Data Collection Fleet team technical debt Improvement of the software architecture and operational architecture v8.2.0
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

Ensure compatibility of Client and KibanaClient interfaces