-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
LogsBatchQuery.cs
67 lines (63 loc) · 2.87 KB
/
LogsBatchQuery.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Collections.Generic;
using System.Globalization;
using Azure.Core;
using Azure.Monitor.Query.Models;
namespace Azure.Monitor.Query
{
/// <summary>
/// Represents a batch that consists of multiple log queries.
/// </summary>
public class LogsBatchQuery
{
private int _counter;
internal List<BatchQueryRequest> Requests { get; } = new();
/// <summary>
/// Initializes a new instance of <see cref="LogsBatchQuery"/>.
/// </summary>
public LogsBatchQuery()
{
}
/// <summary>
/// Adds the specified query to the batch. Results can be retrieved after the query is submitted via the <see cref="LogsQueryClient.QueryBatchAsync"/> call.
/// <example snippet="Snippet:BatchQueryAddAndGet">
/// <code language="csharp">
/// string countQueryId = batch.AddWorkspaceQuery(
/// workspaceId,
/// "AzureActivity | count",
/// new QueryTimeRange(TimeSpan.FromDays(1)));
/// string topQueryId = batch.AddWorkspaceQuery(
/// workspaceId,
/// "AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count",
/// new QueryTimeRange(TimeSpan.FromDays(1)));
///
/// Response<LogsBatchQueryResultCollection> response = await client.QueryBatchAsync(batch);
///
/// var count = response.Value.GetResult<int>(countQueryId).Single();
/// var topEntries = response.Value.GetResult<MyLogEntryModel>(topQueryId);
/// </code>
/// </example>
/// </summary>
/// <param name="workspaceId">The workspace to include in the query.</param>
/// <param name="query">The query text to execute.</param>
/// <param name="timeRange">The timespan over which to query data.</param>
/// <param name="options">The <see cref="LogsQueryOptions"/> to configure the query.</param>
/// <returns>The query identifier that has to be passed into <see cref="LogsBatchQueryResultCollection.GetResult"/> to get the result.</returns>
public virtual string AddWorkspaceQuery(string workspaceId, string query, QueryTimeRange timeRange, LogsQueryOptions options = null)
{
var id = _counter.ToString("G", CultureInfo.InvariantCulture);
_counter++;
var logQueryRequest = new BatchQueryRequest(id, LogsQueryClient.CreateQueryBody(query, timeRange, options, out string prefer), workspaceId)
{
Options = options
};
if (prefer != null)
{
logQueryRequest.Headers.Add(HttpHeader.Names.Prefer, prefer);
}
Requests.Add(logQueryRequest);
return id;
}
}
}