Beispiel #1
0
 public virtual Response <MetricQueryResult> Query(string resource, DateTimeOffset startTime, DateTimeOffset endTime, TimeSpan interval, CancellationToken cancellationToken = default)
 {
     using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(MetricsClient)}.{nameof(Query)}");
     scope.Start();
     try
     {
         return(_metricsRestClient.List(resource, GetTimespan(startTime, endTime), interval, cancellationToken: cancellationToken));
     }
     catch (Exception e)
     {
         scope.Failed(e);
         throw;
     }
 }
Beispiel #2
0
 /// <summary>
 /// Queries metrics for a resource.
 /// <code snippet="Snippet:QueryMetrics" language="csharp">
 /// string resourceId =
 ///     &quot;/subscriptions/&lt;subscription_id&gt;/resourceGroups/&lt;resource_group_name&gt;/providers/&lt;resource_provider&gt;/&lt;resource&gt;&quot;;
 ///
 /// var metricsClient = new MetricsQueryClient(new DefaultAzureCredential());
 ///
 /// Response&lt;MetricsQueryResult&gt; results = await metricsClient.QueryAsync(
 ///     resourceId,
 ///     new[] {&quot;Microsoft.OperationalInsights/workspaces&quot;}
 /// );
 ///
 /// foreach (var metric in results.Value.Metrics)
 /// {
 ///     Console.WriteLine(metric.Name);
 ///     foreach (var element in metric.TimeSeries)
 ///     {
 ///         Console.WriteLine(&quot;Dimensions: &quot; + string.Join(&quot;,&quot;, element.Metadata));
 ///
 ///         foreach (var metricValue in element.Data)
 ///         {
 ///             Console.WriteLine(metricValue);
 ///         }
 ///     }
 /// }
 /// </code>
 /// </summary>
 /// <param name="resourceId">The resource name.
 /// For example:
 /// <c>/subscriptions/&lt;subscription_id&gt;/resourceGroups/&lt;resource_group_name&gt;/providers/&lt;resource_provider&gt;/&lt;resource&gt;</c>,<br/>
 /// <c>/subscriptions/faa080af-c1d8-40ad-9cce-e1a450ca5b57/resourceGroups/resource-group/providers/Microsoft.Storage/storageAccounts/mystorage</c>,<br/>
 /// <c>/subscriptions/faa080af-c1d8-40ad-9cce-e1a450ca5b57/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachines/myvm</c><br/>
 /// </param>
 /// <param name="metrics">The list of metrics to query.</param>
 /// <param name="options">The additional request options.</param>
 /// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
 /// <returns>The <see cref="MetricsQueryResult"/> instance containing the query results.</returns>
 public virtual Response <MetricsQueryResult> Query(string resourceId, IEnumerable <string> metrics, MetricsQueryOptions options = null, CancellationToken cancellationToken = default)
 {
     using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(MetricsQueryClient)}.{nameof(Query)}");
     scope.Start();
     try
     {
         return(_metricsRestClient.List(resourceId,
                                        timespan: options?.TimeRange?.ToString(),
                                        interval: options?.Granularity,
                                        filter: options?.Filter,
                                        top: options?.Top,
                                        aggregation: GetAggregation(options),
                                        metricnames: string.Join(",", metrics),
                                        orderby: options?.OrderBy,
                                        metricnamespace: options?.MetricNamespace,
                                        cancellationToken: cancellationToken));
     }
     catch (Exception e)
     {
         scope.Failed(e);
         throw;
     }
 }