UNPKG

@stolostron/multicluster-sdk

Version:

Provides extensions and APIs that dynamic plugins can use to leverage multicluster capabilities provided by Red Hat Advanced Cluster Management.

76 lines (75 loc) 4.5 kB
import { PrometheusPollProps, PrometheusResponse } from '@openshift-console/dynamic-plugin-sdk'; import { Fleet } from '../types'; /** * A fleet version of [`usePrometheusPoll`](https://github.com/openshift/console/blob/main/frontend/packages/console-dynamic-plugin-sdk/docs/api.md#useprometheuspoll) from * the [dynamic plugin SDK](https://www.npmjs.com/package/@openshift-console/dynamic-plugin-sdk) that polls Prometheus for metrics data from a specific cluster or across all clusters. * * Although this is intended as a drop-in replacement for usePrometheusPoll there are a couple of considerations: * 1. The Observabilty service must be running on the hub in order to access metric data outside of the hub. The useIsFleetObservabilityInstalled() hook can check this * 2. The PromQL query will be different for clusters outside of the hub. The query may be completely different but at the very least it will contain the cluster name(s) * 3. Ideally the Observabilty team will setup your queries so that you only need to add the cluster name-- see example * * @param {PrometheusEndpoint} endpoint - one of the PrometheusEndpoint (label, query, range, rules, targets) * @param {string} [cluster] - The target cluster name. If not specified or matches hub cluster, queries local Prometheus * @param {boolean} [allClusters] - If true, queries across all clusters in the fleet (requires observability) * @param {string} [query] - (optional) Prometheus query string. If empty or undefined, polling is not started. (See note above on format) * @param {number} [delay] - (optional) polling delay interval (ms) * @param {number} [endTime] - (optional) for QUERY_RANGE enpoint, end of the query range * @param {number} [samples] - (optional) for QUERY_RANGE enpoint * @param {number} [timespan] - (optional) for QUERY_RANGE enpoint * @param {string} [namespace] - (optional) a search param to append * @param {string} [timeout] - (optional) a search param to append * * @returns A tuple containing: * - `response`: PrometheusResponse object with query results, or undefined if loading/error * - `loaded`: Boolean indicating if the request has completed (successfully or with error) * - `error`: Any error that occurred during the request, including dependency check failures * @example * ```typescript * // (OPTIONAL) Check if the Observability service has been installed * const [response, loaded, error] = useIsFleetObservabilityInstalled() * if (!loaded) { * return <Loading /> * } * if (error) { * return <ErrorState error={error} /> * } * * // Get the query * const [hubClusterName] = useHubClusterName(); * const clusterFilter = cluster !== hubClusterName ? `,cluster='$cluster}'` : ''; * const sumByCluster = !isEmpty(obj?.cluster) && obj?.cluster === hubClusterName ? ', cluster' : ''; * // NOTE: this assumes your queries are identical between hub and other fleet clusters * // if not, you may need to use an entirely different query for fleet--consult the Observability team * const query = `sum(rate(kubevirt_vmi_cpu_usage_seconds_total{name='${name}',namespace='${namespace}'${clusterFilter}}[${duration}])) BY (name, namespace${sumByCluster})`, * * // Query metrics data * const [response, loaded, error] = useFleetPrometheusPoll({ * cluster: 'cluster', * query * }); * if (!loaded) { * return <Loading /> * } * if (error) { * return <ErrorState error={error} /> * } * ``` * * @remarks * This hook intelligently routes Prometheus queries based on the target cluster: * - If no cluster is specified or the cluster matches the hub cluster, it uses the local Prometheus instance * - If a specific managed cluster is specified, it uses the fleet observability service (requires multicluster observability to be installed) * - If `allClusters` is true, it queries across all clusters in the fleet using the observability service * * The hook automatically handles: * - Checking if multicluster observability is installed when needed using useIsFleetObservabilityInstalled() hook * - Determining the hub cluster name for comparison * - Routing queries to the appropriate Prometheus endpoint * - Providing appropriate error states when dependencies are not available * */ export declare const useFleetPrometheusPoll: (props: Fleet<PrometheusPollProps> & { allClusters?: boolean; }) => [response: PrometheusResponse | undefined, loaded: boolean, error: unknown]; //# sourceMappingURL=useFleetPrometheusPoll.d.ts.map