Skip to content

Commit b140b95

Browse files
authored
Make the query batch a simpler model (#21009)
1 parent f6d49e3 commit b140b95

File tree

6 files changed

+67
-82
lines changed

6 files changed

+67
-82
lines changed

sdk/monitor/Azure.Monitor.Query/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ string workspaceId = "<workspace_id>";
131131

132132
// Query TOP 10 resource groups by event count
133133
// And total event count
134-
LogsBatchQuery batch = client.CreateBatchQuery();
134+
LogsBatchQuery batch = new LogsBatchQuery();
135135
string countQueryId = batch.AddQuery(workspaceId, "AzureActivity | count", TimeSpan.FromDays(1));
136136
string topQueryId = batch.AddQuery(workspaceId, "AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count", TimeSpan.FromDays(1));
137137

138-
Response<LogsBatchQueryResult> response = await batch.SubmitAsync();
138+
Response<LogsBatchQueryResult> response = await client.QueryBatchAsync(batch);
139139

140140
var count = response.Value.GetResult<int>(countQueryId).Single();
141141
var topEntries = response.Value.GetResult<MyLogEntryModel>(topQueryId);

sdk/monitor/Azure.Monitor.Query/api/Azure.Monitor.Query.netstandard2.0.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,19 @@ namespace Azure.Monitor.Query
2121
}
2222
public partial class LogsBatchQuery
2323
{
24-
protected LogsBatchQuery() { }
24+
public LogsBatchQuery() { }
2525
public virtual string AddQuery(string workspaceId, string query, Azure.Monitor.Query.DateTimeRange timeRange, Azure.Monitor.Query.LogsQueryOptions options = null) { throw null; }
26-
public virtual Azure.Response<Azure.Monitor.Query.Models.LogsBatchQueryResult> Submit(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
27-
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Monitor.Query.Models.LogsBatchQueryResult>> SubmitAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
2826
}
2927
public partial class LogsClient
3028
{
3129
protected LogsClient() { }
3230
public LogsClient(Azure.Core.TokenCredential credential) { }
3331
public LogsClient(Azure.Core.TokenCredential credential, Azure.Monitor.Query.LogsClientOptions options) { }
34-
public virtual Azure.Monitor.Query.LogsBatchQuery CreateBatchQuery() { throw null; }
3532
public virtual Azure.Response<Azure.Monitor.Query.Models.LogsQueryResult> Query(string workspaceId, string query, Azure.Monitor.Query.DateTimeRange timeRange, Azure.Monitor.Query.LogsQueryOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
3633
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Monitor.Query.Models.LogsQueryResult>> QueryAsync(string workspaceId, string query, Azure.Monitor.Query.DateTimeRange timeRange, Azure.Monitor.Query.LogsQueryOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
3734
public virtual System.Threading.Tasks.Task<Azure.Response<System.Collections.Generic.IReadOnlyList<T>>> QueryAsync<T>(string workspaceId, string query, Azure.Monitor.Query.DateTimeRange timeRange, Azure.Monitor.Query.LogsQueryOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
35+
public virtual Azure.Response<Azure.Monitor.Query.Models.LogsBatchQueryResult> QueryBatch(Azure.Monitor.Query.LogsBatchQuery batch, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
36+
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Monitor.Query.Models.LogsBatchQueryResult>> QueryBatchAsync(Azure.Monitor.Query.LogsBatchQuery batch, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
3837
public virtual Azure.Response<System.Collections.Generic.IReadOnlyList<T>> Query<T>(string workspaceId, string query, Azure.Monitor.Query.DateTimeRange timeRange, Azure.Monitor.Query.LogsQueryOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
3938
}
4039
public partial class LogsClientOptions : Azure.Core.ClientOptions

sdk/monitor/Azure.Monitor.Query/src/LogsClient.cs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,54 @@ public virtual async Task<Response<LogsQueryResult>> QueryAsync(string workspace
135135
}
136136

137137
/// <summary>
138-
/// Creates an instance of <see cref="LogsBatchQuery"/> that allows executing multiple queries at once.
138+
/// Submits the batch query.
139139
/// </summary>
140-
/// <returns>The <see cref="LogsBatchQuery"/> instance that allows building a list of queries and submitting them.</returns>
141-
public virtual LogsBatchQuery CreateBatchQuery()
140+
/// <param name="batch">The batch of queries to send.</param>
141+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
142+
/// <returns>The <see cref="LogsBatchQueryResult"/> containing the query identifier that has to be passed into <see cref="LogsBatchQueryResult.GetResult"/> to get the result.</returns>
143+
public virtual Response<LogsBatchQueryResult> QueryBatch(LogsBatchQuery batch, CancellationToken cancellationToken = default)
142144
{
143-
return new LogsBatchQuery(_clientDiagnostics, _queryClient, _rowBinder);
145+
Argument.AssertNotNull(batch, nameof(batch));
146+
147+
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(LogsClient)}.{nameof(QueryBatch)}");
148+
scope.Start();
149+
try
150+
{
151+
var response = _queryClient.Batch(batch.Batch, cancellationToken);
152+
response.Value.RowBinder = _rowBinder;
153+
return response;
154+
}
155+
catch (Exception e)
156+
{
157+
scope.Failed(e);
158+
throw;
159+
}
144160
}
145161

162+
/// <summary>
163+
/// Submits the batch query.
164+
/// </summary>
165+
/// <param name="batch">The batch of queries to send.</param>
166+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
167+
/// <returns>The <see cref="LogsBatchQueryResult"/> that allows retrieving query results.</returns>
168+
public virtual async Task<Response<LogsBatchQueryResult>> QueryBatchAsync(LogsBatchQuery batch, CancellationToken cancellationToken = default)
169+
{
170+
Argument.AssertNotNull(batch, nameof(batch));
171+
172+
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(LogsClient)}.{nameof(QueryBatch)}");
173+
scope.Start();
174+
try
175+
{
176+
var response = await _queryClient.BatchAsync(batch.Batch, cancellationToken).ConfigureAwait(false);
177+
response.Value.RowBinder = _rowBinder;
178+
return response;
179+
}
180+
catch (Exception e)
181+
{
182+
scope.Failed(e);
183+
throw;
184+
}
185+
}
146186
internal static QueryBody CreateQueryBody(string query, DateTimeRange timeRange, LogsQueryOptions options, out string prefer)
147187
{
148188
var queryBody = new QueryBody(query);

sdk/monitor/Azure.Monitor.Query/src/Models/LogsBatchQuery.cs

Lines changed: 6 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,19 @@ namespace Azure.Monitor.Query
1515
/// </summary>
1616
public class LogsBatchQuery
1717
{
18-
private readonly ClientDiagnostics _clientDiagnostics;
19-
private readonly QueryRestClient _restClient;
20-
private readonly RowBinder _rowBinder;
21-
private readonly BatchRequest _batch;
18+
internal BatchRequest Batch { get; }
2219
private int _counter;
2320

24-
internal LogsBatchQuery(ClientDiagnostics clientDiagnostics, QueryRestClient restClient, RowBinder rowBinder)
25-
{
26-
_clientDiagnostics = clientDiagnostics;
27-
_restClient = restClient;
28-
_rowBinder = rowBinder;
29-
_batch = new BatchRequest();
30-
}
31-
3221
/// <summary>
33-
/// Initializes a new instance of <see cref="LogsBatchQuery"/> for mocking.
22+
/// Initializes a new instance of <see cref="LogsBatchQuery"/>.
3423
/// </summary>
35-
protected LogsBatchQuery()
24+
public LogsBatchQuery()
3625
{
26+
Batch = new BatchRequest();
3727
}
3828

3929
/// <summary>
40-
/// Adds the specified query to the batch. Results can be retrieved after the query is submitted via the <see cref="SubmitAsync"/> call.
30+
/// Adds the specified query to the batch. Results can be retrieved after the query is submitted via the <see cref="LogsClient.QueryBatchAsync"/> call.
4131
/// </summary>
4232
/// <param name="workspaceId">The workspace to include in the query.</param>
4333
/// <param name="query">The query text to execute.</param>
@@ -58,52 +48,8 @@ public virtual string AddQuery(string workspaceId, string query, DateTimeRange t
5848
{
5949
logQueryRequest.Headers.Add("prefer", prefer);
6050
}
61-
_batch.Requests.Add(logQueryRequest);
51+
Batch.Requests.Add(logQueryRequest);
6252
return id;
6353
}
64-
65-
/// <summary>
66-
/// Submits the batch.
67-
/// </summary>
68-
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
69-
/// <returns>The <see cref="LogsBatchQueryResult"/> containing the query identifier that has to be passed into <see cref="LogsBatchQueryResult.GetResult"/> to get the result.</returns>
70-
public virtual Response<LogsBatchQueryResult> Submit(CancellationToken cancellationToken = default)
71-
{
72-
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(LogsBatchQuery)}.{nameof(Submit)}");
73-
scope.Start();
74-
try
75-
{
76-
var response = _restClient.Batch(_batch, cancellationToken);
77-
response.Value.RowBinder = _rowBinder;
78-
return response;
79-
}
80-
catch (Exception e)
81-
{
82-
scope.Failed(e);
83-
throw;
84-
}
85-
}
86-
87-
/// <summary>
88-
/// Submits the batch.
89-
/// </summary>
90-
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
91-
/// <returns>The <see cref="LogsBatchQueryResult"/> that allows retrieving query results.</returns>
92-
public virtual async Task<Response<LogsBatchQueryResult>> SubmitAsync(CancellationToken cancellationToken = default)
93-
{
94-
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(LogsBatchQuery)}.{nameof(Submit)}");
95-
scope.Start();
96-
try
97-
{
98-
var response = await _restClient.BatchAsync(_batch, cancellationToken).ConfigureAwait(false);
99-
response.Value.RowBinder = _rowBinder;
100-
return response;
101-
}
102-
catch (Exception e)
103-
{
104-
scope.Failed(e);
105-
throw;
106-
}
107-
}
10854
}
10955
}

sdk/monitor/Azure.Monitor.Query/tests/LogsClientSamples.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ public async Task BatchQuery()
133133

134134
// Query TOP 10 resource groups by event count
135135
// And total event count
136-
LogsBatchQuery batch = client.CreateBatchQuery();
136+
LogsBatchQuery batch = new LogsBatchQuery();
137137
string countQueryId = batch.AddQuery(workspaceId, "AzureActivity | count", TimeSpan.FromDays(1));
138138
string topQueryId = batch.AddQuery(workspaceId, "AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count", TimeSpan.FromDays(1));
139139

140-
Response<LogsBatchQueryResult> response = await batch.SubmitAsync();
140+
Response<LogsBatchQueryResult> response = await client.QueryBatchAsync(batch);
141141

142142
var count = response.Value.GetResult<int>(countQueryId).Single();
143143
var topEntries = response.Value.GetResult<MyLogEntryModel>(topQueryId);

sdk/monitor/Azure.Monitor.Query/tests/LogsQueryClientClientLiveTests.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,10 @@ public async Task CanQueryIntoIDictionary()
148148
public async Task CanQueryBatch()
149149
{
150150
var client = CreateClient();
151-
LogsBatchQuery batch = InstrumentClient(client.CreateBatchQuery());
151+
LogsBatchQuery batch = new LogsBatchQuery();
152152
string id1 = batch.AddQuery(TestEnvironment.WorkspaceId, "Heartbeat", _logsTestData.DataTimeRange);
153153
string id2 = batch.AddQuery(TestEnvironment.WorkspaceId, "Heartbeat", _logsTestData.DataTimeRange);
154-
Response<LogsBatchQueryResult> response = await batch.SubmitAsync();
154+
Response<LogsBatchQueryResult> response = await client.QueryBatchAsync(batch);
155155

156156
var result1 = response.Value.GetResult(id1);
157157
var result2 = response.Value.GetResult(id2);
@@ -380,10 +380,10 @@ public async Task CanQueryBatchWithTimespan()
380380
timespan = timespan.Add(TimeSpan.FromDays(1));
381381

382382
var client = CreateClient();
383-
LogsBatchQuery batch = InstrumentClient(client.CreateBatchQuery());
383+
LogsBatchQuery batch = new LogsBatchQuery();
384384
string id1 = batch.AddQuery(TestEnvironment.WorkspaceId, $"{_logsTestData.TableAName} | project {LogsTestData.TimeGeneratedColumnName}", _logsTestData.DataTimeRange);
385385
string id2 = batch.AddQuery(TestEnvironment.WorkspaceId, $"{_logsTestData.TableAName} | project {LogsTestData.TimeGeneratedColumnName}", timespan);
386-
Response<LogsBatchQueryResult> response = await batch.SubmitAsync();
386+
Response<LogsBatchQueryResult> response = await client.QueryBatchAsync(batch);
387387

388388
var result1 = response.Value.GetResult<DateTimeOffset>(id1);
389389
var result2 = response.Value.GetResult<DateTimeOffset>(id2);
@@ -410,9 +410,9 @@ public async Task ThrowsExceptionWhenQueryFailsBatch()
410410
{
411411
var client = CreateClient();
412412

413-
LogsBatchQuery batch = InstrumentClient(client.CreateBatchQuery());
413+
LogsBatchQuery batch = new LogsBatchQuery();
414414
var queryId = batch.AddQuery(TestEnvironment.WorkspaceId, "this won't work", _logsTestData.DataTimeRange);
415-
var batchResult = await batch.SubmitAsync();
415+
var batchResult = await client.QueryBatchAsync(batch);
416416

417417
var exception = Assert.Throws<RequestFailedException>(() => batchResult.Value.GetResult(queryId));
418418

@@ -425,9 +425,9 @@ public async Task ThrowsExceptionWhenBatchQueryNotFound()
425425
{
426426
var client = CreateClient();
427427

428-
LogsBatchQuery batch = InstrumentClient(client.CreateBatchQuery());
428+
LogsBatchQuery batch = new LogsBatchQuery();
429429
batch.AddQuery(TestEnvironment.WorkspaceId, _logsTestData.TableAName, _logsTestData.DataTimeRange);
430-
var batchResult = await batch.SubmitAsync();
430+
var batchResult = await client.QueryBatchAsync(batch);
431431

432432
var exception = Assert.Throws<ArgumentException>(() => batchResult.Value.GetResult("12345"));
433433

@@ -464,12 +464,12 @@ public async Task CanQueryWithStatisticsBatch(bool include)
464464
{
465465
var client = CreateClient();
466466

467-
LogsBatchQuery batch = InstrumentClient(client.CreateBatchQuery());
467+
LogsBatchQuery batch = new LogsBatchQuery();
468468
var queryId = batch.AddQuery(TestEnvironment.WorkspaceId, _logsTestData.TableAName, _logsTestData.DataTimeRange, options: new LogsQueryOptions()
469469
{
470470
IncludeStatistics = include
471471
});
472-
var batchResult = await batch.SubmitAsync();
472+
var batchResult = await client.QueryBatchAsync(batch);
473473
var result = batchResult.Value.GetResult(queryId);
474474

475475
if (include)

0 commit comments

Comments
 (0)