Skip to content

Commit 40eca8e

Browse files
committed
Update .NET code samples
1 parent 9efdbf8 commit 40eca8e

2 files changed

Lines changed: 90 additions & 110 deletions

File tree

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

Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -159,29 +159,30 @@ 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. Add a reference to the [Azure.Monitor.Query](https://www.nuget.org/packages/Azure.Monitor.Query) and [Azure.Identity](https://www.nuget.org/packages/Azure.Identity) NuGet packages before running these samples.
163163

164164
#### List the account-level metric definition
165165

166166
The following example shows how to list a metric definition at the account level:
167167

168168
```csharp
169+
using Azure.Identity;
170+
using Azure.Monitor.Query;
171+
using Azure.Monitor.Query.Models;
172+
169173
public static async Task ListStorageMetricDefinition()
170174
{
171175
var resourceId = "<resource-ID>";
172-
var subscriptionId = "<subscription-ID>";
173-
var tenantId = "<tenant-ID>";
174-
var applicationId = "<application-ID>";
175-
var accessKey = "<AccessKey>";
176176

177-
MonitorManagementClient readOnlyClient = AuthenticateWithReadOnlyClient(tenantId, applicationId, accessKey, subscriptionId).Result;
178-
IEnumerable<MetricDefinition> metricDefinitions = await readOnlyClient.MetricDefinitions.ListAsync(resourceUri: resourceId, cancellationToken: new CancellationToken());
177+
var credential = new DefaultAzureCredential();
178+
var client = new MetricsQueryClient(credential);
179+
180+
var metricDefinitions = client.GetMetricDefinitionsAsync(resourceId);
179181

180-
foreach (var metricDefinition in metricDefinitions)
182+
await foreach (var metricDefinition in metricDefinitions)
181183
{
182184
// Enumerate metric definition:
183185
// Id
184-
// ResourceId
185186
// Name
186187
// Unit
187188
// MetricAvailabilities
@@ -198,33 +199,28 @@ The following example shows how to list a metric definition at the account level
198199
The following example shows how to read `UsedCapacity` data at the account level:
199200

200201
```csharp
202+
using Azure.Identity;
203+
using Azure.Monitor.Query;
204+
using Azure.Monitor.Query.Models;
205+
201206
public static async Task ReadStorageMetricValue()
202207
{
203208
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;
210209

211-
Microsoft.Azure.Management.Monitor.Models.Response Response;
210+
var credential = new DefaultAzureCredential();
211+
var client = new MetricsQueryClient(credential);
212212

213-
string startDate = DateTime.Now.AddHours(-3).ToUniversalTime().ToString("o");
214-
string endDate = DateTime.Now.ToUniversalTime().ToString("o");
215-
string timeSpan = startDate + "/" + endDate;
213+
var response = await client.QueryResourceAsync(
214+
resourceId,
215+
new[] { "UsedCapacity" },
216+
new MetricsQueryOptions
217+
{
218+
TimeRange = new QueryTimeRange(TimeSpan.FromHours(3)),
219+
Granularity = TimeSpan.FromHours(1),
220+
Aggregations = { MetricAggregationType.Average }
221+
});
216222

217-
Response = await readOnlyClient.Metrics.ListAsync(
218-
resourceUri: resourceId,
219-
timespan: timeSpan,
220-
interval: System.TimeSpan.FromHours(1),
221-
metricnames: "UsedCapacity",
222-
223-
aggregation: "Average",
224-
resultType: ResultType.Data,
225-
cancellationToken: CancellationToken.None);
226-
227-
foreach (var metric in Response.Value)
223+
foreach (var metric in response.Value.Metrics)
228224
{
229225
// Enumerate metric value
230226
// Id
@@ -246,38 +242,32 @@ For multidimensional metrics, you need to define metadata filters if you want to
246242
The following example shows how to read metric data on the metric supporting multidimensional values:
247243

248244
```csharp
245+
using Azure.Identity;
246+
using Azure.Monitor.Query;
247+
using Azure.Monitor.Query.Models;
248+
249249
public static async Task ReadStorageMetricValueTest()
250250
{
251251
// Resource ID for queue storage
252252
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;
260253

261-
Microsoft.Azure.Management.Monitor.Models.Response Response;
254+
var credential = new DefaultAzureCredential();
255+
var client = new MetricsQueryClient(credential);
262256

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
257+
// It's applicable to define a metadata filter when a metric supports dimensions.
267258
// 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)
259+
var response = await client.QueryResourceAsync(
260+
resourceId,
261+
new[] { "BlobCapacity" },
262+
new MetricsQueryOptions
263+
{
264+
TimeRange = new QueryTimeRange(TimeSpan.FromHours(3)),
265+
Granularity = TimeSpan.FromHours(1),
266+
Aggregations = { MetricAggregationType.Average },
267+
Filter = "BlobType eq 'BlockBlob'"
268+
});
269+
270+
foreach (var metric in response.Value.Metrics)
281271
{
282272
// Enumerate metric value
283273
// Id

articles/storage/tables/monitor-table-storage.md

Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -159,29 +159,30 @@ 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 Table Storage service. 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](/azure/active-directory/develop/howto-create-service-principal-portal).
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. Add a reference to the [Azure.Monitor.Query](https://www.nuget.org/packages/Azure.Monitor.Query) and [Azure.Identity](https://www.nuget.org/packages/Azure.Identity) NuGet packages before running these samples.
163163

164164
#### List the account-level metric definition
165165

166166
The following example shows how to list a metric definition at the account level:
167167

168168
```csharp
169+
using Azure.Identity;
170+
using Azure.Monitor.Query;
171+
using Azure.Monitor.Query.Models;
172+
169173
public static async Task ListStorageMetricDefinition()
170174
{
171175
var resourceId = "<resource-ID>";
172-
var subscriptionId = "<subscription-ID>";
173-
var tenantId = "<tenant-ID>";
174-
var applicationId = "<application-ID>";
175-
var accessKey = "<AccessKey>";
176176

177-
MonitorManagementClient readOnlyClient = AuthenticateWithReadOnlyClient(tenantId, applicationId, accessKey, subscriptionId).Result;
178-
IEnumerable<MetricDefinition> metricDefinitions = await readOnlyClient.MetricDefinitions.ListAsync(resourceUri: resourceId, cancellationToken: new CancellationToken());
177+
var credential = new DefaultAzureCredential();
178+
var client = new MetricsQueryClient(credential);
179+
180+
var metricDefinitions = client.GetMetricDefinitionsAsync(resourceId);
179181

180-
foreach (var metricDefinition in metricDefinitions)
182+
await foreach (var metricDefinition in metricDefinitions)
181183
{
182184
// Enumerate metric definition:
183185
// Id
184-
// ResourceId
185186
// Name
186187
// Unit
187188
// MetricAvailabilities
@@ -198,33 +199,28 @@ The following example shows how to list a metric definition at the account level
198199
The following example shows how to read `UsedCapacity` data at the account level:
199200

200201
```csharp
202+
using Azure.Identity;
203+
using Azure.Monitor.Query;
204+
using Azure.Monitor.Query.Models;
205+
201206
public static async Task ReadStorageMetricValue()
202207
{
203208
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;
210209

211-
Microsoft.Azure.Management.Monitor.Models.Response Response;
210+
var credential = new DefaultAzureCredential();
211+
var client = new MetricsQueryClient(credential);
212212

213-
string startDate = DateTime.Now.AddHours(-3).ToUniversalTime().ToString("o");
214-
string endDate = DateTime.Now.ToUniversalTime().ToString("o");
215-
string timeSpan = startDate + "/" + endDate;
213+
var response = await client.QueryResourceAsync(
214+
resourceId,
215+
new[] { "UsedCapacity" },
216+
new MetricsQueryOptions
217+
{
218+
TimeRange = new QueryTimeRange(TimeSpan.FromHours(3)),
219+
Granularity = TimeSpan.FromHours(1),
220+
Aggregations = { MetricAggregationType.Average }
221+
});
216222

217-
Response = await readOnlyClient.Metrics.ListAsync(
218-
resourceUri: resourceId,
219-
timespan: timeSpan,
220-
interval: System.TimeSpan.FromHours(1),
221-
metricnames: "UsedCapacity",
222-
223-
aggregation: "Average",
224-
resultType: ResultType.Data,
225-
cancellationToken: CancellationToken.None);
226-
227-
foreach (var metric in Response.Value)
223+
foreach (var metric in response.Value.Metrics)
228224
{
229225
// Enumerate metric value
230226
// Id
@@ -246,38 +242,32 @@ For multidimensional metrics, you need to define metadata filters if you want to
246242
The following example shows how to read metric data on the metric supporting multidimensional values:
247243

248244
```csharp
245+
using Azure.Identity;
246+
using Azure.Monitor.Query;
247+
using Azure.Monitor.Query.Models;
248+
249249
public static async Task ReadStorageMetricValueTest()
250250
{
251251
// Resource ID for table storage
252252
var resourceId = "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{storageAccountName}/tableServices/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;
260253

261-
Microsoft.Azure.Management.Monitor.Models.Response Response;
254+
var credential = new DefaultAzureCredential();
255+
var client = new MetricsQueryClient(credential);
262256

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
257+
// It's applicable to define a metadata filter when a metric supports dimensions.
267258
// 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)
259+
var response = await client.QueryResourceAsync(
260+
resourceId,
261+
new[] { "BlobCapacity" },
262+
new MetricsQueryOptions
263+
{
264+
TimeRange = new QueryTimeRange(TimeSpan.FromHours(3)),
265+
Granularity = TimeSpan.FromHours(1),
266+
Aggregations = { MetricAggregationType.Average },
267+
Filter = "BlobType eq 'BlockBlob'"
268+
});
269+
270+
foreach (var metric in response.Value.Metrics)
281271
{
282272
// Enumerate metric value
283273
// Id

0 commit comments

Comments
 (0)