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

AzureMonitor: Ensure resourceURI template variable is migrated #56095

Merged
merged 4 commits into from Oct 4, 2022

Conversation

aangelisc
Copy link
Contributor

Related to grafana/support-escalations#4104. #52897 removed a check on the existence of the resourceUri prop. This then leads to a query containing a resource URI that is a template variable being filtered out.

This PR readds the check on resourceUri to ensure queries will get migrated.

- Do not filter queries containing a resource URI template
- Update migration
- Add test
@aangelisc aangelisc added type/bug effort/small datasource/Azure Azure Monitor Datasource add to changelog backport v9.2.x Mark PR for automatic backport to v9.2.x labels Sep 30, 2022
@aangelisc aangelisc added this to the 9.2.0 milestone Sep 30, 2022
@aangelisc aangelisc self-assigned this Sep 30, 2022
@aangelisc aangelisc requested a review from a team as a code owner September 30, 2022 13:40
@aangelisc aangelisc requested review from andresmgot and yaelleC and removed request for a team September 30, 2022 13:40
@github-actions
Copy link
Contributor

Backend code coverage report for PR #56095
No changes

@github-actions
Copy link
Contributor

Frontend code coverage report for PR #56095

Plugin Main PR Difference
grafana-azure-monitor-datasource 76.27% 76.27% 0%

Copy link
Contributor

@andresmgot andresmgot left a comment

Choose a reason for hiding this comment

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

LGTM! Just one question here.

BTW, it may be worth it to also backport this to 9.1.x (just in case the user cannot upgrade to the new minor anytime soon)

@@ -146,7 +146,7 @@ function migrateDimensionToResourceObj(query: AzureMonitorQuery): AzureMonitorQu
resourceGroup: details?.resourceGroup,
metricNamespace: details?.metricNamespace,
resourceName: details?.resourceName,
resourceUri: undefined,
resourceUri: details.subscription ? undefined : query.azureMonitor.resourceUri,
Copy link
Contributor

Choose a reason for hiding this comment

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

🤔 why does this depend on having a subscription if the resourceUri already includes it?

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 added this to maintain the behaviour of setting the resourceUri to undefined. I'm assuming (it may be a poor assumption) that if the subscription value is set then we can unset the resourceUri, otherwise we maintain the value so that the migration can be carried out later when applyTemplateVariables is called.

The current behaviour of resourceUri: undefined means that we remove the template variable value before it is actually applied.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would not remove it if it's defined: If it's defined, it should take precedence over the rest of params (subs, group, name...) and the backend will skip the URI generation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does this not go against our deprecation of resourceUri?

Copy link
Contributor

@andresmgot andresmgot Oct 3, 2022

Choose a reason for hiding this comment

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

You are right, we need to clean up the resourceUri value, since if not, the UI, showing a subs + group + name, won't reflect what is actually being used in the backend (the resource URI). BTW, that also means that even after this PR, the client won't be able to see the selection in the resource picker (even though the query will be executed).

I was thinking about our conversation in #55372. If we have a template variable with multiple resources from different resource groups, the more straight forward way to support that is to have a list of resource IDs so supporting both formats sounds like something we will need to do. In any case, it's not something we are prepared to do at the moment.

Going to the original comment here, the condition is probably better to also take into account the resource group and name, since sometimes the subscription is automatically added using the default value from the data source configuration.

Suggested change
resourceUri: details.subscription ? undefined : query.azureMonitor.resourceUri,
resourceUri: (details.subscription && details?.resourceGroup && details?.resourceName) ? undefined : query.azureMonitor.resourceUri,

