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: Pass refId from query for expression queries #66147

Merged
merged 7 commits into from
Apr 13, 2023

Conversation

idastambuk
Copy link
Contributor

What is this feature?

This fixes a bug first reported in support-escalations here.
When an expression is added to two or more cloudwatch queries, the BE code for handling this doesn't pass on refId, instead overwriting it with "A". This passes the refId, in case it's defined, otherwise reverts to "A" like before.

Tested in dashboard and alerts and it looks good.

Special notes for your reviewer:
To help with testing and repro:

I used these queries with our demo cluster and provisioning creds:
fields @message | stats count(*) as qtd_error_1 by bin(5m)
and
fields @message | filter level = 'Metadata' | stats count(*) as qtd_error_2 by bin(5m)
and
$A + $B for the Math->Expression query

This probably needs a test, but I haven't had time to do them and I'd rather this not wait for me. If anyone wants to add them, you're welcome to push into this branch! 🙏🏻

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.

@idastambuk idastambuk added no-changelog Skip including change in changelog/release notes backport v9.5.x Bot will automatically open backport PR labels Apr 6, 2023
@idastambuk idastambuk added this to the 10.0.0 milestone Apr 6, 2023
@idastambuk idastambuk requested a review from iwysiu April 6, 2023 17:40
@idastambuk idastambuk requested a review from a team as a code owner April 6, 2023 17:40
@idastambuk idastambuk requested review from sarahzinger and removed request for a team April 6, 2023 17:40
@grafanabot
Copy link
Contributor

Hello @idastambuk!
Backport pull requests need to be either:

  • Pull requests which address bugs,
  • Urgent fixes which need product approval, in order to get merged,
  • Docs changes.

Please, if the current pull request addresses a bug fix, label it with the type/bug label.
If it already has the product approval, please add the product-approved label. For docs changes, please add the type/docs label.
If none of the above applies, please consider removing the backport label and target the next major/minor release.
Thanks!

@github-actions
Copy link
Contributor

github-actions bot commented Apr 6, 2023

Backend code coverage report for PR #66147

Plugin Main PR Difference
cloudwatch 82.3% 82.4% .1%

@github-actions
Copy link
Contributor

github-actions bot commented Apr 6, 2023

Frontend code coverage report for PR #66147
No changes

Copy link
Member

@sarahzinger sarahzinger left a comment

Choose a reason for hiding this comment

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

Feels very plausible to me! A few suggestions:

Copy link
Contributor

@iwysiu iwysiu left a comment

Choose a reason for hiding this comment

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

the change itself looks good! I agree that we should add a test.
While I don't think that backporting it should be an issue, as long as the support escalation is ok with it not being backported I think its fine to be cautious. While it's unlikely that someone is depending on the behavior of forcibly setting refIds to "A", you could theoretically build a functioning panel that does that and would be broken by fixing it.

assert.True(t, ok)
})

t.Run("when RefIDs are provided, correctly pass them on with the results", func(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@fridgepoet and I paired on these tests and decided to add this one, primarily to play around with the "github.com/stretchr/testify/mock" to see how we could mock returns from internal functions QueryData calls. We managed to successfully test that the method appends the correct refId to the results of the query.

We do realize this one might be a bit difficult to understand if you don't have experience with the testing library, so let us know if you'd like us (well, mostly @fridgepoet since she's the expert there) to take you through it and show you how the library api and this test works!

In any case, we decided to keep it since it's probably useful to test for this in case there is some refactoring in the future, so we thought the tradeoff is worth it. What do you think?

@idastambuk idastambuk removed the no-changelog Skip including change in changelog/release notes label Apr 11, 2023
@idastambuk
Copy link
Contributor Author

@iwysiu @sarahzinger I would lean towards backporting, since the problem was reported in a support-escalation, so we might have to do it anyway between now and 10.
Added a test and a bit of context next to it, let me know what you think!

pkg/tsdb/cloudwatch/log_sync_query_test.go Outdated Show resolved Hide resolved
executeSyncLogQuery = origExecuteSyncLogQuery
})
}
func Test_executeSyncLogQueryMocks(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

This test block (two tests) are the new tests

"github.com/stretchr/testify/require"
)

func Test_executeSyncLogQuery(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

This large block of tests was just moved from cloudwatch_test.go to this file.

Co-authored-by: Shirley <4163034+fridgepoet@users.noreply.github.com>
Copy link
Contributor

@iwysiu iwysiu left a comment

Choose a reason for hiding this comment

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

Nice work on the test!

pkg/tsdb/cloudwatch/log_sync_query_test.go Outdated Show resolved Hide resolved
pkg/tsdb/cloudwatch/log_sync_query_test.go Outdated Show resolved Hide resolved
assert.True(t, ok)
})

t.Run("when a query refId is provided, it is returned in the response", func(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there something that this test is covering that the one below it itsn't?

Copy link
Member

Choose a reason for hiding this comment

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

this test was written after the other test and is easier to understand and debug. it also may be a lot less fragile/brittle than the test below

i think of the below test as a bonus/exercise and this test as our strict minimum

Copy link
Member

@sarahzinger sarahzinger left a comment

Choose a reason for hiding this comment

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

I think the milestone here should be 9.5.x based on reading https://github.com/grafana/grafana/blob/afc9925dbfa55886cc85a9a2106ed9892d7df239/.github/bot.md#backport-pr

Cool mocking stuff :)

@idastambuk idastambuk removed this from the 10.0.0 milestone Apr 12, 2023
@idastambuk idastambuk added this to the 9.5.x milestone Apr 12, 2023
idastambuk and others added 2 commits April 12, 2023 17:24
Co-authored-by: Isabella Siu <Isabella.siu@grafana.com>
Co-authored-by: Isabella Siu <Isabella.siu@grafana.com>
@idastambuk idastambuk merged commit 6309d3f into main Apr 13, 2023
@idastambuk idastambuk deleted the cloudwatch/expression-refId-fix branch April 13, 2023 08:07
grafanabot pushed a commit that referenced this pull request Apr 13, 2023
Co-authored-by: Shirley <4163034+fridgepoet@users.noreply.github.com>
Co-authored-by: Isabella Siu <Isabella.siu@grafana.com>
(cherry picked from commit 6309d3f)
@grafanabot
Copy link
Contributor

The backport to v9.5.x failed:

Server Error

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new branch
git switch --create backport-66147-to-v9.5.x origin/v9.5.x
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x 6309d3fae62583c2570e6b975c7761215a830380
# Push it to GitHub
git push --set-upstream origin backport-66147-to-v9.5.x
git switch main
# Remove the local backport branch
git branch -D backport-66147-to-v9.5.x

Then, create a pull request where the base branch is v9.5.x and the compare/head branch is backport-66147-to-v9.5.x.

@grafanabot grafanabot added the backport-failed Failed to generate backport PR. Please resolve conflicts and create one manually. label Apr 13, 2023
alexmobo pushed a commit that referenced this pull request Apr 14, 2023
Co-authored-by: Shirley <4163034+fridgepoet@users.noreply.github.com>
Co-authored-by: Isabella Siu <Isabella.siu@grafana.com>
@guicaulada guicaulada modified the milestones: 9.5.x, 9.5.0 Apr 24, 2023
@zerok zerok modified the milestones: 9.5.0, 10.0.0 May 3, 2023
@zerok zerok modified the milestones: 10.0.0, 10.0.0-preview May 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
add to changelog area/backend backport v9.5.x Bot will automatically open backport PR backport-failed Failed to generate backport PR. Please resolve conflicts and create one manually. datasource/CloudWatch type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants