Skip to content

Commit 984cf47

Browse files
Merge pull request #312713 from alexwolfmsft/update-dotnet-samples
Update .NET auth code samples for queue and table storage
2 parents aab2b37 + c70ff8b commit 984cf47

2 files changed

Lines changed: 184 additions & 199 deletions

File tree

articles/storage/queues/monitor-queue-storage.md

Lines changed: 93 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -159,84 +159,84 @@ Azure Monitor provides the [.NET SDK](https://www.nuget.org/packages/microsoft.a
159159

160160
In these examples, replace the `<resource-ID>` placeholder with the resource ID of the entire storage account or the queue. You can find these resource IDs on the **Properties** pages of your storage account in the Azure portal.
161161

162-
Replace the `<subscription-ID>` variable with the ID of your subscription. For guidance on how to obtain values for `<tenant-ID>`, `<application-ID>`, and `<AccessKey>`, see [Use the portal to create a Microsoft Entra application and service principal that can access resources](../../active-directory/develop/howto-create-service-principal-portal.md).
162+
These examples use `DefaultAzureCredential` from the `Azure.Identity` package, which supports passwordless authentication using your local developer credentials or a managed identity in Azure. Before running these samples, install the [Azure.Monitor.Query](https://www.nuget.org/packages/Azure.Monitor.Query) and [Azure.Identity](https://www.nuget.org/packages/Azure.Identity) NuGet packages:
163+
164+
```bash
165+
dotnet add package Azure.Monitor.Query
166+
dotnet add package Azure.Identity
167+
```
163168

164169
#### List the account-level metric definition
165170

166171
The following example shows how to list a metric definition at the account level:
167172

168173
```csharp
169-
public static async Task ListStorageMetricDefinition()
174+
using Azure.Identity;
175+
using Azure.Monitor.Query;
176+
using Azure.Monitor.Query.Models;
177+
178+
async Task ListStorageMetricDefinition()
179+
{
180+
var resourceId = "<resource-ID>";
181+
182+
var credential = new DefaultAzureCredential();
183+
var client = new MetricsQueryClient(credential);
184+
185+
// Get metric definitions for the resource. The metrics namespace is optional. If not specified, it will return metric definitions for all namespaces.
186+
var metricDefinitions = client.GetMetricDefinitionsAsync(resourceId, "<metrics-namespace>");
187+
188+
await foreach (var metricDefinition in metricDefinitions)
170189
{
171-
var resourceId = "<resource-ID>";
172-
var subscriptionId = "<subscription-ID>";
173-
var tenantId = "<tenant-ID>";
174-
var applicationId = "<application-ID>";
175-
var accessKey = "<AccessKey>";
176-
177-
MonitorManagementClient readOnlyClient = AuthenticateWithReadOnlyClient(tenantId, applicationId, accessKey, subscriptionId).Result;
178-
IEnumerable<MetricDefinition> metricDefinitions = await readOnlyClient.MetricDefinitions.ListAsync(resourceUri: resourceId, cancellationToken: new CancellationToken());
179-
180-
foreach (var metricDefinition in metricDefinitions)
181-
{
182-
// Enumerate metric definition:
183-
// Id
184-
// ResourceId
185-
// Name
186-
// Unit
187-
// MetricAvailabilities
188-
// PrimaryAggregationType
189-
// Dimensions
190-
// IsDimensionRequired
191-
}
190+
// Enumerate metric definition:
191+
// Id
192+
// Name
193+
// Unit
194+
// MetricAvailabilities
195+
// PrimaryAggregationType
196+
// Dimensions
197+
// IsDimensionRequired
192198
}
193-
199+
}
194200
```
195201

196202
#### Read account-level metric values
197203

198204
The following example shows how to read `UsedCapacity` data at the account level:
199205

200206
```csharp
201-
public static async Task ReadStorageMetricValue()
202-
{
203-
var resourceId = "<resource-ID>";
204-
var subscriptionId = "<subscription-ID>";
205-
var tenantId = "<tenant-ID>";
206-
var applicationId = "<application-ID>";
207-
var accessKey = "<AccessKey>";
208-
209-
MonitorClient readOnlyClient = AuthenticateWithReadOnlyClient(tenantId, applicationId, accessKey, subscriptionId).Result;
210-
211-
Microsoft.Azure.Management.Monitor.Models.Response Response;
207+
using Azure.Identity;
208+
using Azure.Monitor.Query;
209+
using Azure.Monitor.Query.Models;
212210

213-
string startDate = DateTime.Now.AddHours(-3).ToUniversalTime().ToString("o");
214-
string endDate = DateTime.Now.ToUniversalTime().ToString("o");
215-
string timeSpan = startDate + "/" + endDate;
211+
async Task ReadStorageMetricValue()
212+
{
213+
var resourceId = "<resource-ID>";
216214

217-
Response = await readOnlyClient.Metrics.ListAsync(
218-
resourceUri: resourceId,
219-
timespan: timeSpan,
220-
interval: System.TimeSpan.FromHours(1),
221-
metricnames: "UsedCapacity",
215+
var credential = new DefaultAzureCredential();
216+
var client = new MetricsQueryClient(credential);
222217

223-
aggregation: "Average",
224-
resultType: ResultType.Data,
225-
cancellationToken: CancellationToken.None);
226-
227-
foreach (var metric in Response.Value)
218+
var response = await client.QueryResourceAsync(
219+
resourceId,
220+
new[] { "UsedCapacity" },
221+
new MetricsQueryOptions
228222
{
229-
// Enumerate metric value
230-
// Id
231-
// Name
232-
// Type
233-
// Unit
234-
// Timeseries
235-
// - Data
236-
// - Metadatavalues
237-
}
238-
}
223+
TimeRange = new QueryTimeRange(TimeSpan.FromHours(3)),
224+
Granularity = TimeSpan.FromHours(1),
225+
Aggregations = { MetricAggregationType.Average }
226+
});
239227

228+
foreach (var metric in response.Value.Metrics)
229+
{
230+
// Enumerate metric value
231+
// Id
232+
// Name
233+
// Type
234+
// Unit
235+
// Timeseries
236+
// - Data
237+
// - Metadatavalues
238+
}
239+
}
240240
```
241241

242242
#### Read multidimensional metric values
@@ -246,50 +246,43 @@ For multidimensional metrics, you need to define metadata filters if you want to
246246
The following example shows how to read metric data on the metric supporting multidimensional values:
247247

248248
```csharp
249-
public static async Task ReadStorageMetricValueTest()
250-
{
251-
// Resource ID for queue storage
252-
var resourceId = "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{storageAccountName}/queueServices/default";
253-
var subscriptionId = "<subscription-ID}";
254-
// How to identify Tenant ID, Application ID and Access Key: https://azure.microsoft.com/documentation/articles/resource-group-create-service-principal-portal/
255-
var tenantId = "<tenant-ID>";
256-
var applicationId = "<application-ID>";
257-
var accessKey = "<AccessKey>";
258-
259-
MonitorManagementClient readOnlyClient = AuthenticateWithReadOnlyClient(tenantId, applicationId, accessKey, subscriptionId).Result;
260-
261-
Microsoft.Azure.Management.Monitor.Models.Response Response;
262-
263-
string startDate = DateTime.Now.AddHours(-3).ToUniversalTime().ToString("o");
264-
string endDate = DateTime.Now.ToUniversalTime().ToString("o");
265-
string timeSpan = startDate + "/" + endDate;
266-
// It's applicable to define meta data filter when a metric support dimension
267-
// More conditions can be added with the 'or' and 'and' operators, example: BlobType eq 'BlockBlob' or BlobType eq 'PageBlob'
268-
ODataQuery<MetadataValue> odataFilterMetrics = new ODataQuery<MetadataValue>(
269-
string.Format("BlobType eq '{0}'", "BlockBlob"));
270-
271-
Response = readOnlyClient.Metrics.List(
272-
resourceUri: resourceId,
273-
timespan: timeSpan,
274-
interval: System.TimeSpan.FromHours(1),
275-
metricnames: "BlobCapacity",
276-
odataQuery: odataFilterMetrics,
277-
aggregation: "Average",
278-
resultType: ResultType.Data);
279-
280-
foreach (var metric in Response.Value)
249+
using Azure.Identity;
250+
using Azure.Monitor.Query;
251+
using Azure.Monitor.Query.Models;
252+
253+
async Task ReadStorageMetricValueTest()
254+
{
255+
// Resource ID for queue storage
256+
var resourceId = "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{storageAccountName}/queueServices/default";
257+
258+
var credential = new DefaultAzureCredential();
259+
var client = new MetricsQueryClient(credential);
260+
261+
// It's applicable to define a metadata filter when a metric supports dimensions.
262+
// More conditions can be added with the 'or' and 'and' operators, example: BlobType eq 'BlockBlob' or BlobType eq 'PageBlob'
263+
var response = await client.QueryResourceAsync(
264+
resourceId,
265+
new[] { "BlobCapacity" },
266+
new MetricsQueryOptions
281267
{
282-
// Enumerate metric value
283-
// Id
284-
// Name
285-
// Type
286-
// Unit
287-
// Timeseries
288-
// - Data
289-
// - Metadatavalues
290-
}
291-
}
268+
TimeRange = new QueryTimeRange(TimeSpan.FromHours(3)),
269+
Granularity = TimeSpan.FromHours(1),
270+
Aggregations = { MetricAggregationType.Average },
271+
Filter = "BlobType eq 'BlockBlob'"
272+
});
292273

274+
foreach (var metric in response.Value.Metrics)
275+
{
276+
// Enumerate metric value
277+
// Id
278+
// Name
279+
// Type
280+
// Unit
281+
// Timeseries
282+
// - Data
283+
// - Metadatavalues
284+
}
285+
}
293286
```
294287

295288
---

0 commit comments

Comments
 (0)