def test_metrics_namespaces():
    client = MetricsQueryClient(_credential())

    response = client.list_metric_namespaces(
        os.environ['METRICS_RESOURCE_URI'])

    assert response is not None
def test_metrics_definitions():
    client = MetricsQueryClient(_credential())

    response = client.list_metric_definitions(
        os.environ['METRICS_RESOURCE_URI'],
        namespace='microsoft.eventgrid/topics')

    assert response is not None
Ejemplo n.º 3
0
def test_metrics_auth():
    credential = _credential()
    client = MetricsQueryClient(credential)
    response = client.query(os.environ['METRICS_RESOURCE_URI'],
                            metric_names=["MatchedEventCount"],
                            timespan=timedelta(days=1),
                            aggregations=[MetricAggregationType.COUNT])
    assert response
    assert response.metrics
Ejemplo n.º 4
0
def test_metrics_auth():
    credential = _credential()
    client = MetricsQueryClient(credential)
    # returns LogsQueryResults
    response = client.query(os.environ['METRICS_RESOURCE_URI'],
                            metric_names=["PublishSuccessCount"],
                            timespan='P2D')

    assert response is not None
    assert response.metrics is not None
def test_metrics_granularity():
    credential = _credential()
    client = MetricsQueryClient(credential)
    response = client.query_resource(
        os.environ['METRICS_RESOURCE_URI'],
        metric_names=["MatchedEventCount"],
        timespan=timedelta(days=1),
        granularity=timedelta(minutes=5),
        aggregations=[MetricAggregationType.COUNT])
    assert response
    assert response.granularity == timedelta(minutes=5)
def test_metrics_filter():
    credential = _credential()
    client = MetricsQueryClient(credential)
    response = client.query_resource(
        os.environ['METRICS_RESOURCE_URI'],
        metric_names=["MatchedEventCount"],
        timespan=timedelta(days=1),
        granularity=timedelta(minutes=5),
        filter="EventSubscriptionName eq '*'",
        aggregations=[MetricAggregationType.COUNT])
    assert response
    metric = response.metrics['MatchedEventCount']
    for t in metric.timeseries:
        assert t.metadata_values is not None
def test_metrics_list():
    credential = _credential()
    client = MetricsQueryClient(credential)
    response = client.query_resource(
        os.environ['METRICS_RESOURCE_URI'],
        metric_names=["MatchedEventCount"],
        timespan=timedelta(days=1),
        granularity=timedelta(minutes=5),
        aggregations=[MetricAggregationType.COUNT])
    assert response
    metrics = response.metrics
    assert len(metrics) == 1
    assert metrics[0].__class__ == Metric
    assert metrics['MatchedEventCount'].__class__ == Metric
    assert metrics['MatchedEventCount'] == metrics[0]
Ejemplo n.º 8
0
class MetricsPerfTest(PerfStressTest):
    def __init__(self, arguments):
        super().__init__(arguments)

        # auth configuration
        self.metrics_uri = self.get_from_env('METRICS_RESOURCE_URI')
        self.names = ["MatchedEventCount"]
        self.aggregations = [MetricAggregationType.COUNT]

        # Create clients
        self.metrics_client = SyncMetricsQueryClient(
            credential=SyncDefaultAzureCredential())
        self.async_metrics_client = AsyncMetricsQueryClient(
            credential=AsyncDefaultAzureCredential())

    async def close(self):
        """This is run after cleanup.
        
        Use this to close any open handles or clients.
        """
        await self.async_metrics_client.close()
        await super().close()

    def run_sync(self):
        """The synchronous perf test.
        
        Try to keep this minimal and focused. Using only a single client API.
        Avoid putting any ancilliary logic (e.g. generating UUIDs), and put this in the setup/init instead
        so that we're only measuring the client API call.
        """
        self.metrics_client.query(self.metrics_uri,
                                  self.names,
                                  aggregations=self.aggregations)

    async def run_async(self):
        """The asynchronous perf test.
        
        Try to keep this minimal and focused. Using only a single client API.
        Avoid putting any ancilliary logic (e.g. generating UUIDs), and put this in the setup/init instead
        so that we're only measuring the client API call.
        """
        await self.async_metrics_client.query(self.metrics_uri,
                                              self.names,
                                              aggregations=self.aggregations)
