Skip to content

Conversation

@justinkambic
Copy link
Contributor

@justinkambic justinkambic commented Nov 21, 2019

Summary

Working toward removing legacy elasticsearch client from Uptime backend.

Code is WIP.

Do not review until #51125 is merged.

The biggest change in this patch is that we are getting our elasticsearch client from the request object provided by the Kibana route handler. Before we were storing references to an elasticsearch client that was provided at bootstrap time.

Changes

GraphQL route creation

We were previously passing the request object to our GraphQL resolvers by including it in an options object that was included in the resolver parameters. We're doing essentially the same thing, but now are including the callWithCurrentUser function provided by the request context.

The implementation of this is largely the same in GQL resolvers, except that we're simply passing this client to the adapters instead of the request object itself. Example:

Before:

async getStatesIndexStatus(resolver, {}, { req }): Promise<StatesIndexStatus> {
  return await libs.monitorStates.statesIndexExists(req);
},

After:

async getStatesIndexStatus(_resolver, {}, { callAsCurrentUser }): Promise<StatesIndexStatus> {
  return await libs.monitorStates.statesIndexExists(callAsCurrentUser, undefined);
},

Adapter interfaces

Adapter functions now subscribe to a shared interface. Their first parameter is the elasticsearch client used by the adapter to query ES. The second parameter is either an object containing data needed for the adapter query, or undefined. I might revise this before opening the PR for review to take only a shared object.

This interface can be found here.

Before:

const result = await libs.monitorStates.getSnapshotCount(
  request,
  dateRangeStart,
  dateRangeEnd,
  filters,
  statusFilter
);

After:

const result = await libs.monitorStates.getSnapshotCount(callAsCurrentUser, {
  dateRangeStart,
  dateRangeEnd,
  filters,
  statusFilter,
});

The goal is to eventually extract all these methods out of their current class configuration into pure functions exported from their own modules, which will make them simpler to test and create shorter adapter files.

Reviewing the PR

Primary changes here are architectural - if all tests are passing and you're able to open/run the Uptime app like it would normally work, all should be well. The larger portion of the review should be to ensure that I've preserved existing code quality as much as possible, and that new patterns introduced make sense.

Checklist

Use strikethroughs to remove checklist items you don't feel are applicable to this PR.

For maintainers

@justinkambic justinkambic added v8.0.0 Team:Uptime - DEPRECATED Synthetics & RUM sub-team of Application Observability release_note:skip Skip the PR/issue when compiling release notes v7.6.0 labels Nov 21, 2019
@justinkambic justinkambic self-assigned this Nov 21, 2019
@elasticmachine
Copy link
Contributor

Pinging @elastic/uptime (Team:uptime)

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@justinkambic justinkambic force-pushed the uptime_remove-legacy-es-client branch from b62319c to 84fdd3d Compare December 3, 2019 15:46
@elasticmachine
Copy link
Contributor

💔 Build Failed

@justinkambic justinkambic force-pushed the uptime_remove-legacy-es-client branch from 84fdd3d to 5267e09 Compare December 3, 2019 22:51
@elasticmachine
Copy link
Contributor

💔 Build Failed

Copy link
Contributor

@andrewvc andrewvc left a comment

Choose a reason for hiding this comment

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

Generally looks great, though I did have some questions about some aspects of the code.

Code runs fine in a smoke test locally.

@justinkambic justinkambic requested a review from andrewvc December 9, 2019 23:18
@justinkambic
Copy link
Contributor Author

@elasticmachine merge upstream

Comment on lines 42 to 48
export interface GetSnapshotCountParams {
dateRangeStart: string;
dateRangeEnd: string;
filters?: string | null;
statusFilter?: string | null;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

it's generally makes sense to move all the interfaces to the top of the file, can you move this close to MonitorStatesParams?
And also may be use MonitorStatesParams for snapshot as well, since pagination is optional. Once you do that i guess it will also makes sense to have a bit more generic name Just QueryParams, WDYT?

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 already have that in the first Maps PR, when we merge one we can overwrite this for the other and yes we can just use a generic interface for components that only require these four basic params I agree!

dateRangeEnd,
pagination,
filters,
statusFilter,
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably not related to this PR, but i have always been confused by filters and statusFilter, i mean they always gives my brain a challenge to process the information. "Ok we have filters and then we have statusFilter :) let's think about it , what they means."
i guess we should probably rename statusFilter to monitorStatus or maybe just merge it with filters.or maybe it's fine, it's just me :D

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 should chat about this, I'm realizing maybe @andrewvc and myself have never explained why we have this weirdness. I'd be fine to rename it to something else in a separate PR if we can think of a less confusing way to refer to it.

filterClause: filters && filters !== '' ? JSON.parse(filters) : null,
size: CONTEXT_DEFAULTS.MAX_MONITORS_FOR_SNAPSHOT_COUNT,
statusFilter,
statusFilter: statusFilter || undefined,
Copy link
Contributor

Choose a reason for hiding this comment

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

i wonder why are you explicitly assigning it as undefined, if it's already undefined, i mean that's how short-hand objection notation works.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reason for this: QueryContext requires statusFilter to be string | undefined, but the typing for statusFilter from API requests historically can be string | null | undefined. I didn't want to break any code that might be explicitly testing the value for undefined so I overwrite any falsy value with undefined. We can clean this up by modifying the type provided by the request handler, which I will do.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed this in c0d5187.

value: number;
};
};
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Another unrelated to PR , suggestion is to rename adapter_types file to just types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 this naming convention predates Uptime and was a result of mirroring other new plugins. I think we've done away with many of the conventions, so I am fine with renaming these files to make them less verbose.

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 created elastic/uptime#126

}
}
},
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Again file naming, unrelated to this PR, elasticsearch_pings_adapter.ts should be just elasticsearch_pings, since it's inside adapters folder, so it doesn't make sense to prefix/postfix files. It's a twitter rules as well, when you have already used "lol", "haha" can't be used in the same tweet :P

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lol, I see what you're saying haha.

