forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[APM] Progressive fetching (experimental) (elastic#127598)
Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
Showing
33 changed files
with
662 additions
and
237 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
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
155 changes: 155 additions & 0 deletions
155
x-pack/plugins/apm/public/hooks/use_progressive_fetcher.tsx
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,155 @@ | ||
/* | ||
* 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 { OmitByValue, Assign } from 'utility-types'; | ||
import type { | ||
ClientRequestParamsOf, | ||
EndpointOf, | ||
ReturnOf, | ||
} from '@kbn/server-route-repository'; | ||
import { useKibana } from '@kbn/kibana-react-plugin/public'; | ||
import { | ||
apmProgressiveLoading, | ||
getProbabilityFromProgressiveLoadingQuality, | ||
ProgressiveLoadingQuality, | ||
} from '@kbn/observability-plugin/common'; | ||
// eslint-disable-next-line @kbn/eslint/no-restricted-paths | ||
import type { APMServerRouteRepository } from '../../server'; | ||
|
||
import type { | ||
APMClient, | ||
APMClientOptions, | ||
} from '../services/rest/create_call_apm_api'; | ||
import { FetcherResult, FETCH_STATUS, useFetcher } from './use_fetcher'; | ||
|
||
type APMProgressivelyLoadingServerRouteRepository = OmitByValue< | ||
{ | ||
[key in keyof APMServerRouteRepository]: ClientRequestParamsOf< | ||
APMServerRouteRepository, | ||
key | ||
> extends { | ||
params: { query: { probability: any } }; | ||
} | ||
? APMServerRouteRepository[key] | ||
: undefined; | ||
}, | ||
undefined | ||
>; | ||
|
||
type WithoutProbabilityParameter<T extends Record<string, any>> = { | ||
params: { query: {} }; | ||
} & Assign< | ||
T, | ||
{ | ||
params: Omit<T['params'], 'query'> & { | ||
query: Omit<T['params']['query'], 'probability'>; | ||
}; | ||
} | ||
>; | ||
|
||
type APMProgressiveAPIClient = < | ||
TEndpoint extends EndpointOf<APMProgressivelyLoadingServerRouteRepository> | ||
>( | ||
endpoint: TEndpoint, | ||
options: Omit<APMClientOptions, 'signal'> & | ||
WithoutProbabilityParameter< | ||
ClientRequestParamsOf< | ||
APMProgressivelyLoadingServerRouteRepository, | ||
TEndpoint | ||
> | ||
> | ||
) => Promise<ReturnOf<APMProgressivelyLoadingServerRouteRepository, TEndpoint>>; | ||
|
||
function clientWithProbability( | ||
regularCallApmApi: APMClient, | ||
probability: number | ||
) { | ||
return < | ||
TEndpoint extends EndpointOf<APMProgressivelyLoadingServerRouteRepository> | ||
>( | ||
endpoint: TEndpoint, | ||
options: Omit<APMClientOptions, 'signal'> & | ||
WithoutProbabilityParameter< | ||
ClientRequestParamsOf< | ||
APMProgressivelyLoadingServerRouteRepository, | ||
TEndpoint | ||
> | ||
> | ||
) => { | ||
return regularCallApmApi(endpoint, { | ||
...options, | ||
params: { | ||
...options.params, | ||
query: { | ||
...options.params.query, | ||
probability, | ||
}, | ||
}, | ||
} as any); | ||
}; | ||
} | ||
|
||
export function useProgressiveFetcher<TReturn>( | ||
callback: ( | ||
callApmApi: APMProgressiveAPIClient | ||
) => Promise<TReturn> | undefined, | ||
dependencies: any[], | ||
options?: Parameters<typeof useFetcher>[2] | ||
): FetcherResult<TReturn> { | ||
const { | ||
services: { uiSettings }, | ||
} = useKibana(); | ||
|
||
const progressiveLoadingQuality = | ||
uiSettings?.get<ProgressiveLoadingQuality>(apmProgressiveLoading) ?? | ||
ProgressiveLoadingQuality.off; | ||
|
||
const sampledProbability = getProbabilityFromProgressiveLoadingQuality( | ||
progressiveLoadingQuality | ||
); | ||
|
||
const sampledFetch = useFetcher( | ||
(regularCallApmApi) => { | ||
if (progressiveLoadingQuality === ProgressiveLoadingQuality.off) { | ||
return; | ||
} | ||
return callback( | ||
clientWithProbability(regularCallApmApi, sampledProbability) | ||
); | ||
}, | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
dependencies, | ||
options | ||
); | ||
|
||
const unsampledFetch = useFetcher( | ||
(regularCallApmApi) => { | ||
return callback(clientWithProbability(regularCallApmApi, 1)); | ||
}, | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
dependencies | ||
); | ||
|
||
const fetches = [unsampledFetch, sampledFetch]; | ||
|
||
const isError = unsampledFetch.status === FETCH_STATUS.FAILURE; | ||
|
||
const usedFetch = | ||
(!isError && | ||
fetches.find((fetch) => fetch.status === FETCH_STATUS.SUCCESS)) || | ||
unsampledFetch; | ||
|
||
const status = | ||
unsampledFetch.status === FETCH_STATUS.LOADING && | ||
usedFetch.status === FETCH_STATUS.SUCCESS | ||
? FETCH_STATUS.LOADING | ||
: usedFetch.status; | ||
|
||
return { | ||
...usedFetch, | ||
status, | ||
}; | ||
} |
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.