Azure / azure-cli

Azure Command-Line Interface
MIT License
3.98k stars 2.95k forks source link

az monitor scheduled-query create Condition with "=" fails #27138

Closed bryanhunwardsen closed 1 year ago

bryanhunwardsen commented 1 year ago

Describe the bug

Execute: az monitor scheduled-query create With: --condition "count 'SEARCH_QUERY' = 0" (all other parameters being valid including SEARCH_QUERY) Fails: ERROR: (BadRequest) Invalid input at properties.criteria.allOf[0].operator: Requested value 'Equals' was not found.

Alternatively, same call using: --condition "count 'SEARCH_QUERY' < 1" Succeeds(!)

Documentation for "az monitor scheduled-query create" indicates "=" is a valid value: https://github.com/MicrosoftDocs/azure-docs-cli/blob/d8571c97f9e8edaace4dc67ba28a025a2e3396cd/latest/docs-ref-autogen/monitor/scheduled-query.yml#L46

Searching the error directs to a bug report that may very well be related to the root cause here, although it does not appear to have been addressed: https://github.com/Azure/azure-rest-api-specs/issues/21794

If "az monitor scheduled-query create" executes via the rest api and is binding "=" to "Equals" when the correct expected value is "Equal" this could be the cause.

When I create this alert in the portal and then view the json view the value is: "operator": "Equal",

{ "properties": { "criteria": { "allOf": [ { "query": "traces | where timestamp > ago(15m) and message == 'Heartbeat'", "timeAggregation": "Count", "dimensions": [], "resourceIdColumn": "", "operator": "Equal", "threshold": 0, "failingPeriods": { "numberOfEvaluationPeriods": 1, "minFailingPeriodsToAlert": 1 } } ] } } }

Related command

az monitor scheduled-query create --condition

Errors

ERROR: (BadRequest) Invalid input at properties.criteria.allOf[0].operator: Requested value 'Equals' was not found.

Issue script & Debug output

az monitor scheduled-query create --subscription "aaaaaaaa-1111-bbbb-2222-cccccccccccc" --condition "count 'SEARCH_QUERY' = 0" --name "Test Alert Name" --resource-group "TestResourceGroup" --scopes "/subscriptions/aaaaaaaa-1111-bbbb-2222-cccccccccccc/resourceGroups/TestResourceGroup/providers/microsoft.insights/components/TestAppInsightsInstance" --auto-mitigate true --condition-query SEARCH_QUERY="traces | where timestamp > ago(15m) and message == 'Heartbeat'" --description "Alert Description" --evaluation-frequency "1d" --location "southcentralus" --severity "3" --window-size "1d";