Ejemplo n.º 9
0
    def __init__(self, arguments):
        super().__init__(arguments)

        # auth configuration
        self.metrics_uri = self.get_from_env('METRICS_RESOURCE_URI')
        self.names = ["MatchedEventCount"]
        self.aggregations = [MetricAggregationType.COUNT]

        # Create clients
        self.metrics_client = SyncMetricsQueryClient(
            credential=SyncDefaultAzureCredential())
        self.async_metrics_client = AsyncMetricsQueryClient(
            credential=AsyncDefaultAzureCredential())
Ejemplo n.º 10
0
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
"""
FILE: sample_metric_namespaces.py
DESCRIPTION:
    This sample demonstrates listing all the metric namespaces of a resource.
USAGE:
    python sample_metric_namespaces.py
    Set the environment variables with your own values before running the sample:
    1) METRICS_RESOURCE_URI - The resource URI of the resource for which the metrics are being queried.
    
    This example uses DefaultAzureCredential, which requests a token from Azure Active Directory.
    For more information on DefaultAzureCredential, see https://docs.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python#defaultazurecredential.

    In this example, a Storage account resource URI is taken.
"""
import os
from azure.monitor.query import MetricsQueryClient
from azure.identity import DefaultAzureCredential

credential = DefaultAzureCredential()

client = MetricsQueryClient(credential)

metrics_uri = os.environ['METRICS_RESOURCE_URI']
response = client.list_metric_namespaces(metrics_uri)

for item in response:
    print(item.fully_qualified_namespace)
    print(item.type)
Ejemplo n.º 11
0
import os
from datetime import datetime
import urllib3
from azure.monitor.query import MetricsQueryClient
from azure.identity import ClientSecretCredential

urllib3.disable_warnings()

# [START metrics_client_auth_with_token_cred]
credential = ClientSecretCredential(
    client_id=os.environ['AZURE_CLIENT_ID'],
    client_secret=os.environ['AZURE_CLIENT_SECRET'],
    tenant_id=os.environ['AZURE_TENANT_ID'])

client = MetricsQueryClient(credential)
# [END metrics_client_auth_with_token_cred]

# [START send_metrics_query]
metrics_uri = os.environ['METRICS_RESOURCE_URI']
response = client.query(metrics_uri,
                        metric_names=["PublishSuccessCount"],
                        start_time=datetime(2021, 5, 25),
                        duration='P1D')

for metric in response.metrics:
    print(metric.name)
    for time_series_element in metric.timeseries:
        for metric_value in time_series_element.data:
            print(metric_value.time_stamp)
# [END send_metrics_query]
"""
FILE: sample_metric_definitions.py
DESCRIPTION:
    This sample demonstrates listing all the metric definitions of a resource.
USAGE:
    python sample_metric_definitions.py
    Set the environment variables with your own values before running the sample:
    1) METRICS_RESOURCE_URI - The resource URI of the resource for which the metrics are being queried.
    
    This example uses DefaultAzureCredential, which requests a token from Azure Active Directory.
    For more information on DefaultAzureCredential, see https://docs.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python#defaultazurecredential.

    In this example, an Event Grid account resource URI is taken.
"""
import os
from azure.monitor.query import MetricsQueryClient
from azure.identity import DefaultAzureCredential

credential = DefaultAzureCredential()

client = MetricsQueryClient(credential)

metrics_uri = os.environ.get('METRICS_RESOURCE_URI')
response = client.list_metric_definitions(
    metrics_uri, namespace='microsoft.eventgrid/topics')

for item in response:
    print(item.name)
    for availability in item.metric_availabilities:
        print(availability.granularity)
    In this example, a Storage account resource URI is taken.
"""
import os
from datetime import timedelta
import urllib3
from azure.monitor.query import MetricsQueryClient, MetricAggregationType
from azure.identity import DefaultAzureCredential

# provide a cert or disable warnings to run this sample
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# [START metrics_client_auth_with_token_cred]
credential = DefaultAzureCredential()

client = MetricsQueryClient(credential)
# [END metrics_client_auth_with_token_cred]

# [START send_metrics_query]
metrics_uri = os.environ.get('METRICS_RESOURCE_URI')
response = client.query_resource(
    metrics_uri,
    metric_names=["Ingress"],
    timespan=timedelta(hours=2),
    granularity=timedelta(minutes=5),
    aggregations=[MetricAggregationType.AVERAGE],
)

for metric in response.metrics:
    print(metric.name + ' -- ' + metric.display_description)
    for time_series_element in metric.timeseries: