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
Conversation
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.
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) |
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.
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)
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.
This is just meta data, so we don't need to return an error. But we should definitely add key/value pair to plog!
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.
Or actually I think you're right. Lets return an 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.
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) |
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.
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)
if link != "" { | ||
for _, field := range frame.Fields { | ||
field.Config = &data.FieldConfig{ | ||
Links: []data.DataLink{ | ||
{ | ||
Title: "View in CloudWatch console", | ||
TargetBlank: true, | ||
URL: link, | ||
}, | ||
}, | ||
} | ||
} | ||
} |
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.
It's more idiomatic to avoid deep nesting in Go.
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, | |
}, | |
}, | |
} | |
} |
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.
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 |
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.
Even though this isn't exported, it's best to follow the standard doc comment style.
// 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") |
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.
Shouldn't drop the original error:
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()) |
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.
decodedLink, _ := url.PathUnescape(parsedURL.String()) | |
decodedLink, err := url.PathUnescape(parsedURL.String()) | |
require.NoError(t, err) |
start, _ := time.Parse(time.RFC3339, "2018-03-15T13:00:00Z") | ||
end, _ := time.Parse(time.RFC3339, "2018-03-18T13:34:00Z") |
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.
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()) |
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.
decodedLink, _ := url.PathUnescape(parsedURL.String()) | |
decodedLink, err := url.PathUnescape(parsedURL.String()) | |
require.NoError(t, err) |
start, _ := time.Parse(time.RFC3339, "2018-03-15T13:00:00Z") | ||
end, _ := time.Parse(time.RFC3339, "2018-03-18T13:34:00Z") |
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.
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()) |
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.
decodedLink, _ := url.PathUnescape(parsedURL.String()) | |
decodedLink, err := url.PathUnescape(parsedURL.String()) | |
require.NoError(t, err) |
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.
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) |
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.
Go errors shouldn't be capitalized, also the message looks misleading:
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) |
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.
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 { |
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 have tested, we don't need to add config to all field anymore, just Fields[1] would be good.
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.
that's great news! I'll update accordingly
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.
actually that didn't work for me @ying-jeanne
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.
AFAICT modifications look good and it works as expected.
* 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
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