Skip to content
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

Explore: Change Logs.tsx to a functional component #87808

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

gelicia
Copy link
Contributor

@gelicia gelicia commented May 14, 2024

What is this feature?

This changes Logs.tsx to be a functional component. There should be no visible difference to the end user.

Why do we need this feature?

Converting this component to be functional will help further improvements. See #86431 (comment) for more details.

Which issue(s) does this PR fix?:

Fixes #87047

Special notes for your reviewer:

Please check that:

  • It works as expected from a user's perspective.
  • If this is a pre-GA feature, it is behind a feature toggle.
  • The docs are updated, and if this is a notable improvement, it's added to our What's New doc.

gelicia and others added 5 commits May 14, 2024 14:12
onHiddenSeriesChanged should be called only when dataWithConfig changes, not when the callback changes itself.

This is actually causing an infinite loop because onHiddenSeriesChanged may not be memoized in the parent and passed as a  new callback function on each render.
@gelicia gelicia added no-backport Skip backport of PR no-changelog Skip including change in changelog/release notes labels May 21, 2024
@gelicia gelicia marked this pull request as ready for review May 21, 2024 18:55
@gelicia gelicia requested review from a team as code owners May 21, 2024 18:55
Copy link
Contributor

@matyax matyax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for taking care of this refactor!

In general this is a very big change that doesn't have enough previous testing coverage to guide it. In general what worries me the most is the problem with dependency arrays. Disabling it will cause the effects and clean up functions to use potentially stale references with unexpected results, so it gives me very little confidence to release it as it is. I would greatly appreciate if you can work around them.

If I can help with this, I'm happy to do it, as this is a key component for logs in Explore.

Comment on lines 202 to 204
const toggleLegendRef: React.MutableRefObject<(name: string, mode: SeriesVisibilityChangeMode) => void> =
React.createRef();
const topLogsRef = React.createRef<HTMLDivElement>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these could be useRef().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 06dcdc8

);
}
};
// eslint-disable-next-line react-hooks/exhaustive-deps
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disabling exhaustive deps should be an exceptional measure of last resort, so in general in this PR I would ask you to not use it.

For this particular case, the clean up function will be created and returned with values that might not be valid or present when it's executed, specially those pointing to timers.

Copy link
Contributor

@ifrost ifrost May 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! We may need to use useRef to store latest panelState.logs and timers and call it only on unmount. In long-run this code looks like should not be here at all and Explore should clean up panel state for each panel automatically 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in effed37 + a75b5c8.

})
);
}
if (this.props.panelState?.logs?.visualisationType !== prevProps.panelState?.logs?.visualisationType) {
const visualisationType = this.props.panelState?.logs?.visualisationType ?? getDefaultVisualisationType();
// eslint-disable-next-line react-hooks/exhaustive-deps
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Forcing the dependencies to not update should not be the solution, because the point of the dependency array and the linting rule is to use valid referenced values and prevent bugs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was fixed in b1e60ee

behavior: 'auto',
top: 0,
});
}
}
this.topLogsRef.current?.scrollIntoView();
topLogsRef.current?.scrollIntoView();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aprox from line 314 to 634, these used to be class methods that would not be recreated on every render. Could you please wrap them around useCallback(). The reason is to maintain the previous behavior and to not cause performance regressions and/or unnecessary re-renders.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made memoized utils static in 5d2f0eb and added wrappers in fed463c

@matyax matyax requested a review from a team May 22, 2024 09:23
Copy link
Contributor

@harisrozajac harisrozajac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great to me. I've tested it with content outline and it works as expected. Also tested with loki, elastic, and cloudwatch and didn't find any issues. Great work at this @gelicia and @ifrost 🥇

@ifrost
Copy link
Contributor

ifrost commented May 29, 2024

@matyax / @grafana/observability-logs -> it's ready for another round of review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/explore area/frontend no-backport Skip backport of PR no-changelog Skip including change in changelog/release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Logs: Convert Logs.tsx to a functional component
4 participants