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] Improve router types #83620

Merged
merged 3 commits into from
Nov 18, 2020
Merged

Conversation

dgieselaar
Copy link
Member

Main changes:

  • Use a top-level params runtime type, instead of a plain object with a runtime type for each of path, query and body. This makes our types a little easier to maintain (as we can just use much of io-ts its tooling) and more composable.
  • Use a runtime type to fail on excess keys. Previously we were doing a very simple check on top-level properties, but that doesn't work anymore with the top-level runtime type. Moving it to a runtime type allows us to validate deeply nested keys as well, and test it separately.
  • Combine pathname and method into endpoint. Previously, if we registered two HTTP methods for the same pathname, TypeScript would either fail to resolve the route params or merge them together regardless of which one was being used. For an example, see /api/apm/settings/custom_links, where (according to TypeScript) you can leave out both body and query, or specify both, even though one and only one is required on both the GET and the POST route.

@dgieselaar dgieselaar added Team:APM All issues that need APM UI Team support release_note:skip Skip the PR/issue when compiling release notes v7.11.0 labels Nov 18, 2020
@dgieselaar dgieselaar requested review from a team as code owners November 18, 2020 10:13
@elasticmachine
Copy link
Contributor

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

@botelastic botelastic bot added the Team:Uptime - DEPRECATED Synthetics & RUM sub-team of Application Observability label Nov 18, 2020
@elasticmachine
Copy link
Contributor

Pinging @elastic/uptime (Team:uptime)

// this is similar to t.intersection, but does a deep merge
// instead of a shallow merge

export function merge<A extends t.Mixed, B extends t.Mixed>(
Copy link
Member

Choose a reason for hiding this comment

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

Should it be called deepMerge then?
Btw. I'm surprised this doesn't already exist in the shared typescript utils.

Copy link
Member Author

Choose a reason for hiding this comment

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

it's merge because it uses lodash.merge. I figured deepMerge would be overkill.

@@ -24,8 +24,7 @@ import { APIReturnType } from '../../services/rest/createCallApmApi';
import { units } from '../../style/variables';

export type AnomalyDetectionApiResponse = APIReturnType<
'/api/apm/settings/anomaly-detection',
'GET'
'GET /api/apm/settings/anomaly-detection'
Copy link
Member

Choose a reason for hiding this comment

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

Reads much nicer 👍

@@ -60,7 +59,7 @@ export function AnomalyDetectionSetupLink() {
export function MissingJobsAlert({ environment }: { environment?: string }) {
const { data = DEFAULT_DATA, status } = useFetcher(
(callApmApi) =>
callApmApi({ pathname: `/api/apm/settings/anomaly-detection` }),
callApmApi({ endpoint: `GET /api/apm/settings/anomaly-detection` }),
Copy link
Member

Choose a reason for hiding this comment

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

feels controversial. I like it!


const path = (params.path || {}) as Record<string, any>;
const [method, pathname] = endpoint.split(' ');
Copy link
Member

@sorenlouv sorenlouv Nov 18, 2020

Choose a reason for hiding this comment

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

This will cause problems if the endpoint contains spaces. Something that's theoretically possible but widely frowned upon (for good reasons).
I don't think we need to handle this - just wanted to point it out so I can link to this conversation when this causes a production incident 5 years from now :D

Copy link
Member Author

Choose a reason for hiding this comment

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

I have enough faith in humanity to assume that no one will ever try to register an endpoint with spaces 😅

]),
}),
},
export const dynamicIndexPatternRoute = createRoute({
Copy link
Member

@sorenlouv sorenlouv Nov 18, 2020

Choose a reason for hiding this comment

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

Shouldn't we be passing processorEvent along to getDynamicIndexPattern? If not, will you remove processorEvent from getDynamicIndexPattern and getPatternIndices?

Copy link
Member

Choose a reason for hiding this comment

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

Probably also remove it from here

processorEvent?: ProcessorEvent;

Copy link
Member Author

Choose a reason for hiding this comment

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

I've put it back, we were not using it but looks like it was a bug I introduced in 429805d. Previously, we were using query.processorEvent in setup_request, but we moved it out of setup_request into its own function, which required the query param to be passed explicitly, but looked like that never happened.

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.

Nice improvement, thanks!

@@ -53,7 +53,7 @@ export function ClientMetrics() {
(callApmApi) => {
if (uxQuery) {
return callApmApi({
pathname: '/api/apm/rum/client-metrics',
endpoint: 'GET /api/apm/rum/client-metrics',
Copy link
Contributor

Choose a reason for hiding this comment

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

i am yet to take a look at that apm client contract you have created, will these types continue to work once we move ux code out of apm?

Copy link
Member Author

Choose a reason for hiding this comment

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

You would have to create your own pseudo-router (create_ux_api) and client (callUxApi), but that was already the case, nothing in this PR changes what you need to do I think.

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.

LGTM !!

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
apm 3.1MB 3.1MB +1.7KB

Distributable file count

id before after diff
default 42858 42860 +2

History

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

@dgieselaar dgieselaar merged commit 2a365ff into elastic:master Nov 18, 2020
@dgieselaar dgieselaar deleted the simplify-router-types branch November 18, 2020 18:50
dgieselaar added a commit to dgieselaar/kibana that referenced this pull request Nov 18, 2020
* [APM] Improve router types

* Pass processorEvent param to useDynamicIndexPattern
dgieselaar added a commit that referenced this pull request Nov 18, 2020
* [APM] Improve router types

* Pass processorEvent param to useDynamicIndexPattern
phillipb added a commit to phillipb/kibana that referenced this pull request Nov 19, 2020
… into add-logs-to-node-details

* 'add-logs-to-node-details' of github.com:phillipb/kibana: (87 commits)
  [Maps] Add 'crossed' & 'exited' events to tracking alert (elastic#82463)
  Updating code-owners to use new core/app-services team names (elastic#83731)
  Add Managed label to data streams and a view switch for the table (elastic#83049)
  [Maps] Add query bar inputs to geo threshold alerts tracked points & boundaries (elastic#80871)
  fix(NA): search examples kibana version declaration (elastic#83182)
  Fixed console error, which appears when saving changes in Edit Alert flyout (elastic#83610)
  [Alerting] Add `alert.updatedAt` field to represent date of last user edit (elastic#83578)
  Not resetting server log level if level is defined (elastic#83651)
  disable incremenetal build for legacy tsconfig.json (elastic#82986)
  [Workplace Search] Migrate SourceLogic from ent-search (elastic#83593)
  [Workplace Search] Port Box changes from ent-search (elastic#83675)
  [APM] Improve router types (elastic#83620)
  Bump flat to v4.1.1 (elastic#83647)
  Bump y18n@5 to v5.0.5 (elastic#83644)
  Bump jsonpointer to v4.1.0 (elastic#83641)
  Bump is-my-json-valid to v2.20.5 (elastic#83642)
  [Telemetry] Move Monitoring collection strategy to a collector (elastic#82638)
  Update typescript eslint to v4.8 (elastic#83520)
  [ML] Persist URL state for Anomaly detection jobs using metric function (elastic#83507)
  [ML] Performance improvements to annotations editing in Single Metric Viewer & buttons placement (elastic#83216)
  ...
chrisronline pushed a commit to chrisronline/kibana that referenced this pull request Nov 19, 2020
* [APM] Improve router types

* Pass processorEvent param to useDynamicIndexPattern
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:APM All issues that need APM UI Team support Team:Uptime - DEPRECATED Synthetics & RUM sub-team of Application Observability v7.11.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants