File: sample_authentication.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 (86 lines) | stat: -rw-r--r-- 3,254 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
"""
FILE: sample_authentication.py
DESCRIPTION:
    This sample demonstrates how to authenticate to the Azure Monitor service using both
    LogQueryClient and MetricsQueryClient.
USAGE:
    python sample_authentication.py

This example uses DefaultAzureCredential, which requests a token from Azure Active Directory.
For more information on DefaultAzureCredential, see https://learn.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python#defaultazurecredential.
"""


def create_logs_query_client():
    # [START create_logs_query_client]
    from azure.identity import DefaultAzureCredential
    from azure.monitor.query import LogsQueryClient

    credential = DefaultAzureCredential()
    client = LogsQueryClient(credential)
    # [END create_logs_query_client]


def create_logs_query_client_sovereign_cloud():
    # [START create_logs_query_client_sovereign_cloud]
    from azure.identity import AzureAuthorityHosts, DefaultAzureCredential
    from azure.monitor.query import LogsQueryClient

    credential = DefaultAzureCredential(authority=AzureAuthorityHosts.AZURE_GOVERNMENT)
    client = LogsQueryClient(credential, endpoint="https://api.loganalytics.us/v1")
    # [END create_logs_query_client_sovereign_cloud]


def create_metrics_query_client():
    # [START create_metrics_query_client]
    from azure.identity import DefaultAzureCredential
    from azure.monitor.query import MetricsQueryClient

    credential = DefaultAzureCredential()
    client = MetricsQueryClient(credential)
    # [END create_metrics_query_client]


def create_metrics_query_client_sovereign_cloud():
    # [START create_metrics_query_client_sovereign_cloud]
    from azure.identity import AzureAuthorityHosts, DefaultAzureCredential
    from azure.monitor.query import MetricsQueryClient

    credential = DefaultAzureCredential(authority=AzureAuthorityHosts.AZURE_GOVERNMENT)
    client = MetricsQueryClient(credential, endpoint="https://management.usgovcloudapi.net")
    # [END create_metrics_query_client_sovereign_cloud]


def create_metrics_client():
    # [START create_metrics_client]
    from azure.identity import DefaultAzureCredential
    from azure.monitor.query import MetricsClient

    credential = DefaultAzureCredential()
    client = MetricsClient("https://eastus.metrics.monitor.azure.com", credential)
    # [END create_metrics_client]


def create_metrics_client_sovereign_cloud():
    # [START create_metrics_client_sovereign_cloud]
    from azure.identity import AzureAuthorityHosts, DefaultAzureCredential
    from azure.monitor.query import MetricsClient

    credential = DefaultAzureCredential(authority=AzureAuthorityHosts.AZURE_GOVERNMENT)
    client = MetricsClient(
        "https://usgovvirginia.metrics.monitor.azure.us",
        credential,
        audience="https://metrics.monitor.azure.us",
    )
    # [END create_metrics_client_sovereign_cloud]


if __name__ == "__main__":
    create_logs_query_client()
    create_logs_query_client_sovereign_cloud()
    create_metrics_query_client()
    create_metrics_query_client_sovereign_cloud()
    create_metrics_client()
    create_metrics_client_sovereign_cloud()