Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add QueryTimeSpan type #20841

Merged
merged 5 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
18 changes: 10 additions & 8 deletions sdk/monitor/Azure.Monitor.Query/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ You can query logs using the `LogsClient.QueryAsync`. The result would be return
```C# Snippet:QueryLogsAsTable
LogsClient client = new LogsClient(new DefaultAzureCredential());
string workspaceId = "<workspace_id>";
Response<LogsQueryResult> response = await client.QueryAsync(workspaceId, "AzureActivity | top 10 by TimeGenerated");
Response<LogsQueryResult> response = await client.QueryAsync(workspaceId, "AzureActivity | top 10 by TimeGenerated", TimeSpan.FromDays(1));

LogsQueryResultTable table = response.Value.PrimaryTable;

Expand Down Expand Up @@ -93,7 +93,8 @@ string workspaceId = "<workspace_id>";

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

foreach (var logEntryModel in response.Value)
{
Expand All @@ -111,7 +112,8 @@ string workspaceId = "<workspace_id>";

// 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");
"AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count | project ResourceGroup",
TimeSpan.FromDays(1));

foreach (var resourceGroup in response.Value)
{
Expand All @@ -130,8 +132,8 @@ string workspaceId = "<workspace_id>";
// Query TOP 10 resource groups by event count
// And total event count
LogsBatchQuery batch = client.CreateBatchQuery();
string countQueryId = batch.AddQuery(workspaceId, "AzureActivity | count");
string topQueryId = batch.AddQuery(workspaceId, "AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count");
string countQueryId = batch.AddQuery(workspaceId, "AzureActivity | count", TimeSpan.FromDays(1));
string topQueryId = batch.AddQuery(workspaceId, "AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count", TimeSpan.FromDays(1));

Response<LogsBatchQueryResult> response = await batch.SubmitAsync();

Expand All @@ -152,7 +154,7 @@ You can also dynamically inspect the list of columns. The following example prin
```C# Snippet:QueryLogsPrintTable
LogsClient client = new LogsClient(new DefaultAzureCredential());
string workspaceId = "<workspace_id>";
Response<LogsQueryResult> response = await client.QueryAsync(workspaceId, "AzureActivity | top 10 by TimeGenerated");
Response<LogsQueryResult> response = await client.QueryAsync(workspaceId, "AzureActivity | top 10 by TimeGenerated", TimeSpan.FromDays(1));

LogsQueryResultTable table = response.Value.PrimaryTable;