cli.knack.cli: Command arguments: ['monitor', 'scheduled-query', 'create', '--subscription', 'aaaaaaaa-1111-bbbb-2222-cccccccccccc', '--condition', "count 'SEARCH_QUERY' = 0", '--name', 'Test Alert Name', '--resource-group', 'TestResourceGroup', '--scopes', '/subscriptions/aaaaaaaa-1111-bbbb-2222-cccccccccccc/resourceGroups/TestResourceGroup/providers/microsoft.insights/components/TestAppInsightsInstance', '--auto-mitigate', 'true', '--condition-query', "SEARCH_QUERY=traces | where timestamp > ago(15m) and message == 'Heartbeat'", '--description', 'Alert Description', '--evaluation-frequency', '1d', '--location', 'southcentralus', '--severity', '3', '--window-size', '1d', '--debug'] cli.knack.cli: init debug log: Enable color in terminal. cli.knack.cli: Event: Cli.PreExecute [] cli.knack.cli: Event: CommandParser.OnGlobalArgumentsCreate [<function CLILogging.on_global_arguments at 0x03C3B538>, <function OutputProducer.on_global_arguments at 0x03E3C778>, <function CLIQuery.on_global_arguments at 0x03E583D0>] cli.knack.cli: Event: CommandInvoker.OnPreCommandTableCreate [] cli.azure.cli.core: Modules found from index for 'monitor': ['azure.cli.command_modules.monitor', 'azext_alertsmanagement', 'azext_applicationinsights', 'azext_scheduled_query'] cli.azure.cli.core: Loading command modules: cli.azure.cli.core: Name Load Time Groups Commands cli.azure.cli.core: monitor 0.224 29 83 cli.azure.cli.core: Total (1) 0.224 29 83 cli.azure.cli.core: These extensions are not installed and will be skipped: ['azext_ai_examples', 'azext_next'] cli.azure.cli.core: Loading extensions: cli.azure.cli.core: Name Load Time Groups Commands Directory cli.azure.cli.core: alertsmanagement 0.015 4 6 C:\Users\user01.azure\cliextensions\alertsmanagement cli.azure.cli.core: application-insights 0.007 10 30 C:\Users\user01.azure\cliextensions\application-insights cli.azure.cli.core: scheduled-query 0.006 2 5 C:\Users\user01.azure\cliextensions\scheduled-query cli.azure.cli.core: Total (3) 0.028 16 41 cli.azure.cli.core: Loaded 42 groups, 124 commands. cli.azure.cli.core: Found a match in the command table. cli.azure.cli.core: Raw command : monitor scheduled-query create cli.azure.cli.core: Command table: monitor scheduled-query create cli.knack.cli: Event: CommandInvoker.OnPreCommandTableTruncate [<function AzCliLogging.init_command_file_logging at 0x048DE580>] cli.azure.cli.core.azlogging: metadata file logging enabled - writing logs to 'C:\Users\user01.azure\commands\2023-08-09.17-19-24.monitor_scheduled-query_create.42956.log'. az_command_data_logger: command args: monitor scheduled-query create --subscription {} --condition {} --name {} --resource-group {} --scopes {} --auto-mitigate {} --condition-query {} --description {} --evaluation-frequency {} --location {} --severity {} --window-size {} --debug cli.knack.cli: Event: CommandInvoker.OnPreArgumentLoad [<function register_global_subscription_argument..add_subscription_parameter at 0x049056A0>] cli.knack.cli: Event: CommandInvoker.OnPostArgumentLoad [] cli.knack.cli: Event: CommandInvoker.OnPostCommandTableCreate [<function register_ids_argument..add_ids_arguments at 0x04914610>, <function register_cache_arguments..add_cache_arguments at 0x04914808>] cli.knack.cli: Event: CommandInvoker.OnCommandTableLoaded [] cli.knack.cli: Event: CommandInvoker.OnPreParseArgs [] cli.knack.cli: Event: CommandInvoker.OnPostParseArgs [<function OutputProducer.handle_output_argument at 0x03E3C7C0>, <function CLIQuery.handle_query_parameter at 0x03E58418>, <function register_ids_argument..parse_ids_arguments at 0x049147C0>] az_command_data_logger: extension name: scheduled-query az_command_data_logger: extension version: 0.5.1 cli.azure.cli.core.commands.client_factory: Getting management service client client_type=MonitorManagementClient cli.azure.cli.core.auth.persistence: build_persistence: location='C:\Users\user01\.azure\msal_token_cache.bin', encrypt=True cli.azure.cli.core.auth.binary_cache: load: C:\Users\user01.azure\msal_http_cache.bin urllib3.util.retry: Converted retries value: 1 -> Retry(total=1, connect=None, read=None, redirect=None, status=None) msal.authority: openid_config = {'token_endpoint': 'https://login.microsoftonline.com/aaaaaaaa-1111-BBBB-2222-cccccccccccc/oauth2/v2.0/token', 'token_endpoint_auth_methods_supported': ['client_secret_post', 'private_key_jwt', 'client_secret_basic'], 'jwks_uri': 'https://login.microsoftonline.com/aaaaaaaa-1111-BBBB-2222-cccccccccccc/discovery/v2.0/keys', 'response_modes_supported': ['query', 'fragment', 'form_post'], 'subject_types_supported': ['pairwise'], 'id_token_signing_alg_values_supported': ['RS256'], 'response_types_supported': ['code', 'id_token', 'code id_token', 'id_token token'], 'scopes_supported': ['openid', 'profile', 'email', 'offline_access'], 'issuer': 'https://login.microsoftonline.com/aaaaaaaa-1111-BBBB-2222-cccccccccccc/v2.0', 'request_uri_parameter_supported': False, 'userinfo_endpoint': 'https://graph.microsoft.com/oidc/userinfo', 'authorization_endpoint': 'https://login.microsoftonline.com/aaaaaaaa-1111-BBBB-2222-cccccccccccc/oauth2/v2.0/authorize', 'device_authorization_endpoint': 'https://login.microsoftonline.com/aaaaaaaa-1111-BBBB-2222-cccccccccccc/oauth2/v2.0/devicecode', 'http_logout_supported': True, 'frontchannel_logout_supported': True, 'end_session_endpoint': 'https://login.microsoftonline.com/aaaaaaaa-1111-BBBB-2222-cccccccccccc/oauth2/v2.0/logout', 'claims_supported': ['sub', 'iss', 'cloud_instance_name', 'cloud_instance_host_name', 'cloud_graph_host_name', 'msgraph_host', 'aud', 'exp', 'iat', 'auth_time', 'acr', 'nonce', 'preferred_username', 'name', 'tid', 'ver', 'at_hash', 'c_hash', 'email'], 'kerberos_endpoint': 'https://login.microsoftonline.com/aaaaaaaa-1111-BBBB-2222-cccccccccccc/kerberos', 'tenant_region_scope': 'NA', 'cloud_instance_name': 'microsoftonline.com', 'cloud_graph_host_name': 'graph.windows.net', 'msgraph_host': 'graph.microsoft.com', 'rbac_url': 'https://pas.windows.net'} msal.application: Broker enabled? False cli.azure.cli.core.auth.credential_adaptor: CredentialAdaptor.get_token: scopes=('https://management.core.windows.net//.default',), kwargs={} cli.azure.cli.core.auth.msal_authentication: UserCredential.get_token: scopes=('https://management.core.windows.net//.default',), claims=None, kwargs={} msal.application: Cache hit an AT msal.telemetry: Generate or reuse correlation_id: 3be9b430-0a9c-4cee-a972-941d7627c811 cli.azure.cli.core.sdk.policies: Request URL: 'https://management.azure.com/subscriptions/aaaaaaaa-1111-bbbb-2222-cccccccccccc/resourceGroups/TestResourceGroup/providers/Microsoft.Insights/scheduledQueryRules/Test%20Alert%20Name?api-version=2021-08-01' cli.azure.cli.core.sdk.policies: Request method: 'PUT' cli.azure.cli.core.sdk.policies: Request headers: cli.azure.cli.core.sdk.policies: 'Content-Type': 'application/json' cli.azure.cli.core.sdk.policies: 'Content-Length': '782' cli.azure.cli.core.sdk.policies: 'Accept': 'application/json' cli.azure.cli.core.sdk.policies: 'x-ms-client-request-id': '8f70a4c7-3713-11ee-a77c-fc7774f4dd59' cli.azure.cli.core.sdk.policies: 'CommandName': 'monitor scheduled-query create' cli.azure.cli.core.sdk.policies: 'ParameterSetName': '--subscription --condition --name --resource-group --scopes --auto-mitigate --condition-query --description --evaluation-frequency --location --severity --window-size --debug' cli.azure.cli.core.sdk.policies: 'User-Agent': 'AZURECLI/2.51.0 (MSI) azsdk-python-mgmt-monitor/1.0.0b1 Python/3.10.10 (Windows-10-10.0.19044-SP0)' cli.azure.cli.core.sdk.policies: 'Authorization': '*****' cli.azure.cli.core.sdk.policies: Request body: cli.azure.cli.core.sdk.policies: {"location": "southcentralus", "properties": {"description": "Alert Description", "severity": 3, "enabled": true, "scopes": ["/subscriptions/aaaaaaaa-1111-bbbb-2222-cccccccccccc/resourceGroups/TestResourceGroup/providers/microsoft.insights/components/TestAppInsightsInstance"], "evaluationFrequency": "P1D", "windowSize": "P1D", "criteria": {"allOf": [{"query": "traces | where timestamp > ago(15m) and message == 'Heartbeat'", "timeAggregation": "Count", "dimensions": [], "operator": "Equals", "threshold": 0.0, "failingPeriods": {"numberOfEvaluationPeriods": 1, "minFailingPeriodsToAlert": 1}}]}, "actions": {"actionGroups": [], "customProperties": {}}, "checkWorkspaceAlertsStorageConfigured": false, "skipQueryValidation": false, "autoMitigate": true}} urllib3.connectionpool: Starting new HTTPS connection (1): management.azure.com:443 urllib3.connectionpool: https://management.azure.com:443 "PUT /subscriptions/aaaaaaaa-1111-bbbb-2222-cccccccccccc/resourceGroups/TestResourceGroup/providers/Microsoft.Insights/scheduledQueryRules/Test%20Alert%20Name?api-version=2021-08-01 HTTP/1.1" 400 139 cli.azure.cli.core.sdk.policies: Response status: 400 cli.azure.cli.core.sdk.policies: Response headers: cli.azure.cli.core.sdk.policies: 'Cache-Control': 'no-cache' cli.azure.cli.core.sdk.policies: 'Pragma': 'no-cache' cli.azure.cli.core.sdk.policies: 'Content-Length': '139' cli.azure.cli.core.sdk.policies: 'Content-Type': 'application/json' cli.azure.cli.core.sdk.policies: 'Expires': '-1' cli.azure.cli.core.sdk.policies: 'x-ms-ratelimit-remaining-subscription-writes': '1199' cli.azure.cli.core.sdk.policies: 'X-Rate-Limit-Limit': '1m' cli.azure.cli.core.sdk.policies: 'X-Rate-Limit-Remaining': '14' cli.azure.cli.core.sdk.policies: 'X-Rate-Limit-Reset': '2023-08-10T00:20:26.4144095Z' cli.azure.cli.core.sdk.policies: 'Server': 'Kestrel' cli.azure.cli.core.sdk.policies: 'x-ms-request-id': '9e3c7bf6-57ca-4773-b92f-19eb7e3dddd2' cli.azure.cli.core.sdk.policies: 'x-ms-correlation-request-id': '9e3c7bf6-57ca-4773-b92f-19eb7e3dddd2' cli.azure.cli.core.sdk.policies: 'x-ms-routing-request-id': 'WESTUS2:20230810T001926Z:9e3c7bf6-57ca-4773-b92f-19eb7e3dddd2' cli.azure.cli.core.sdk.policies: 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains' cli.azure.cli.core.sdk.policies: 'X-Content-Type-Options': 'nosniff' cli.azure.cli.core.sdk.policies: 'Date': 'Thu, 10 Aug 2023 00:19:26 GMT' cli.azure.cli.core.sdk.policies: Response content: cli.azure.cli.core.sdk.policies: {"error":{"code":"BadRequest","message":"Invalid input at properties.criteria.allOf[0].operator: Requested value 'Equals' was not found."}} cli.azure.cli.core.azclierror: Traceback (most recent call last): File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\knack/cli.py", line 233, in invoke File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/init.py", line 663, in execute File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/init.py", line 726, in _run_jobs_serially File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/init.py", line 697, in _run_job File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/init.py", line 333, in call File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/command_operation.py", line 121, in handler File "C:\Users\user01.azure\cliextensions\scheduled-query\azext_scheduled_query\custom.py", line 73, in create_scheduled_query return client.create_or_update(resource_group_name=resource_group_name, rule_name=rule_name, parameters=parameters) File "D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/core/tracing/decorator.py", line 78, in wrapper_use_tracer File "C:\Users\user01.azure\cliextensions\scheduled-query\azext_scheduled_query\vendored_sdks\azure_mgmt_scheduled_query\operations_scheduled_query_rules_operations.py", line 515, in create_or_update raise HttpResponseError(response=response, model=error, error_format=ARMErrorFormat) azure.core.exceptions.HttpResponseError: (BadRequest) Invalid input at properties.criteria.allOf[0].operator: Requested value 'Equals' was not found. Code: BadRequest Message: Invalid input at properties.criteria.allOf[0].operator: Requested value 'Equals' was not found.

