0

I am trying to get the metrics of Microsoft.Sql/servers/ resource: The resource_id is: /subscriptions/******-8**2-44**-95**-****13a5****/resourceGroups/SQLTesting/providers/Microsoft.Sql/servers/sqltest As in example metrics

resource_id="/subscriptions/******-8**2-44**-95**-****13a5****/resourceGroups/SQLTesting/providers/Microsoft.Sql/servers/sqltest"
today = datetime.datetime.now().date()
    yesterday = today - datetime.timedelta(days=1)

    metrics_data = client.metrics.list(
        resource_id,
        timespan="{}/{}".format(yesterday, today),
        interval='PT1H',
        metricnames='dtu_used',
        aggregation='Average'
    )

    for item in metrics_data.value:
        # azure.mgmt.monitor.models.Metric
        print("{} ({})".format(item.name.localized_value, item.unit.name))
        for timeserie in item.timeseries:
            for data in timeserie.data:
                # azure.mgmt.monitor.models.MetricData
                print("{}: {}".format(data.time_stamp, data.total))

But I am getting ErrorResponseException: Metric: dtu_used does not accept zero dimension case error. How to fix it?

gd1
  • 655
  • 1
  • 10
  • 21

2 Answers2

1

I can reproduce your issue, the metric dtu_used is for the sql db not the sql server, i.e for the resource type Microsoft.Sql/servers/databases, not Microsoft.Sql/servers, see this link.

To fix the issue, you need to use the resource_id with the resource id of a specific sql db, like /subscriptions/******-8**2-44**-95**-****13a5****/resourceGroups/SQLTesting/providers/Microsoft.Sql/servers/sqltest/databases/Mydatabase, then it will work fine.

Joy Wang
  • 39,905
  • 3
  • 30
  • 54
1

In metric definition, there is a property called isDimensionRequired, when querying for its value you must provide a filter for the dimension; if you do not do so the metric query will fail.

So, for dtu_used metric of SQL Server, the value is true which means that you need to provide the DataBaseResourceId in filter.

enter image description here

Here is a ample:

enter image description here

So, basically, you need to add a filter in the metric query.

Jack Jia
  • 5,268
  • 1
  • 12
  • 14