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 LogsQueryClient client = new LogsQueryClient(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 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 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")); }