Expand Down Expand Up @@ -182,7 +184,7 @@ Some queries take longer to execute than the default service timeout allows. You
```C# Snippet:QueryLogsPrintTable
LogsClient client = new LogsClient(new DefaultAzureCredential());
string workspaceId = "<workspace_id>";
Response<LogsQueryResult> response = await client.QueryAsync(workspaceId, "AzureActivity | top 10 by TimeGenerated");
Response<LogsQueryResult> response = await client.QueryAsync(workspaceId, "AzureActivity | top 10 by TimeGenerated", TimeSpan.FromDays(1));

LogsQueryResultTable table = response.Value.PrimaryTable;

Expand Down Expand Up @@ -218,7 +220,7 @@ string workspaceId = "<workspace_id>";
LogsClient client = new LogsClient(new DefaultAzureCredential());
try
{
await client.QueryAsync(workspaceId, "My Not So Valid Query");
await client.QueryAsync(workspaceId, "My Not So Valid Query", TimeSpan.FromDays(1));
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Azure.Monitor.Query
public partial class LogsBatchQuery
{
protected LogsBatchQuery() { }
public virtual string AddQuery(string workspaceId, string query, System.TimeSpan? timeSpan = default(System.TimeSpan?), Azure.Monitor.Query.LogsQueryOptions options = null) { throw null; }
public virtual string AddQuery(string workspaceId, string query, Azure.Monitor.Query.QueryTimeSpan timeSpan, Azure.Monitor.Query.LogsQueryOptions options = null) { throw null; }
public virtual Azure.Response<Azure.Monitor.Query.Models.LogsBatchQueryResult> Submit(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Monitor.Query.Models.LogsBatchQueryResult>> SubmitAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
}
Expand All @@ -13,10 +13,10 @@ protected LogsClient() { }
public LogsClient(Azure.Core.TokenCredential credential) { }
public LogsClient(Azure.Core.TokenCredential credential, Azure.Monitor.Query.LogsClientOptions options) { }
public virtual Azure.Monitor.Query.LogsBatchQuery CreateBatchQuery() { throw null; }
public virtual Azure.Response<Azure.Monitor.Query.Models.LogsQueryResult> Query(string workspaceId, string query, System.TimeSpan? timeSpan = default(System.TimeSpan?), 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>> QueryAsync(string workspaceId, string query, System.TimeSpan? timeSpan = default(System.TimeSpan?), 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>>> QueryAsync<T>(string workspaceId, string query, System.TimeSpan? timeSpan = default(System.TimeSpan?), 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>> Query<T>(string workspaceId, string query, System.TimeSpan? timeSpan = default(System.TimeSpan?), 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> Query(string workspaceId, string query, Azure.Monitor.Query.QueryTimeSpan timeSpan, 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>> QueryAsync(string workspaceId, string query, Azure.Monitor.Query.QueryTimeSpan timeSpan, 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>>> QueryAsync<T>(string workspaceId, string query, Azure.Monitor.Query.QueryTimeSpan timeSpan, 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>> Query<T>(string workspaceId, string query, Azure.Monitor.Query.QueryTimeSpan timeSpan, Azure.Monitor.Query.LogsQueryOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
}
public partial class LogsClientOptions : Azure.Core.ClientOptions
{
Expand All @@ -36,13 +36,11 @@ public partial class MetricQueryOptions
{
public MetricQueryOptions() { }
public System.Collections.Generic.IList<Azure.Monitor.Query.Models.MetricAggregationType> Aggregations { get { throw null; } }
public System.TimeSpan? Duration { get { throw null; } set { } }
public System.DateTimeOffset? EndTime { get { throw null; } set { } }
public string Filter { get { throw null; } set { } }
public System.TimeSpan? Interval { get { throw null; } set { } }
public string MetricNamespace { get { throw null; } set { } }
public string OrderBy { get { throw null; } set { } }
public System.DateTimeOffset? StartTime { get { throw null; } set { } }
public Azure.Monitor.Query.QueryTimeSpan? TimeSpan { get { throw null; } set { } }
public int? Top { get { throw null; } set { } }
}
public partial class MetricsClient
Expand All @@ -65,6 +63,25 @@ public enum ServiceVersion
V2018_01_01 = 0,
}
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public readonly partial struct QueryTimeSpan : System.IEquatable<Azure.Monitor.Query.QueryTimeSpan>
{
public QueryTimeSpan(System.DateTimeOffset startTime, System.DateTimeOffset endTime) { throw null; }
public QueryTimeSpan(System.DateTimeOffset startTime, System.TimeSpan duration) { throw null; }
public QueryTimeSpan(System.TimeSpan duration) { throw null; }
public QueryTimeSpan(System.TimeSpan duration, System.DateTimeOffset endTime) { throw null; }
public System.TimeSpan Duration { get { throw null; } }
public System.DateTimeOffset? EndTime { get { throw null; } }
public static Azure.Monitor.Query.QueryTimeSpan MaxValue { get { throw null; } }
public System.DateTimeOffset? StartTime { get { throw null; } }
public bool Equals(Azure.Monitor.Query.QueryTimeSpan other) { throw null; }
public override bool Equals(object obj) { throw null; }
public override int GetHashCode() { throw null; }
public static bool operator ==(Azure.Monitor.Query.QueryTimeSpan left, Azure.Monitor.Query.QueryTimeSpan right) { throw null; }
public static implicit operator Azure.Monitor.Query.QueryTimeSpan (System.TimeSpan timeSpan) { throw null; }
public static bool operator !=(Azure.Monitor.Query.QueryTimeSpan left, Azure.Monitor.Query.QueryTimeSpan right) { throw null; }
public override string ToString() { throw null; }
}
}
namespace Azure.Monitor.Query.Models
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<Compile Include="$(AzureCoreSharedSources)HttpMessageSanitizer.cs" LinkBase="Shared" />
<Compile Include="$(AzureCoreSharedSources)TaskExtensions.cs" LinkBase="Shared" />
<Compile Include="$(AzureCoreSharedSources)OperationHelpers.cs" LinkBase="Shared" />
<Compile Include="$(AzureCoreSharedSources)HashCodeBuilder.cs" LinkBase="Shared" />
<Compile Include="$(AzureCoreSharedSources)AzureResourceProviderNamespaceAttribute.cs" LinkBase="Shared" />
</ItemGroup>

Expand Down
16 changes: 8 additions & 8 deletions sdk/monitor/Azure.Monitor.Query/src/LogsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected LogsClient()
/// <param name="options">The <see cref="LogsQueryOptions"/> to configure the query.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
/// <returns>Query results mapped to a type <typeparamref name="T"/>.</returns>
public virtual Response<IReadOnlyList<T>> Query<T>(string workspaceId, string query, TimeSpan? timeSpan = null, LogsQueryOptions options = null, CancellationToken cancellationToken = default)
public virtual Response<IReadOnlyList<T>> Query<T>(string workspaceId, string query, QueryTimeSpan timeSpan, LogsQueryOptions options = null, CancellationToken cancellationToken = default)
{
Response<LogsQueryResult> response = Query(workspaceId, query, timeSpan, options, cancellationToken);

Expand All @@ -79,7 +79,7 @@ public virtual Response<IReadOnlyList<T>> Query<T>(string workspaceId, string qu
/// <param name="options">The <see cref="LogsQueryOptions"/> to configure the query.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
/// <returns>Query results mapped to a type <typeparamref name="T"/>.</returns>
public virtual async Task<Response<IReadOnlyList<T>>> QueryAsync<T>(string workspaceId, string query, TimeSpan? timeSpan = null, LogsQueryOptions options = null, CancellationToken cancellationToken = default)
public virtual async Task<Response<IReadOnlyList<T>>> QueryAsync<T>(string workspaceId, string query, QueryTimeSpan timeSpan, LogsQueryOptions options = null, CancellationToken cancellationToken = default)
{
Response<LogsQueryResult> response = await QueryAsync(workspaceId, query, timeSpan, options, cancellationToken).ConfigureAwait(false);

Expand All @@ -95,7 +95,7 @@ public virtual async Task<Response<IReadOnlyList<T>>> QueryAsync<T>(string works
/// <param name="options">The <see cref="LogsQueryOptions"/> to configure the query.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
/// <returns>The <see cref="LogsQueryResult"/> containing the query results.</returns>
public virtual Response<LogsQueryResult> Query(string workspaceId, string query, TimeSpan? timeSpan = null, LogsQueryOptions options = null, CancellationToken cancellationToken = default)
public virtual Response<LogsQueryResult> Query(string workspaceId, string query, QueryTimeSpan timeSpan, LogsQueryOptions options = null, CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(LogsClient)}.{nameof(Query)}");
scope.Start();
Expand All @@ -119,7 +119,7 @@ public virtual Response<LogsQueryResult> Query(string workspaceId, string query,
/// <param name="options">The <see cref="LogsQueryOptions"/> to configure the query.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
/// <returns>The <see cref="LogsQueryResult"/> with the query results.</returns>
public virtual async Task<Response<LogsQueryResult>> QueryAsync(string workspaceId, string query, TimeSpan? timeSpan = null, LogsQueryOptions options = null, CancellationToken cancellationToken = default)
public virtual async Task<Response<LogsQueryResult>> QueryAsync(string workspaceId, string query, QueryTimeSpan timeSpan, LogsQueryOptions options = null, CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(LogsClient)}.{nameof(Query)}");
scope.Start();
Expand All @@ -143,12 +143,12 @@ public virtual LogsBatchQuery CreateBatchQuery()
return new LogsBatchQuery(_clientDiagnostics, _queryClient, _rowBinder);
}

internal static QueryBody CreateQueryBody(string query, TimeSpan? timeSpan, LogsQueryOptions options, out string prefer)
internal static QueryBody CreateQueryBody(string query, QueryTimeSpan timeSpan, LogsQueryOptions options, out string prefer)
{
var queryBody = new QueryBody(query);
if (timeSpan != null)
if (timeSpan != QueryTimeSpan.MaxValue)
{
queryBody.Timespan = TypeFormatters.ToString(timeSpan.Value, "P");
queryBody.Timespan = timeSpan.ToString();
}

prefer = null;
Expand All @@ -166,7 +166,7 @@ internal static QueryBody CreateQueryBody(string query, TimeSpan? timeSpan, Logs
return queryBody;
}

private async Task<Response<LogsQueryResult>> ExecuteAsync(string workspaceId, string query, TimeSpan? timeSpan, LogsQueryOptions options, bool async, CancellationToken cancellationToken = default)
private async Task<Response<LogsQueryResult>> ExecuteAsync(string workspaceId, string query, QueryTimeSpan timeSpan, LogsQueryOptions options, bool async, CancellationToken cancellationToken = default)
{
if (workspaceId == null)
{
Expand Down
18 changes: 2 additions & 16 deletions sdk/monitor/Azure.Monitor.Query/src/MetricQueryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,9 @@ namespace Azure.Monitor.Query
public class MetricQueryOptions
{
/// <summary>
/// Gets ot sets the start of the time range for the query.
/// This value has to be used in combination with the <see cref="EndTime"/> or <see cref="Duration"/>.
/// Gets or sets the time span over which the metric would be queried.
/// </summary>
public DateTimeOffset? StartTime { get; set; }

/// <summary>
/// Gets ot sets the end of the time range for the query.
/// This value has to be used in combination with the <see cref="StartTime"/> or <see cref="Duration"/>.
/// </summary>
public DateTimeOffset? EndTime { get; set; }

/// <summary>
/// Gets ot sets the duration that of the time range for the query.
/// This value can be set on it's own or in combination with the <see cref="StartTime"/> or <see cref="EndTime"/>
/// but not both.
/// </summary>
public TimeSpan? Duration { get; set; }
public QueryTimeSpan? TimeSpan { get; set; }

/// <summary>
/// Gets ot sets the interval to sample metrics at.
Expand Down
32 changes: 2 additions & 30 deletions sdk/monitor/Azure.Monitor.Query/src/MetricsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public virtual Response<MetricQueryResult> Query(string resource, IEnumerable<st
try
{
return _metricsRestClient.List(resource,
timespan: GetTimespan(options),
timespan: options?.TimeSpan?.ToString(),
interval: options?.Interval,
filter: options?.Filter,
top: options?.Top,
Expand Down Expand Up @@ -104,7 +104,7 @@ public virtual async Task<Response<MetricQueryResult>> QueryAsync(string resourc
try
{
return await _metricsRestClient.ListAsync(resource,
timespan: GetTimespan(options),
timespan: options?.TimeSpan?.ToString(),
interval: options?.Interval,
filter: options?.Filter,
top: options?.Top,
Expand Down Expand Up @@ -220,34 +220,6 @@ public virtual async Task<Response<IReadOnlyList<MetricNamespace>>> GetMetricNam
}
}

private static string GetTimespan(MetricQueryOptions options)
{
var startTime = options?.StartTime != null ? TypeFormatters.ToString(options.StartTime.Value, "o") : null;
var endTime = options?.EndTime != null ? TypeFormatters.ToString(options.EndTime.Value, "o") : null;
var duration = options?.Duration != null ? TypeFormatters.ToString(options.Duration.Value, "P") : null;

switch (startTime, endTime, duration)
{
case (null, null, string):
return duration;
case (string, string, null):
return $"{startTime}/{endTime}";
case (string, null, string):
return $"{startTime}/{duration}";
case (null, string, string):
return $"{duration}/{endTime}";
case (null, null, null):
return null;
default:
throw new ArgumentException(
$"The following combinations of {nameof(MetricQueryOptions.Duration)}, {nameof(MetricQueryOptions.StartTime)}, {nameof(MetricQueryOptions.EndTime)} are allowed: " + Environment.NewLine +
$" {nameof(MetricQueryOptions.Duration)}, " + Environment.NewLine +
$" {nameof(MetricQueryOptions.StartTime)} + {nameof(MetricQueryOptions.Duration)}" + Environment.NewLine +
$" {nameof(MetricQueryOptions.Duration)} + {nameof(MetricQueryOptions.EndTime)}" + Environment.NewLine +
$" {nameof(MetricQueryOptions.StartTime)} + {nameof(MetricQueryOptions.EndTime)}");
}
}

private static string GetAggregation(MetricQueryOptions options)
{
if (options?.Aggregations == null ||
Expand Down
Loading