|
| 1 | +# Guide for migrating from `Azure.Monitor.Query` to `Azure.ResourceManager.Monitor` |
| 2 | + |
| 3 | +This guide assists in migrating metrics query operations in `Azure.Monitor.Query` to the `Azure.ResourceManager.Monitor` library. |
| 4 | + |
| 5 | +## Table of contents |
| 6 | + |
| 7 | +- [Migration benefits](#migration-benefits) |
| 8 | +- [Important changes](#important-changes) |
| 9 | + - [Package names](#package-names) |
| 10 | + - [Namespaces](#namespaces) |
| 11 | + - [Client differences](#client-differences) |
| 12 | + - [API changes](#api-changes) |
| 13 | + - [Query metrics for a single resource](#query-metrics-for-a-single-resource) |
| 14 | + - [Get metric definitions for a single resource](#get-metric-definitions-for-a-single-resource) |
| 15 | + - [Get metric namespaces for a single resource](#get-metric-namespaces-for-a-single-resource) |
| 16 | + |
| 17 | +## Migration benefits |
| 18 | + |
| 19 | +The Azure Monitor Query library for .NET has been modularized to provide more focused functionality. The operations for querying metrics via `MetricsQueryClient` have moved from the combined `Azure.Monitor.Query` package to a dedicated `Azure.ResourceManager.Monitor` package. This separation offers several advantages: |
| 20 | + |
| 21 | +- Smaller dependency footprint for applications that only need to query metrics for an Azure resource |
| 22 | +- More focused API design specific to metrics query operations |
| 23 | +- Independent versioning allowing metrics functionality to evolve separately |
| 24 | +- Clearer separation of concerns between logs and metrics query operations |
| 25 | + |
| 26 | +## Important changes |
| 27 | + |
| 28 | +### Package names |
| 29 | + |
| 30 | +- Previous package for querying metrics on an Azure resource: `Azure.Monitor.Query` |
| 31 | +- New package: `Azure.ResourceManager.Monitor` |
| 32 | + |
| 33 | +### Namespaces |
| 34 | + |
| 35 | +The root namespace has changed to `Azure.ResourceManager.Monitor` in the new package. |
| 36 | + |
| 37 | +### Client differences |
| 38 | + |
| 39 | +In the `Azure.Monitor.Query` package, `MetricsQueryClient` was used for metrics query options on a single Azure resource. That same functionality can be found in `ArmClient` within the `Azure.ResourceManager.Monitor` package. See the [API changes](#api-changes) section for code samples demonstrating the changes. |
| 40 | + |
| 41 | +### API changes |
| 42 | + |
| 43 | +#### Query metrics for a single resource |
| 44 | + |
| 45 | +**Previous code:** |
| 46 | + |
| 47 | +```csharp |
| 48 | +const string resourceId = |
| 49 | + "/subscriptions/<subscription_id>/resourceGroups/<resource_group_name>/providers/Microsoft.Storage/storageAccounts/<resource_name>"; |
| 50 | + |
| 51 | +MetricsQueryClient client = new(new DefaultAzureCredential()); |
| 52 | +MetricsQueryResult result = await client.QueryResourceAsync( |
| 53 | + resourceId, |
| 54 | + ["Availability"]); |
| 55 | + |
| 56 | +foreach (MetricResult metric in result.Metrics) |
| 57 | +{ |
| 58 | + // Process each metric as needed |
| 59 | + Console.WriteLine($"Metric Name: {metric.Name}, Unit: {metric.Unit}"); |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +**New code:** |
| 64 | + |
| 65 | +```C# Snippet:QueryResource |
| 66 | +const string resourceId = |
| 67 | + "/subscriptions/<subscription_id>/resourceGroups/<resource_group_name>/providers/Microsoft.Storage/storageAccounts/<resource_name>"; |
| 68 | + |
| 69 | +ArmClient client = new(new DefaultAzureCredential()); |
| 70 | +ArmResourceGetMonitorMetricsOptions options = new() |
| 71 | +{ |
| 72 | + Metricnames = "Availability", |
| 73 | +}; |
| 74 | +AsyncPageable<MonitorMetric> metrics = client.GetMonitorMetricsAsync( |
| 75 | + new ResourceIdentifier(resourceId), |
| 76 | + options); |
| 77 | + |
| 78 | +await foreach (MonitorMetric metric in metrics) |
| 79 | +{ |
| 80 | + // Process each metric as needed |
| 81 | + Console.WriteLine($"Metric Name: {metric.Name?.Value}, Unit: {metric.Unit}"); |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +#### Get metric definitions for a single resource |
| 86 | + |
| 87 | +**Previous code:** |
| 88 | + |
| 89 | +```csharp |
| 90 | +const string resourceId = |
| 91 | + "/subscriptions/<subscription_id>/resourceGroups/<resource_group_name>/providers/Microsoft.Storage/storageAccounts/<resource_name>"; |
| 92 | +const string metricsNamespace = "Microsoft.Storage/storageAccounts"; |
| 93 | + |
| 94 | +MetricsQueryClient client = new(new DefaultAzureCredential()); |
| 95 | +AsyncPageable<MetricDefinition> definitions = |
| 96 | + client.GetMetricDefinitionsAsync(resourceId, metricsNamespace); |
| 97 | + |
| 98 | +await foreach (MetricDefinition definition in definitions) |
| 99 | +{ |
| 100 | + // Process each definition as needed |
| 101 | + Console.WriteLine($"Metric Name: {definition.Name}, Unit: {definition.Unit}"); |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +**New code:** |
| 106 | + |
| 107 | +```C# Snippet:GetMetricDefinitions |
| 108 | +const string resourceId = |
| 109 | + "/subscriptions/<subscription_id>/resourceGroups/<resource_group_name>/providers/Microsoft.Storage/storageAccounts/<resource_name>"; |
| 110 | +const string metricsNamespace = "Microsoft.Storage/storageAccounts"; |
| 111 | + |
| 112 | +ArmClient client = new(new DefaultAzureCredential()); |
| 113 | +AsyncPageable<MonitorMetricDefinition> definitions = |
| 114 | + client.GetMonitorMetricDefinitionsAsync(new ResourceIdentifier(resourceId), metricsNamespace); |
| 115 | + |
| 116 | +await foreach (MonitorMetricDefinition definition in definitions) |
| 117 | +{ |
| 118 | + // Process each definition as needed |
| 119 | + Console.WriteLine($"Metric Name: {definition.Name?.Value}, Unit: {definition.Unit}"); |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +#### Get metric namespaces for a single resource |
| 124 | + |
| 125 | +**Previous code:** |
| 126 | + |
| 127 | +```csharp |
| 128 | +const string resourceId = |
| 129 | + "/subscriptions/<subscription_id>/resourceGroups/<resource_group_name>/providers/Microsoft.Storage/storageAccounts/<resource_name>"; |
| 130 | + |
| 131 | +MetricsQueryClient client = new(new DefaultAzureCredential()); |
| 132 | +AsyncPageable<MetricNamespace> namespaces = |
| 133 | + client.GetMetricNamespacesAsync(resourceId); |
| 134 | + |
| 135 | +await foreach (MetricNamespace ns in namespaces) |
| 136 | +{ |
| 137 | + // Process each namespace as needed |
| 138 | + Console.WriteLine($"Namespace Name: {ns.Name}, Type: {ns.FullyQualifiedName}"); |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +**New code:** |
| 143 | + |
| 144 | +```C# Snippet:GetMetricNamespaces |
| 145 | +const string resourceId = |
| 146 | + "/subscriptions/<subscription_id>/resourceGroups/<resource_group_name>/providers/Microsoft.Storage/storageAccounts/<resource_name>"; |
| 147 | + |
| 148 | +ArmClient client = new(new DefaultAzureCredential()); |
| 149 | +AsyncPageable<MonitorMetricNamespace> namespaces = |
| 150 | + client.GetMonitorMetricNamespacesAsync(new ResourceIdentifier(resourceId)); |
| 151 | + |
| 152 | +await foreach (MonitorMetricNamespace ns in namespaces) |
| 153 | +{ |
| 154 | + // Process each namespace as needed |
| 155 | + Console.WriteLine($"Namespace Name: {ns.Name}, Type: {ns.MetricNamespaceNameValue}"); |
| 156 | +} |
| 157 | +``` |
0 commit comments