Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 21 additions & 23 deletions sdk/monitor/Azure.Monitor.Query/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,12 @@ All client instance methods are thread-safe and independent of each other ([guid
You can query logs using the `LogsQueryClient.QueryAsync` method. The result is returned as a table with a collection of rows:

```C# Snippet:QueryLogsAsTable
var endpoint = new Uri("https://api.loganalytics.io");
string workspaceId = "<workspace_id>";

var client = new LogsQueryClient(endpoint, new DefaultAzureCredential());
var client = new LogsQueryClient(new DefaultAzureCredential());
Response<LogsQueryResult> response = await client.QueryAsync(
workspaceId,
"AzureActivity | top 10 by TimeGenerated",
TimeSpan.FromDays(1));
new DateTimeRange(TimeSpan.FromDays(1)));

LogsQueryResultTable table = response.Value.PrimaryTable;

Expand Down Expand Up @@ -102,7 +100,7 @@ string workspaceId = "<workspace_id>";
Response<IReadOnlyList<MyLogEntryModel>> response = await client.QueryAsync<MyLogEntryModel>(
workspaceId,
"AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count",
TimeSpan.FromDays(1));
new DateTimeRange(TimeSpan.FromDays(1)));

foreach (var logEntryModel in response.Value)
{
Expand All @@ -115,16 +113,15 @@ foreach (var logEntryModel in response.Value)
If your query returns a single column (or a single value) of a primitive type, use the `LogsQueryClient.QueryAsync<T>` overload to deserialize it:

```C# Snippet:QueryLogsAsPrimitive
var endpoint = new Uri("https://api.loganalytics.io");
string workspaceId = "<workspace_id>";

var client = new LogsQueryClient(endpoint, new DefaultAzureCredential());
var client = new LogsQueryClient(new DefaultAzureCredential());

// Query TOP 10 resource groups by event count
Response<IReadOnlyList<string>> response = await client.QueryAsync<string>(
workspaceId,
"AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count | project ResourceGroup",
TimeSpan.FromDays(1));
new DateTimeRange(TimeSpan.FromDays(1)));

foreach (var resourceGroup in response.Value)
{
Expand All @@ -137,10 +134,9 @@ foreach (var resourceGroup in response.Value)
You can execute multiple queries in a single request using the `LogsQueryClient.CreateBatchQuery` method:

```C# Snippet:BatchQuery
var endpoint = new Uri("https://api.loganalytics.io");
string workspaceId = "<workspace_id>";

var client = new LogsQueryClient(endpoint, new DefaultAzureCredential());
var client = new LogsQueryClient(new DefaultAzureCredential());

// Query TOP 10 resource groups by event count
// And total event count
Expand All @@ -149,11 +145,11 @@ var batch = new LogsBatchQuery();
string countQueryId = batch.AddQuery(
workspaceId,
"AzureActivity | count",
TimeSpan.FromDays(1));
new DateTimeRange(TimeSpan.FromDays(1)));
string topQueryId = batch.AddQuery(
workspaceId,
"AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count",
TimeSpan.FromDays(1));
new DateTimeRange(TimeSpan.FromDays(1)));

Response<LogsBatchQueryResults> response = await client.QueryBatchAsync(batch);

Expand All @@ -172,14 +168,13 @@ foreach (var logEntryModel in topEntries)
You can also dynamically inspect the list of columns. The following example prints the query result as a table:

```C# Snippet:QueryLogsPrintTable
var endpoint = new Uri("https://api.loganalytics.io");
string workspaceId = "<workspace_id>";

var client = new LogsQueryClient(endpoint, new DefaultAzureCredential());
var client = new LogsQueryClient(new DefaultAzureCredential());
Response<LogsQueryResult> response = await client.QueryAsync(
workspaceId,
"AzureActivity | top 10 by TimeGenerated",
TimeSpan.FromDays(1));
new DateTimeRange(TimeSpan.FromDays(1)));

LogsQueryResultTable table = response.Value.PrimaryTable;

Expand Down Expand Up @@ -207,16 +202,15 @@ foreach (var row in table.Rows)
Some Logs queries take longer than 3 minutes to execute. The default server timeout is 3 minutes. You can increase the server timeout to a maximum of 10 minutes. In the following example, the `LogsQueryOptions` object's `ServerTimeout` property is used to set the server timeout to 10 minutes:

```C# Snippet:QueryLogsWithTimeout
var endpoint = new Uri("https://api.loganalytics.io");
string workspaceId = "<workspace_id>";

var client = new LogsQueryClient(endpoint, new DefaultAzureCredential());
var client = new LogsQueryClient(new DefaultAzureCredential());

// Query TOP 10 resource groups by event count
Response<IReadOnlyList<int>> response = await client.QueryAsync<int>(
workspaceId,
"AzureActivity | summarize count()",
TimeSpan.FromDays(1),
new DateTimeRange(TimeSpan.FromDays(1)),
options: new LogsQueryOptions
{
ServerTimeout = TimeSpan.FromMinutes(10)
Expand All @@ -232,12 +226,17 @@ foreach (var resourceGroup in response.Value)

You can query metrics using the `MetricsQueryClient.QueryAsync` method. For every requested metric, a set of aggregated values is returned inside the `TimeSeries` collection.

A resource ID is required to query metrics. To find the resource ID:

1. Navigate to your resource's page in the Azure portal.
2. From the **Overview** blade, select the **JSON View** link.
3. In the resulting JSON, copy the value of the `id` property.

```C# Snippet:QueryMetrics
var endpoint = new Uri("https://management.azure.com");
string resourceId =
"/subscriptions/<subscription_id>/resourceGroups/<resource_group_name>/providers/Microsoft.OperationalInsights/workspaces/<workspace_name>";

var metricsClient = new MetricsQueryClient(endpoint, new DefaultAzureCredential());
var metricsClient = new MetricsQueryClient(new DefaultAzureCredential());

Response<MetricQueryResult> results = await metricsClient.QueryAsync(
resourceId,
Expand Down Expand Up @@ -268,15 +267,14 @@ When you interact with the Azure Monitor Query client library using the .NET SDK
For example, if you submit an invalid query, an HTTP 400 error is returned, indicating "Bad Request".

```C# Snippet:BadRequest
var endpoint = new Uri("https://api.loganalytics.io");
string workspaceId = "<workspace_id>";

var client = new LogsQueryClient(endpoint, new DefaultAzureCredential());
var client = new LogsQueryClient(new DefaultAzureCredential());

try
{
await client.QueryAsync(
workspaceId, "My Not So Valid Query", TimeSpan.FromDays(1));
workspaceId, "My Not So Valid Query", new DateTimeRange(TimeSpan.FromDays(1)));
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ public LogsQueryOptions() { }
public partial class MetricsQueryClient
{
protected MetricsQueryClient() { }
public MetricsQueryClient(System.Uri endpoint, Azure.Core.TokenCredential credential) { }
public MetricsQueryClient(System.Uri endpoint, Azure.Core.TokenCredential credential, Azure.Monitor.Query.MetricsQueryClientOptions options) { }
public MetricsQueryClient(Azure.Core.TokenCredential credential) { }
public MetricsQueryClient(Azure.Core.TokenCredential credential, Azure.Monitor.Query.MetricsQueryClientOptions options) { }
public MetricsQueryClient(System.Uri endpoint, Azure.Core.TokenCredential credential, Azure.Monitor.Query.MetricsQueryClientOptions options = null) { }
public virtual Azure.Response<System.Collections.Generic.IReadOnlyList<Azure.Monitor.Query.Models.MetricNamespace>> GetMetricNamespaces(string resourceId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<System.Collections.Generic.IReadOnlyList<Azure.Monitor.Query.Models.MetricNamespace>>> GetMetricNamespacesAsync(string resourceId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response<System.Collections.Generic.IReadOnlyList<Azure.Monitor.Query.Models.MetricDefinition>> GetMetrics(string resourceId, string metricsNamespace, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@

<ItemGroup>
<PackageReference Include="System.Text.Json" />
<!--
<PackageReference Include="Azure.Core" />
<PackageReference Include="Azure.Core.Experimental" />
-->
<ProjectReference Include="..\..\..\core\Azure.Core\src\Azure.Core.csproj" />
<ProjectReference Include="..\..\..\core\Azure.Core.Experimental\src\Azure.Core.Experimental.csproj" />
<PackageReference Include="Azure.Core" />
<PackageReference Include="Azure.Core.Experimental" />
</ItemGroup>

<!-- Shared source from Azure.Core -->
Expand Down
25 changes: 13 additions & 12 deletions sdk/monitor/Azure.Monitor.Query/src/LogsQueryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class LogsQueryClient

/// <summary>
/// Initializes a new instance of <see cref="LogsQueryClient"/>. Uses the default 'https://api.loganalytics.io' endpoint.
/// <code snippet="Snippet:CreateLogsClient" language="csharp">
/// var client = new LogsQueryClient(new DefaultAzureCredential());
/// </code>
/// </summary>
/// <param name="credential">The <see cref="TokenCredential"/> instance to use for authentication.</param>
public LogsQueryClient(TokenCredential credential) : this(credential, null)
Expand Down Expand Up @@ -86,15 +89,15 @@ protected LogsQueryClient()
/// Response&lt;IReadOnlyList&lt;MyLogEntryModel&gt;&gt; response = await client.QueryAsync&lt;MyLogEntryModel&gt;(
/// workspaceId,
/// &quot;AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count&quot;,
/// TimeSpan.FromDays(1));
/// new DateTimeRange(TimeSpan.FromDays(1)));
/// </code>
///
/// Example of querying a primitive:
/// <code snippet="Snippet:QueryLogsAsPrimitiveCall" language="csharp">
/// Response&lt;IReadOnlyList&lt;string&gt;&gt; response = await client.QueryAsync&lt;string&gt;(
/// workspaceId,
/// &quot;AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count | project ResourceGroup&quot;,
/// TimeSpan.FromDays(1));
/// new DateTimeRange(TimeSpan.FromDays(1)));
/// </code>
/// </summary>
/// <param name="workspaceId">The workspace id to include in the query (<c>xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx</c>).</param>
Expand All @@ -118,15 +121,15 @@ public virtual Response<IReadOnlyList<T>> Query<T>(string workspaceId, string qu
/// Response&lt;IReadOnlyList&lt;MyLogEntryModel&gt;&gt; response = await client.QueryAsync&lt;MyLogEntryModel&gt;(
/// workspaceId,
/// &quot;AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count&quot;,
/// TimeSpan.FromDays(1));
/// new DateTimeRange(TimeSpan.FromDays(1)));
/// </code>
///
/// Example of querying a primitive:
/// <code snippet="Snippet:QueryLogsAsPrimitiveCall" language="csharp">
/// Response&lt;IReadOnlyList&lt;string&gt;&gt; response = await client.QueryAsync&lt;string&gt;(
/// workspaceId,
/// &quot;AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count | project ResourceGroup&quot;,
/// TimeSpan.FromDays(1));
/// new DateTimeRange(TimeSpan.FromDays(1)));
/// </code>
/// </summary>
/// <param name="workspaceId">The workspace id to include in the query (<c>xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx</c>).</param>
Expand Down Expand Up @@ -193,10 +196,9 @@ public virtual async Task<Response<LogsQueryResult>> QueryAsync(string workspace
/// <summary>
/// Submits the batch query. Use the <see cref="LogsBatchQuery"/> to compose a batch query.
/// <code snippet="Snippet:BatchQuery" language="csharp">
/// var endpoint = new Uri(&quot;https://api.loganalytics.io&quot;);
/// string workspaceId = &quot;&lt;workspace_id&gt;&quot;;
///
/// var client = new LogsQueryClient(endpoint, new DefaultAzureCredential());
/// var client = new LogsQueryClient(new DefaultAzureCredential());
///
/// // Query TOP 10 resource groups by event count
/// // And total event count
Expand All @@ -205,11 +207,11 @@ public virtual async Task<Response<LogsQueryResult>> QueryAsync(string workspace
/// string countQueryId = batch.AddQuery(
/// workspaceId,
/// &quot;AzureActivity | count&quot;,
/// TimeSpan.FromDays(1));
/// new DateTimeRange(TimeSpan.FromDays(1)));
/// string topQueryId = batch.AddQuery(
/// workspaceId,
/// &quot;AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count&quot;,
/// TimeSpan.FromDays(1));
/// new DateTimeRange(TimeSpan.FromDays(1)));
///
/// Response&lt;LogsBatchQueryResults&gt; response = await client.QueryBatchAsync(batch);
///
Expand Down Expand Up @@ -247,10 +249,9 @@ public virtual Response<LogsBatchQueryResults> QueryBatch(LogsBatchQuery batch,
/// <summary>
/// Submits the batch query. Use the <see cref="LogsBatchQuery"/> to compose a batch query.
/// <code snippet="Snippet:BatchQuery" language="csharp">
/// var endpoint = new Uri(&quot;https://api.loganalytics.io&quot;);
/// string workspaceId = &quot;&lt;workspace_id&gt;&quot;;
///
/// var client = new LogsQueryClient(endpoint, new DefaultAzureCredential());
/// var client = new LogsQueryClient(new DefaultAzureCredential());
///
/// // Query TOP 10 resource groups by event count
/// // And total event count
Expand All @@ -259,11 +260,11 @@ public virtual Response<LogsBatchQueryResults> QueryBatch(LogsBatchQuery batch,
/// string countQueryId = batch.AddQuery(
/// workspaceId,
/// &quot;AzureActivity | count&quot;,
/// TimeSpan.FromDays(1));
/// new DateTimeRange(TimeSpan.FromDays(1)));
/// string topQueryId = batch.AddQuery(
/// workspaceId,
/// &quot;AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count&quot;,
/// TimeSpan.FromDays(1));
/// new DateTimeRange(TimeSpan.FromDays(1)));
///
/// Response&lt;LogsBatchQueryResults&gt; response = await client.QueryBatchAsync(batch);
///
Expand Down
27 changes: 19 additions & 8 deletions sdk/monitor/Azure.Monitor.Query/src/MetricsQueryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,30 @@ namespace Azure.Monitor.Query
/// </summary>
public class MetricsQueryClient
{
private static readonly Uri _defaultEndpoint = new Uri("https://management.azure.com");

private readonly MetricDefinitionsRestClient _metricDefinitionsClient;
private readonly MetricsRestClient _metricsRestClient;
private readonly MetricNamespacesRestClient _namespacesRestClient;
private readonly ClientDiagnostics _clientDiagnostics;

/// <summary>
/// Initializes a new instance of <see cref="MetricsQueryClient"/>.
/// Initializes a new instance of <see cref="MetricsQueryClient"/>. Uses the default 'https://management.azure.com' endpoint.
/// <code snippet="Snippet:CreateMetricsClient" language="csharp">
/// var metricsClient = new MetricsQueryClient(new DefaultAzureCredential());
/// </code>
/// </summary>
/// <param name="credential">The <see cref="TokenCredential"/> instance to use for authentication.</param>
public MetricsQueryClient(TokenCredential credential) : this(credential, null)
{
}

/// <summary>
/// Initializes a new instance of <see cref="MetricsQueryClient"/>. Uses the default 'https://management.azure.com' endpoint.
/// </summary>
/// <param name="endpoint">The resource manager service endpoint to use. For example, <c>https://management.azure.com/</c> for public cloud.</param>
/// <param name="credential">The <see cref="TokenCredential"/> instance to use for authentication.</param>
public MetricsQueryClient(Uri endpoint, TokenCredential credential) : this(endpoint, credential, null)
/// <param name="options">The <see cref="MetricsQueryClientOptions"/> instance to as client configuration.</param>
public MetricsQueryClient(TokenCredential credential, MetricsQueryClientOptions options) : this(_defaultEndpoint, credential, options)
{
}

Expand All @@ -36,7 +49,7 @@ public MetricsQueryClient(Uri endpoint, TokenCredential credential) : this(endpo
/// <param name="endpoint">The resource manager service endpoint to use. For example <c>https://management.azure.com/</c> for public cloud.</param>
/// <param name="credential">The <see cref="TokenCredential"/> instance to use for authentication.</param>
/// <param name="options">The <see cref="MetricsQueryClientOptions"/> instance to as client configuration.</param>
public MetricsQueryClient(Uri endpoint, TokenCredential credential, MetricsQueryClientOptions options)
public MetricsQueryClient(Uri endpoint, TokenCredential credential, MetricsQueryClientOptions options = null)
{
Argument.AssertNotNull(credential, nameof(credential));

Expand All @@ -60,11 +73,10 @@ protected MetricsQueryClient()
/// <summary>
/// Queries metrics for a resource.
/// <code snippet="Snippet:QueryMetrics" language="csharp">
/// var endpoint = new Uri(&quot;https://management.azure.com&quot;);
/// string resourceId =
/// &quot;/subscriptions/&lt;subscription_id&gt;/resourceGroups/&lt;resource_group_name&gt;/providers/Microsoft.OperationalInsights/workspaces/&lt;workspace_name&gt;&quot;;
///
/// var metricsClient = new MetricsQueryClient(endpoint, new DefaultAzureCredential());
/// var metricsClient = new MetricsQueryClient(new DefaultAzureCredential());
///
/// Response&lt;MetricQueryResult&gt; results = await metricsClient.QueryAsync(
/// resourceId,
Expand Down Expand Up @@ -119,11 +131,10 @@ public virtual Response<MetricQueryResult> Query(string resourceId, IEnumerable<
/// <summary>
/// Queries metrics for a resource.
/// <code snippet="Snippet:QueryMetrics" language="csharp">
/// var endpoint = new Uri(&quot;https://management.azure.com&quot;);
/// string resourceId =
/// &quot;/subscriptions/&lt;subscription_id&gt;/resourceGroups/&lt;resource_group_name&gt;/providers/Microsoft.OperationalInsights/workspaces/&lt;workspace_name&gt;&quot;;
///
/// var metricsClient = new MetricsQueryClient(endpoint, new DefaultAzureCredential());
/// var metricsClient = new MetricsQueryClient(new DefaultAzureCredential());
///
/// Response&lt;MetricQueryResult&gt; results = await metricsClient.QueryAsync(
/// resourceId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public LogsBatchQuery()
/// string countQueryId = batch.AddQuery(
/// workspaceId,
/// &quot;AzureActivity | count&quot;,
/// TimeSpan.FromDays(1));
/// new DateTimeRange(TimeSpan.FromDays(1)));
/// string topQueryId = batch.AddQuery(
/// workspaceId,
/// &quot;AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count&quot;,
/// TimeSpan.FromDays(1));
/// new DateTimeRange(TimeSpan.FromDays(1)));
///
/// Response&lt;LogsBatchQueryResults&gt; response = await client.QueryBatchAsync(batch);
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public partial class LogsBatchQueryResults
/// string countQueryId = batch.AddQuery(
/// workspaceId,
/// &quot;AzureActivity | count&quot;,
/// TimeSpan.FromDays(1));
/// new DateTimeRange(TimeSpan.FromDays(1)));
/// string topQueryId = batch.AddQuery(
/// workspaceId,
/// &quot;AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count&quot;,
/// TimeSpan.FromDays(1));
/// new DateTimeRange(TimeSpan.FromDays(1)));
///
/// Response&lt;LogsBatchQueryResults&gt; response = await client.QueryBatchAsync(batch);
///
Expand Down
Loading