@aangelisc aangelisc added the backport v9.1.x Bot will automatically open backport PR label Sep 30, 2022
},
});
const templatedQuery = ctx.ds.azureMonitorDatasource.applyTemplateVariables(query, {});
expect(templatedQuery).toMatchObject({
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we have a test for not undefined resourceUri ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean a test for the case where resourceUri is entirely undefined or a test for the case where we return the resourceUri because it could not be parsed?

resourceUri:
details?.subscription && details?.resourceGroup && details?.resourceName
? undefined
: query.azureMonitor.resourceUri,
Copy link
Contributor

Choose a reason for hiding this comment

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

if we leave the resourceUri , does it mean the query will be migrated again next time? As query.azureMonitor?.resourceUri will be true?
Maybe I'm misunderstanding what you're trying to achieve here 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the resourceUri is a template variable then we'd require the query to be migrated every time to account for any changes to the variable 😊

The goal of this PR is to ensure that if a template variable is used for the resourceUri (as is the case for the support escalation) then it should be appropriately migrated to the relevant components.

Copy link
Contributor

Choose a reason for hiding this comment

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

well, we don't need to migrate it in the case it's a template variable, since it will never be parsed. Maybe you can update the condition to something like:

  if (query.azureMonitor?.resourceUri && !query.azureMonitor.resourceUri.startsWith('$')) {

To be clear that template variables won't be migrated (but the result will be the same than with this code).

What would be nice, is to modify the "Advance" section of the resource picker to show the resource URI (in this case, the template variable) rather than the empty fields that they are going to see for the Subscription + Resource Group + Resource Name. But you can do that in a separate PR

Copy link
Contributor

Choose a reason for hiding this comment

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

Gotcha! Thanks, I think Andres' suggestion makes sense, maybe we can add some comment in the code for the next person that is trying to make sense of it?

// migrating if the query is using the deprecated `resourceURI` prop and it's not a template variable

Could we always set the uri as undefined if we change the condition then?
If not, could we maybe extract details?.subscription && details?.resourceGroup && details?.resourceName and give it a name that would help us understand what we are trying to achieve, such as isWellFormedURI or isNotTemplateVariableURI?

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 it's helpful to not set always the uri as undefined, to cover corner cases, like template variables covering weird parts of the resource URI (e.g. /subscriptions/$restOfTheThing). +1 to define the condition as a variable.

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've made the suggested changes and will set this to auto-merge 😊 Thanks!

@aangelisc aangelisc modified the milestones: 9.2.0, 9.1.7 Oct 4, 2022
@aangelisc aangelisc enabled auto-merge (squash) October 4, 2022 08:23
Copy link
Contributor

@yaelleC yaelleC left a comment

Choose a reason for hiding this comment

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

LGTM thanks for the changes 👍

Copy link
Contributor

@andresmgot andresmgot left a comment

Choose a reason for hiding this comment

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

👍

@grafanabot
Copy link
Contributor

@aangelisc aangelisc merged commit 462ca50 into main Oct 4, 2022
@aangelisc aangelisc deleted the 4104-azure-monitor-update-migration branch October 4, 2022 08:44
grafanabot pushed a commit that referenced this pull request Oct 4, 2022
* Ensure resourceURI template vars are migrated

- Do not filter queries containing a resource URI template
- Update migration
- Add test

* Update condition

* Review

(cherry picked from commit 462ca50)
grafanabot pushed a commit that referenced this pull request Oct 4, 2022
* Ensure resourceURI template vars are migrated

- Do not filter queries containing a resource URI template
- Update migration
- Add test

* Update condition

* Review

(cherry picked from commit 462ca50)
aangelisc added a commit that referenced this pull request Oct 4, 2022
… (#56249)

* Ensure resourceURI template vars are migrated

- Do not filter queries containing a resource URI template
- Update migration
- Add test

* Update condition

* Review

(cherry picked from commit 462ca50)

Co-authored-by: Andreas Christou <andreas.christou@grafana.com>
aangelisc added a commit that referenced this pull request Oct 4, 2022
… (#56250)

* Ensure resourceURI template vars are migrated

- Do not filter queries containing a resource URI template
- Update migration
- Add test

* Update condition

* Review

(cherry picked from commit 462ca50)

Co-authored-by: Andreas Christou <andreas.christou@grafana.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
add to changelog area/frontend backport v9.1.x Bot will automatically open backport PR backport v9.2.x Mark PR for automatic backport to v9.2.x datasource/Azure Azure Monitor Datasource effort/small enterprise-ok type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants