Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a hook to watch Prometheus rules and move alert/metrics fetchi…
…ng into hooks
- Loading branch information
1 parent
02f3490
commit e9c636e
Showing
11 changed files
with
152 additions
and
102 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
34 changes: 34 additions & 0 deletions
34
frontend/packages/dev-console/src/components/topology/hooks/useOverviewAlertsUpdater.ts
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,34 @@ | ||
import { fetchMonitoringAlerts } from '@console/internal/components/overview/metricUtils'; | ||
import { Alert } from '@console/internal/components/monitoring/types'; | ||
|
||
type StopOverviewAUpdater = () => void; | ||
|
||
export const useOverviewAlertsUpdater = ( | ||
namespace: string, | ||
updateMonitoringAlerts: (alerts: Alert[]) => void, | ||
interval: number = 15 * 1000, | ||
): StopOverviewAUpdater => { | ||
let alertsInterval: any = null; | ||
|
||
const fetchAlerts = (): void => { | ||
fetchMonitoringAlerts(namespace) | ||
.then((alerts) => { | ||
updateMonitoringAlerts(alerts); | ||
}) | ||
.catch((e) => { | ||
console.error(e); // eslint-disable-line no-console | ||
}) | ||
.then(() => { | ||
alertsInterval = setTimeout(fetchAlerts, interval); | ||
}) | ||
.catch((e) => { | ||
console.error(e); // eslint-disable-line no-console | ||
}); | ||
}; | ||
|
||
fetchAlerts(); | ||
|
||
return () => { | ||
clearTimeout(alertsInterval); | ||
}; | ||
}; |
50 changes: 50 additions & 0 deletions
50
frontend/packages/dev-console/src/components/topology/hooks/useOverviewMetricsUpdater.ts
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,50 @@ | ||
import * as _ from 'lodash'; | ||
import { METRICS_POLL_INTERVAL } from '@console/shared/src'; | ||
import { | ||
fetchOverviewMetrics, | ||
METRICS_FAILURE_CODES, | ||
OverviewMetrics, | ||
} from '@console/internal/components/overview/metricUtils'; | ||
import { PROMETHEUS_TENANCY_BASE_PATH } from '@console/internal/components/graphs'; | ||
|
||
type StopOverviewMetricsUpdater = () => void; | ||
|
||
export const useOverviewMetricsUpdater = ( | ||
namespace: string, | ||
metrics: OverviewMetrics, | ||
updateMetrics: (metrics: OverviewMetrics) => void, | ||
interval: number = METRICS_POLL_INTERVAL, | ||
): StopOverviewMetricsUpdater => { | ||
let metricsInterval = null; | ||
|
||
const fetchMetrics = () => { | ||
if (!PROMETHEUS_TENANCY_BASE_PATH) { | ||
return; | ||
} | ||
fetchOverviewMetrics(namespace) | ||
.then((updatedMetrics) => { | ||
updateMetrics(updatedMetrics); | ||
}) | ||
.catch((res) => { | ||
const status = res?.response?.status; | ||
// eslint-disable-next-line no-console | ||
console.error('Could not fetch metrics, status:', status); | ||
// Don't retry on some status codes unless a previous request succeeded. | ||
if (_.includes(METRICS_FAILURE_CODES, status) && _.isEmpty(metrics)) { | ||
throw new Error(`Could not fetch metrics, status: ${status}`); | ||
} | ||
}) | ||
.then(() => { | ||
metricsInterval = setTimeout(fetchMetrics, interval); | ||
}) | ||
.catch((e) => { | ||
console.error(e); // eslint-disable-line no-console | ||
}); | ||
}; | ||
|
||
fetchMetrics(); | ||
|
||
return () => { | ||
clearTimeout(metricsInterval); | ||
}; | ||
}; |
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
17 changes: 17 additions & 0 deletions
17
frontend/public/components/graphs/prometheus-rules-hook.ts
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,17 @@ | ||
import { useURLPoll } from '../utils/url-poll-hook'; | ||
import { getPrometheusURL, PrometheusEndpoint } from './helpers'; | ||
import { PrometheusRulesResponse } from '../monitoring/types'; | ||
|
||
export const usePrometheusRulesPoll = ({ delay, namespace }: PrometheusPollProps) => { | ||
const url = getPrometheusURL({ | ||
endpoint: PrometheusEndpoint.RULES, | ||
namespace, | ||
}); | ||
|
||
return useURLPoll<PrometheusRulesResponse>(url, delay, namespace); | ||
}; | ||
|
||
type PrometheusPollProps = { | ||
delay?: number; | ||
namespace?: string; | ||
}; |
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.