cli.azure.cli.core.azclierror: (BadRequest) Invalid input at properties.criteria.allOf[0].operator: Requested value 'Equals' was not found. Code: BadRequest Message: Invalid input at properties.criteria.allOf[0].operator: Requested value 'Equals' was not found. az_command_data_logger: (BadRequest) Invalid input at properties.criteria.allOf[0].operator: Requested value 'Equals' was not found. Code: BadRequest Message: Invalid input at properties.criteria.allOf[0].operator: Requested value 'Equals' was not found. cli.knack.cli: Event: Cli.PostExecute [<function AzCliLogging.deinit_cmd_metadata_logging at 0x048DE6A0>] az_command_data_logger: exit code: 1 cli.main: Command ran in 2.930 seconds (init: 0.528, invoke: 2.402) telemetry.main: Begin splitting cli events and extra events, total events: 1 telemetry.client: Accumulated 0 events. Flush the clients. telemetry.main: Finish splitting cli events and extra events, cli events: 1 telemetry.save: Save telemetry record of length 3865 in cache telemetry.main: Begin creating telemetry upload process. telemetry.process: Creating upload process: "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\python.exe C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\Lib\site-packages\azure\cli\telemetry__init__.pyc C:\Users\user01.azure" telemetry.process: Return from creating process telemetry.main: Finish creating telemetry upload process.

