Original post was here.
I try to follow MS official doc to get the log from my resource in Azure Log Monitor but never success. My code is like below.
from azure.loganalytics import LogAnalyticsDataClient
from azure.common.client_factory import get_client_from_cli_profile
from azure.loganalytics.models import QueryBody
log_client = get_client_from_cli_profile(LogAnalyticsDataClient)
myWorkSpaceId = '1234567890...'
result = log_client.query(myWorkSpaceId, QueryBody(**{'query': 'Heartbeat| limit 50'}))
And I always get exception like below:
result = log_client.query(myWorkSpaceId, QueryBody(**{'query': 'Heartbeat| limit 50'}))
File ".../lib/python2.7/site-packages/azure/loganalytics/log_analytics_data_client.py", line 121, in query
raise models.ErrorResponseException(self._deserialize, response)
azure.loganalytics.models.error_response.ErrorResponseException: (MissingApiVersionParameter)
The api-version query parameter (?api-version=) is required for all requests
I trace code into library in /azure/loganalytics/log_analytics_data_client.py, and dump the url string used for query like below.
print(url, query_parameters, header_parameters, body_content)
request = self._client.post(url, query_parameters)
response = self._client.send(request, header_parameters, body_content, stream=False, **operation_config)
The output of the url and query information is like below and it look like no version information in between and I doubt this is why I get the exception:
('https://management.azure.com/workspaces/1234567890.../query', {}, {'Content-Type': 'application/json; charset=utf-8'}, {'query': 'Heartbeat| limit 50'})
My azure SDK version is 4.0.0, and my azure-loganalytics library version is v0.1.0, running on Ubuntu. Does anyone run into same issue or know how to fix this?
Thanks.
@alexeldeib any idea? Because in your query test, there is no API version, so I'm quite confused about what is happening here? Is this an authentication problem with an incorrect message?
@dennissian it's because of get_client_from_cli_profile:
log_client = get_client_from_cli_profile(LogAnalyticsDataClient)
This does two things which will cause issues for data plane calls: uses management.azure.com as the authorization resource, and sets the base_url to the same.
You can pass base_url='https://api.loganalytics.io/v1 as a keyword argument to get_client_from_cli_profile, but the token resource will still be wrong. I think the best way is probably to do something like:
creds, _ = get_azure_cli_credentials(resource="https://api.loganalytics.io")
client = LogAnalyticsDataClient(creds)
@lmazuel modifying get_client_from_cli_profile to support a resource kwarg instead of using this line and this function could work. I see Key Vault required some customization, could also go that route.
BTW, the debug URL hints at the issue here: https://management.azure.com/workspaces/1234567890.../query doesn't have the structure of an Azure resource URL like https://management.azure.com/subscriptions/GUID/resourceGroups/STRING/etc...
@alexeldeib @lmazuel
Thanks for your help, the provided solution can work.
Most helpful comment
@alexeldeib @lmazuel
Thanks for your help, the provided solution can work.