-
Notifications
You must be signed in to change notification settings - Fork 38
OU-632: Split ALERTS query_range into several requests for Incidents #678
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
Open
rioloc
wants to merge
6
commits into
openshift:main
Choose a base branch
from
rioloc:feat/split_query_range_requests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fe94d4b
feat: split ALERTS query_range into several requests
rioloc dfa749c
feat: updated createAlertsQuery to avoid duplicates + unit
rioloc a086b87
feat: handle the case of no matchingIncident found
rioloc 58cdea9
feat: update url limit
rioloc b9ce5f8
test: added unit tests for fetchDataForIncidentsAndAlerts
rioloc d0a451c
feat: use consoleFetchJSON
rioloc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,150 @@ | ||
| // Setup global.window before importing modules that use it | ||
| (global as any).window = { | ||
| SERVER_FLAGS: { | ||
| prometheusBaseURL: '/api/prometheus', | ||
| prometheusTenancyBaseURL: '/api/prometheus-tenancy', | ||
| alertManagerBaseURL: '/api/alertmanager', | ||
| }, | ||
| }; | ||
|
|
||
| import { createAlertsQuery, fetchDataForIncidentsAndAlerts } from './api'; | ||
| import { PrometheusResponse, consoleFetchJSON } from '@openshift-console/dynamic-plugin-sdk'; | ||
| import { buildPrometheusUrl } from '../utils'; | ||
|
|
||
| // Mock the SDK | ||
| jest.mock('@openshift-console/dynamic-plugin-sdk', () => ({ | ||
| PrometheusEndpoint: { | ||
| QUERY_RANGE: 'api/v1/query_range', | ||
| }, | ||
| consoleFetchJSON: jest.fn(), | ||
| })); | ||
|
|
||
| // Mock the global utils to avoid window access side effects | ||
| jest.mock('../utils', () => ({ | ||
| getPrometheusBasePath: jest.fn(), | ||
| buildPrometheusUrl: jest.fn(), | ||
| })); | ||
|
|
||
| describe('createAlertsQuery', () => { | ||
| it('should create a valid alerts query', () => { | ||
| const alertsQuery = createAlertsQuery([ | ||
| { | ||
| src_alertname: 'test', | ||
| src_severity: 'critical', | ||
| src_namespace: 'test', | ||
| src_silenced: 'false', | ||
| }, | ||
| { | ||
| src_alertname: 'test2', | ||
| src_severity: 'warning', | ||
| src_namespace: 'test2', | ||
| src_silenced: 'false', | ||
| }, | ||
| { | ||
| src_alertname: 'test2', | ||
| src_severity: 'warning', | ||
| src_namespace: 'test2', | ||
| src_silenced: 'true', | ||
| }, | ||
| ]); | ||
| expect(alertsQuery).toEqual([ | ||
| 'ALERTS{alertname="test", severity="critical", namespace="test"} or ALERTS{alertname="test2", severity="warning", namespace="test2"}', | ||
| ]); | ||
| }); | ||
| it('should create valid alerts queries array', () => { | ||
| const alertsQuery = createAlertsQuery( | ||
| [ | ||
| { | ||
| src_alertname: 'test', | ||
| src_severity: 'critical', | ||
| src_namespace: 'test', | ||
| src_silenced: 'false', | ||
| }, | ||
| { | ||
| src_alertname: 'test2', | ||
| src_severity: 'warning', | ||
| src_namespace: 'test2', | ||
| src_silenced: 'false', | ||
| }, | ||
| { | ||
| src_alertname: 'test2', | ||
| src_severity: 'warning', | ||
| src_namespace: 'test2', | ||
| src_silenced: 'true', | ||
| }, | ||
| ], | ||
| 100, | ||
| ); | ||
| expect(alertsQuery).toEqual([ | ||
| 'ALERTS{alertname="test", severity="critical", namespace="test"}', | ||
| 'ALERTS{alertname="test2", severity="warning", namespace="test2"}', | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('fetchDataForIncidentsAndAlerts', () => { | ||
| it('should fetch data for incidents and alerts', async () => { | ||
| (buildPrometheusUrl as jest.Mock).mockReturnValue('/mock/url'); | ||
| const now = Date.now(); | ||
|
|
||
| const result1 = { | ||
| metric: { | ||
| alertname: 'test', | ||
| severity: 'critical', | ||
| namespace: 'test', | ||
| }, | ||
| values: [ | ||
| [now - 1000, '1'], | ||
| [now - 500, '2'], | ||
| ] as [number, string][], | ||
| }; | ||
|
|
||
| const result2 = { | ||
| metric: { | ||
| alertname: 'test2', | ||
| severity: 'warning', | ||
| namespace: 'test2', | ||
| }, | ||
| values: [ | ||
| [now - 2000, '3'], | ||
| [now - 1500, '4'], | ||
| ] as [number, string][], | ||
| }; | ||
|
|
||
| const mockPrometheusResponse1: PrometheusResponse = { | ||
| status: 'success', | ||
| data: { | ||
| resultType: 'matrix', | ||
| result: [result1], | ||
| }, | ||
| }; | ||
|
|
||
| const mockPrometheusResponse2: PrometheusResponse = { | ||
| status: 'success', | ||
| data: { | ||
| resultType: 'matrix', | ||
| result: [result2], | ||
| }, | ||
| }; | ||
|
|
||
| const mockConsoleFetchJSON = consoleFetchJSON as jest.MockedFunction<typeof consoleFetchJSON>; | ||
| mockConsoleFetchJSON | ||
| .mockResolvedValueOnce(mockPrometheusResponse1) | ||
| .mockResolvedValueOnce(mockPrometheusResponse2); | ||
|
|
||
| const range = { endTime: now, duration: 86400000 }; | ||
| const customQuery = [ | ||
| 'ALERTS{alertname="test", severity="critical", namespace="test"}', | ||
| 'ALERTS{alertname="test2", severity="warning", namespace="test2"}', | ||
| ]; | ||
| const result = await fetchDataForIncidentsAndAlerts(mockConsoleFetchJSON, range, customQuery); | ||
| expect(result).toEqual({ | ||
| status: 'success', | ||
| data: { | ||
| resultType: 'matrix', | ||
| result: [result1, result2], | ||
| }, | ||
| }); | ||
| expect(mockConsoleFetchJSON).toHaveBeenCalledTimes(2); | ||
| }); | ||
| }); |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.