Expected behavior

Using "=" in condition parameter should produce a successful result to mirror what is in the doumentation and what is available through the azure portal.

Environment Summary

azure-cli 2.49.0 *

core 2.49.0 telemetry 1.0.8

Extensions: account 0.2.5 alertsmanagement 0.2.3 application-insights 0.1.16 azure-devops 0.26.0 scheduled-query 0.5.1

Dependencies: msal 1.20.0 azure-mgmt-resource 22.0.0

Python location 'C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\python.exe' Extensions directory 'C:\Users\bryhun01.azure\cliextensions'

Python (Windows) 3.10.10 (tags/v3.10.10:aad5f6a, Feb 7 2023, 17:05:00) [MSC v.1929 32 bit (Intel)]

Additional context

No response

azure-client-tools-bot-prd[bot] commented 1 year ago

Hi @bryanhunwardsen,

2.49.0 is not the latest Azure CLI(2.51.0).

Please upgrade to the latest Azure CLI version by following https://learn.microsoft.com/en-us/cli/azure/update-azure-cli.

yonzhan commented 1 year ago

Thank you for opening this issue, we will look into it.

bryanhunwardsen commented 1 year ago

Hi @bryanhunwardsen,

2.49.0 is not the latest Azure CLI(2.51.0).

Please upgrade to the latest Azure CLI version by following https://learn.microsoft.com/en-us/cli/azure/update-azure-cli.

