public async Task BatchQuery() { #region Snippet:BatchQuery #if SNIPPET Uri endpoint = new Uri("https://api.loganalytics.io"); string workspaceId = "<workspace_id>"; #else Uri endpoint = TestEnvironment.LogsEndpoint; string workspaceId = TestEnvironment.WorkspaceId; #endif LogsClient client = new LogsClient(endpoint, new DefaultAzureCredential()); // Query TOP 10 resource groups by event count // And total event count LogsBatchQuery batch = new LogsBatchQuery(); 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 client.QueryBatchAsync(batch); var count = response.Value.GetResult <int>(countQueryId).Single(); var topEntries = response.Value.GetResult <MyLogEntryModel>(topQueryId); Console.WriteLine($"AzureActivity has total {count} events"); foreach (var logEntryModel in topEntries) { Console.WriteLine($"{logEntryModel.ResourceGroup} had {logEntryModel.Count} events"); } #endregion }
public async Task BatchQuery() { #region Snippet:BatchQuery LogsClient client = new LogsClient(new DefaultAzureCredential()); /*@@*/ string workspaceId = TestEnvironment.WorkspaceId; //@@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"); Response <LogsBatchQueryResult> response = await batch.SubmitAsync(); var count = response.Value.GetResult <int>(countQueryId).Single(); var topEntries = response.Value.GetResult <MyLogEntryModel>(topQueryId); Console.WriteLine($"AzureActivity has total {count} events"); foreach (var logEntryModel in topEntries) { Console.WriteLine($"{logEntryModel.ResourceGroup} had {logEntryModel.Count} events"); } #endregion }
public async Task ThrowsExceptionWhenQueryFailsBatch() { var client = CreateClient(); LogsBatchQuery batch = new LogsBatchQuery(); var queryId = batch.AddQuery(TestEnvironment.WorkspaceId, "this won't work", _logsTestData.DataTimeRange); var batchResult = await client.QueryBatchAsync(batch); var exception = Assert.Throws <RequestFailedException>(() => batchResult.Value.GetResult(queryId)); Assert.AreEqual("BadArgumentError", exception.ErrorCode); StringAssert.StartsWith("The request had some invalid properties", exception.Message); }
public async Task ThrowsExceptionWhenPartialSuccess() { var client = CreateClient(); LogsBatchQuery batch = new LogsBatchQuery(); var queryId = batch.AddQuery(TestEnvironment.WorkspaceId, "set truncationmaxrecords=1; datatable (s: string) ['a', 'b']", _logsTestData.DataTimeRange); var batchResult = await client.QueryBatchAsync(batch); var exception = Assert.Throws<RequestFailedException>(() => batchResult.Value.GetResult(queryId)); Assert.AreEqual("PartialError", exception.ErrorCode); StringAssert.StartsWith("The result was returned but contained a partial error", exception.Message); }
public async Task ThrowsExceptionWhenBatchQueryNotFound() { var client = CreateClient(); LogsBatchQuery batch = new LogsBatchQuery(); batch.AddQuery(TestEnvironment.WorkspaceId, _logsTestData.TableAName, _logsTestData.DataTimeRange); var batchResult = await client.QueryBatchAsync(batch); var exception = Assert.Throws <ArgumentException>(() => batchResult.Value.GetResult("12345")); Assert.AreEqual("queryId", exception.ParamName); StringAssert.StartsWith("Query with ID '12345' wasn't part of the batch. Please use the return value of the LogsBatchQuery.AddQuery as the 'queryId' argument.", exception.Message); }
public async Task CanQueryBatch() { var client = CreateClient(); LogsBatchQuery batch = new LogsBatchQuery(); string id1 = batch.AddQuery(TestEnvironment.WorkspaceId, "Heartbeat", _logsTestData.DataTimeRange); string id2 = batch.AddQuery(TestEnvironment.WorkspaceId, "Heartbeat", _logsTestData.DataTimeRange); Response <LogsBatchQueryResult> response = await client.QueryBatchAsync(batch); var result1 = response.Value.GetResult(id1); var result2 = response.Value.GetResult(id2); CollectionAssert.IsNotEmpty(result1.Tables[0].Columns); CollectionAssert.IsNotEmpty(result2.Tables[0].Columns); }
public async Task CanQueryBatch() { var client = CreateClient(); LogsBatchQuery batch = InstrumentClient(client.CreateBatchQuery()); string id1 = batch.AddQuery(TestEnvironment.WorkspaceId, "Heartbeat"); string id2 = batch.AddQuery(TestEnvironment.WorkspaceId, "Heartbeat"); Response <LogsBatchQueryResult> response = await batch.SubmitAsync(); var result1 = response.Value.GetResult(id1); var result2 = response.Value.GetResult(id2); CollectionAssert.IsNotEmpty(result1.Tables[0].Columns); CollectionAssert.IsNotEmpty(result2.Tables[0].Columns); }
public void CanSetServiceTimeoutForBatch_Mocked() { string preferHeader = null; TimeSpan?networkOverride = default; var mockTransport = MockTransport.FromMessageCallback(message => { Assert.True(message.Request.Headers.TryGetValue("prefer", out preferHeader)); networkOverride = message.NetworkTimeout; return(new MockResponse(403)); }); var client = new LogsQueryClient(new Uri("https://api.loganalytics.io"), new MockCredential(), new LogsQueryClientOptions() { Transport = mockTransport }); var batch = new LogsBatchQuery(); batch.AddWorkspaceQuery("wid", "tid", TimeSpan.FromDays(1), options: new LogsQueryOptions() { ServerTimeout = TimeSpan.FromMinutes(1) }); batch.AddWorkspaceQuery("wid", "tid", TimeSpan.FromDays(1), options: new LogsQueryOptions() { ServerTimeout = TimeSpan.FromMinutes(2) }); batch.AddWorkspaceQuery("wid", "tid", TimeSpan.FromDays(1), options: new LogsQueryOptions() { ServerTimeout = TimeSpan.FromMinutes(3) }); Assert.ThrowsAsync <RequestFailedException>(() => client.QueryBatchAsync(batch)); // 3 minutes (180 sec) is the max out of all individual queries Assert.AreEqual("wait=180", preferHeader); // The network timeout is adjusted with 15 sec buffer Assert.AreEqual(TimeSpan.FromMinutes(3).Add(TimeSpan.FromSeconds(15)), networkOverride); }
public async Task BatchQuery() { #region Snippet:BatchQuery #if SNIPPET string workspaceId = "<workspace_id>"; #else string workspaceId = TestEnvironment.WorkspaceId; #endif var client = new LogsQueryClient(new DefaultAzureCredential()); // Query TOP 10 resource groups by event count // And total event count var batch = new LogsBatchQuery(); #region Snippet:BatchQueryAddAndGet 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); #endregion Console.WriteLine($"AzureActivity has total {count} events"); foreach (var logEntryModel in topEntries) { Console.WriteLine($"{logEntryModel.ResourceGroup} had {logEntryModel.Count} events"); } #endregion }
public async Task CanQueryWithStatisticsBatch(bool include) { var client = CreateClient(); LogsBatchQuery batch = new LogsBatchQuery(); var queryId = batch.AddQuery(TestEnvironment.WorkspaceId, _logsTestData.TableAName, _logsTestData.DataTimeRange, options: new LogsQueryOptions() { IncludeStatistics = include }); var batchResult = await client.QueryBatchAsync(batch); var result = batchResult.Value.GetResult(queryId); if (include) { Assert.Greater(result.Statistics.GetProperty("query").GetProperty("executionTime").GetDouble(), 0); } else { Assert.AreEqual(JsonValueKind.Undefined, result.Statistics.ValueKind); } }
public async Task CanQueryBatchWithTimespan() { // Get the time of the second event and add a bit of buffer to it (events are 2d apart) var minOffset = (DateTimeOffset)_logsTestData.TableA[1][LogsTestData.TimeGeneratedColumnNameSent]; var timespan = Recording.UtcNow - minOffset; timespan = timespan.Add(TimeSpan.FromDays(1)); var client = CreateClient(); LogsBatchQuery batch = new LogsBatchQuery(); string id1 = batch.AddQuery(TestEnvironment.WorkspaceId, $"{_logsTestData.TableAName} | distinct * | project {LogsTestData.TimeGeneratedColumnName}", _logsTestData.DataTimeRange); string id2 = batch.AddQuery(TestEnvironment.WorkspaceId, $"{_logsTestData.TableAName} | distinct * | project {LogsTestData.TimeGeneratedColumnName}", timespan); Response<LogsBatchQueryResultCollection> response = await client.QueryBatchAsync(batch); var result1 = response.Value.GetResult<DateTimeOffset>(id1); var result2 = response.Value.GetResult<DateTimeOffset>(id2); // All rows Assert.AreEqual(3, result1.Count); // Filtered by the timestamp Assert.AreEqual(2, result2.Count); Assert.True(result2.All(r => r >= minOffset)); }
public async Task CanQueryBatchMixed() { var client = CreateClient(); LogsBatchQuery batch = new LogsBatchQuery(); string id1 = batch.AddQuery(TestEnvironment.WorkspaceId, "Heartbeat", _logsTestData.DataTimeRange); string id2 = batch.AddQuery(TestEnvironment.WorkspaceId, "Heartbeats", _logsTestData.DataTimeRange); string id3 = batch.AddQuery(TestEnvironment.WorkspaceId, "set truncationmaxrecords=1; datatable (s: string) ['a', 'b']", _logsTestData.DataTimeRange); Response<LogsBatchQueryResultCollection> response = await client.QueryBatchAsync(batch); Assert.AreEqual(LogsQueryResultStatus.Success, response.Value.Single(r => r.Id == id1).Status); var failedResult = response.Value.Single(r => r.Id == id2); Assert.AreEqual(LogsQueryResultStatus.Failure, failedResult.Status); Assert.NotNull(failedResult.Error.Code); Assert.NotNull(failedResult.Error.Message); var partialResult = response.Value.Single(r => r.Id == id3); Assert.AreEqual(LogsQueryResultStatus.PartialFailure, partialResult.Status); CollectionAssert.IsNotEmpty(partialResult.Table.Rows); Assert.NotNull(partialResult.Error.Code); Assert.NotNull(partialResult.Error.Message); }
public async Task QueryBatchHandledInvalidResponse() { var badResponse = @"{ ""responses"": [ { ""id"": ""0"", ""status"": 200, ""headers"": { ""Age"": ""3"", ""request-context"": ""appId=cid-v1:70941e4f-7e8f-40b7-b730-183893db0297"" }, ""body"": ""{\""tables\"":[{\""name\"":\""PrimaryResult\"",\""columns\"":[{\""name\"":\""TenantId\"",\""type\"":\""string\""},{\""name\"":\""SourceSystem\"",\""type\"":\""string\""},{\""name\"":\""MG\"",\""type\"":\""string\""},{\""name\"":\""ManagementGroupName\"",\""type\"":\""string\""},{\""name\"":\""TimeGenerated\"",\""type\"":\""datetime\""},{\""name\"":\""Computer\"",\""type\"":\""string\""},{\""name\"":\""RawData\"",\""type\"":\""string\""},{\""name\"":\""IntColumn_d\"",\""type\"":\""real\""},{\""name\"":\""StringColumn_s\"",\""type\"":\""string\""},{\""name\"":\""BoolColumn_b\"",\""type\"":\""bool\""},{\""name\"":\""FloatColumn_d\"",\""type\"":\""real\""},{\""name\"":\""Type\"",\""type\"":\""string\""},{\""name\"":\""_ResourceId\"",\""type\"":\""string\""}],\""rows\"":[[\""e7bf7412-576d-4978-b47c-2edf669e3e2a\"",\""RestAPI\"",\""\"",\""\"",\""2021-05-31T00:00:00Z\"",\""\"",\""\"",1,\""a\"",false,0,\""TableA1_151_CL\"",\""\""],[\""e7bf7412-576d-4978-b47c-2edf669e3e2a\"",\""RestAPI\"",\""\"",\""\"",\""2021-06-02T00:00:00Z\"",\""\"",\""\"",3,\""b\"",true,1.20000005,\""TableA1_151_CL\"",\""\""],[\""e7bf7412-576d-4978-b47c-2edf669e3e2a\"",\""RestAPI\"",\""\"",\""\"",\""2021-06-05T00:00:00Z\"",\""\"",\""\"",1,\""c\"",false,1.10000002,\""TableA1_151_CL\"",\""\""]]}]}"" } ] } "; var mockTransport = MockTransport.FromMessageCallback(message => { var mockResponse = new MockResponse(200); mockResponse.SetContent(badResponse); return(mockResponse); }); var client = new LogsQueryClient(new Uri("https://api.loganalytics.io"), new MockCredential(), new LogsQueryClientOptions() { Transport = mockTransport }); LogsBatchQuery batch = new LogsBatchQuery(); batch.AddWorkspaceQuery("wid", "query", QueryTimeRange.All); LogsBatchQueryResultCollection batchResults = await client.QueryBatchAsync(batch); Assert.NotNull(batchResults.GetResult("0")); }
internal LogsBatchQueryResultCollection(IList <LogsBatchQueryResult> results, LogsBatchQuery query) : base(results) { _query = query; }