-
Notifications
You must be signed in to change notification settings - Fork 8.9k
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
disable fetching alertmanagers on status page in agent mode #9941
disable fetching alertmanagers on status page in agent mode #9941
Conversation
{ fetchResult: useFetch<Record<string, string>>(`${path}/alertmanagers`), title: 'Alertmanagers' }, | ||
].map(({ fetchResult, title }) => { | ||
const { response, isLoading, error } = fetchResult; | ||
{ FetchResult: () => useFetch<Record<string, string>>(`${path}/status/runtimeinfo`), title: 'Runtime Information' }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason for the capitalization of these functions? It seems unrelated to the rest of the change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The linter didn't like fetchResult being lowercase:
Failed to compile.
src/pages/status/Status.tsx
Line 94:30: React Hook "useFetch" is called in function "fetchResult" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter react-hooks/rules-of-hooks
Line 95:30: React Hook "useFetch" is called in function "fetchResult" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter react-hooks/rules-of-hooks
Line 96:30: React Hook "useFetch" is called in function "fetchResult" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter react-hooks/rules-of-hooks
Search for the keywords to learn more about each error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just capitalizing these may not be the best approach but it seemed to appease the linter. Open to suggestions here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, some of those hook-related linter rules are really annoying, because they are not smart enough to understand enough context much of the time. In this case, the upper-casing only shuts up the linter because it now thinks that we are defining a React component, which we are not (it is actually a custom hook function, but the linter doesn't recognize it as such), so we just "tricking" the linter into believing something wrong to shut it up. Could you try renaming these to useFetchResult
instead? Hopefully if they start with useXXX
, the linter will detect them as custom hooks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in this case, the linter complains now about using a hook in a callback:
Line 101:48: React Hook "useFetchResult" cannot be called inside a callback. React Hooks must be called in a React function component or a custom React Hook function react-hooks/rules-of-hooks```
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah, the general pattern here of calling hooks from a map
callback is something that isn't super kosher, as it makes it less certain to the linter that the hooks are always all called, and in the same order (https://reactjs.org/docs/hooks-rules.html#only-call-hooks-at-the-top-level). But that was already the problem before.
If we really wanted to abide by the "rules of hooks" here, we would have to restructure everything, probably just write everything out...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved this logic into a new functional component. Does this work?
Thanks, looks good to me besides the comment! Could you change the branch to merge into to be |
Signed-off-by: Robbie Lankford <robert.lankford@grafana.com>
713d872
to
6df4776
Compare
if (agentMode && title === 'Alertmanagers') { | ||
return null; | ||
} | ||
return <StatusResult fetchPath={fetchPath} title={title} agentMode={agentMode} />; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah, great! But I think you don't need to pass agentMode
in here, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah already fixed :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right, noticed this a moment ago as well.
Signed-off-by: Robbie Lankford <robert.lankford@grafana.com>
69a14a6
to
5fb63c1
Compare
Looks good now, but would be great to add a regression test for this to https://github.com/prometheus/prometheus/blob/main/web/ui/react-app/src/pages/status/Status.test.tsx - that's a snapshot-based test. I think you can just duplicate the few |
hmm ok, I tried to create a quick test but it seems like the existing tests cover it('should match table snapshot with agent mode', () => {
const wrapper = shallow(<Status agentMode={true} />);
expect(toJson(wrapper)).toMatchSnapshot();
jest.restoreAllMocks();
}); |
Ah, you are right of course. Sorry, didn't see it tested only the inner component. I think it's good enough for now then, but it would definitely be good to add tests for the outer component somehow later. Will merge for now, thanks! 👍 |
thanks @juliusv 🙏 |
Took a shot at fixing #9928. This disables fetching alertmanagers from /status page in agent mode.