I upgraded to 2.51.0 the issue still exists

AllyW commented 1 year ago

@bryanhunwardsen hi Bryan, very informative debug log. I've made a pr to fix this operator mapping issue. please refer to https://github.com/Azure/azure-cli-extensions/pull/6669.

bryanhunwardsen commented 1 year ago

@AllyW @yonzhan I see the PR merged 2 days ago, but I still experience the error. It is not clear from the documentation if there is something I need to do to avail myself of the update as an end user trying to create an alert rule via the az api as listed in the description. Any direction here appreciated. Thx

AllyW commented 1 year ago

@bryanhunwardsen , az monitor scheduled-query create -h could provide some help when you manage your query conditions, but yes, the api doc sometime is not consistent with the azure service code and we will co-work with service team to refine it. If any other errors occur, you can add it here and we will help with it, along with azure services colleagues.

bryanhunwardsen commented 1 year ago

@AllyW @yonzhan - My prior comment was insufficiently clear.

There has been no change in performance, the original reported issue still exists(!) "az monitor scheduled-query create" still fails when "=" is used as documented in the opening post to this issue.

This issue should not be closed until it is resolved and working.

AllyW commented 1 year ago

@bryanhunwardsen , I tested in my local env and "=" works in condition parameter for az monitor scheduled-query create now.

‘scheduled-query’ is in az extension repo and you can use az extension update --name scheduled-query to update it to the latest version which is 0.5.2.

When executing cmd, you can add --debug to check out exactly where errors come from when parsing the condition inputs, like below. And you can also find out which version scheduled-query is in with --debug. Hope that helps.

image image

bryanhunwardsen commented 1 year ago

@AllyW Thank you that was the missing piece! I dont recall explicitly installing the extension, so Im assuming the first time it gets called it gets installed silently. I had no reference to think of it as anything more than part of the main az suite that might require a new release to pick up the changes. Now I know ;-)