Ejemplo n.º 1
0
 private static string GetAggregation(MetricsQueryOptions options)
 {
     if (options?.Aggregations == null ||
         options.Aggregations.Count == 0)
     {
         return(null);
     }
     return(string.Join(",", options.Aggregations));
 }
Ejemplo n.º 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 with query results.</returns>
 public virtual async Task <Response <MetricsQueryResult> > QueryAsync(string resourceId, IEnumerable <string> metrics, MetricsQueryOptions options = null, CancellationToken cancellationToken = default)
 {
     using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(MetricsQueryClient)}.{nameof(Query)}");
     scope.Start();
     try
     {
         return(await _metricsRestClient.ListAsync(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).ConfigureAwait(false));
     }
     catch (Exception e)
     {
         scope.Failed(e);
         throw;
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Queries metrics for a resource.
 /// </summary>
 /// <param name="resourceId">The resource name.
 /// For example: <c>/subscriptions/[subscription_id]/resourceGroups/[resource_group_name]/providers/Microsoft.OperationalInsights/workspaces/[workspace_name]</c>.</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="MetricQueryResult"/> instance containing the query results.</returns>
 public virtual Response <MetricQueryResult> Query(string resourceId, IEnumerable <string> metrics, MetricsQueryOptions options = null, CancellationToken cancellationToken = default)
 {
     using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(MetricsClient)}.{nameof(Query)}");
     scope.Start();
     try
     {
         return(_metricsRestClient.List(resourceId,
                                        timespan: options?.TimeSpan?.ToString(),
                                        interval: options?.Interval,
                                        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;
     }
 }