File: test_metrics_client.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (60 lines) | stat: -rw-r--r-- 2,642 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See LICENSE.txt in the project root for
# license information.
# -------------------------------------------------------------------------
from datetime import timedelta

from azure.monitor.query import MetricsClient, MetricAggregationType
from azure.monitor.query._version import VERSION

from base_testcase import MetricsClientTestCase


METRIC_NAME = "requests/count"
METRIC_RESOURCE_PROVIDER = "Microsoft.Insights/components"


class TestMetricsClient(MetricsClientTestCase):

    def test_batch_metrics_auth(self, recorded_test, monitor_info):
        client: MetricsClient = self.get_client(MetricsClient, self.get_credential(MetricsClient))
        responses = client.query_resources(
            resource_ids=[monitor_info["metrics_resource_id"]],
            metric_namespace=METRIC_RESOURCE_PROVIDER,
            metric_names=[METRIC_NAME],
            aggregations=[MetricAggregationType.COUNT],
        )
        assert responses
        assert len(responses) == 1

    def test_batch_metrics_granularity(self, recorded_test, monitor_info):
        client: MetricsClient = self.get_client(MetricsClient, self.get_credential(MetricsClient))
        responses = client.query_resources(
            resource_ids=[monitor_info["metrics_resource_id"]],
            metric_namespace=METRIC_RESOURCE_PROVIDER,
            metric_names=[METRIC_NAME],
            granularity=timedelta(minutes=5),
            aggregations=[MetricAggregationType.COUNT],
        )
        assert responses
        for response in responses:
            assert response.granularity == timedelta(minutes=5)
            response.metrics
            metric = response.metrics[METRIC_NAME]
            assert metric.timeseries
            for t in metric.timeseries:
                assert t.metadata_values is not None

    def test_client_different_endpoint(self):
        credential = self.get_credential(MetricsClient)
        endpoint = "https://usgovvirginia.metrics.monitor.azure.us"
        audience = "https://metrics.monitor.azure.us"
        client = MetricsClient(endpoint, credential, audience=audience)

        assert client._endpoint == endpoint
        assert f"{audience}/.default" in client._client._config.authentication_policy._scopes

    def test_client_user_agent(self):
        client: MetricsClient = self.get_client(MetricsClient, self.get_credential(MetricsClient))
        assert f"monitor-query/{VERSION}" in client._client._config.user_agent_policy.user_agent