Skip to content

Commit ced8877

Browse files
Application insights migration guide (Azure#23002)
* async tests * Revert "async tests" This reverts commit f4b2a14. * Migration guide for application insights * Apply suggestions from code review Co-authored-by: Scott Addie <[email protected]> * Apply suggestions from code review Co-authored-by: Scott Addie <[email protected]> * Apply suggestions from code review Co-authored-by: Scott Addie <[email protected]> Co-authored-by: Scott Addie <[email protected]>
1 parent ce6c2e8 commit ced8877

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Guide for migrating from azure-applicationinsights v0.1.0 to azure-monitor-query v1.0.x
2+
3+
This guide assists you in the migration from [azure-applicationinsights](https://pypi.org/project/azure-applicationinsights/) v0.1.0 to [azure-monitor-query](https://pypi.org/project/azure-monitor-query/) v1.0.x. Side-by-side comparisons are provided for similar operations between the two packages.
4+
5+
Familiarity with the `azure-applicationinsights` v0.1.0 package is assumed. If you're new to the Azure Monitor Query client library for Python, see the [README for `azure-monitor-query`](https://docs.microsoft.com/python/api/overview/azure/monitor-query-readme?view=azure-python) instead of this guide.
6+
7+
## Table of contents
8+
9+
- [Migration benefits](#migration-benefits)
10+
- [Cross-service SDK improvements](#cross-service-sdk-improvements)
11+
- [New features](#new-features)
12+
- [Important changes](#important-changes)
13+
- [The client](#the-client)
14+
- [Client constructors and authentication](#client-constructors-and-authentication)
15+
- [Send a single query request](#sending-a-single-query-request)
16+
- [Additional samples](#additional-samples)
17+
18+
## Migration benefits
19+
20+
A natural question to ask when considering whether to adopt a new version or library is what the benefits of doing so would be. As Azure has matured and been embraced by a more diverse group of developers, we've focused on learning the patterns and practices to best support developer productivity and to understand the gaps that the Python client libraries have.
21+
22+
Several areas of consistent feedback were expressed across the Azure client library ecosystem. One of the most important is that the client libraries for different Azure services haven't had a consistent approach to organization, naming, and API structure. Additionally, many developers have felt that the learning curve was too steep. The APIs didn't offer an approachable and consistent onboarding story for those learning Azure or exploring a specific Azure service.
23+
24+
To improve the development experience across Azure services, a set of uniform [design guidelines](https://azure.github.io/azure-sdk/general_introduction.html) was created for all languages to drive a consistent experience with established API patterns for all services. A set of [Python-specific guidelines](https://azure.github.io/azure-sdk/python/guidelines/index.html) was also introduced to ensure that Python clients have a natural and idiomatic feel with respect to the Python ecosystem. Further details are available in the guidelines.
25+
26+
### Cross-service SDK improvements
27+
28+
The Azure Monitor Query client library also takes advantage of the cross-service improvements made to the Azure development experience. Examples include:
29+
30+
- Using the new [Azure Identity](https://docs.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python) library to share a single authentication approach between clients.
31+
- A unified logging and diagnostics pipeline offering a common view of the activities across each of the client libraries.
32+
33+
### New features
34+
35+
There are various new features in version 1.0 of the Monitor Query library. Some include:
36+
37+
- The ability to execute a batch of queries with the `LogsQueryClient.query_batch()` API.
38+
- The ability to configure the retry policy used by the operations on the client.
39+
- Authentication with Azure Active Directory (Azure AD) credentials using [azure-identity](https://docs.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python).
40+
41+
For more new features, changes, and bug fixes, see the [change log](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/CHANGELOG.md).
42+
43+
## Important changes
44+
45+
### The client
46+
47+
To provide a more intuitive experience, the top-level client to [ApplicationInsightsDataClient](https://docs.microsoft.com/python/api/azure-applicationinsights/azure.applicationinsights.applicationinsightsdataclient?view=azure-python) has been split into two different clients:
48+
49+
- [LogsQueryClient](https://docs.microsoft.com/python/api/azure-monitor-query/azure.monitor.query.logsqueryclient?view=azure-python) serves as a single point of entry to execute a single Kusto query or a batch of Kusto queries.
50+
- [MetricsQueryClient](https://docs.microsoft.com/python/api/azure-monitor-query/azure.monitor.query.metricsqueryclient?view=azure-python) is used to query metrics, list metric namespaces, and to list metric definitions.
51+
52+
Both clients can be authenticated using Azure AD.
53+
54+
#### Consistency
55+
56+
There are now methods with similar names, signatures, and locations to create senders and receivers. The result is consistency and predictability on the various features of the library.
57+
58+
### Client constructors and authentication
59+
60+
In `azure-applicationinsights` v0.1.0:
61+
62+
```python
63+
from azure.applicationinsights import ApplicationInsightsDataClient
64+
from msrestazure.azure_active_directory import ServicePrincipalCredentials
65+
66+
credential = ServicePrincipalCredentials(...)
67+
client = ApplicationInsightsDataClient(credentials=credential)
68+
```
69+
70+
In `azure-monitor-query` v1.0.x:
71+
72+
```python
73+
from azure.monitor.query import LogsQueryClient, MetricsQueryClient
74+
from azure.identity import DefaultAzureCredential
75+
76+
credential = DefaultAzureCredential()
77+
logs_query_client = LogsQueryClient(credential=credential)
78+
metrics_query_client = MetricsQueryClient(credential=credential)
79+
```
80+
81+
### Send a single query request
82+
83+
In version 1.0 of the Monitor Query library:
84+
85+
- The `QueryBody` is flattened. Users are expected to pass the Kusto query directly to the API.
86+
- The `timespan` attribute is now required, which helped to avoid querying over the entire data set.
87+
88+
In `azure-applicationinsights` v0.1.0:
89+
90+
```python
91+
from azure.applicationinsights.models import QueryBody
92+
93+
query = 'requests | take 10'
94+
application = 'DEMO_APP'
95+
result = self.client.query.execute(application, QueryBody(query = query))
96+
```
97+
98+
In `azure-monitor-query` v1.0.x:
99+
100+
```python
101+
query = 'AppRequests | take 5'
102+
logs_query_client.query(workspace_id, query, timespan=timedelta(days=1))
103+
```
104+
105+
### A note about the Events API
106+
107+
The `azure-applicationinsights` package includes an Events API, which is just a different "API head" on the same logs data. It enables the querying of some logs in Application Insights without writing Kusto queries. The API translates to Kusto queries in the background. The same data can be accessed via regular Kusto queries, as shown in the preceding example.
108+
109+
### Query metrics from a resource
110+
111+
In `azure-applicationinsights` v0.1.0:
112+
113+
```python
114+
metricId = 'availabilityResults/count'
115+
application = 'DEMO_APP'
116+
result = client.metrics.get(application, metricId)
117+
```
118+
119+
In `azure-monitor-query` v1.0.x:
120+
121+
```python
122+
metrics_resource_uri = os.environ.get('METRICS_RESOURCE_URI')
123+
result = metrics_query_client.query_resource(
124+
metrics_resource_uri,
125+
metric_names=["Ingress"],
126+
timespan=timedelta(hours=2),
127+
granularity=timedelta(minutes=5),
128+
aggregations=[MetricAggregationType.AVERAGE],
129+
)
130+
```
131+
132+
## Additional samples
133+
134+
For more examples, see [Samples for azure-monitor-query](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/monitor/azure-monitor-query/samples).

0 commit comments

Comments
 (0)