How to Use Azure Monitoring API to fetch Used Capacity?

Saksham Agarwal 5 Reputation points
2023-06-15T06:47:14.6266667+00:00

Hello , I am trying to get Used Capacity details using python on databricks. I have generated token using service principal. and when hitting the API it returns empty json.

import requests
from datetime import datetime, timedelta
from azure.identity import ClientSecretCredential

#start_time = (datetime.utcnow() - timedelta(hours=1)).replace(microsecond=0).isoformat() + "Z"
#end_time = datetime.utcnow().replace(microsecond=0).isoformat() + "Z"

start_time = (datetime.utcnow() - timedelta(hours=23)).replace(microsecond=0).isoformat() + "Z"
end_time = (datetime.utcnow() - timedelta(hours=22)).replace(microsecond=0).isoformat() + "Z"



#print("Start_Time:",start_time)
#print("End_Time:",end_time)

subscription_id = ""
resource_group = ""
storage_account_name = ""
metric_name = "UsedCapacity" 
time_range = f"{start_time}/{end_time}"  
api_version = "2021-05-01"

tenant_id = dbutils.secrets.get(scope="sn_scope",key="tenant-id")
client_id = dbutils.secrets.get(scope="sn_scope", key="clientid")
client_secret = dbutils.secrets.get(scope="sn_scope", key="secret")

token_endpoint = f"https://login.microsoftonline.com/{tenant_id}/oauth2/token" 

data = {
  "grant_type": "client_credentials",
  "client_id": f"{client_id}",  
  "client_secret": f"{client_secret}",  
  "resource": "https://management.azure.com/"
}

response = requests.post(token_endpoint, data=data)

if response.status_code == 200:
  post_response = response.text
  print("POST_Response:",post_response)
  access_token = response.json()["access_token"]
  #print("Access Token:", access_token)
else:
  print("Error generating access token:", response.status_code)


headers = {
  "Authorization": f"Bearer {access_token}",
  "Content-Type" : "application/json"
}


api_url = f"https://management.azure.com/subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/Microsoft.Storage/storageAccounts/{storage_account_name}/providers/microsoft.insights/metrics?timespan={time_range}&metricnames={metric_name}&api-version={api_version}"


print("API:",api_url)

response = requests.get(api_url,headers=headers)

if response.status_code == 200:
  data = response.text
  print("GET_Response:",data)
else:
  print("Error retrieving metrics data:", response.status_code)
  print("Error Message:",response.text)
Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
3,604 questions
Azure Storage Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,488 questions
Azure Databricks
Azure Databricks
An Apache Spark-based analytics platform optimized for Azure.
2,439 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Stanislav Zhelyazkov 27,556 Reputation points MVP Moderator
    2023-06-15T07:36:23.2533333+00:00

    Hi,

    Can you try to add the following properties in the URL as well and see if it works:

    Namespace=microsoft.storage%2Fstorageaccounts
    aggregation=Average
    interval=PT1
    

    Please "Accept the answer" if the information helped you. This will help us and others in the community as well.


  2. David Hagan 0 Reputation points
    2025-04-11T19:48:39.4733333+00:00

    I will vouch that the code works, thanks OP.

    I did have to ensure I had the resource endpoints and the URL fqdn portion and token_endpoint correct for my particular cloud.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.