Skip to content

Commit

Permalink
[Azure.Monitor.Query] Add QueryResource (#35385)
Browse files Browse the repository at this point in the history
  • Loading branch information
nisha-bhatia authored Apr 26, 2023
1 parent 9cf66ee commit 2dd0ec9
Show file tree
Hide file tree
Showing 10 changed files with 802 additions and 14 deletions.
3 changes: 2 additions & 1 deletion sdk/monitor/Azure.Monitor.Query/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## 1.2.0-beta.1 (2023-04-11)
### Features Added
Added Client Builder Extension methods for `LogsQueryClient` and `MetricsQueryClient`
- Added Client Builder Extension methods for `LogsQueryClient` and `MetricsQueryClient`
- Added the `QueryResource` method to `LogsQueryClient` to allow users to query Azure resources directly without the context of a workspace.

## 1.1.0 (2022-01-25)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public LogsQueryClient(System.Uri endpoint, Azure.Core.TokenCredential credentia
public static string CreateQuery(System.FormattableString query) { throw null; }
public virtual Azure.Response<Azure.Monitor.Query.Models.LogsBatchQueryResultCollection> QueryBatch(Azure.Monitor.Query.LogsBatchQuery batch, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Monitor.Query.Models.LogsBatchQueryResultCollection>> QueryBatchAsync(Azure.Monitor.Query.LogsBatchQuery batch, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response<Azure.Monitor.Query.Models.LogsQueryResult> QueryResource(Azure.Core.ResourceIdentifier resourceId, string query, Azure.Monitor.Query.QueryTimeRange timeRange, Azure.Monitor.Query.LogsQueryOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Monitor.Query.Models.LogsQueryResult>> QueryResourceAsync(Azure.Core.ResourceIdentifier resourceId, string query, Azure.Monitor.Query.QueryTimeRange timeRange, Azure.Monitor.Query.LogsQueryOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<System.Collections.Generic.IReadOnlyList<T>>> QueryResourceAsync<T>(Azure.Core.ResourceIdentifier resourceId, string query, Azure.Monitor.Query.QueryTimeRange timeRange, Azure.Monitor.Query.LogsQueryOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response<System.Collections.Generic.IReadOnlyList<T>> QueryResource<T>(Azure.Core.ResourceIdentifier resourceId, string query, Azure.Monitor.Query.QueryTimeRange timeRange, Azure.Monitor.Query.LogsQueryOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response<Azure.Monitor.Query.Models.LogsQueryResult> QueryWorkspace(string workspaceId, string query, Azure.Monitor.Query.QueryTimeRange timeRange, Azure.Monitor.Query.LogsQueryOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Monitor.Query.Models.LogsQueryResult>> QueryWorkspaceAsync(string workspaceId, string query, Azure.Monitor.Query.QueryTimeRange timeRange, Azure.Monitor.Query.LogsQueryOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<System.Collections.Generic.IReadOnlyList<T>>> QueryWorkspaceAsync<T>(string workspaceId, string query, Azure.Monitor.Query.QueryTimeRange timeRange, Azure.Monitor.Query.LogsQueryOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
Expand Down
358 changes: 358 additions & 0 deletions sdk/monitor/Azure.Monitor.Query/src/Generated/QueryRestClient.cs

Large diffs are not rendered by default.

194 changes: 188 additions & 6 deletions sdk/monitor/Azure.Monitor.Query/src/LogsQueryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public virtual Response<LogsQueryResult> QueryWorkspace(string workspaceId, stri
scope.Start();
try
{
return ExecuteAsync(workspaceId, query, timeRange, options, false, cancellationToken).EnsureCompleted();
return ExecuteAsync(workspaceId, query, timeRange, options, async: false, isWorkspace: true, cancellationToken).EnsureCompleted();
}
catch (Exception e)
{
Expand All @@ -201,7 +201,7 @@ public virtual async Task<Response<LogsQueryResult>> QueryWorkspaceAsync(string
scope.Start();
try
{
return await ExecuteAsync(workspaceId, query, timeRange, options, true, cancellationToken).ConfigureAwait(false);
return await ExecuteAsync(workspaceId, query, timeRange, options, async: true, isWorkspace: true, cancellationToken).ConfigureAwait(false);
}
catch (Exception e)
{
Expand Down Expand Up @@ -318,6 +318,186 @@ public virtual async Task<Response<LogsBatchQueryResultCollection>> QueryBatchAs
}
}

/// <summary>
/// Returns all the Azure Monitor logs matching the given query for an Azure resource.
/// <example snippet="Snippet:QueryResource">
/// <code language="csharp">
/// var client = new LogsQueryClient(new DefaultAzureCredential());
///
/// var results = await client.QueryResourceAsync(new ResourceIdentifier(TestEnvironment.StorageAccountId),
/// &quot;search *&quot;,
/// new QueryTimeRange(TimeSpan.FromDays(5)));
///
/// var resultTable = results.Value.Table;
///
/// foreach (LogsTableRow rows in resultTable.Rows)
/// {
/// foreach (var row in rows)
/// {
/// Console.WriteLine(row);
/// }
/// }
///
/// foreach (LogsTableColumn columns in resultTable.Columns)
/// {
/// Console.WriteLine(&quot;Name: &quot; + columns.Name + &quot; Type: &quot; + columns.Type);
/// }
/// </code>
/// </example>
/// </summary>
/// <param name="resourceId"> The resourceId where the query should be executed. </param>
/// <param name="query"> The Kusto query to fetch the logs. </param>
/// <param name="timeRange"> The time period for which the logs should be looked up. </param>
/// <param name="options">The <see cref="LogsQueryOptions"/> to configure the query.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
/// <returns>The logs matching the query.</returns>
public virtual Response<IReadOnlyList<T>> QueryResource<T>(ResourceIdentifier resourceId, string query, QueryTimeRange timeRange, LogsQueryOptions options = null, CancellationToken cancellationToken = default)
{
Response<LogsQueryResult> response = QueryResource(resourceId, query, timeRange, options, cancellationToken);

return Response.FromValue(RowBinder.Shared.BindResults<T>(response.Value.AllTables), response.GetRawResponse());
}

/// <summary>
/// Returns all the Azure Monitor logs matching the given query for an Azure resource.
/// <example snippet="Snippet:QueryResource">
/// <code language="csharp">
/// var client = new LogsQueryClient(new DefaultAzureCredential());
///
/// var results = await client.QueryResourceAsync(new ResourceIdentifier(TestEnvironment.StorageAccountId),
/// &quot;search *&quot;,
/// new QueryTimeRange(TimeSpan.FromDays(5)));
///
/// var resultTable = results.Value.Table;
///
/// foreach (LogsTableRow rows in resultTable.Rows)
/// {
/// foreach (var row in rows)
/// {
/// Console.WriteLine(row);
/// }
/// }
///
/// foreach (LogsTableColumn columns in resultTable.Columns)
/// {
/// Console.WriteLine(&quot;Name: &quot; + columns.Name + &quot; Type: &quot; + columns.Type);
/// }
/// </code>
/// </example>
/// </summary>
/// <param name="resourceId"> The resourceId where the query should be executed. </param>
/// <param name="query"> The Kusto query to fetch the logs. </param>
/// <param name="timeRange"> The time period for which the logs should be looked up. </param>
/// <param name="options">The <see cref="LogsQueryOptions"/> to configure the query.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
/// <returns>The logs matching the query.</returns>
public virtual async Task<Response<IReadOnlyList<T>>> QueryResourceAsync<T>(ResourceIdentifier resourceId, string query, QueryTimeRange timeRange, LogsQueryOptions options = null, CancellationToken cancellationToken = default)
{
Response<LogsQueryResult> response = await QueryWorkspaceAsync(resourceId, query, timeRange, options, cancellationToken).ConfigureAwait(false);

return Response.FromValue(RowBinder.Shared.BindResults<T>(response.Value.AllTables), response.GetRawResponse());
}

/// <summary>
/// Returns all the Azure Monitor logs matching the given query for an Azure resource.
/// <example snippet="Snippet:QueryResource">
/// <code language="csharp">
/// var client = new LogsQueryClient(new DefaultAzureCredential());
///
/// var results = await client.QueryResourceAsync(new ResourceIdentifier(TestEnvironment.StorageAccountId),
/// &quot;search *&quot;,
/// new QueryTimeRange(TimeSpan.FromDays(5)));
///
/// var resultTable = results.Value.Table;
///
/// foreach (LogsTableRow rows in resultTable.Rows)
/// {
/// foreach (var row in rows)
/// {
/// Console.WriteLine(row);
/// }
/// }
///
/// foreach (LogsTableColumn columns in resultTable.Columns)
/// {
/// Console.WriteLine(&quot;Name: &quot; + columns.Name + &quot; Type: &quot; + columns.Type);
/// }
/// </code>
/// </example>
/// </summary>
/// <param name="resourceId"> The resourceId where the query should be executed. </param>
/// <param name="query"> The Kusto query to fetch the logs. </param>
/// <param name="timeRange"> The time period for which the logs should be looked up. </param>
/// <param name="options">The <see cref="LogsQueryOptions"/> to configure the query.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
/// <returns>The logs matching the query.</returns>
public virtual Response<LogsQueryResult> QueryResource(ResourceIdentifier resourceId, string query, QueryTimeRange timeRange, LogsQueryOptions options = null, CancellationToken cancellationToken = default)
{
string resource = resourceId.ToString();
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(LogsQueryClient)}.{nameof(QueryResource)}");
scope.Start();
try
{
resource.TrimStart('/');
return ExecuteAsync(resource, query, timeRange, options, async: false, isWorkspace: false, cancellationToken).EnsureCompleted();
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}

/// <summary>
/// Returns all the Azure Monitor logs matching the given query for an Azure resource.
/// <example snippet="Snippet:QueryResource">
/// <code language="csharp">
/// var client = new LogsQueryClient(new DefaultAzureCredential());
///
/// var results = await client.QueryResourceAsync(new ResourceIdentifier(TestEnvironment.StorageAccountId),
/// &quot;search *&quot;,
/// new QueryTimeRange(TimeSpan.FromDays(5)));
///
/// var resultTable = results.Value.Table;
///
/// foreach (LogsTableRow rows in resultTable.Rows)
/// {
/// foreach (var row in rows)
/// {
/// Console.WriteLine(row);
/// }
/// }
///
/// foreach (LogsTableColumn columns in resultTable.Columns)
/// {
/// Console.WriteLine(&quot;Name: &quot; + columns.Name + &quot; Type: &quot; + columns.Type);
/// }
/// </code>
/// </example>
/// </summary>
/// <param name="resourceId"> The resourceId where the query should be executed. </param>
/// <param name="query"> The Kusto query to fetch the logs. </param>
/// <param name="timeRange"> The time period for which the logs should be looked up. </param>
/// <param name="options">The <see cref="LogsQueryOptions"/> to configure the query.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
/// <returns>The logs matching the query.</returns>
public virtual async Task<Response<LogsQueryResult>> QueryResourceAsync(ResourceIdentifier resourceId, string query, QueryTimeRange timeRange, LogsQueryOptions options = null, CancellationToken cancellationToken = default)
{
string resource = resourceId.ToString();
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(LogsQueryClient)}.{nameof(QueryResource)}");
scope.Start();
try
{
resource.TrimStart('/');
return await ExecuteAsync(resource, query, timeRange, options, async: true, isWorkspace: false, cancellationToken).ConfigureAwait(false);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}

/// <summary>
/// Create a Kusto query from an interpolated string. The interpolated values will be quoted and escaped as necessary.
/// </summary>
Expand Down Expand Up @@ -551,15 +731,17 @@ Response<LogsBatchQueryResultCollection> ConvertBatchResponse(BatchResponse resp
}
}

private async Task<Response<LogsQueryResult>> ExecuteAsync(string workspaceId, string query, QueryTimeRange timeRange, LogsQueryOptions options, bool async, CancellationToken cancellationToken = default)
private async Task<Response<LogsQueryResult>> ExecuteAsync(string id, string query, QueryTimeRange timeRange, LogsQueryOptions options, bool async, bool isWorkspace, CancellationToken cancellationToken = default)
{
if (workspaceId == null)
if (id == null)
{
throw new ArgumentNullException(nameof(workspaceId));
throw new ArgumentNullException(nameof(id));
}

QueryBody queryBody = CreateQueryBody(query, timeRange, options, out string prefer);
using var message = _queryClient.CreateExecuteRequest(workspaceId, queryBody, prefer);

using var message = isWorkspace ? _queryClient.CreateExecuteRequest(id, queryBody, prefer)
: _queryClient.CreateResourceExecuteRequest(new ResourceIdentifier(id), queryBody, prefer);

if (options?.ServerTimeout != null)
{
Expand Down
16 changes: 12 additions & 4 deletions sdk/monitor/Azure.Monitor.Query/src/autorest.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ Run `dotnet build /t:GenerateCode` to generate code.
``` yaml
title: MonitorQuery
input-file:
- https://github.com/Azure/azure-rest-api-specs/blob/dba6ed1f03bda88ac6884c0a883246446cc72495/specification/operationalinsights/data-plane/Microsoft.OperationalInsights/preview/2021-05-19_Preview/OperationalInsights.json
- https://github.com/Azure/azure-rest-api-specs/blob/dba6ed1f03bda88ac6884c0a883246446cc72495/specification/monitor/resource-manager/Microsoft.Insights/stable/2018-01-01/metricDefinitions_API.json
- https://github.com/Azure/azure-rest-api-specs/blob/dba6ed1f03bda88ac6884c0a883246446cc72495/specification/monitor/resource-manager/Microsoft.Insights/stable/2018-01-01/metrics_API.json
- https://github.com/Azure/azure-rest-api-specs/blob/dba6ed1f03bda88ac6884c0a883246446cc72495/specification/monitor/resource-manager/Microsoft.Insights/preview/2017-12-01-preview/metricNamespaces_API.json
- https://github.com/Azure/azure-rest-api-specs/blob/9a46bdbfea7730a3521ac11f5ea1120007d67f79/specification/operationalinsights/data-plane/Microsoft.OperationalInsights/stable/2022-10-27/OperationalInsights.json
- https://github.com/Azure/azure-rest-api-specs/blob/fc3c409825d6a31ba909a88ea34dcc13165edc9c/specification/monitor/resource-manager/Microsoft.Insights/stable/2018-01-01/metricDefinitions_API.json
- https://github.com/Azure/azure-rest-api-specs/blob/fc3c409825d6a31ba909a88ea34dcc13165edc9c/specification/monitor/resource-manager/Microsoft.Insights/stable/2018-01-01/metrics_API.json
- https://github.com/Azure/azure-rest-api-specs/blob/fc3c409825d6a31ba909a88ea34dcc13165edc9c/specification/monitor/resource-manager/Microsoft.Insights/preview/2017-12-01-preview/metricNamespaces_API.json
generation1-convenience-client: true
modelerfour:
lenient-model-deduplication: true
Expand Down Expand Up @@ -69,3 +69,11 @@ directive:
transform: >
$.required = ["name", "type"]
```
``` yaml
directive:
- from: swagger-document
where: $.parameters.ResourceIdParameter
transform:
$["x-ms-format"] = "arm-id"
```
1 change: 1 addition & 0 deletions sdk/monitor/Azure.Monitor.Query/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ extends:
parameters:
ServiceDirectory: monitor
Project: Azure.Monitor.Query
TimeoutInMinutes: 120
Loading

0 comments on commit 2dd0ec9

Please sign in to comment.