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

CloudWatch Logs: Support fetching fields in monaco editor #78244

Merged
merged 5 commits into from
Nov 29, 2023
Merged

Conversation

iwysiu
Copy link
Contributor

@iwysiu iwysiu commented Nov 15, 2023

What is this feature?

Updates the monaco completion provider for cloudwatch logs to also get field suggestions.

Editor now fetching fields:
Screenshot 2023-11-15 at 3 46 00 PM

Why do we need this feature?

It makes it easier to write cloudwatch logs queries. Also, the old editor had this feature so we we should have it for the monaco editor.

Who is this feature for?

Users who want to write cloudwatch logs queries in grafana.

Which issue(s) does this PR fix?:

Fixes #77223

Special notes for your reviewer:
The conditions I thought of for testing were:

  1. new visualization
  2. edit existing panel
  3. opening an editor, closing it and then opening another editor
  4. removing and adding log groups

I didn't write tests for the new LogsQueryField logic because I wasn't sure how to, but if y'all have a suggestion I'm open to it.

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.

@@ -39,6 +39,9 @@ export interface CodeEditorProps {
*/
onEditorDidMount?: (editor: MonacoEditor, monaco: Monaco) => void;

/** Callback before the edior has unmounted */
onEditorWillUnmount?: () => void;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I needed to be able to call dispose on the CompletionItemProvider on unmount (otherwise we end up with two set on the language when another CodeEditor is opened). There may be a better way to do this, but this seemed cleanest to me.

@iwysiu iwysiu added add to changelog no-backport Skip backport of PR labels Nov 15, 2023
@@ -56,6 +71,7 @@ export class LogsCompletionItemProvider extends CompletionItemProvider {
insertText: `${command} $0`,
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
command: TRIGGER_SUGGEST,
kind: monaco.languages.CompletionItemKind.Method,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changing these was mostly to make it so that they weren't all using the Field color coding. I can change them back if we want.

@iwysiu iwysiu marked this pull request as ready for review November 15, 2023 22:27
@iwysiu iwysiu requested review from a team as code owners November 15, 2023 22:27
@iwysiu iwysiu requested review from fridgepoet, idastambuk, joshhunt and JoaoSilvaGrafana and removed request for a team November 15, 2023 22:27
@@ -27,6 +27,10 @@ class UnthemedCodeEditor extends PureComponent<Props> {
if (this.completionCancel) {
this.completionCancel.dispose();
}

if (this.props.onEditorWillUnmount) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can also use a shorthand here: this.props.onEditorWillUnmount?.();

@idastambuk
Copy link
Contributor

Looks good, great job with this!

I played around with it and the only "bug" I noticed is when we have two or more logs editors open in one panel. In this case the field suggestions are the same for both editors, even if log groups are different (or missing in one of them, for example).

It seems like multiple editors in the same window use a singleton instance of CompletionProvider that wouldn't be so simple to separate between each editor. I guess that means that this was a problem even with the old editor, maybe? Meaning it wouldn't be a showstopper for merging this.
What do you think?

@iwysiu
Copy link
Contributor Author

iwysiu commented Nov 17, 2023

I guess that means that this was a problem even with the old editor, maybe?

Unfortunately it probably wasn't. The old editor was able to send the query fields along with the actual request for suggestions, unlike monaco which needs them to be set on the completion item provider, so it probably wouldn't have this issue. Hmm, how do we work around this 😞

@iwysiu iwysiu requested a review from a team as a code owner November 21, 2023 21:30
@iwysiu
Copy link
Contributor Author

iwysiu commented Nov 21, 2023

@idastambuk Fixed the issue with multiple queries! Now it reregisters on onFocus and disposes on onBlur, which handles that case.

@idastambuk
Copy link
Contributor

I guess that means that this was a problem even with the old editor, maybe?

Unfortunately it probably wasn't. The old editor was able to send the query fields along with the actual request for suggestions, unlike monaco which needs them to be set on the completion item provider, so it probably wouldn't have this issue. Hmm, how do we work around this 😞

Tested it and looks good now!

@iwysiu iwysiu merged commit c623235 into main Nov 29, 2023
20 checks passed
@iwysiu iwysiu deleted the iwysiu/77223 branch November 29, 2023 15:37
@aangelisc aangelisc modified the milestones: 10.3.x, 10.2.3 Dec 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CloudWatch Logs: Monaco Editor should suggest log field names
3 participants