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: Move deep link creation to the backend #30206

Merged
merged 13 commits into from Jan 13, 2021

Conversation

sunker
Copy link
Contributor

@sunker sunker commented Jan 11, 2021

Moving the deep link code from the frontend to the backend also enabled fixing #30204 and that is also done in this PR.

Fixes #30204
Fixes #30205

@sunker sunker marked this pull request as ready for review January 12, 2021 08:18
@sunker sunker requested a review from a team as a code owner January 12, 2021 08:18
@sunker sunker requested a review from a team January 12, 2021 08:18
@sunker sunker requested a review from a team as a code owner January 12, 2021 08:18
@sunker sunker requested review from aknuds1, jessabe, mckn, thisisobate, davkal and papagian and removed request for a team, jessabe, mckn, thisisobate and davkal January 12, 2021 08:18
Copy link
Contributor

@aknuds1 aknuds1 left a comment

Choose a reason for hiding this comment

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

See comments

@@ -104,10 +99,119 @@ func (e *cloudWatchExecutor) transformQueryResponsesToQueryResult(cloudwatchResp
queryResult.ErrorString = "Cloudwatch GetMetricData error: Too many datapoints requested - your search has been limited. Please try to reduce the time range"
}

eq, err := json.Marshal(executedQueries)
if err != nil {
plog.Error("Could not marshal executedString struct", err)
Copy link
Contributor

@aknuds1 aknuds1 Jan 12, 2021

Choose a reason for hiding this comment

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

Shouldn't you return if it fails here? I don't think you should be using eq further down, if the encoding failed. Also, doesn't plog.Error take key/value pairs? I.e., I would think it should be

plog.Error("Could not marshal executedString struct", "err", err)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is just meta data, so we don't need to return an error. But we should definitely add key/value pair to plog!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or actually I think you're right. Lets return an error

Copy link
Contributor

Choose a reason for hiding this comment

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

I actually didn't mean to return an error, but to return in the first place. This code just continues in case of an error.


link, err := buildDeepLink(refID, requestQueries, executedQueries, startTime, endTime)
if err != nil {
plog.Error("Could not build deep link", err)
Copy link
Contributor

@aknuds1 aknuds1 Jan 12, 2021

Choose a reason for hiding this comment

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

Shouldn't you return if this fails? Also, doesn't plog.Error take key/value pairs? I.e., I would think it should be

plog.Error("Could not marshal executedString struct", "err", err)

Comment on lines 117 to 129
if link != "" {
for _, field := range frame.Fields {
field.Config = &data.FieldConfig{
Links: []data.DataLink{
{
Title: "View in CloudWatch console",
TargetBlank: true,
URL: link,
},
},
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

It's more idiomatic to avoid deep nesting in Go.

Suggested change
if link != "" {
for _, field := range frame.Fields {
field.Config = &data.FieldConfig{
Links: []data.DataLink{
{
Title: "View in CloudWatch console",
TargetBlank: true,
URL: link,
},
},
}
}
}
if link != "" {
continue
}
for _, field := range frame.Fields {
field.Config = &data.FieldConfig{
Links: []data.DataLink{
{
Title: "View in CloudWatch console",
TargetBlank: true,
URL: link,
},
},
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, good idea!

results[refID] = queryResult
}

return results
}

// Generates a deep link from Grafana to the CloudWatch console. The link params are based on metric(s) for a given query row in the Query Editor
Copy link
Contributor

Choose a reason for hiding this comment

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

Even though this isn't exported, it's best to follow the standard doc comment style.

Suggested change
// Generates a deep link from Grafana to the CloudWatch console. The link params are based on metric(s) for a given query row in the Query Editor
// buildDeepLink generates a deep link from Grafana to the CloudWatch console. The link params are based on metric(s) for a given query row in the Query Editor.


linkProps, err := json.Marshal(cloudWatchLinkProps)
if err != nil {
return "", fmt.Errorf("could not marshal link")
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't drop the original error:

Suggested change
return "", fmt.Errorf("could not marshal link")
return "", fmt.Errorf("could not marshal link: %w", err)

parsedURL, err := url.Parse(link)
require.NoError(t, err)

decodedLink, _ := url.PathUnescape(parsedURL.String())
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
decodedLink, _ := url.PathUnescape(parsedURL.String())
decodedLink, err := url.PathUnescape(parsedURL.String())
require.NoError(t, err)

Comment on lines 193 to 194
start, _ := time.Parse(time.RFC3339, "2018-03-15T13:00:00Z")
end, _ := time.Parse(time.RFC3339, "2018-03-18T13:34:00Z")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
start, _ := time.Parse(time.RFC3339, "2018-03-15T13:00:00Z")
end, _ := time.Parse(time.RFC3339, "2018-03-18T13:34:00Z")
start, err := time.Parse(time.RFC3339, "2018-03-15T13:00:00Z")
require.NoError(t, err)
end, err := time.Parse(time.RFC3339, "2018-03-18T13:34:00Z")
require.NoError(t, err)

parsedURL, err := url.Parse(link)
require.NoError(t, err)

decodedLink, _ := url.PathUnescape(parsedURL.String())
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
decodedLink, _ := url.PathUnescape(parsedURL.String())
decodedLink, err := url.PathUnescape(parsedURL.String())
require.NoError(t, err)

Comment on lines 213 to 214
start, _ := time.Parse(time.RFC3339, "2018-03-15T13:00:00Z")
end, _ := time.Parse(time.RFC3339, "2018-03-18T13:34:00Z")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
start, _ := time.Parse(time.RFC3339, "2018-03-15T13:00:00Z")
end, _ := time.Parse(time.RFC3339, "2018-03-18T13:34:00Z")
start, err := time.Parse(time.RFC3339, "2018-03-15T13:00:00Z")
require.NoError(t, err)
end, err := time.Parse(time.RFC3339, "2018-03-18T13:34:00Z")
require.NoError(t, err)

parsedURL, err := url.Parse(link)
require.NoError(t, err)

decodedLink, _ := url.PathUnescape(parsedURL.String())
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
decodedLink, _ := url.PathUnescape(parsedURL.String())
decodedLink, err := url.PathUnescape(parsedURL.String())
require.NoError(t, err)

@sunker sunker requested a review from aknuds1 January 12, 2021 11:09
Copy link
Contributor

@aknuds1 aknuds1 left a comment

Choose a reason for hiding this comment

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

Looks good now, but see comments for nits.

@@ -101,30 +101,32 @@ func (e *cloudWatchExecutor) transformQueryResponsesToQueryResult(cloudwatchResp

eq, err := json.Marshal(executedQueries)
if err != nil {
plog.Error("Could not marshal executedString struct", err)
return nil, fmt.Errorf("Could not marshal executedString struct: %w", err)
Copy link
Contributor

@aknuds1 aknuds1 Jan 12, 2021

Choose a reason for hiding this comment

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

Go errors shouldn't be capitalized, also the message looks misleading:

Suggested change
return nil, fmt.Errorf("Could not marshal executedString struct: %w", err)
return nil, fmt.Errorf("could not marshal executed queries: %w", err)

}

link, err := buildDeepLink(refID, requestQueries, executedQueries, startTime, endTime)
if err != nil {
plog.Error("Could not build deep link", err)
return nil, fmt.Errorf("Could not build deep link: %w", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return nil, fmt.Errorf("Could not build deep link: %w", err)
return nil, fmt.Errorf("could not build deep link: %w", err)

continue
}

for _, field := range frame.Fields {
Copy link
Contributor

Choose a reason for hiding this comment

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

i have tested, we don't need to add config to all field anymore, just Fields[1] would be good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that's great news! I'll update accordingly

Copy link
Contributor Author

Choose a reason for hiding this comment

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

actually that didn't work for me @ying-jeanne

Copy link
Contributor

@papagian papagian left a comment

Choose a reason for hiding this comment

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

AFAICT modifications look good and it works as expected.

@sunker sunker merged commit 64d600f into master Jan 13, 2021
@sunker sunker deleted the cloudwatch/use-dataframe-meta branch January 13, 2021 14:30
ryantxu pushed a commit that referenced this pull request Jan 13, 2021
* get meta data from dataframe meta

* gmdMeta is not used anymore

* wip: move deep link creation to backend

* Refactor backend. Remove not used front end code

* Unit test deep links. Remove not used frontend tests

* remove reference to gmdmeta

* fix goimports error

* fixed after pr feedback

* more changes according to pr feedback

* fix lint issue

* fix bad math expression check and fix bad test

* Decrease nesting

* put link on first data field only
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: Move deep links code from frontend to backend CloudWatch: "Show query preview" is broken
4 participants