Addressed in #51403 (comment).

Copy link
Contributor

@shahzad31 shahzad31 left a comment

Choose a reason for hiding this comment

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

i have left some suggestions, those are totally optional.
other PR is good to go, smoke tested and didn't find any issue.

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

History

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

Copy link
Contributor

@andrewvc andrewvc left a comment

Choose a reason for hiding this comment

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

LGTM

@justinkambic justinkambic merged commit b7ff35d into elastic:master Dec 17, 2019
justinkambic added a commit to justinkambic/kibana that referenced this pull request Dec 19, 2019
* Move a REST endpoint and the GQL endpoint to NP routing.

* Delete obsolete REST endpoint.

* Update remaining REST routes to work with NP router.

* Remove obsolete code, update some unit tests.

* Simplify route creation.

* Remove tests of API decommissioned API endpoint.

* Rename domain check.

* Make return shape of index pattern endpoint correspond to required NP resp body.

* Move validate to appropriate level of route definition object for monitor details endpoint.

* Update snapshot count route.

* Fix broken lint rule.

* Move a REST endpoint and the GQL endpoint to NP routing.

* Update remaining REST routes to work with NP router.

* Update remaining REST routes to work with NP router.

* Refactor query functions to accept new es client from request contexts.

* WIP updating framework adapter.

* Refactor remaining routes/resolvers to remove usage of legacy Elasticsearch client.

* Fix broken unit tests.

* Fix incorrect user usage for a REST endpoint.

* Fix some broken imports and types.

* Port monitor details REST endpoint to NP.

* Remove some merge errors.

* Update adapters to take a single options parameter.

* Update broken test files.

* Resolve typescript warnings.

* Update resolver types.

* Change GraphQL interface name for es client.

* Delete unused code and fix incorrect type.

* Rename type for REST endpoint creators.

* Nest message values in body object for invalid response messages.

* Reorganize a file and clean up some types.

* Add wrapper function to reduce boilerplate route code.
justinkambic added a commit that referenced this pull request Dec 19, 2019
* Move a REST endpoint and the GQL endpoint to NP routing.

* Delete obsolete REST endpoint.

* Update remaining REST routes to work with NP router.

* Remove obsolete code, update some unit tests.

* Simplify route creation.

* Remove tests of API decommissioned API endpoint.

* Rename domain check.

* Make return shape of index pattern endpoint correspond to required NP resp body.

* Move validate to appropriate level of route definition object for monitor details endpoint.

* Update snapshot count route.

* Fix broken lint rule.

* Move a REST endpoint and the GQL endpoint to NP routing.

* Update remaining REST routes to work with NP router.

* Update remaining REST routes to work with NP router.

* Refactor query functions to accept new es client from request contexts.

* WIP updating framework adapter.

* Refactor remaining routes/resolvers to remove usage of legacy Elasticsearch client.

* Fix broken unit tests.

* Fix incorrect user usage for a REST endpoint.

* Fix some broken imports and types.

* Port monitor details REST endpoint to NP.

* Remove some merge errors.

* Update adapters to take a single options parameter.

* Update broken test files.

* Resolve typescript warnings.

* Update resolver types.

* Change GraphQL interface name for es client.

* Delete unused code and fix incorrect type.

* Rename type for REST endpoint creators.

* Nest message values in body object for invalid response messages.

* Reorganize a file and clean up some types.

* Add wrapper function to reduce boilerplate route code.
@justinkambic
Copy link
Contributor Author

Backported to:
7.x/7.6.0 c620d11
#53615

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release_note:skip Skip the PR/issue when compiling release notes Team:Uptime - DEPRECATED Synthetics & RUM sub-team of Application Observability v7.6.0 v8.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants